X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=vserver-start%2Finterface-add.c;fp=vserver-start%2Finterface-add.c;h=17e631957e8d995a54b8645ce53318d37aff2917;hb=d56ea1e992af7da828e89b98f2878bcc04250708;hp=0000000000000000000000000000000000000000;hpb=616c79d7eee3bff076671ee8d475df2f2b640a16;p=util-vserver.git diff --git a/vserver-start/interface-add.c b/vserver-start/interface-add.c new file mode 100644 index 0000000..17e6319 --- /dev/null +++ b/vserver-start/interface-add.c @@ -0,0 +1,133 @@ +// $Id: interface-add.c 1939 2005-03-19 02:07:40Z ensc $ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "interface.h" +#include "pathconfig.h" + +#include +#include +#include + +#include +#include + +unsigned int +Iface_getIPv4Prefix(struct Interface const *iface) +{ + uint32_t mask = iface->addr.ipv4.mask; + unsigned int res = 0; + while (mask!=0) { + res += mask & 1; + mask >>= 1; + } + + return res; +} + +static bool +invokeIpAddr(struct Interface const *iface) +{ + struct Command cmd; + unsigned int prefix = Iface_getIPv4Prefix(iface); + char * tmp = inet_ntoa(*reinterpret_cast(struct in_addr *)(&iface->addr.ipv4.ip)); + size_t l = strlen(tmp); + char addr[l + sizeof("/") + sizeof(unsigned int)*3 + 1]; + char * ptr; + size_t l1 = strlen(iface->dev); + size_t l2 = iface->name ? strlen(iface->name) : 0; + char devlabel[l1 + l2 + sizeof(":")]; + bool result = true; + + ptr = Xmemcpy(addr, tmp, l); + *ptr++ = '/'; + l = utilvserver_fmt_uint(ptr, prefix); + ptr[l] = '\0'; + + Command_init(&cmd); + + size_t idx = 6; + char const * argv[] = { + "/bin/echo", + PROG_IP, "addr", "add", + addr, + "broadcast", 0, + 0, 0, // label + 0, 0, // dev + 0 + }; + + if (iface->addr.ipv4.bcast!=0) + argv[idx++] = inet_ntoa(*reinterpret_cast(struct in_addr *)(&iface->addr.ipv4.bcast)); + else + argv[idx++] = "+"; + + if (iface->name) { + ptr = Xmemcpy(devlabel, iface->dev, l1); + *ptr++ = ':'; + ptr = Xmemcpy(ptr, iface->name, l2); + *ptr = '\0'; + + argv[idx++] = "label"; + argv[idx++] = devlabel; + } + + argv[idx++] = "dev"; + argv[idx++] = iface->dev; + + Command_setParams(&cmd, argv); + if (!Command_exec(&cmd, true) || + !Command_wait(&cmd, true) || + cmd.rc!=0) + result = false; + + Command_free(&cmd); + + return result; +} + +static bool +addVLAN(struct Interface const UNUSED *iface) +{ + abort(); // TODO: implement me +} + +static bool +addIndirect(struct Interface const UNUSED *iface) +{ + abort(); // TODO: implement me +} + +static bool +addIP(struct Interface const *iface) +{ + return invokeIpAddr(iface); + //invokeIpLink(iface); +} + +bool +Iface_add(struct Interface const *iface) +{ + if (iface->nodev) return true; + if (strchr(iface->dev, '.')!=0) return addVLAN(iface); + if (!iface->direct) return addIndirect(iface); + return addIP(iface); +}