1 // $Id: interface-add.c,v 1.3 2005/03/19 02:07:40 ensc Exp $ --*- c -*--
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "interface.h"
24 #include "pathconfig.h"
26 #include <lib_internal/command.h>
27 #include <lib_internal/util.h>
28 #include <ensc_fmt/fmt.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 Iface_getIPv4Prefix(struct Interface const *iface)
36 uint32_t mask = iface->addr.ipv4.mask;
47 invokeIpAddr(struct Interface const *iface)
50 unsigned int prefix = Iface_getIPv4Prefix(iface);
51 char * tmp = inet_ntoa(*reinterpret_cast(struct in_addr *)(&iface->addr.ipv4.ip));
52 size_t l = strlen(tmp);
53 char addr[l + sizeof("/") + sizeof(unsigned int)*3 + 1];
55 size_t l1 = strlen(iface->dev);
56 size_t l2 = iface->name ? strlen(iface->name) : 0;
57 char devlabel[l1 + l2 + sizeof(":")];
60 ptr = Xmemcpy(addr, tmp, l);
62 l = utilvserver_fmt_uint(ptr, prefix);
68 char const * argv[] = {
70 PROG_IP, "addr", "add",
78 if (iface->addr.ipv4.bcast!=0)
79 argv[idx++] = inet_ntoa(*reinterpret_cast(struct in_addr *)(&iface->addr.ipv4.bcast));
84 ptr = Xmemcpy(devlabel, iface->dev, l1);
86 ptr = Xmemcpy(ptr, iface->name, l2);
89 argv[idx++] = "label";
90 argv[idx++] = devlabel;
94 argv[idx++] = iface->dev;
96 Command_setParams(&cmd, argv);
97 if (!Command_exec(&cmd, true) ||
98 !Command_wait(&cmd, true) ||
108 addVLAN(struct Interface const UNUSED *iface)
110 abort(); // TODO: implement me
114 addIndirect(struct Interface const UNUSED *iface)
116 abort(); // TODO: implement me
120 addIP(struct Interface const *iface)
122 return invokeIpAddr(iface);
123 //invokeIpLink(iface);
127 Iface_add(struct Interface const *iface)
129 if (iface->nodev) return true;
130 if (strchr(iface->dev, '.')!=0) return addVLAN(iface);
131 if (!iface->direct) return addIndirect(iface);