| 1 |
/* printif.c -- print an interface configuration |
| 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 |
#include <sys/stat.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <errno.h> |
| 25 |
|
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#if STDC_HEADERS |
| 31 |
# include <stdlib.h> |
| 32 |
#else |
| 33 |
# ifndef HAVE_STRCHR |
| 34 |
# define strchr index |
| 35 |
# define strrchr rindex |
| 36 |
# endif |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <alloca.h> |
| 40 |
#include <ctype.h> |
| 41 |
#include <sys/types.h> |
| 42 |
#include <sys/socket.h> |
| 43 |
#include <sys/ioctl.h> |
| 44 |
#include <net/if.h> |
| 45 |
#include <netinet/in.h> |
| 46 |
#include <arpa/inet.h> |
| 47 |
#include "ifconfig.h" |
| 48 |
#include "xalloc.h" |
| 49 |
#include <attribute.h> |
| 50 |
|
| 51 |
FILE *ostream; /* Either stdout or stderror. */ |
| 52 |
int column_stdout; /* The column position of the cursor on stdout. */ |
| 53 |
int column_stderr; /* The column position of the cursor on stderr. */ |
| 54 |
int *column = &column_stdout; /* The column marker of ostream. */ |
| 55 |
int had_output; /* True if we had any output. */ |
| 56 |
|
| 57 |
struct format_handle format_handles[] = { |
| 58 |
#ifdef SYSTEM_FORMAT_HANDLER |
| 59 |
SYSTEM_FORMAT_HANDLER |
| 60 |
#endif |
| 61 |
{"", fh_nothing}, |
| 62 |
{"format?", fh_format_query}, |
| 63 |
{"docstr", fh_docstr}, |
| 64 |
{"defn", fh_defn}, |
| 65 |
{"foreachformat", fh_foreachformat}, |
| 66 |
{"verbose?", fh_verbose_query}, |
| 67 |
{"newline", fh_newline}, |
| 68 |
{"\\n", fh_newline}, |
| 69 |
{"\\t", fh_tabulator}, |
| 70 |
{"rep", fh_rep}, |
| 71 |
{"first?", fh_first}, |
| 72 |
{"ifdisplay?", fh_ifdisplay_query}, |
| 73 |
{"tab", fh_tab}, |
| 74 |
{"join", fh_join}, |
| 75 |
{"exists?", fh_exists_query}, |
| 76 |
{"format", fh_format}, |
| 77 |
{"error", fh_error}, |
| 78 |
{"progname", fh_progname}, |
| 79 |
{"exit", fh_exit}, |
| 80 |
|
| 81 |
{"name", fh_name}, |
| 82 |
{"index?", fh_index_query}, |
| 83 |
{"index", fh_index}, |
| 84 |
{"addr?", fh_addr_query}, |
| 85 |
{"addr", fh_addr}, |
| 86 |
{"netmask?", fh_netmask_query}, |
| 87 |
{"netmask", fh_netmask}, |
| 88 |
{"brdaddr?", fh_brdaddr_query}, |
| 89 |
{"brdaddr", fh_brdaddr}, |
| 90 |
{"dstaddr?", fh_dstaddr_query}, |
| 91 |
{"dstaddr", fh_dstaddr}, |
| 92 |
{"flags?", fh_flags_query}, |
| 93 |
{"flags", fh_flags}, |
| 94 |
{"mtu?", fh_mtu_query}, |
| 95 |
{"mtu", fh_mtu}, |
| 96 |
{"metric?", fh_metric_query}, |
| 97 |
{"metric", fh_metric}, |
| 98 |
{"media?", fh_media_query}, |
| 99 |
{"media", fh_media}, |
| 100 |
{"status?", fh_status_query}, |
| 101 |
{"status", fh_status}, |
| 102 |
#ifdef HAVE_STRUCT_IFREQ_IFR_MAP |
| 103 |
{"map?", fh_map_query}, |
| 104 |
{"irq?", fh_irq_query}, |
| 105 |
{"irq", fh_irq}, |
| 106 |
{"baseaddr?", fh_baseaddr_query}, |
| 107 |
{"baseaddr", fh_baseaddr}, |
| 108 |
{"memstart?", fh_memstart_query}, |
| 109 |
{"memstart", fh_memstart}, |
| 110 |
{"memend?", fh_memend_query}, |
| 111 |
{"memend", fh_memend}, |
| 112 |
{"dma?", fh_dma_query}, |
| 113 |
{"dma", fh_dma}, |
| 114 |
#endif /* HAVE_STRUCT_IFREQ_IFR_MAP */ |
| 115 |
{NULL, NULL} |
| 116 |
}; |
| 117 |
|
| 118 |
/* Various helper functions to get the job done. */ |
| 119 |
|
| 120 |
void |
| 121 |
put_char (format_data_t form MAYBE_UNUSED, char c) |
| 122 |
{ |
| 123 |
switch (c) |
| 124 |
{ |
| 125 |
case '\n': |
| 126 |
*column = 0; |
| 127 |
break; |
| 128 |
case '\t': |
| 129 |
*column = ((*column / TAB_STOP) + 1) * TAB_STOP; |
| 130 |
break; |
| 131 |
default: |
| 132 |
(*column)++; |
| 133 |
} |
| 134 |
putc (c, ostream); |
| 135 |
had_output = 1; |
| 136 |
} |
| 137 |
|
| 138 |
/* This is a simple print function, which tries to keep track of the |
| 139 |
column. Of course, terminal behaviour can defeat this. We should |
| 140 |
provide a handler to switch on/off column counting. */ |
| 141 |
void |
| 142 |
put_string (format_data_t form, const char *s) |
| 143 |
{ |
| 144 |
while (*s != '\0') |
| 145 |
put_char (form, *(s++)); |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
put_int (format_data_t form MAYBE_UNUSED, int argc, char *argv[], int nr) |
| 150 |
{ |
| 151 |
char *fmt; |
| 152 |
if (argc > 0) |
| 153 |
{ |
| 154 |
char *p = argv[0]; |
| 155 |
|
| 156 |
if (*p != '%') |
| 157 |
fmt = "%i"; |
| 158 |
else |
| 159 |
{ |
| 160 |
p++; |
| 161 |
|
| 162 |
if (*p == '#') |
| 163 |
p++; |
| 164 |
|
| 165 |
while (isdigit (*p)) |
| 166 |
p++; |
| 167 |
|
| 168 |
if ((*p == 'h' || *p == 'H') && p[1]) |
| 169 |
++p; /* Half length modifier, go to type specifier. */ |
| 170 |
|
| 171 |
switch (*p) |
| 172 |
{ |
| 173 |
default: |
| 174 |
case 'i': |
| 175 |
case 'd': |
| 176 |
case 'D': |
| 177 |
*p = 'i'; |
| 178 |
break; |
| 179 |
case 'x': |
| 180 |
case 'h': |
| 181 |
*p = 'x'; |
| 182 |
break; |
| 183 |
case 'X': |
| 184 |
case 'H': |
| 185 |
*p = 'X'; |
| 186 |
break; |
| 187 |
case 'o': |
| 188 |
case 'O': |
| 189 |
*p = 'o'; |
| 190 |
break; |
| 191 |
} |
| 192 |
p++; |
| 193 |
*p = '\0'; |
| 194 |
fmt = argv[0]; |
| 195 |
} |
| 196 |
} |
| 197 |
else |
| 198 |
fmt = "%i"; |
| 199 |
|
| 200 |
*column += printf (fmt, nr); |
| 201 |
had_output = 1; |
| 202 |
} |
| 203 |
|
| 204 |
void |
| 205 |
put_ulong (format_data_t form MAYBE_UNUSED, |
| 206 |
int argc, char *argv[], unsigned long value) |
| 207 |
{ |
| 208 |
char *fmt; |
| 209 |
if (argc > 0) |
| 210 |
{ |
| 211 |
char *p = argv[0]; |
| 212 |
|
| 213 |
if (*p != '%') |
| 214 |
fmt = "%lu"; |
| 215 |
else |
| 216 |
{ |
| 217 |
p++; |
| 218 |
|
| 219 |
while (isdigit (*p)) |
| 220 |
p++; |
| 221 |
|
| 222 |
if (*p == '#') |
| 223 |
p++; |
| 224 |
|
| 225 |
if (*p == 'l') |
| 226 |
p++; |
| 227 |
|
| 228 |
switch (*p) |
| 229 |
{ |
| 230 |
default: |
| 231 |
case 'i': |
| 232 |
case 'd': |
| 233 |
case 'D': |
| 234 |
*p = 'i'; |
| 235 |
break; |
| 236 |
case 'x': |
| 237 |
case 'h': |
| 238 |
*p = 'x'; |
| 239 |
break; |
| 240 |
case 'X': |
| 241 |
case 'H': |
| 242 |
*p = 'X'; |
| 243 |
break; |
| 244 |
case 'o': |
| 245 |
case 'O': |
| 246 |
*p = 'o'; |
| 247 |
break; |
| 248 |
} |
| 249 |
p++; |
| 250 |
*p = '\0'; |
| 251 |
fmt = argv[0]; |
| 252 |
} |
| 253 |
} |
| 254 |
else |
| 255 |
fmt = "%lu"; |
| 256 |
|
| 257 |
*column += printf (fmt, value); |
| 258 |
had_output = 1; |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
select_arg (format_data_t form, int argc, char *argv[], int nr) |
| 263 |
{ |
| 264 |
if (nr < argc) |
| 265 |
{ |
| 266 |
form->format = argv[nr]; |
| 267 |
print_interfaceX (form, 0); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void |
| 272 |
put_addr (format_data_t form, int argc, char *argv[], struct sockaddr *sa) |
| 273 |
{ |
| 274 |
struct sockaddr_in *sin = (struct sockaddr_in *) sa; |
| 275 |
char *addr = inet_ntoa (sin->sin_addr); |
| 276 |
long byte[4]; |
| 277 |
char *p = strchr (addr, '.'); |
| 278 |
|
| 279 |
*p = '\0'; |
| 280 |
byte[0] = strtol (addr, NULL, 0); |
| 281 |
addr = p + 1; |
| 282 |
p = strchr (addr, '.'); |
| 283 |
*p = '\0'; |
| 284 |
byte[1] = strtol (addr, NULL, 0); |
| 285 |
addr = p + 1; |
| 286 |
p = strchr (addr, '.'); |
| 287 |
*p = '\0'; |
| 288 |
byte[2] = strtol (addr, NULL, 0); |
| 289 |
byte[3] = strtol (p + 1, NULL, 0); |
| 290 |
|
| 291 |
addr = inet_ntoa (sin->sin_addr); |
| 292 |
|
| 293 |
if (argc > 0) |
| 294 |
{ |
| 295 |
long i = strtol (argv[0], NULL, 0); |
| 296 |
if (i >= 0 && i <= 3) |
| 297 |
put_int (form, argc - 1, &argv[1], byte[i]); |
| 298 |
} |
| 299 |
else |
| 300 |
put_string (form, addr); |
| 301 |
} |
| 302 |
|
| 303 |
void |
| 304 |
put_flags (format_data_t form, int argc, char *argv[], int flags) |
| 305 |
{ |
| 306 |
unsigned int f = 1; |
| 307 |
const char *name; |
| 308 |
int first = 1; |
| 309 |
unsigned int uflags = (unsigned int) flags; |
| 310 |
|
| 311 |
while (uflags && f) |
| 312 |
{ |
| 313 |
if (f & uflags) |
| 314 |
{ |
| 315 |
name = if_flagtoname (f, NULL); |
| 316 |
if (name) |
| 317 |
{ |
| 318 |
if (!first) |
| 319 |
{ |
| 320 |
if (argc > 0) |
| 321 |
put_string (form, argv[0]); |
| 322 |
else |
| 323 |
put_char (form, ' '); |
| 324 |
} |
| 325 |
put_string (form, name); |
| 326 |
uflags &= ~f; |
| 327 |
first = 0; |
| 328 |
} |
| 329 |
} |
| 330 |
f = f << 1; |
| 331 |
} |
| 332 |
if (uflags) |
| 333 |
{ |
| 334 |
if (!first) |
| 335 |
{ |
| 336 |
if (argc > 0) |
| 337 |
put_string (form, argv[0]); |
| 338 |
else |
| 339 |
put_char (form, ' '); |
| 340 |
} |
| 341 |
put_int (form, argc - 1, &argv[1], uflags); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
void |
| 346 |
put_flags_short (format_data_t form, int argc MAYBE_UNUSED, |
| 347 |
MAYBE_UNUSED char *argv[], int flags) |
| 348 |
{ |
| 349 |
char buf[IF_FORMAT_FLAGS_BUFSIZE]; |
| 350 |
if_format_flags (flags, buf, sizeof buf); |
| 351 |
put_string (form, buf); |
| 352 |
} |
| 353 |
|
| 354 |
/* Format handler can mangle form->format, so update it after calling |
| 355 |
here. */ |
| 356 |
void |
| 357 |
format_handler (const char *name, format_data_t form, int argc, char *argv[]) |
| 358 |
{ |
| 359 |
struct format_handle *fh; |
| 360 |
|
| 361 |
for (fh = format_handles; fh->name; fh++) |
| 362 |
{ |
| 363 |
if (!strcmp (fh->name, name)) |
| 364 |
{ |
| 365 |
if (fh->handler) |
| 366 |
(fh->handler) (form, argc, argv); |
| 367 |
return; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
*column += printf ("("); |
| 372 |
put_string (form, name); |
| 373 |
*column += printf (" unknown)"); |
| 374 |
had_output = 1; |
| 375 |
} |
| 376 |
|
| 377 |
void |
| 378 |
fh_nothing (format_data_t form MAYBE_UNUSED, |
| 379 |
int argc MAYBE_UNUSED, MAYBE_UNUSED char *argv[]) |
| 380 |
{ |
| 381 |
} |
| 382 |
|
| 383 |
void |
| 384 |
fh_format_query (format_data_t form, int argc, char *argv[]) |
| 385 |
{ |
| 386 |
if (argc < 1) |
| 387 |
return; |
| 388 |
select_arg (form, argc, argv, format_find (argv[0]) ? 1 : 2); |
| 389 |
} |
| 390 |
|
| 391 |
void |
| 392 |
fh_docstr (format_data_t form, int argc, char *argv[]) |
| 393 |
{ |
| 394 |
const char *name; |
| 395 |
struct format *frm; |
| 396 |
|
| 397 |
name = (argc == 0) ? form->name : argv[0]; |
| 398 |
frm = format_find (name); |
| 399 |
if (!frm) |
| 400 |
error (EXIT_FAILURE, errno, "unknown format: `%s'", name); |
| 401 |
put_string (form, frm->docstr); |
| 402 |
} |
| 403 |
|
| 404 |
void |
| 405 |
fh_defn (format_data_t form, int argc, char *argv[]) |
| 406 |
{ |
| 407 |
const char *name; |
| 408 |
struct format *frm; |
| 409 |
|
| 410 |
name = (argc == 0) ? form->name : argv[0]; |
| 411 |
|
| 412 |
frm = format_find (name); |
| 413 |
if (!frm) |
| 414 |
error (EXIT_FAILURE, errno, "unknown format: `%s'", name); |
| 415 |
put_string (form, frm->templ); |
| 416 |
} |
| 417 |
|
| 418 |
void |
| 419 |
fh_foreachformat (format_data_t form, int argc, char *argv[]) |
| 420 |
{ |
| 421 |
struct format *frm; |
| 422 |
const char *save_name; |
| 423 |
|
| 424 |
if (argc == 0) |
| 425 |
return; |
| 426 |
|
| 427 |
save_name = form->name; |
| 428 |
for (frm = formats; frm->name; frm++) |
| 429 |
{ |
| 430 |
form->name = frm->name; |
| 431 |
form->format = argv[0]; |
| 432 |
print_interfaceX (form, 0); |
| 433 |
} |
| 434 |
form->name = save_name; |
| 435 |
} |
| 436 |
|
| 437 |
void |
| 438 |
fh_newline (format_data_t form, int argc MAYBE_UNUSED, |
| 439 |
MAYBE_UNUSED char *argv[]) |
| 440 |
{ |
| 441 |
put_char (form, '\n'); |
| 442 |
} |
| 443 |
|
| 444 |
void |
| 445 |
fh_tabulator (format_data_t form, int argc MAYBE_UNUSED, |
| 446 |
MAYBE_UNUSED char *argv[]) |
| 447 |
{ |
| 448 |
put_char (form, '\t'); |
| 449 |
} |
| 450 |
|
| 451 |
void |
| 452 |
fh_rep (format_data_t form, int argc, char *argv[]) |
| 453 |
{ |
| 454 |
unsigned int count; |
| 455 |
char *p; |
| 456 |
|
| 457 |
if (argc < 2) |
| 458 |
return; |
| 459 |
count = strtoul (argv[0], &p, 10); |
| 460 |
if (*p) |
| 461 |
error (EXIT_FAILURE, 0, "invalid repeat count"); |
| 462 |
while (count--) |
| 463 |
{ |
| 464 |
form->format = argv[1]; |
| 465 |
print_interfaceX (form, 0); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
void |
| 470 |
fh_first (format_data_t form, int argc, char *argv[]) |
| 471 |
{ |
| 472 |
select_arg (form, argc, argv, form->first ? 0 : 1); |
| 473 |
} |
| 474 |
|
| 475 |
void |
| 476 |
fh_ifdisplay_query (format_data_t form, int argc, char *argv[]) |
| 477 |
{ |
| 478 |
int n; |
| 479 |
|
| 480 |
#ifdef SIOCGIFFLAGS |
| 481 |
/* Request for all, or for a specified interface? */ |
| 482 |
n = all_option || ifs_cmdline; |
| 483 |
if (!n) |
| 484 |
{ |
| 485 |
/* Otherwise, only interfaces in state `UP' are displayed. */ |
| 486 |
int rev = 0; |
| 487 |
int f = if_nameztoflag ("UP", &rev); |
| 488 |
|
| 489 |
n = f && ioctl (form->sfd, SIOCGIFFLAGS, form->ifr) == 0; |
| 490 |
if (n) |
| 491 |
{ |
| 492 |
unsigned int uflags = (unsigned short) form->ifr->ifr_flags; |
| 493 |
|
| 494 |
# ifdef ifr_flagshigh |
| 495 |
uflags |= (unsigned short) form->ifr->ifr_flagshigh << 16; |
| 496 |
# endif/* ifr_flagshigh */ |
| 497 |
|
| 498 |
n = n && (f & uflags); |
| 499 |
}; |
| 500 |
} |
| 501 |
#else |
| 502 |
n = 1; /* Display all of them. */ |
| 503 |
#endif |
| 504 |
select_arg (form, argc, argv, !n); |
| 505 |
} |
| 506 |
|
| 507 |
void |
| 508 |
fh_verbose_query (format_data_t form, int argc, char *argv[]) |
| 509 |
{ |
| 510 |
select_arg (form, argc, argv, verbose ? 0 : 1); |
| 511 |
} |
| 512 |
|
| 513 |
/* A tab implementation, which fills with spaces up to requested column or next |
| 514 |
tabstop. */ |
| 515 |
void |
| 516 |
fh_tab (format_data_t form, int argc, char *argv[]) |
| 517 |
{ |
| 518 |
long goal = 0; |
| 519 |
|
| 520 |
errno = 0; |
| 521 |
if (argc >= 1) |
| 522 |
goal = strtol (argv[0], NULL, 0); |
| 523 |
if (goal <= 0) |
| 524 |
goal = ((*column / TAB_STOP) + 1) * TAB_STOP; |
| 525 |
|
| 526 |
while (*column < goal) |
| 527 |
put_char (form, ' '); |
| 528 |
} |
| 529 |
|
| 530 |
void |
| 531 |
fh_join (format_data_t form, int argc, char *argv[]) |
| 532 |
{ |
| 533 |
int had_output_saved = had_output; |
| 534 |
int count = 0; |
| 535 |
|
| 536 |
if (argc < 2) |
| 537 |
return; |
| 538 |
|
| 539 |
/* Suppress delimiter before first argument. */ |
| 540 |
had_output = 0; |
| 541 |
|
| 542 |
while (++count < argc) |
| 543 |
{ |
| 544 |
if (had_output) |
| 545 |
{ |
| 546 |
put_string (form, argv[0]); |
| 547 |
had_output = 0; |
| 548 |
had_output_saved = 1; |
| 549 |
} |
| 550 |
form->format = argv[count]; |
| 551 |
print_interfaceX (form, 0); |
| 552 |
} |
| 553 |
had_output = had_output_saved; |
| 554 |
} |
| 555 |
|
| 556 |
void |
| 557 |
fh_exists_query (format_data_t form, int argc, char *argv[]) |
| 558 |
{ |
| 559 |
if (argc > 0) |
| 560 |
{ |
| 561 |
struct format_handle *fh; |
| 562 |
int sel = 2; /* assume 2nd arg by default */ |
| 563 |
|
| 564 |
for (fh = format_handles; fh->name; fh++) |
| 565 |
{ |
| 566 |
if (!strcmp (fh->name, argv[0])) |
| 567 |
{ |
| 568 |
sel = 1; /* select 1st argument */ |
| 569 |
break; |
| 570 |
} |
| 571 |
} |
| 572 |
select_arg (form, argc, argv, sel); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
void |
| 577 |
fh_format (format_data_t form, int argc, char *argv[]) |
| 578 |
{ |
| 579 |
int i; |
| 580 |
|
| 581 |
for (i = 0; i < argc; i++) |
| 582 |
{ |
| 583 |
struct format *frm = format_find (argv[i]); |
| 584 |
|
| 585 |
if (frm) |
| 586 |
{ |
| 587 |
/* XXX: Avoid infinite recursion by appending name to a list |
| 588 |
during the next call (but removing it afterwards, and |
| 589 |
checking in this function if the name is in the list |
| 590 |
already. */ |
| 591 |
form->format = frm->templ; |
| 592 |
print_interfaceX (form, 0); |
| 593 |
break; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
void |
| 599 |
fh_error (format_data_t form, int argc, char *argv[]) |
| 600 |
{ |
| 601 |
int i; |
| 602 |
FILE *s = ostream; |
| 603 |
int *c = column; |
| 604 |
|
| 605 |
ostream = stderr; |
| 606 |
column = &column_stderr; |
| 607 |
for (i = 0; i < argc; i++) |
| 608 |
select_arg (form, argc, argv, i); |
| 609 |
ostream = s; |
| 610 |
column = c; |
| 611 |
} |
| 612 |
|
| 613 |
void |
| 614 |
fh_progname (format_data_t form, int argc MAYBE_UNUSED, |
| 615 |
MAYBE_UNUSED char *argv[]) |
| 616 |
{ |
| 617 |
put_string (form, program_name); |
| 618 |
} |
| 619 |
|
| 620 |
void |
| 621 |
fh_exit (format_data_t form MAYBE_UNUSED, int argc, char *argv[]) |
| 622 |
{ |
| 623 |
int err = 0; |
| 624 |
|
| 625 |
if (argc > 0) |
| 626 |
err = strtoul (argv[0], NULL, 0); |
| 627 |
|
| 628 |
exit (err); |
| 629 |
} |
| 630 |
|
| 631 |
void |
| 632 |
fh_name (format_data_t form, int argc MAYBE_UNUSED, MAYBE_UNUSED char *argv[]) |
| 633 |
{ |
| 634 |
put_string (form, form->name); |
| 635 |
} |
| 636 |
|
| 637 |
void |
| 638 |
fh_index_query (format_data_t form, int argc, char *argv[]) |
| 639 |
{ |
| 640 |
select_arg (form, argc, argv, (if_nametoindex (form->name) == 0) ? 1 : 0); |
| 641 |
} |
| 642 |
|
| 643 |
void |
| 644 |
fh_index (format_data_t form, int argc MAYBE_UNUSED, |
| 645 |
MAYBE_UNUSED char *argv[]) |
| 646 |
{ |
| 647 |
int indx = if_nametoindex (form->name); |
| 648 |
|
| 649 |
if (indx == 0) |
| 650 |
error (EXIT_FAILURE, errno, |
| 651 |
"No index number found for interface `%s'", form->name); |
| 652 |
*column += printf ("%i", indx); |
| 653 |
had_output = 1; |
| 654 |
} |
| 655 |
|
| 656 |
void |
| 657 |
fh_addr_query (format_data_t form, int argc, char *argv[]) |
| 658 |
{ |
| 659 |
#ifdef SIOCGIFADDR |
| 660 |
if (ioctl (form->sfd, SIOCGIFADDR, form->ifr) >= 0) |
| 661 |
select_arg (form, argc, argv, 0); |
| 662 |
else |
| 663 |
#endif |
| 664 |
select_arg (form, argc, argv, 1); |
| 665 |
} |
| 666 |
|
| 667 |
void |
| 668 |
fh_addr (format_data_t form, int argc, char *argv[]) |
| 669 |
{ |
| 670 |
#ifdef SIOCGIFADDR |
| 671 |
if (ioctl (form->sfd, SIOCGIFADDR, form->ifr) < 0) |
| 672 |
error (EXIT_FAILURE, errno, |
| 673 |
"SIOCGIFADDR failed for interface `%s'", form->ifr->ifr_name); |
| 674 |
else |
| 675 |
put_addr (form, argc, argv, &form->ifr->ifr_addr); |
| 676 |
#else |
| 677 |
*column += printf ("(not available)"); |
| 678 |
had_output = 1; |
| 679 |
#endif |
| 680 |
} |
| 681 |
|
| 682 |
void |
| 683 |
fh_netmask_query (format_data_t form, int argc, char *argv[]) |
| 684 |
{ |
| 685 |
#ifdef SIOCGIFNETMASK |
| 686 |
if (ioctl (form->sfd, SIOCGIFNETMASK, form->ifr) >= 0) |
| 687 |
select_arg (form, argc, argv, 0); |
| 688 |
else |
| 689 |
#endif |
| 690 |
select_arg (form, argc, argv, 1); |
| 691 |
} |
| 692 |
|
| 693 |
void |
| 694 |
fh_netmask (format_data_t form, int argc, char *argv[]) |
| 695 |
{ |
| 696 |
#ifdef SIOCGIFNETMASK |
| 697 |
if (ioctl (form->sfd, SIOCGIFNETMASK, form->ifr) < 0) |
| 698 |
error (EXIT_FAILURE, errno, |
| 699 |
"SIOCGIFNETMASK failed for interface `%s'", form->ifr->ifr_name); |
| 700 |
else |
| 701 |
put_addr (form, argc, argv, &form->ifr->ifr_netmask); |
| 702 |
#else |
| 703 |
*column += printf ("(not available)"); |
| 704 |
had_output = 1; |
| 705 |
#endif |
| 706 |
} |
| 707 |
|
| 708 |
void |
| 709 |
fh_brdaddr_query (format_data_t form, int argc, char *argv[]) |
| 710 |
{ |
| 711 |
#ifdef SIOCGIFBRDADDR |
| 712 |
# ifdef SIOCGIFFLAGS |
| 713 |
int f; |
| 714 |
int rev; |
| 715 |
unsigned int uflags; |
| 716 |
|
| 717 |
f = if_nameztoflag ("BROADCAST", &rev); |
| 718 |
|
| 719 |
if (f == 0 || (ioctl (form->sfd, SIOCGIFFLAGS, form->ifr) < 0)) |
| 720 |
{ |
| 721 |
select_arg (form, argc, argv, 1); |
| 722 |
return; |
| 723 |
} |
| 724 |
|
| 725 |
uflags = (unsigned short) form->ifr->ifr_flags; |
| 726 |
# ifdef ifr_flagshigh |
| 727 |
uflags |= (unsigned short) form->ifr->ifr_flagshigh << 16; |
| 728 |
# endif |
| 729 |
/* ifr_flagshigh */ |
| 730 |
|
| 731 |
if ((f & uflags) == 0) |
| 732 |
{ |
| 733 |
select_arg (form, argc, argv, 1); |
| 734 |
return; |
| 735 |
} |
| 736 |
# endif/* SIOCGIFFLAGS */ |
| 737 |
|
| 738 |
if (ioctl (form->sfd, SIOCGIFBRDADDR, form->ifr) >= 0) |
| 739 |
select_arg (form, argc, argv, 0); |
| 740 |
else |
| 741 |
#endif /* SIOCGIFBRDADDR */ |
| 742 |
select_arg (form, argc, argv, 1); |
| 743 |
} |
| 744 |
|
| 745 |
void |
| 746 |
fh_brdaddr (format_data_t form, int argc, char *argv[]) |
| 747 |
{ |
| 748 |
#ifdef SIOCGIFBRDADDR |
| 749 |
if (ioctl (form->sfd, SIOCGIFBRDADDR, form->ifr) < 0) |
| 750 |
error (EXIT_FAILURE, errno, |
| 751 |
"SIOCGIFBRDADDR failed for interface `%s'", form->ifr->ifr_name); |
| 752 |
else |
| 753 |
put_addr (form, argc, argv, &form->ifr->ifr_broadaddr); |
| 754 |
#else |
| 755 |
*column += printf ("(not available)"); |
| 756 |
had_output = 1; |
| 757 |
#endif |
| 758 |
} |
| 759 |
|
| 760 |
void |
| 761 |
fh_dstaddr_query (format_data_t form, int argc, char *argv[]) |
| 762 |
{ |
| 763 |
#ifdef SIOCGIFDSTADDR |
| 764 |
# ifdef SIOCGIFFLAGS |
| 765 |
int f; |
| 766 |
int rev; |
| 767 |
unsigned int uflags; |
| 768 |
|
| 769 |
f = if_nameztoflag ("POINTOPOINT", &rev); |
| 770 |
|
| 771 |
if (f == 0 || (ioctl (form->sfd, SIOCGIFFLAGS, form->ifr) < 0)) |
| 772 |
{ |
| 773 |
select_arg (form, argc, argv, 1); |
| 774 |
return; |
| 775 |
} |
| 776 |
|
| 777 |
uflags = (unsigned short) form->ifr->ifr_flags; |
| 778 |
# ifdef ifr_flagshigh |
| 779 |
uflags |= (unsigned short) form->ifr->ifr_flagshigh << 16; |
| 780 |
# endif |
| 781 |
/* ifr_flagshigh */ |
| 782 |
|
| 783 |
if ((f & uflags) == 0) |
| 784 |
{ |
| 785 |
select_arg (form, argc, argv, 1); |
| 786 |
return; |
| 787 |
} |
| 788 |
# endif/* SIOCGIFFLAGS */ |
| 789 |
|
| 790 |
if (ioctl (form->sfd, SIOCGIFDSTADDR, form->ifr) >= 0) |
| 791 |
select_arg (form, argc, argv, 0); |
| 792 |
else |
| 793 |
#endif /* SIOCGIFDSTADDR */ |
| 794 |
select_arg (form, argc, argv, 1); |
| 795 |
} |
| 796 |
|
| 797 |
void |
| 798 |
fh_dstaddr (format_data_t form, int argc, char *argv[]) |
| 799 |
{ |
| 800 |
#ifdef SIOCGIFDSTADDR |
| 801 |
if (ioctl (form->sfd, SIOCGIFDSTADDR, form->ifr) < 0) |
| 802 |
error (EXIT_FAILURE, errno, |
| 803 |
"SIOCGIFDSTADDR failed for interface `%s'", form->ifr->ifr_name); |
| 804 |
else |
| 805 |
put_addr (form, argc, argv, &form->ifr->ifr_dstaddr); |
| 806 |
#else |
| 807 |
*column += printf ("(not available)"); |
| 808 |
had_output = 1; |
| 809 |
#endif |
| 810 |
} |
| 811 |
|
| 812 |
void |
| 813 |
fh_mtu_query (format_data_t form, int argc, char *argv[]) |
| 814 |
{ |
| 815 |
#ifdef SIOCGIFMTU |
| 816 |
if (ioctl (form->sfd, SIOCGIFMTU, form->ifr) >= 0) |
| 817 |
select_arg (form, argc, argv, 0); |
| 818 |
else |
| 819 |
#endif |
| 820 |
select_arg (form, argc, argv, 1); |
| 821 |
} |
| 822 |
|
| 823 |
void |
| 824 |
fh_mtu (format_data_t form, int argc, char *argv[]) |
| 825 |
{ |
| 826 |
#ifdef SIOCGIFMTU |
| 827 |
if (ioctl (form->sfd, SIOCGIFMTU, form->ifr) < 0) |
| 828 |
error (EXIT_FAILURE, errno, |
| 829 |
"SIOCGIFMTU failed for interface `%s'", form->ifr->ifr_name); |
| 830 |
else |
| 831 |
put_int (form, argc, argv, form->ifr->ifr_mtu); |
| 832 |
#else |
| 833 |
*column += printf ("(not available)"); |
| 834 |
had_output = 1; |
| 835 |
#endif |
| 836 |
} |
| 837 |
|
| 838 |
/* The portable behaviour is to display strictly positive |
| 839 |
* metrics, but to suppress the default value naught. |
| 840 |
*/ |
| 841 |
void |
| 842 |
fh_metric_query (format_data_t form, int argc, char *argv[]) |
| 843 |
{ |
| 844 |
#ifdef SIOCGIFMETRIC |
| 845 |
if (ioctl (form->sfd, SIOCGIFMETRIC, form->ifr) >= 0 |
| 846 |
&& form->ifr->ifr_metric > 0) |
| 847 |
select_arg (form, argc, argv, 0); |
| 848 |
else |
| 849 |
#endif |
| 850 |
select_arg (form, argc, argv, 1); |
| 851 |
} |
| 852 |
|
| 853 |
void |
| 854 |
fh_metric (format_data_t form, int argc, char *argv[]) |
| 855 |
{ |
| 856 |
#ifdef SIOCGIFMETRIC |
| 857 |
if (ioctl (form->sfd, SIOCGIFMETRIC, form->ifr) < 0) |
| 858 |
error (EXIT_FAILURE, errno, |
| 859 |
"SIOCGIFMETRIC failed for interface `%s'", form->ifr->ifr_name); |
| 860 |
else |
| 861 |
put_int (form, argc, argv, form->ifr->ifr_metric); |
| 862 |
#else |
| 863 |
*column += printf ("(not available)"); |
| 864 |
had_output = 1; |
| 865 |
#endif |
| 866 |
} |
| 867 |
|
| 868 |
void |
| 869 |
fh_flags_query (format_data_t form, int argc, char *argv[]) |
| 870 |
{ |
| 871 |
#ifdef SIOCGIFFLAGS |
| 872 |
if (ioctl (form->sfd, SIOCGIFFLAGS, form->ifr) >= 0) |
| 873 |
select_arg (form, argc, argv, 0); |
| 874 |
else |
| 875 |
#endif |
| 876 |
select_arg (form, argc, argv, 1); |
| 877 |
} |
| 878 |
|
| 879 |
void |
| 880 |
fh_flags (format_data_t form, int argc, char *argv[]) |
| 881 |
{ |
| 882 |
#ifdef SIOCGIFFLAGS |
| 883 |
if (ioctl (form->sfd, SIOCGIFFLAGS, form->ifr) < 0) |
| 884 |
error (EXIT_FAILURE, errno, |
| 885 |
"SIOCGIFFLAGS failed for interface `%s'", form->ifr->ifr_name); |
| 886 |
else |
| 887 |
{ |
| 888 |
unsigned int uflags = (unsigned short) form->ifr->ifr_flags; |
| 889 |
|
| 890 |
# ifdef ifr_flagshigh |
| 891 |
uflags |= (unsigned short) form->ifr->ifr_flagshigh << 16; |
| 892 |
# endif/* ifr_flagshigh */ |
| 893 |
|
| 894 |
if (argc >= 1) |
| 895 |
{ |
| 896 |
if (!strcmp (argv[0], "number")) |
| 897 |
put_int (form, argc - 1, &argv[1], uflags); |
| 898 |
else if (!strcmp (argv[0], "short")) |
| 899 |
put_flags_short (form, argc - 1, &argv[1], uflags); |
| 900 |
else if (!strcmp (argv[0], "string")) |
| 901 |
put_flags (form, argc - 1, &argv[1], uflags); |
| 902 |
} |
| 903 |
else |
| 904 |
put_flags (form, argc, argv, uflags); |
| 905 |
} |
| 906 |
#else |
| 907 |
*column += printf ("(not available)"); |
| 908 |
had_output = 1; |
| 909 |
#endif |
| 910 |
} |
| 911 |
|
| 912 |
void |
| 913 |
fh_media_query (format_data_t form, int argc, char *argv[]) |
| 914 |
{ |
| 915 |
/* Must be overridden by a system dependent implementation. */ |
| 916 |
|
| 917 |
/* Claim it to be absent. */ |
| 918 |
select_arg (form, argc, argv, 1); |
| 919 |
} |
| 920 |
|
| 921 |
void |
| 922 |
fh_media (format_data_t form, int argc MAYBE_UNUSED, |
| 923 |
MAYBE_UNUSED char *argv[]) |
| 924 |
{ |
| 925 |
/* Must be overridden by a system dependent implementation. */ |
| 926 |
put_string (form, "(not known)"); |
| 927 |
} |
| 928 |
|
| 929 |
void |
| 930 |
fh_status_query (format_data_t form, int argc, char *argv[]) |
| 931 |
{ |
| 932 |
/* Must be overridden by a system dependent implementation. */ |
| 933 |
|
| 934 |
/* Claim it to be absent. */ |
| 935 |
select_arg (form, argc, argv, 1); |
| 936 |
} |
| 937 |
|
| 938 |
void |
| 939 |
fh_status (format_data_t form, int argc MAYBE_UNUSED, |
| 940 |
MAYBE_UNUSED char *argv[]) |
| 941 |
{ |
| 942 |
/* Must be overridden by a system dependent implementation. */ |
| 943 |
put_string (form, "(not known)"); |
| 944 |
} |
| 945 |
|
| 946 |
#ifdef HAVE_STRUCT_IFREQ_IFR_MAP |
| 947 |
|
| 948 |
void |
| 949 |
fh_map_query (format_data_t form, int argc, char *argv[]) |
| 950 |
{ |
| 951 |
# ifdef SIOCGIFMAP |
| 952 |
if (ioctl (form->sfd, SIOCGIFMAP, form->ifr) >= 0) |
| 953 |
select_arg (form, argc, argv, 0); |
| 954 |
else |
| 955 |
# endif |
| 956 |
select_arg (form, argc, argv, 1); |
| 957 |
} |
| 958 |
|
| 959 |
void |
| 960 |
fh_irq_query (format_data_t form, int argc, char *argv[]) |
| 961 |
{ |
| 962 |
if (form->ifr->ifr_map.irq) |
| 963 |
select_arg (form, argc, argv, 0); |
| 964 |
else |
| 965 |
select_arg (form, argc, argv, 1); |
| 966 |
} |
| 967 |
|
| 968 |
void |
| 969 |
fh_irq (format_data_t form, int argc, char *argv[]) |
| 970 |
{ |
| 971 |
put_int (form, argc, argv, form->ifr->ifr_map.irq); |
| 972 |
} |
| 973 |
|
| 974 |
void |
| 975 |
fh_baseaddr_query (format_data_t form, int argc, char *argv[]) |
| 976 |
{ |
| 977 |
if (form->ifr->ifr_map.base_addr >= 0x100) |
| 978 |
select_arg (form, argc, argv, 0); |
| 979 |
else |
| 980 |
select_arg (form, argc, argv, 1); |
| 981 |
} |
| 982 |
|
| 983 |
void |
| 984 |
fh_baseaddr (format_data_t form, int argc, char *argv[]) |
| 985 |
{ |
| 986 |
if (form->ifr->ifr_map.base_addr >= 0x100) |
| 987 |
put_int (form, argc, argv, form->ifr->ifr_map.base_addr); |
| 988 |
else |
| 989 |
put_string (form, "(not available)"); |
| 990 |
} |
| 991 |
|
| 992 |
void |
| 993 |
fh_memstart_query (format_data_t form, int argc, char *argv[]) |
| 994 |
{ |
| 995 |
if (form->ifr->ifr_map.mem_start) |
| 996 |
select_arg (form, argc, argv, 0); |
| 997 |
else |
| 998 |
select_arg (form, argc, argv, 1); |
| 999 |
} |
| 1000 |
|
| 1001 |
void |
| 1002 |
fh_memstart (format_data_t form, int argc, char *argv[]) |
| 1003 |
{ |
| 1004 |
if (form->ifr->ifr_map.mem_start) |
| 1005 |
put_ulong (form, argc, argv, form->ifr->ifr_map.mem_start); |
| 1006 |
else |
| 1007 |
put_string (form, "(not available)"); |
| 1008 |
} |
| 1009 |
|
| 1010 |
void |
| 1011 |
fh_memend_query (format_data_t form, int argc, char *argv[]) |
| 1012 |
{ |
| 1013 |
if (form->ifr->ifr_map.mem_end) |
| 1014 |
select_arg (form, argc, argv, 0); |
| 1015 |
else |
| 1016 |
select_arg (form, argc, argv, 1); |
| 1017 |
} |
| 1018 |
|
| 1019 |
void |
| 1020 |
fh_memend (format_data_t form, int argc, char *argv[]) |
| 1021 |
{ |
| 1022 |
if (form->ifr->ifr_map.mem_end) |
| 1023 |
put_ulong (form, argc, argv, form->ifr->ifr_map.mem_end); |
| 1024 |
else |
| 1025 |
put_string (form, "(not available)"); |
| 1026 |
} |
| 1027 |
|
| 1028 |
void |
| 1029 |
fh_dma_query (format_data_t form, int argc, char *argv[]) |
| 1030 |
{ |
| 1031 |
if (form->ifr->ifr_map.dma) |
| 1032 |
select_arg (form, argc, argv, 0); |
| 1033 |
else |
| 1034 |
select_arg (form, argc, argv, 1); |
| 1035 |
} |
| 1036 |
|
| 1037 |
void |
| 1038 |
fh_dma (format_data_t form, int argc, char *argv[]) |
| 1039 |
{ |
| 1040 |
if (form->ifr->ifr_map.dma) |
| 1041 |
put_int (form, argc, argv, form->ifr->ifr_map.dma); |
| 1042 |
else |
| 1043 |
put_string (form, "(not available)"); |
| 1044 |
} |
| 1045 |
|
| 1046 |
#endif /* HAVE_STRUCT_IFREQ_IFR_MAP */ |
| 1047 |
|
| 1048 |
void |
| 1049 |
print_interfaceX (format_data_t form, int quiet) |
| 1050 |
{ |
| 1051 |
const char *p = form->format; |
| 1052 |
const char *q; |
| 1053 |
|
| 1054 |
form->depth++; |
| 1055 |
|
| 1056 |
while (!(*p == '\0' || (form->depth > 1 && *p == '}'))) |
| 1057 |
{ |
| 1058 |
/* Echo until end of string or '$'. */ |
| 1059 |
while (!(*p == '$' || *p == '\0' || (form->depth > 1 && *p == '}'))) |
| 1060 |
{ |
| 1061 |
quiet || (put_char (form, *p), 0); |
| 1062 |
p++; |
| 1063 |
} |
| 1064 |
|
| 1065 |
if (*p != '$') |
| 1066 |
break; |
| 1067 |
|
| 1068 |
/* Look at next character. If it is a '$' or '}', print that |
| 1069 |
and skip the '$'. If it is something else than '{', print |
| 1070 |
both. Otherwise enter substitution mode. */ |
| 1071 |
switch (*(++p)) |
| 1072 |
{ |
| 1073 |
default: |
| 1074 |
quiet || (put_char (form, '$'), 0); |
| 1075 |
/* Fallthrough. */ |
| 1076 |
|
| 1077 |
case '$': |
| 1078 |
case '}': |
| 1079 |
quiet || (put_char (form, *p), 0); |
| 1080 |
p++; |
| 1081 |
continue; |
| 1082 |
/* Not reached. */ |
| 1083 |
|
| 1084 |
case '{': |
| 1085 |
p++; |
| 1086 |
break; |
| 1087 |
} |
| 1088 |
|
| 1089 |
/* P points to character following '{' now. */ |
| 1090 |
q = strchr (p, '}'); |
| 1091 |
if (!q) |
| 1092 |
{ |
| 1093 |
/* Without a following '}', no substitution at all can occur, |
| 1094 |
so just dump the string that is missing. */ |
| 1095 |
p -= 2; |
| 1096 |
put_string (form, p); |
| 1097 |
p = strchr (p, '\0'); |
| 1098 |
continue; |
| 1099 |
} |
| 1100 |
else |
| 1101 |
{ |
| 1102 |
char *id; |
| 1103 |
id = alloca (q - p + 1); |
| 1104 |
memcpy (id, p, q - p); |
| 1105 |
id[q - p] = '\0'; |
| 1106 |
p = q + 1; |
| 1107 |
|
| 1108 |
/* We have now in ID the content of the first field, and |
| 1109 |
in P the following string. Now take the arguments. */ |
| 1110 |
if (quiet) |
| 1111 |
{ |
| 1112 |
/* Just consume all arguments. */ |
| 1113 |
form->format = p; |
| 1114 |
|
| 1115 |
while (*(form->format) == '{') |
| 1116 |
{ |
| 1117 |
form->format++; |
| 1118 |
print_interfaceX (form, 1); |
| 1119 |
if (*(form->format) == '}') |
| 1120 |
form->format++; |
| 1121 |
} |
| 1122 |
|
| 1123 |
p = form->format; |
| 1124 |
} |
| 1125 |
else |
| 1126 |
{ |
| 1127 |
int argc = 0; |
| 1128 |
char **argv = NULL; |
| 1129 |
|
| 1130 |
while (*p == '{') |
| 1131 |
{ |
| 1132 |
p++; |
| 1133 |
form->format = p; |
| 1134 |
print_interfaceX (form, 1); |
| 1135 |
q = form->format; |
| 1136 |
argv = xrealloc (argv, (argc + 1) * sizeof (char *)); |
| 1137 |
argv[argc] = xmalloc (q - p + 1); |
| 1138 |
memcpy (argv[argc], p, q - p); |
| 1139 |
argv[argc][q - p] = '\0'; |
| 1140 |
if (*q == '}') |
| 1141 |
q++; |
| 1142 |
p = q; |
| 1143 |
argc++; |
| 1144 |
} |
| 1145 |
|
| 1146 |
format_handler (id, form, argc, argv); |
| 1147 |
if (argv != NULL) |
| 1148 |
{ |
| 1149 |
/* Clean up. */ |
| 1150 |
while (--argc >= 0) |
| 1151 |
free (argv[argc]); |
| 1152 |
free (argv); |
| 1153 |
} |
| 1154 |
form->format = p; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
form->format = p; |
| 1160 |
form->depth--; |
| 1161 |
} |
| 1162 |
|
| 1163 |
void |
| 1164 |
print_interface (int sfd, const char *name, struct ifreq *ifr, |
| 1165 |
const char *format) |
| 1166 |
{ |
| 1167 |
struct format_data form; |
| 1168 |
static int first_passed_already; |
| 1169 |
|
| 1170 |
if (!ostream) |
| 1171 |
ostream = stdout; |
| 1172 |
|
| 1173 |
if (!first_passed_already) |
| 1174 |
first_passed_already = form.first = 1; |
| 1175 |
else |
| 1176 |
form.first = 0; |
| 1177 |
|
| 1178 |
form.name = name; |
| 1179 |
form.ifr = ifr; |
| 1180 |
form.format = format; |
| 1181 |
form.sfd = sfd; |
| 1182 |
form.depth = 0; |
| 1183 |
|
| 1184 |
print_interfaceX (&form, 0); |
| 1185 |
} |