| 1 |
/* options.c -- process the command line options |
| 2 |
Copyright (C) 2001-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* Written by Marcus Brinkmann. */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <errno.h> |
| 25 |
|
| 26 |
#if STDC_HEADERS |
| 27 |
# include <stdlib.h> |
| 28 |
#else |
| 29 |
# ifndef HAVE_STRCHR |
| 30 |
# define strchr index |
| 31 |
# define strrchr rindex |
| 32 |
# endif |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <string.h> |
| 36 |
|
| 37 |
#include <sys/socket.h> |
| 38 |
#include <net/if.h> |
| 39 |
|
| 40 |
#include <attribute.h> |
| 41 |
|
| 42 |
#include "ifconfig.h" |
| 43 |
|
| 44 |
/* List available interfaces. */ |
| 45 |
int list_mode; |
| 46 |
|
| 47 |
/* Be verbose about actions. */ |
| 48 |
int verbose; |
| 49 |
|
| 50 |
/* Flags asked for, possibly still pending application. */ |
| 51 |
static int pending_setflags = 0; |
| 52 |
static int pending_clrflags = 0; |
| 53 |
static int pending_valid = 0; |
| 54 |
|
| 55 |
/* Array of all interfaces on the command line. */ |
| 56 |
struct ifconfig *ifs; |
| 57 |
|
| 58 |
/* Size of IFS. */ |
| 59 |
int nifs; |
| 60 |
|
| 61 |
static struct ifconfig ifconfig_initializer = { |
| 62 |
NULL, /* name */ |
| 63 |
0, /* valid */ |
| 64 |
NULL, NULL, 0, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NULL |
| 65 |
}; |
| 66 |
|
| 67 |
struct format formats[] = { |
| 68 |
/* This is the default output format if no system_default_format is |
| 69 |
specified. */ |
| 70 |
{"default", |
| 71 |
"Default format. Equivalent to \"gnu\".", |
| 72 |
"${format}{gnu}"}, |
| 73 |
/* This is the standard GNU output. */ |
| 74 |
{"gnu", |
| 75 |
"Standard GNU output format.", |
| 76 |
"${ifdisplay?}{${first?}{}{${\\n}}${format}{gnu-one-entry}}"}, |
| 77 |
{"gnu-one-entry", |
| 78 |
"Same as GNU, but without additional newlines between the entries.", |
| 79 |
"${format}{check-existence}" |
| 80 |
"${ifdisplay?}{" |
| 81 |
"${name} (${index}):${\\n}" |
| 82 |
"${addr?}{ inet address ${tab}{16}${addr}${\\n}}" |
| 83 |
"${netmask?}{ netmask ${tab}{16}${netmask}${\\n}}" |
| 84 |
"${brdaddr?}{ broadcast ${tab}{16}${brdaddr}${\\n}}" |
| 85 |
"${dstaddr?}{ peer address ${tab}{16}${dstaddr}${\\n}}" |
| 86 |
"${exists?}{tunnel?}{" |
| 87 |
"${tunnel?}{ tunnel src ${tab}{16}${tunsrc}${\\n}}}" |
| 88 |
"${exists?}{tunnel?}{" |
| 89 |
"${tunnel?}{ tunnel dst ${tab}{16}${tundst}${\\n}}}" |
| 90 |
"${flags?}{ flags ${tab}{16}${flags}${\\n}}" |
| 91 |
"${mtu?}{ mtu ${tab}{16}${mtu}${\\n}}" |
| 92 |
"${metric?}{ metric ${tab}{16}${metric}${\\n}}" |
| 93 |
"${exists?}{hwtype?}{${hwtype?}{ link encap ${tab}{16}${hwtype}${\\n}}}" |
| 94 |
"${exists?}{hwaddr?}{${hwaddr?}{ hardware addr ${tab}{16}${hwaddr}${\\n}}}" |
| 95 |
"${media?}{ link medium ${tab}{16}${media}${\\n}}" |
| 96 |
"${status?}{ link status ${tab}{16}${status}${\\n}}" |
| 97 |
"${exists?}{txqlen?}{${txqlen?}{ tx queue len ${tab}{16}${txqlen}${\\n}}}" |
| 98 |
"}"}, |
| 99 |
/* Resembles the output of ifconfig 1.39 (1999-03-19) in net-tools 1.52. */ |
| 100 |
{"net-tools", |
| 101 |
"Similar to the output of net-tools. Default for GNU/Linux.", |
| 102 |
"${format}{check-existence}" |
| 103 |
"${ifdisplay?}{" |
| 104 |
"${name}${exists?}{hwtype?}{${hwtype?}{${tab}{10}Link encap:${hwtype}}" |
| 105 |
"${hwaddr?}{ HWaddr ${hwaddr}}}${\\n}" |
| 106 |
"${addr?}{${tab}{10}inet addr:${addr}" |
| 107 |
"${dstaddr?}{ P-t-P:${dstaddr}}" |
| 108 |
"${brdaddr?}{ Bcast:${brdaddr}}" |
| 109 |
"${netmask?}{ Mask:${netmask}}" |
| 110 |
"${newline}}" |
| 111 |
"${tab}{10}${flags}" |
| 112 |
"${mtu?}{ MTU:${mtu}}" |
| 113 |
"${metric?}{ Metric:${metric}}" |
| 114 |
"${media?}{" |
| 115 |
"${newline}${tab}{10}Media: ${media}" |
| 116 |
"${status?}{, ${status}}}" |
| 117 |
"${exists?}{ifstat?}{" |
| 118 |
"${ifstat?}{" |
| 119 |
"${newline} RX packets:${rxpackets}" |
| 120 |
" errors:${rxerrors} dropped:${rxdropped} overruns:${rxfifoerr}" |
| 121 |
" frame:${rxframeerr}" |
| 122 |
"${newline} TX packets:${txpackets}" |
| 123 |
" errors:${txerrors} dropped:${txdropped} overruns:${txfifoerr}" |
| 124 |
" carrier:${txcarrier}" |
| 125 |
"${newline}" |
| 126 |
" collisions:${collisions}" |
| 127 |
"${exists?}{txqlen?}{${txqlen?}{ txqueuelen:${txqlen}}}" |
| 128 |
"${newline}" |
| 129 |
" RX bytes:${rxbytes} TX bytes:${txbytes}" |
| 130 |
"}}{" |
| 131 |
"${exists?}{txqlen?}{${txqlen?}{ ${tab}{10}txqueuelen:${txqlen}}${\\n}}}" |
| 132 |
"${newline}" |
| 133 |
"${exists?}{map?}{${map?}{${irq?}{" |
| 134 |
" Interrupt:${irq}" |
| 135 |
"${baseaddr?}{ Base address:0x${baseaddr}{%x}}" |
| 136 |
"${memstart?}{ Memory:${memstart}{%lx}-${memend}{%lx}}" |
| 137 |
"${dma?}{ DMA chan:${dma}{%x}}" "${newline}" "}}}" "${newline}" "}"}, |
| 138 |
{"netstat", |
| 139 |
"Terse output, similar to that of \"netstat -i\".", |
| 140 |
"${first?}{Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg${newline}}" |
| 141 |
"${format}{check-existence}" "${name}${tab}{6}${mtu}{%6d} ${metric}{%3d}" |
| 142 |
/* Insert blanks without ifstat. */ |
| 143 |
"${exists?}{ifstat?}{" |
| 144 |
"${ifstat?}{" |
| 145 |
" ${rxpackets}{%8lu} ${rxerrors}{%6lu} ${rxdropped}{%6lu} ${rxfifoerr}{%6lu}" |
| 146 |
" ${txpackets}{%8lu} ${txerrors}{%6lu} ${txdropped}{%6lu} ${txfifoerr}{%6lu}" |
| 147 |
"}{ - no statistics available -}}" |
| 148 |
"${tab}{76} ${flags?}{${flags}{short}}{[NO FLAGS]}" "${newline}"}, |
| 149 |
/* Resembles the output of ifconfig shipped with unix systems like |
| 150 |
Solaris 2.7 or HPUX 10.20. */ |
| 151 |
{"unix", |
| 152 |
"Traditional UNIX interface listing. Default for Solaris, BSD and HPUX.", |
| 153 |
"${format}{check-existence}" |
| 154 |
"${ifdisplay?}{" |
| 155 |
"${name}: flags=${flags}{number}{%x}<${flags}{string}{,}>" |
| 156 |
"${metric?}{ metric ${metric}}" "${mtu?}{ mtu ${mtu}}${\\n}" |
| 157 |
/* Print only if hwtype emits something. */ |
| 158 |
"${exists?}{hwtype?}{" |
| 159 |
"${hwtype?}{${\\t}${hwtype}${exists?}{hwaddr?}{" |
| 160 |
"${hwaddr?}{ ${hwaddr}}}${\\n}}}" |
| 161 |
"${exists?}{tunnel?}{" |
| 162 |
"${tunnel?}{${\\t}tunnel inet ${tunsrc} --> ${tundst}${\\n}}}" |
| 163 |
"${addr?}{${\\t}inet ${addr}" |
| 164 |
"${dstaddr?}{ --> ${dstaddr}}" |
| 165 |
" netmask ${netmask}{0}{%#02x}${netmask}{1}{%02x}" |
| 166 |
"${netmask}{2}{%02x}${netmask}{3}{%02x}" |
| 167 |
"${brdaddr?}{ broadcast ${brdaddr}}${\\n}}" |
| 168 |
"${media?}{${\\t}media: ${media}${\\n}}" |
| 169 |
"${status?}{${\\t}status: ${status}${\\n}}" "}"}, |
| 170 |
/* Resembles the output of ifconfig shipped with OSF 4.0g. */ |
| 171 |
{"osf", |
| 172 |
"OSF-style output.", |
| 173 |
"${format}{check-existence}" |
| 174 |
"${ifdisplay?}{" |
| 175 |
"${name}: flags=${flags}{number}{%x}<${flags}{string}{,}>${\\n}" |
| 176 |
"${exists?}{tunnel?}{" |
| 177 |
"${tunnel?}{${\\t}inet tunnel src ${tunsrc} tunnel dst ${tundst}${\\n}}}" |
| 178 |
"${addr?}{${\\t}inet ${addr}" |
| 179 |
"${dstaddr?}{ --> ${dstaddr}}" |
| 180 |
" netmask ${netmask}{0}{%02x}${netmask}{1}{%02x}" |
| 181 |
"${netmask}{2}{%02x}${netmask}{3}{%02x}" |
| 182 |
"${brdaddr?}{ broadcast ${brdaddr}}" "${mtu?}{ ipmtu ${mtu}}${\\n}}" |
| 183 |
"${exists?}{hwtype?}{" |
| 184 |
"${hwtype?}{${\\t}${hwtype}${exists?}{hwaddr?}{" |
| 185 |
"${hwaddr?}{ ${hwaddr}}}${\\n}}}" "}"}, |
| 186 |
{"check", |
| 187 |
"Shorthand for `check-existence'.", |
| 188 |
"${format}{check-existence}"}, |
| 189 |
/* If interface does not exist, print error message and exit. */ |
| 190 |
{"check-existence", |
| 191 |
"If interface does not exist, print error message and exit.", |
| 192 |
"${index?}{}" |
| 193 |
"{${error}{${progname}: error: interface `${name}' does not exist${\\n}}" |
| 194 |
"${exit}{1}}"}, |
| 195 |
{"?", |
| 196 |
"Synonym for `help'.", |
| 197 |
"${format}{help}"}, |
| 198 |
{"help", |
| 199 |
"Display this help output.", |
| 200 |
"${foreachformat}{" |
| 201 |
"${name}:${tab}{17}${docstr}" |
| 202 |
"${verbose?}{" |
| 203 |
"${newline}" |
| 204 |
"${rep}{79}{-}" |
| 205 |
"${newline}" "${defn}" "${newline}" "}" "${newline}" "}" "${exit}{0}"}, |
| 206 |
{NULL, NULL, NULL} |
| 207 |
}; |
| 208 |
|
| 209 |
/* Default format. */ |
| 210 |
const char *default_format; |
| 211 |
/* Display all interfaces, even if down */ |
| 212 |
int all_option; |
| 213 |
/* True if interfaces were given in the command line */ |
| 214 |
int ifs_cmdline; |
| 215 |
|
| 216 |
enum |
| 217 |
{ |
| 218 |
METRIC_OPTION = 256, |
| 219 |
FORMAT_OPTION, |
| 220 |
UP_OPTION, |
| 221 |
DOWN_OPTION, |
| 222 |
}; |
| 223 |
|
| 224 |
static struct argp_option argp_options[] = { |
| 225 |
#define GRP 10 |
| 226 |
{"verbose", 'v', NULL, 0, |
| 227 |
"output information when configuring interface", GRP}, |
| 228 |
{"all", 'a', NULL, 0, |
| 229 |
"display all available interfaces", GRP}, |
| 230 |
{"interface", 'i', "NAME", 0, |
| 231 |
"configure network interface NAME", GRP}, |
| 232 |
{"address", 'A', "ADDR", 0, |
| 233 |
"set interface address to ADDR", GRP}, |
| 234 |
{"netmask", 'm', "MASK", 0, |
| 235 |
"set netmask to MASK", GRP}, |
| 236 |
{"dstaddr", 'd', "ADDR", 0, |
| 237 |
"set destination (peer) address to ADDR", GRP}, |
| 238 |
{"peer", 'p', "ADDR", OPTION_ALIAS, |
| 239 |
"synonym for dstaddr", GRP}, |
| 240 |
{"broadcast", 'B', "ADDR", 0, |
| 241 |
"set broadcast address to ADDR", GRP}, |
| 242 |
{"brdaddr", 'b', NULL, OPTION_ALIAS, |
| 243 |
"synonym for broadcast", GRP}, /* FIXME: Do we really need it? */ |
| 244 |
{"mtu", 'M', "N", 0, |
| 245 |
"set mtu of interface to N", GRP}, |
| 246 |
{"metric", METRIC_OPTION, "N", 0, |
| 247 |
"set metric of interface to N", GRP}, |
| 248 |
{"format", FORMAT_OPTION, "FORMAT", 0, |
| 249 |
"select output format; set to `help' for info", GRP}, |
| 250 |
{"up", UP_OPTION, NULL, 0, |
| 251 |
"activate the interface", GRP}, |
| 252 |
{"down", DOWN_OPTION, NULL, 0, |
| 253 |
"shut the interface down", GRP}, |
| 254 |
{"flags", 'F', "FLAG[,FLAG...]", 0, |
| 255 |
"set interface flags", GRP}, |
| 256 |
{"list", 'l', NULL, 0, |
| 257 |
"list available or selected interfaces", GRP}, |
| 258 |
{"short", 's', NULL, 0, |
| 259 |
"short output format", GRP}, |
| 260 |
#undef GRP |
| 261 |
{NULL, 0, NULL, 0, NULL, 0} |
| 262 |
}; |
| 263 |
|
| 264 |
const char doc[] = "Configure network interfaces."; |
| 265 |
const char *program_authors[] = { |
| 266 |
"Marcus Brinkmann", |
| 267 |
NULL |
| 268 |
}; |
| 269 |
|
| 270 |
struct format * |
| 271 |
format_find (const char *name) |
| 272 |
{ |
| 273 |
struct format *frm; |
| 274 |
|
| 275 |
for (frm = formats; frm->name; frm++) |
| 276 |
{ |
| 277 |
if (strcmp (frm->name, name) == 0) |
| 278 |
return frm; |
| 279 |
} |
| 280 |
return NULL; |
| 281 |
} |
| 282 |
|
| 283 |
struct ifconfig * |
| 284 |
parse_opt_new_ifs (char *name) |
| 285 |
{ |
| 286 |
struct ifconfig *ifp; |
| 287 |
|
| 288 |
ifs_cmdline = 1; |
| 289 |
ifs = realloc (ifs, ++nifs * sizeof (struct ifconfig)); |
| 290 |
if (!ifs) |
| 291 |
error (EXIT_FAILURE, errno, |
| 292 |
"can't get memory for interface configuration"); |
| 293 |
ifp = &ifs[nifs - 1]; |
| 294 |
*ifp = ifconfig_initializer; |
| 295 |
ifp->name = name; |
| 296 |
return ifp; |
| 297 |
} |
| 298 |
|
| 299 |
#define PARSE_OPT_SET_ADDR(field, fname, fvalid) \ |
| 300 |
void \ |
| 301 |
parse_opt_set_##field (struct ifconfig *ifp, char *addr) \ |
| 302 |
{ \ |
| 303 |
if (!ifp) \ |
| 304 |
error (EXIT_FAILURE, 0, \ |
| 305 |
"no interface specified for %s `%s'", #fname, addr); \ |
| 306 |
if (ifp->valid & IF_VALID_##fvalid) \ |
| 307 |
error (EXIT_FAILURE, 0, \ |
| 308 |
"only one %s allowed for interface `%s'", \ |
| 309 |
#fname, ifp->name); \ |
| 310 |
ifp->field = addr; \ |
| 311 |
ifp->valid |= IF_VALID_##fvalid; \ |
| 312 |
} |
| 313 |
|
| 314 |
PARSE_OPT_SET_ADDR (address, address, ADDR) |
| 315 |
PARSE_OPT_SET_ADDR (netmask, netmask, NETMASK) |
| 316 |
PARSE_OPT_SET_ADDR (dstaddr, destination / peer address, DSTADDR) |
| 317 |
PARSE_OPT_SET_ADDR (brdaddr, broadcast address, BRDADDR) |
| 318 |
PARSE_OPT_SET_ADDR (hwaddr, hardware address, HWADDR) |
| 319 |
#define PARSE_OPT_SET_INT(field, fname, fvalid) \ |
| 320 |
void \ |
| 321 |
parse_opt_set_##field (struct ifconfig *ifp, char *arg) \ |
| 322 |
{ \ |
| 323 |
char *end; \ |
| 324 |
if (!ifp) \ |
| 325 |
error (EXIT_FAILURE, 0, \ |
| 326 |
"no interface specified for %s `%s'\n", \ |
| 327 |
#fname, arg); \ |
| 328 |
if (ifp->valid & IF_VALID_##fvalid) \ |
| 329 |
error (EXIT_FAILURE, 0, \ |
| 330 |
"only one %s allowed for interface `%s'", \ |
| 331 |
#fname, ifp->name); \ |
| 332 |
ifp->field = strtol (arg, &end, 0); \ |
| 333 |
if (*arg == '\0' || *end != '\0') \ |
| 334 |
error (EXIT_FAILURE, 0, \ |
| 335 |
"mtu value `%s' for interface `%s' is not a number", \ |
| 336 |
arg, ifp->name); \ |
| 337 |
ifp->valid |= IF_VALID_##fvalid; \ |
| 338 |
} |
| 339 |
PARSE_OPT_SET_INT (mtu, mtu value, MTU) |
| 340 |
PARSE_OPT_SET_INT (metric, metric value, METRIC) |
| 341 |
void parse_opt_set_af (struct ifconfig *ifp, char *af) |
| 342 |
{ |
| 343 |
if (!ifp) |
| 344 |
error (EXIT_FAILURE, 0, |
| 345 |
"no interface specified for address family `%s'", af); |
| 346 |
|
| 347 |
if (!strcasecmp (af, "inet")) |
| 348 |
ifp->af = AF_INET; |
| 349 |
else |
| 350 |
error (EXIT_FAILURE, 0, |
| 351 |
"unknown address family `%s' for interface `%s': is not a number", |
| 352 |
af, ifp->name); |
| 353 |
ifp->valid |= IF_VALID_AF; |
| 354 |
} |
| 355 |
|
| 356 |
void |
| 357 |
parse_opt_set_flag (struct ifconfig *ifp MAYBE_UNUSED, int flag, int rev) |
| 358 |
{ |
| 359 |
if (ifp) |
| 360 |
ifp->valid |= IF_VALID_FLAGS; |
| 361 |
else |
| 362 |
pending_valid |= IF_VALID_FLAGS; |
| 363 |
|
| 364 |
if (rev) |
| 365 |
{ |
| 366 |
pending_clrflags |= flag; |
| 367 |
pending_setflags &= ~flag; |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
pending_setflags |= flag; |
| 372 |
pending_clrflags &= ~flag; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
void |
| 377 |
parse_opt_flag_list (struct ifconfig *ifp, const char *name) |
| 378 |
{ |
| 379 |
while (*name) |
| 380 |
{ |
| 381 |
int mask, rev; |
| 382 |
char *p = strchr (name, ','); |
| 383 |
size_t len; |
| 384 |
|
| 385 |
if (p) |
| 386 |
len = p - name; |
| 387 |
else |
| 388 |
len = strlen (name); |
| 389 |
|
| 390 |
if ((mask = if_nametoflag (name, len, &rev)) == 0) |
| 391 |
error (EXIT_FAILURE, 0, "unknown flag %*.*s", |
| 392 |
(int) len, (int) len, name); |
| 393 |
parse_opt_set_flag (ifp, mask, rev); |
| 394 |
|
| 395 |
name += len; |
| 396 |
if (p) |
| 397 |
name++; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
void |
| 402 |
parse_opt_set_point_to_point (struct ifconfig *ifp, char *addr) |
| 403 |
{ |
| 404 |
parse_opt_set_dstaddr (ifp, addr); |
| 405 |
parse_opt_set_flag (ifp, IFF_POINTOPOINT, 0); |
| 406 |
} |
| 407 |
|
| 408 |
void |
| 409 |
parse_opt_set_default_format (const char *format) |
| 410 |
{ |
| 411 |
struct format *frm; |
| 412 |
|
| 413 |
if (!format) |
| 414 |
format = system_default_format ? system_default_format : "default"; |
| 415 |
|
| 416 |
for (frm = formats; frm->name; frm++) |
| 417 |
if (!strcmp (format, frm->name)) |
| 418 |
break; |
| 419 |
if (frm == NULL || frm->templ == NULL) |
| 420 |
error (EXIT_FAILURE, 0, "%s: unknown output format", format); |
| 421 |
default_format = frm->templ; |
| 422 |
} |
| 423 |
|
| 424 |
static int |
| 425 |
is_comment_line (const char *p, size_t len) |
| 426 |
{ |
| 427 |
while (len--) |
| 428 |
{ |
| 429 |
int c = *p++; |
| 430 |
switch (c) |
| 431 |
{ |
| 432 |
case ' ': |
| 433 |
case '\t': |
| 434 |
continue; |
| 435 |
|
| 436 |
case '#': |
| 437 |
return 1; |
| 438 |
|
| 439 |
default: |
| 440 |
return 0; |
| 441 |
} |
| 442 |
} |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
void |
| 447 |
parse_opt_set_default_format_from_file (const char *file) |
| 448 |
{ |
| 449 |
static struct obstack stk; |
| 450 |
FILE *fp; |
| 451 |
char *buf = NULL; |
| 452 |
size_t size = 0; |
| 453 |
|
| 454 |
fp = fopen (file, "r"); |
| 455 |
if (!fp) |
| 456 |
error (EXIT_FAILURE, errno, "cannot open format file %s", file); |
| 457 |
|
| 458 |
obstack_init (&stk); |
| 459 |
while (getline (&buf, &size, fp) > 0) |
| 460 |
{ |
| 461 |
size_t len = strlen (buf); |
| 462 |
|
| 463 |
if (len >= 1 && buf[len - 1] == '\n') |
| 464 |
len--; |
| 465 |
|
| 466 |
if (len == 0 || is_comment_line (buf, len)) |
| 467 |
continue; |
| 468 |
obstack_grow (&stk, buf, len); |
| 469 |
} |
| 470 |
free (buf); |
| 471 |
fclose (fp); |
| 472 |
obstack_1grow (&stk, 0); |
| 473 |
default_format = obstack_finish (&stk); |
| 474 |
} |
| 475 |
|
| 476 |
/* Must be reentrant! */ |
| 477 |
void |
| 478 |
parse_opt_finalize (struct ifconfig *ifp) |
| 479 |
{ |
| 480 |
/* The flags `--up' and `--down' are allowed early. */ |
| 481 |
if (ifp && pending_valid) |
| 482 |
{ |
| 483 |
ifp->valid |= pending_valid; |
| 484 |
pending_valid = 0; |
| 485 |
} |
| 486 |
|
| 487 |
/* Only the empty set of actions, i.e., only the interface name |
| 488 |
* is present on the command line, merits printout of status. |
| 489 |
*/ |
| 490 |
if (ifp && !ifp->valid) |
| 491 |
{ |
| 492 |
ifp->valid = IF_VALID_FORMAT; |
| 493 |
ifp->format = default_format; |
| 494 |
} |
| 495 |
|
| 496 |
if (ifp && (pending_setflags | pending_clrflags)) |
| 497 |
{ |
| 498 |
ifp->setflags |= pending_setflags; |
| 499 |
ifp->clrflags |= pending_clrflags; |
| 500 |
pending_setflags = pending_clrflags = 0; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
static error_t |
| 505 |
parse_opt (int key, char *arg, struct argp_state *state) |
| 506 |
{ |
| 507 |
struct ifconfig *ifp = *(struct ifconfig **) state->input; |
| 508 |
|
| 509 |
switch (key) |
| 510 |
{ |
| 511 |
case ARGP_KEY_INIT: |
| 512 |
state->child_inputs[0] = state->input; |
| 513 |
break; |
| 514 |
|
| 515 |
case 'i': /* Interface name. */ |
| 516 |
parse_opt_finalize (ifp); |
| 517 |
ifp = parse_opt_new_ifs (arg); |
| 518 |
*(struct ifconfig **) state->input = ifp; |
| 519 |
break; |
| 520 |
|
| 521 |
case 'a': |
| 522 |
all_option = 1; |
| 523 |
break; |
| 524 |
|
| 525 |
case 'A': /* Interface address. */ |
| 526 |
{ |
| 527 |
char *netlen = strchr (arg, '/'); |
| 528 |
|
| 529 |
if (netlen) |
| 530 |
{ |
| 531 |
char *end, *str; |
| 532 |
unsigned n; |
| 533 |
struct in_addr addr; |
| 534 |
|
| 535 |
*netlen++ = 0; |
| 536 |
|
| 537 |
n = strtol (netlen, &end, 10); |
| 538 |
if (!(*netlen && !*end) || n > 32) |
| 539 |
error (EXIT_FAILURE, 0, "Wrong netmask length %s", netlen); |
| 540 |
|
| 541 |
addr.s_addr = |
| 542 |
n ? htonl (INADDR_BROADCAST << (32 - n)) : INADDR_ANY; |
| 543 |
str = strdup (inet_ntoa (addr)); |
| 544 |
parse_opt_set_netmask (ifp, str); |
| 545 |
} |
| 546 |
|
| 547 |
parse_opt_set_address (ifp, arg); |
| 548 |
break; |
| 549 |
} |
| 550 |
|
| 551 |
case 'm': /* Interface netmask. */ |
| 552 |
parse_opt_set_netmask (ifp, arg); |
| 553 |
break; |
| 554 |
|
| 555 |
case 'd': /* Interface dstaddr. */ |
| 556 |
case 'p': |
| 557 |
parse_opt_set_point_to_point (ifp, arg); |
| 558 |
break; |
| 559 |
|
| 560 |
case 'b': /* Interface broadcast address. */ |
| 561 |
case 'B': |
| 562 |
parse_opt_set_brdaddr (ifp, arg); |
| 563 |
break; |
| 564 |
|
| 565 |
case 'F': |
| 566 |
parse_opt_flag_list (ifp, arg); |
| 567 |
break; |
| 568 |
|
| 569 |
case 'M': /* Interface MTU. */ |
| 570 |
parse_opt_set_mtu (ifp, arg); |
| 571 |
break; |
| 572 |
|
| 573 |
case 's': |
| 574 |
parse_opt_set_default_format ("netstat"); |
| 575 |
break; |
| 576 |
|
| 577 |
case 'l': |
| 578 |
list_mode++; |
| 579 |
break; |
| 580 |
|
| 581 |
case 'v': |
| 582 |
verbose++; |
| 583 |
break; |
| 584 |
|
| 585 |
case METRIC_OPTION: /* Interface metric. */ |
| 586 |
parse_opt_set_metric (ifp, arg); |
| 587 |
break; |
| 588 |
|
| 589 |
case FORMAT_OPTION: /* Output format. */ |
| 590 |
if (arg && arg[0] == '@') |
| 591 |
parse_opt_set_default_format_from_file (arg + 1); |
| 592 |
else |
| 593 |
parse_opt_set_default_format (arg); |
| 594 |
break; |
| 595 |
|
| 596 |
case UP_OPTION: |
| 597 |
parse_opt_set_flag (ifp, IFF_UP | IFF_RUNNING, 0); |
| 598 |
break; |
| 599 |
|
| 600 |
case DOWN_OPTION: |
| 601 |
parse_opt_set_flag (ifp, IFF_UP, 1); |
| 602 |
break; |
| 603 |
|
| 604 |
default: |
| 605 |
return ARGP_ERR_UNKNOWN; |
| 606 |
} |
| 607 |
|
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 611 |
static char * |
| 612 |
default_help_filter (int key, const char *text, void *input MAYBE_UNUSED) |
| 613 |
{ |
| 614 |
char *s; |
| 615 |
|
| 616 |
switch (key) |
| 617 |
{ |
| 618 |
default: |
| 619 |
s = (char *) text; |
| 620 |
break; |
| 621 |
|
| 622 |
case ARGP_KEY_HELP_EXTRA: |
| 623 |
s = if_list_flags ("Known flags are: "); |
| 624 |
} |
| 625 |
return s; |
| 626 |
} |
| 627 |
|
| 628 |
static struct argp_child argp_children[2]; |
| 629 |
static struct argp argp = { |
| 630 |
argp_options, |
| 631 |
parse_opt, |
| 632 |
NULL, |
| 633 |
doc, |
| 634 |
NULL, |
| 635 |
default_help_filter, |
| 636 |
NULL |
| 637 |
}; |
| 638 |
|
| 639 |
static int |
| 640 |
cmp_if_name (const void *a, const void *b) |
| 641 |
{ |
| 642 |
const struct ifconfig *ifa = a; |
| 643 |
const struct ifconfig *ifb = b; |
| 644 |
|
| 645 |
return strcmp (ifa->name, ifb->name); |
| 646 |
} |
| 647 |
|
| 648 |
void |
| 649 |
parse_cmdline (int argc, char *argv[]) |
| 650 |
{ |
| 651 |
int index; |
| 652 |
struct ifconfig *ifp = ifs; |
| 653 |
|
| 654 |
parse_opt_set_default_format (NULL); |
| 655 |
iu_argp_init ("ifconfig", program_authors); |
| 656 |
argp_children[0] = system_argp_child; |
| 657 |
argp.children = argp_children; |
| 658 |
argp.args_doc = system_help; |
| 659 |
argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &index, &ifp); |
| 660 |
|
| 661 |
parse_opt_finalize (ifp); |
| 662 |
|
| 663 |
if (index < argc) |
| 664 |
{ |
| 665 |
if (!system_parse_opt_rest (&ifp, argc - index, &argv[index])) |
| 666 |
error (EXIT_FAILURE, 0, "invalid arguments"); |
| 667 |
parse_opt_finalize (ifp); |
| 668 |
} |
| 669 |
if (!ifs) |
| 670 |
{ |
| 671 |
/* No interfaces specified. Get a list of all interfaces. */ |
| 672 |
struct if_nameindex *ifnx, *ifnxp; |
| 673 |
|
| 674 |
ifnx = ifnxp = system_if_nameindex (); |
| 675 |
if (!ifnx) |
| 676 |
error (EXIT_FAILURE, 0, "could not get list of interfaces"); |
| 677 |
while (ifnxp->if_index != 0 || ifnxp->if_name != NULL) |
| 678 |
{ |
| 679 |
struct ifconfig *ifp; |
| 680 |
|
| 681 |
ifs = realloc (ifs, ++nifs * sizeof (struct ifconfig)); |
| 682 |
if (!ifs) |
| 683 |
error (EXIT_FAILURE, errno, |
| 684 |
"can't get memory for interface configuration"); |
| 685 |
ifp = &ifs[nifs - 1]; |
| 686 |
*ifp = ifconfig_initializer; |
| 687 |
ifp->name = strdup (ifnxp->if_name); |
| 688 |
if (!ifs) |
| 689 |
error (EXIT_FAILURE, errno, |
| 690 |
"can't get memory for interface configuration name"); |
| 691 |
ifp->valid = IF_VALID_FORMAT; |
| 692 |
ifp->format = default_format; |
| 693 |
ifnxp++; |
| 694 |
} |
| 695 |
if_freenameindex (ifnx); |
| 696 |
qsort (ifs, nifs, sizeof (ifs[0]), cmp_if_name); |
| 697 |
} |
| 698 |
} |