5
#include <sys/utsname.h>
11
#define UNUSED __attribute__((unused))
16
int _iface_has(char *iface, char *delims)
19
strncpy(_iface, iface, sizeof(_iface));
20
_iface[sizeof(_iface) - 1] = 0;
21
strtok(_iface, delims);
22
void *token = strtok(NULL, delims);
23
return (token != NULL);
26
int execable(char *program)
30
if (0 == stat(program, &buf)) {
31
if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode))
37
void cleanup_hwaddress(interface_defn * ifd UNUSED, char **pparam, int argc UNUSED, char **argv UNUSED)
40
/* we're shrinking the text, so no realloc needed */
41
char *space = strchr(rest, ' ');
47
if (strcasecmp(rest, "ether") == 0 || strcasecmp(rest, "ax25") == 0 || strcasecmp(rest, "ARCnet") == 0 || strcasecmp(rest, "netrom") == 0) {
48
/* found deprecated <class> attribute */
49
memmove(rest, space + 1, strlen(space + 1) + 1);
55
void make_hex_address(interface_defn * ifd UNUSED, char **pparam, int argc UNUSED, char **argv UNUSED)
58
int maxlen = strlen("0000:0000");
60
int ret = sscanf(*pparam, "%3hhu.%3hhu.%3hhu.%3hhu",
61
&addrcomp[0], &addrcomp[1], &addrcomp[2], &addrcomp[3]);
66
*pparam = realloc(*pparam, maxlen + 1);
69
snprintf(*pparam, maxlen + 1, "%.2hhx%.2hhx:%.2hhx%.2hhx",
70
addrcomp[0], addrcomp[1], addrcomp[2], addrcomp[3]);
73
#include <arpa/inet.h>
75
void compute_v4_addr(interface_defn * ifd UNUSED, char **pparam, int argc UNUSED, char **argv UNUSED)
77
char s[INET_ADDRSTRLEN * 2 + 2]; /* 2 is for slash and \0 */
78
strncpy(s, *pparam, sizeof(s));
81
char *token = strtok(s, "/");
85
*pparam = realloc(*pparam, strlen(token) + 1);
88
strcpy(*pparam, token);
91
void compute_v4_mask(interface_defn * ifd UNUSED, char **pparam, int argc UNUSED, char **argv UNUSED)
93
char s[INET_ADDRSTRLEN * 2 + 2]; /* 2 is for slash and \0 */
94
strncpy(s, *pparam, sizeof(s));
97
char *token = strtok(s, "/");
101
uint8_t addr[sizeof(struct in_addr)];
103
if (inet_pton(AF_INET, token, &addr) != 1)
105
token = strtok(NULL, "/");
108
if (addr[0] <= 127) {
110
} else if ((addr[0] >= 128) && (addr[0] <= 191)) {
112
} else if ((addr[0] >= 192) && (addr[0] <= 223)) {
118
switch (inet_pton(AF_INET, token, &mask)) {
123
if (sscanf(token, "%d", &maskwidth) != 1)
127
if (maskwidth != -1) {
128
mask.s_addr = htonl(~((1L << (32 - maskwidth)) - 1));
131
if (inet_ntop(AF_INET, &mask, s, sizeof(s)) == NULL)
133
*pparam = realloc(*pparam, strlen(s) + 1);
139
void compute_v4_broadcast(interface_defn * ifd, char **pparam, int argc UNUSED, char **argv UNUSED)
141
/* If we don't get special value don't do anything */
142
if (strcmp(*pparam, "+") && strcmp(*pparam, "-"))
148
char *s = get_var("address", strlen("address"), ifd);
151
int r = inet_pton(AF_INET, s, &addr);
156
s = get_var("netmask", strlen("netmask"), ifd);
159
r = inet_pton(AF_INET, s, &mask);
164
if (mask.s_addr != htonl(0xfffffffe)) {
165
if (!strcmp(*pparam, "+")) {
166
addr.s_addr |= ~mask.s_addr;
169
if (!strcmp(*pparam, "-")) {
170
addr.s_addr &= mask.s_addr;
173
if (!strcmp(*pparam, "+")) {
174
addr.s_addr = 0xffffffff;
177
if (!strcmp(*pparam, "-")) {
182
char buffer[INET_ADDRSTRLEN + 1];
183
if (inet_ntop(AF_INET, &addr, buffer, sizeof(buffer)) == NULL)
185
*pparam = realloc(*pparam, strlen(buffer) + 1);
188
strcpy(*pparam, buffer);
191
void set_preferred_lft(interface_defn * ifd, char **pparam, int argc UNUSED, char **argv UNUSED)
193
if (!ifd->real_iface)
195
if (iface_has(":")) {
197
*pparam = realloc(*pparam, sizeof(s));
204
void get_token(interface_defn * ifd UNUSED, char **pparam, int argc, char **argv)
213
token_no = atoi(argv[1]);
216
char *s = strdup(*pparam);
217
char *token = strtok(s, argv[0]);
218
while (token_no > 0) {
219
token = strtok(NULL, argv[0]);
223
strcpy(*pparam, token);
226
*pparam = realloc(*pparam, strlen(argv[2]) + 1);
229
strcpy(*pparam, argv[2]);
235
void to_decimal(interface_defn * ifd UNUSED, char **pparam, int argc, char **argv)
240
base = atoi(argv[0]);
244
long value = strtol(*pparam, &result, base);
245
if (result == *pparam)
248
snprintf(*pparam, strlen(*pparam) + 1, "%ld", value);
251
void map_value(interface_defn * ifd UNUSED, char **pparam, int argc, char **argv)
258
value = (atoi(*pparam) || strcasecmp(*pparam, "on") == 0 || strcasecmp(*pparam, "true") == 0 || strcasecmp(*pparam, "yes") == 0);
260
if ((value < argc) && (argv[value] != NULL)) {
261
*pparam = realloc(*pparam, strlen(argv[value]) + 1);
264
strcpy(*pparam, argv[value]);
266
*pparam = realloc(*pparam, 1);