1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// $Id: interface-add.c,v 1.2 2004/08/19 16:06:37 ensc Exp $ --*- c -*--
// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
//
// 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 <config.h>
#endif
#include "interface.h"
#include "pathconfig.h"
#include <lib_internal/command.h>
#include <lib_internal/util.h>
#include <ensc_fmt/fmt.h>
#include <netinet/in.h>
#include <arpa/inet.h>
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 <name>
0, 0, // dev <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 *iface)
{
abort(); // TODO: implement me
}
static bool
addIndirect(struct Interface const *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);
}
|