diff options
| -rw-r--r-- | src/macaddrinfo.c | 433 |
1 files changed, 127 insertions, 306 deletions
diff --git a/src/macaddrinfo.c b/src/macaddrinfo.c index ec6d150..18d96a1 100644 --- a/src/macaddrinfo.c +++ b/src/macaddrinfo.c | |||
| @@ -179,23 +179,30 @@ struct mau_config | |||
| 179 | int quiet; | 179 | int quiet; |
| 180 | int verbose; | 180 | int verbose; |
| 181 | int notation; | 181 | int notation; |
| 182 | int use_oui; | ||
| 183 | int opt_index; | ||
| 182 | int cmd_argc; | 184 | int cmd_argc; |
| 183 | char ** cmd_argv; | 185 | char ** cmd_argv; |
| 184 | const char * cmd_name; | 186 | const char * cmd_name; |
| 187 | const char * rnd_file; | ||
| 185 | size_t cmd_len; | 188 | size_t cmd_len; |
| 186 | const mau_command * cmd; | 189 | const mau_command * cmd; |
| 187 | regex_t regex; | 190 | regex_t regex; |
| 188 | const char * regex_str; | 191 | const char * regex_str; |
| 189 | regmatch_t matches[MAU_REGEX_MAX]; | 192 | regmatch_t matches[MAU_REGEX_MAX]; |
| 190 | char buff[MAU_BUFF_LEN]; | 193 | char buff[MAU_BUFF_LEN]; |
| 194 | mauaddr_t oui; | ||
| 191 | }; | 195 | }; |
| 192 | 196 | ||
| 193 | 197 | ||
| 194 | struct mau_command | 198 | struct mau_command |
| 195 | { | 199 | { |
| 196 | const char * cmd_name; | 200 | const char * cmd_name; |
| 197 | int (*cmd_func)(mau_config * cnf, int argc, char * argv[]); | 201 | int (*cmd_func)(mau_config * cnf); |
| 198 | const char * cmd_shortopts; | 202 | const char * cmd_shortopts; |
| 203 | struct option * long_opt; | ||
| 204 | int min_arg; | ||
| 205 | int max_arg; | ||
| 199 | const char * cmd_help; | 206 | const char * cmd_help; |
| 200 | const char * cmd_desc; | 207 | const char * cmd_desc; |
| 201 | }; | 208 | }; |
| @@ -214,18 +221,21 @@ struct mau_command | |||
| 214 | int main(int argc, char * argv[]); | 221 | int main(int argc, char * argv[]); |
| 215 | 222 | ||
| 216 | 223 | ||
| 217 | int mau_cmd_eui64(mau_config * cnf, int argc, char * argv[]); | 224 | int mau_cmd_eui64(mau_config * cnf); |
| 218 | 225 | ||
| 219 | // generate command | 226 | // generate command |
| 220 | int mau_cmd_generate(mau_config * cnf, int argc, char * argv[]); | 227 | int mau_cmd_generate(mau_config * cnf); |
| 221 | 228 | ||
| 222 | int mau_cmd_info(mau_config * cnf, int argc, char * argv[]); | 229 | int mau_cmd_info(mau_config * cnf); |
| 223 | 230 | ||
| 224 | int mau_cmd_link_local(mau_config * cnf, int argc, char * argv[]); | 231 | int mau_cmd_link_local(mau_config * cnf); |
| 225 | 232 | ||
| 226 | int mau_cmd_macaddress(mau_config * cnf, int argc, char * argv[]); | 233 | int mau_cmd_macaddress(mau_config * cnf); |
| 234 | |||
| 235 | int mau_cmd_test(mau_config * cnf); | ||
| 236 | |||
| 237 | int mau_cmd_update(mau_config * cnf); | ||
| 227 | 238 | ||
| 228 | int mau_cmd_test(mau_config * cnf, int argc, char * argv[]); | ||
| 229 | void mau_cmd_update_usage(void); | 239 | void mau_cmd_update_usage(void); |
| 230 | 240 | ||
| 231 | int mau_conv_eui2sin(mau_config * cnf, const maueui64_t eui, struct sockaddr_in6 * sin); | 241 | int mau_conv_eui2sin(mau_config * cnf, const maueui64_t eui, struct sockaddr_in6 * sin); |
| @@ -279,6 +289,8 @@ const mau_command mau_cmdmap[] = | |||
| 279 | "abcdefghijklmnopqrstuvwxyz" | 289 | "abcdefghijklmnopqrstuvwxyz" |
| 280 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 290 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 281 | "0123456789", // getopt short options | 291 | "0123456789", // getopt short options |
| 292 | (struct option []){ MAU_GETOPT_LONG }, // getopt long options | ||
| 293 | -1, -1, // min/max arguments | ||
| 282 | NULL, // cli usage | 294 | NULL, // cli usage |
| 283 | NULL, // "print entire OUI database" // command description | 295 | NULL, // "print entire OUI database" // command description |
| 284 | }, | 296 | }, |
| @@ -286,20 +298,31 @@ const mau_command mau_cmdmap[] = | |||
| 286 | "dump", // command name | 298 | "dump", // command name |
| 287 | mau_cmd_test, // entry function | 299 | mau_cmd_test, // entry function |
| 288 | MAU_GETOPT_SHORT, // getopt short options | 300 | MAU_GETOPT_SHORT, // getopt short options |
| 301 | (struct option []){ MAU_GETOPT_LONG }, // getopt long options | ||
| 302 | 0, 0, // min/max arguments | ||
| 289 | NULL, // cli usage | 303 | NULL, // cli usage |
| 290 | NULL, // "print entire OUI database" // command description | 304 | NULL, // "print entire OUI database" // command description |
| 291 | }, | 305 | }, |
| 292 | { | 306 | { |
| 293 | "eui64", // command name | 307 | "eui64", // command name |
| 294 | mau_cmd_eui64, // entry function | 308 | mau_cmd_eui64, // entry function |
| 295 | "lu" MAU_GETOPT_SHORT, // getopt short options | 309 | "lu" MAU_GETOPT_SHORT, // getopt short options |
| 310 | (struct option []){ MAU_GETOPT_LONG }, // getopt long options | ||
| 311 | 1, 1, // min/max arguments | ||
| 296 | " <address>", // cli usage | 312 | " <address>", // cli usage |
| 297 | "display modified EUI-64 Identifier" // command description | 313 | "display modified EUI-64 Identifier" // command description |
| 298 | }, | 314 | }, |
| 299 | { | 315 | { |
| 300 | "generate", // command name | 316 | "generate", // command name |
| 301 | mau_cmd_generate, // entry function | 317 | mau_cmd_generate, // entry function |
| 302 | "cDdlRr:uxXW" MAU_GETOPT_SHORT, // getopt short options | 318 | "cDdlRr:uxXW" MAU_GETOPT_SHORT, // getopt short options |
| 319 | (struct option []) | ||
| 320 | { | ||
| 321 | {"vmware", no_argument, NULL, 'W' }, | ||
| 322 | {"xen", no_argument, NULL, 'X' }, | ||
| 323 | MAU_GETOPT_LONG | ||
| 324 | }, // getopt long options | ||
| 325 | 0, 0, // min/max arguments | ||
| 303 | NULL, // cli usage | 326 | NULL, // cli usage |
| 304 | "generate random MAC address" | 327 | "generate random MAC address" |
| 305 | }, | 328 | }, |
| @@ -307,6 +330,8 @@ const mau_command mau_cmdmap[] = | |||
| 307 | "information", // command name | 330 | "information", // command name |
| 308 | mau_cmd_info, // entry function | 331 | mau_cmd_info, // entry function |
| 309 | "cDdlRu" MAU_GETOPT_SHORT, // getopt short options | 332 | "cDdlRu" MAU_GETOPT_SHORT, // getopt short options |
| 333 | (struct option []){ MAU_GETOPT_LONG }, // getopt long options | ||
| 334 | 1, 1, // min/max arguments | ||
| 310 | " <address>", // cli usage | 335 | " <address>", // cli usage |
| 311 | "display meta information about MAC address" // command description | 336 | "display meta information about MAC address" // command description |
| 312 | }, | 337 | }, |
| @@ -314,6 +339,8 @@ const mau_command mau_cmdmap[] = | |||
| 314 | "link-local", // command name | 339 | "link-local", // command name |
| 315 | mau_cmd_link_local, // entry function | 340 | mau_cmd_link_local, // entry function |
| 316 | MAU_GETOPT_SHORT, // getopt short options | 341 | MAU_GETOPT_SHORT, // getopt short options |
| 342 | (struct option []){ MAU_GETOPT_LONG }, // getopt long options | ||
| 343 | 1, 1, // min/max arguments | ||
| 317 | " <address>", // cli usage | 344 | " <address>", // cli usage |
| 318 | "display derived IPv6 link-local address" // command description | 345 | "display derived IPv6 link-local address" // command description |
| 319 | }, | 346 | }, |
| @@ -321,24 +348,30 @@ const mau_command mau_cmdmap[] = | |||
| 321 | "macaddress", // command name | 348 | "macaddress", // command name |
| 322 | mau_cmd_macaddress, // entry function | 349 | mau_cmd_macaddress, // entry function |
| 323 | "cDdlRu" MAU_GETOPT_SHORT, // getopt short options | 350 | "cDdlRu" MAU_GETOPT_SHORT, // getopt short options |
| 351 | (struct option []){ MAU_GETOPT_LONG }, // getopt long options | ||
| 352 | 1, 1, // min/max arguments | ||
| 324 | " <address>", // cli usage | 353 | " <address>", // cli usage |
| 325 | "display MAC address using notation flags" // command description | 354 | "display MAC address using notation flags" // command description |
| 326 | }, | 355 | }, |
| 327 | { | 356 | { |
| 328 | "update", // command name | 357 | "update", // command name |
| 329 | mau_cmd_test, // entry function | 358 | mau_cmd_update, // entry function |
| 330 | "cDdlRu" MAU_GETOPT_SHORT, // getopt short options | 359 | "cDdlRUu" MAU_GETOPT_SHORT, // getopt short options |
| 331 | NULL, // cli usage | 360 | (struct option []){ MAU_GETOPT_LONG }, // getopt long options |
| 332 | NULL, // "update local OUI database" // command description | 361 | 0, 0, // min/max arguments |
| 362 | NULL, // cli usage | ||
| 363 | "update local OUI database" // command description | ||
| 333 | }, | 364 | }, |
| 334 | { | 365 | { |
| 335 | "vendor", // command name | 366 | "vendor", // command name |
| 336 | mau_cmd_test, // entry function | 367 | mau_cmd_test, // entry function |
| 337 | "cDdlRu" MAU_GETOPT_SHORT, // getopt short options | 368 | "cDdlRu" MAU_GETOPT_SHORT, // getopt short options |
| 369 | (struct option []){ MAU_GETOPT_LONG }, // getopt long options | ||
| 370 | 1, 1, // min/max arguments | ||
| 338 | " <organization>", // cli usage | 371 | " <organization>", // cli usage |
| 339 | NULL, // "search for OUI by vendor name" // command description | 372 | NULL, // "search for OUI by vendor name" // command description |
| 340 | }, | 373 | }, |
| 341 | { NULL, NULL, NULL, NULL, NULL } | 374 | { NULL, NULL, NULL, NULL, -1, -1, NULL, NULL } |
| 342 | }; | 375 | }; |
| 343 | 376 | ||
| 344 | 377 | ||
| @@ -366,6 +399,7 @@ int main(int argc, char * argv[]) | |||
| 366 | static struct option long_opt[] = { MAU_GETOPT_LONG }; | 399 | static struct option long_opt[] = { MAU_GETOPT_LONG }; |
| 367 | 400 | ||
| 368 | memset(&cnf, 0, sizeof(cnf)); | 401 | memset(&cnf, 0, sizeof(cnf)); |
| 402 | cnf.rnd_file = "/dev/random"; | ||
| 369 | 403 | ||
| 370 | while((c = mau_getopt(&cnf, argc, argv, short_opt, long_opt, &opt_index)) != -1) | 404 | while((c = mau_getopt(&cnf, argc, argv, short_opt, long_opt, &opt_index)) != -1) |
| 371 | { | 405 | { |
| @@ -432,23 +466,7 @@ int main(int argc, char * argv[]) | |||
| 432 | }; | 466 | }; |
| 433 | 467 | ||
| 434 | optind = 1; | 468 | optind = 1; |
| 435 | return(cnf.cmd->cmd_func(&cnf, cnf.cmd_argc, cnf.cmd_argv)); | 469 | while((c = mau_getopt(&cnf, cnf.cmd_argc, cnf.cmd_argv, cnf.cmd->cmd_shortopts, cnf.cmd->long_opt, &cnf.opt_index)) != -1) |
| 436 | } | ||
| 437 | |||
| 438 | |||
| 439 | int mau_cmd_eui64(mau_config * cnf, int argc, char * argv[]) | ||
| 440 | { | ||
| 441 | int c; | ||
| 442 | int opt_index; | ||
| 443 | mauaddr_t addr; | ||
| 444 | maueui64_t eui; | ||
| 445 | maustr_t eui_str; | ||
| 446 | |||
| 447 | // getopt options | ||
| 448 | static struct option long_opt[] = { MAU_GETOPT_LONG }; | ||
| 449 | |||
| 450 | |||
| 451 | while((c = mau_getopt(cnf, argc, argv, cnf->cmd->cmd_shortopts, long_opt, &opt_index)) != -1) | ||
| 452 | { | 470 | { |
| 453 | switch(c) | 471 | switch(c) |
| 454 | { | 472 | { |
| @@ -463,30 +481,40 @@ int mau_cmd_eui64(mau_config * cnf, int argc, char * argv[]) | |||
| 463 | return(1); | 481 | return(1); |
| 464 | 482 | ||
| 465 | case '?': | 483 | case '?': |
| 466 | fprintf(stderr, "Try `%s format --help' for more information.\n", PROGRAM_NAME); | 484 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf.cmd_name); |
| 467 | return(1); | 485 | return(1); |
| 468 | 486 | ||
| 469 | default: | 487 | default: |
| 470 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | 488 | fprintf(stderr, "%s: %s: unrecognized option `--%c'\n", PROGRAM_NAME, cnf.cmd_name, c); |
| 471 | fprintf(stderr, "Try `%s format --help' for more information.\n", PROGRAM_NAME); | 489 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf.cmd_name); |
| 472 | return(1); | 490 | return(1); |
| 473 | }; | 491 | }; |
| 474 | }; | 492 | }; |
| 475 | 493 | ||
| 476 | if ((argc - optind) < 1) | 494 | if ((cnf.cmd_argc - optind) < cnf.cmd->min_arg) |
| 477 | { | 495 | { |
| 478 | fprintf(stderr, "%s: missing MAC address\n", PROGRAM_NAME); | 496 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); |
| 479 | fprintf(stderr, "Try `%s format --help' for more information.\n", PROGRAM_NAME); | 497 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf.cmd_name); |
| 480 | return(1); | 498 | return(1); |
| 481 | }; | 499 | }; |
| 482 | if ((optind+1) != argc) | 500 | if ((optind+cnf.cmd->max_arg) > cnf.cmd_argc) |
| 483 | { | 501 | { |
| 484 | fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, argv[optind+1]); | 502 | fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, cnf.cmd_argv[optind+1]); |
| 485 | fprintf(stderr, "Try `%s format --help' for more information.\n", PROGRAM_NAME); | 503 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf.cmd_name); |
| 486 | return(1); | 504 | return(1); |
| 487 | }; | 505 | }; |
| 488 | 506 | ||
| 489 | if ((mau_conv_str2mac(cnf, argv[optind], addr))) | 507 | return(cnf.cmd->cmd_func(&cnf)); |
| 508 | } | ||
| 509 | |||
| 510 | |||
| 511 | int mau_cmd_eui64(mau_config * cnf) | ||
| 512 | { | ||
| 513 | mauaddr_t addr; | ||
| 514 | maueui64_t eui; | ||
| 515 | maustr_t eui_str; | ||
| 516 | |||
| 517 | if ((mau_conv_str2mac(cnf, cnf->cmd_argv[optind], addr))) | ||
| 490 | return(1); | 518 | return(1); |
| 491 | mau_conv_mac2eui(cnf, addr, eui); | 519 | mau_conv_mac2eui(cnf, addr, eui); |
| 492 | mau_conv_eui2str(cnf, eui, eui_str); | 520 | mau_conv_eui2str(cnf, eui, eui_str); |
| @@ -498,91 +526,23 @@ int mau_cmd_eui64(mau_config * cnf, int argc, char * argv[]) | |||
| 498 | 526 | ||
| 499 | 527 | ||
| 500 | /// generate command | 528 | /// generate command |
| 501 | int mau_cmd_generate(mau_config * cnf, int argc, char * argv[]) | 529 | int mau_cmd_generate(mau_config * cnf) |
| 502 | { | 530 | { |
| 503 | int c; | ||
| 504 | int opt_index; | ||
| 505 | int fd; | 531 | int fd; |
| 506 | const char * rnd_file; | ||
| 507 | int use_oui; | ||
| 508 | mauaddr_t oui; | ||
| 509 | mauaddr_t addr; | 532 | mauaddr_t addr; |
| 510 | maustr_t str; | 533 | maustr_t str; |
| 511 | 534 | ||
| 512 | // getopt options | ||
| 513 | static struct option long_opt[] = | ||
| 514 | { | ||
| 515 | // command long options | ||
| 516 | {"vmware", no_argument, NULL, 0}, | ||
| 517 | {"xen", no_argument, NULL, 0}, | ||
| 518 | MAU_GETOPT_LONG, | ||
| 519 | }; | ||
| 520 | |||
| 521 | rnd_file = "/dev/random"; | ||
| 522 | use_oui = 0; | ||
| 523 | |||
| 524 | while((c = mau_getopt(cnf, argc, argv, cnf->cmd->cmd_shortopts, long_opt, &opt_index)) != -1) | ||
| 525 | { | ||
| 526 | switch(c) | ||
| 527 | { | ||
| 528 | case -2: /* captured by common options */ | ||
| 529 | case -1: /* no more arguments */ | ||
| 530 | break; | ||
| 531 | |||
| 532 | case 0: /* long options toggles */ | ||
| 533 | if (!(strcmp(long_opt[opt_index].name, "vmware"))) | ||
| 534 | { | ||
| 535 | use_oui = 1; | ||
| 536 | oui[0] = 0x00; | ||
| 537 | oui[1] = 0x50; | ||
| 538 | oui[2] = 0x56; | ||
| 539 | }; | ||
| 540 | if (!(strcmp(long_opt[opt_index].name, "xen"))) | ||
| 541 | { | ||
| 542 | use_oui = 1; | ||
| 543 | oui[0] = 0x00; | ||
| 544 | oui[1] = 0x16; | ||
| 545 | oui[2] = 0x3e; | ||
| 546 | }; | ||
| 547 | break; | ||
| 548 | |||
| 549 | case 1: | ||
| 550 | return(0); | ||
| 551 | case 2: | ||
| 552 | return(1); | ||
| 553 | |||
| 554 | case 'r': | ||
| 555 | rnd_file = optarg; | ||
| 556 | break; | ||
| 557 | |||
| 558 | case '?': | ||
| 559 | fprintf(stderr, "Try `%s generate --help' for more information.\n", PROGRAM_NAME); | ||
| 560 | return(1); | ||
| 561 | |||
| 562 | default: | ||
| 563 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 564 | fprintf(stderr, "Try `%s generate --help' for more information.\n", PROGRAM_NAME); | ||
| 565 | return(1); | ||
| 566 | }; | ||
| 567 | }; | ||
| 568 | if (optind != argc) | ||
| 569 | { | ||
| 570 | fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, argv[optind]); | ||
| 571 | fprintf(stderr, "Try `%s generate --help' for more information.\n", PROGRAM_NAME); | ||
| 572 | return(1); | ||
| 573 | }; | ||
| 574 | |||
| 575 | // generates random MAC | 535 | // generates random MAC |
| 576 | mau_log_verbose(cnf, "opening file (%s)\n", rnd_file); | 536 | mau_log_verbose(cnf, "opening file (%s)\n", cnf->rnd_file); |
| 577 | if ((fd = open(rnd_file, O_RDONLY)) == -1) | 537 | if ((fd = open(cnf->rnd_file, O_RDONLY)) == -1) |
| 578 | { | 538 | { |
| 579 | mau_log_err(cnf, "%s: %s\n", rnd_file, strerror(errno)); | 539 | mau_log_err(cnf, "%s: %s\n", cnf->rnd_file, strerror(errno)); |
| 580 | return(1); | 540 | return(1); |
| 581 | }; | 541 | }; |
| 582 | mau_log_verbose(cnf, "%s: reading random data\n", PROGRAM_NAME); | 542 | mau_log_verbose(cnf, "%s: reading random data\n", PROGRAM_NAME); |
| 583 | if (read(fd, addr, sizeof(addr)) == -1) | 543 | if (read(fd, addr, sizeof(addr)) == -1) |
| 584 | { | 544 | { |
| 585 | mau_log_err(cnf, "%s: %s\n", rnd_file, strerror(errno)); | 545 | mau_log_err(cnf, "%s: %s\n", cnf->rnd_file, strerror(errno)); |
| 586 | mau_log_verbose(cnf, "%s: closing file\n", PROGRAM_NAME); | 546 | mau_log_verbose(cnf, "%s: closing file\n", PROGRAM_NAME); |
| 587 | close(fd); | 547 | close(fd); |
| 588 | return(1); | 548 | return(1); |
| @@ -593,8 +553,8 @@ int mau_cmd_generate(mau_config * cnf, int argc, char * argv[]) | |||
| 593 | 553 | ||
| 594 | 554 | ||
| 595 | // applies OUI | 555 | // applies OUI |
| 596 | if ((use_oui)) | 556 | if ((cnf->use_oui)) |
| 597 | memcpy(addr, oui, 3); | 557 | memcpy(addr, cnf->oui, 3); |
| 598 | 558 | ||
| 599 | mau_conv_mac2str(cnf, addr, str); | 559 | mau_conv_mac2str(cnf, addr, str); |
| 600 | printf("%s\n", str); | 560 | printf("%s\n", str); |
| @@ -603,10 +563,8 @@ int mau_cmd_generate(mau_config * cnf, int argc, char * argv[]) | |||
| 603 | } | 563 | } |
| 604 | 564 | ||
| 605 | 565 | ||
| 606 | int mau_cmd_info(mau_config * cnf, int argc, char * argv[]) | 566 | int mau_cmd_info(mau_config * cnf) |
| 607 | { | 567 | { |
| 608 | int c; | ||
| 609 | int opt_index; | ||
| 610 | mauaddr_t addr; | 568 | mauaddr_t addr; |
| 611 | maustr_t addr_str; | 569 | maustr_t addr_str; |
| 612 | maueui64_t eui; | 570 | maueui64_t eui; |
| @@ -614,49 +572,7 @@ int mau_cmd_info(mau_config * cnf, int argc, char * argv[]) | |||
| 614 | maustr_t sin_str; | 572 | maustr_t sin_str; |
| 615 | struct sockaddr_in6 sin; | 573 | struct sockaddr_in6 sin; |
| 616 | 574 | ||
| 617 | // getopt options | 575 | if ((mau_conv_str2mac(cnf, cnf->cmd_argv[optind], addr))) |
| 618 | static struct option long_opt[] = { MAU_GETOPT_LONG }; | ||
| 619 | |||
| 620 | |||
| 621 | while((c = mau_getopt(cnf, argc, argv, cnf->cmd->cmd_shortopts, long_opt, &opt_index)) != -1) | ||
| 622 | { | ||
| 623 | switch(c) | ||
| 624 | { | ||
| 625 | case -2: /* captured by common options */ | ||
| 626 | case -1: /* no more arguments */ | ||
| 627 | case 0: /* long options toggles */ | ||
| 628 | break; | ||
| 629 | |||
| 630 | case 1: | ||
| 631 | return(0); | ||
| 632 | case 2: | ||
| 633 | return(1); | ||
| 634 | |||
| 635 | case '?': | ||
| 636 | fprintf(stderr, "Try `%s info --help' for more information.\n", PROGRAM_NAME); | ||
| 637 | return(1); | ||
| 638 | |||
| 639 | default: | ||
| 640 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 641 | fprintf(stderr, "Try `%s info --help' for more information.\n", PROGRAM_NAME); | ||
| 642 | return(1); | ||
| 643 | }; | ||
| 644 | }; | ||
| 645 | |||
| 646 | if ((argc - optind) < 1) | ||
| 647 | { | ||
| 648 | fprintf(stderr, "%s: missing MAC address\n", PROGRAM_NAME); | ||
| 649 | fprintf(stderr, "Try `%s info --help' for more information.\n", PROGRAM_NAME); | ||
| 650 | return(1); | ||
| 651 | }; | ||
| 652 | if ((optind+1) != argc) | ||
| 653 | { | ||
| 654 | fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, argv[optind+1]); | ||
| 655 | fprintf(stderr, "Try `%s info --help' for more information.\n", PROGRAM_NAME); | ||
| 656 | return(1); | ||
| 657 | }; | ||
| 658 | |||
| 659 | if ((mau_conv_str2mac(cnf, argv[optind], addr))) | ||
| 660 | return(1); | 576 | return(1); |
| 661 | 577 | ||
| 662 | mau_conv_mac2str(cnf, addr, addr_str); | 578 | mau_conv_mac2str(cnf, addr, addr_str); |
| @@ -675,58 +591,14 @@ int mau_cmd_info(mau_config * cnf, int argc, char * argv[]) | |||
| 675 | } | 591 | } |
| 676 | 592 | ||
| 677 | 593 | ||
| 678 | int mau_cmd_link_local(mau_config * cnf, int argc, char * argv[]) | 594 | int mau_cmd_link_local(mau_config * cnf) |
| 679 | { | 595 | { |
| 680 | int c; | ||
| 681 | int opt_index; | ||
| 682 | mauaddr_t addr; | 596 | mauaddr_t addr; |
| 683 | maueui64_t eui; | 597 | maueui64_t eui; |
| 684 | maustr_t sin_str; | 598 | maustr_t sin_str; |
| 685 | struct sockaddr_in6 sin; | 599 | struct sockaddr_in6 sin; |
| 686 | 600 | ||
| 687 | // getopt options | 601 | if ((mau_conv_str2mac(cnf, cnf->cmd_argv[optind], addr))) |
| 688 | static struct option long_opt[] = { MAU_GETOPT_LONG }; | ||
| 689 | |||
| 690 | |||
| 691 | while((c = mau_getopt(cnf, argc, argv, cnf->cmd->cmd_shortopts, long_opt, &opt_index)) != -1) | ||
| 692 | { | ||
| 693 | switch(c) | ||
| 694 | { | ||
| 695 | case -2: /* captured by common options */ | ||
| 696 | case -1: /* no more arguments */ | ||
| 697 | case 0: /* long options toggles */ | ||
| 698 | break; | ||
| 699 | |||
| 700 | case 1: | ||
| 701 | return(0); | ||
| 702 | case 2: | ||
| 703 | return(1); | ||
| 704 | |||
| 705 | case '?': | ||
| 706 | fprintf(stderr, "Try `%s info --help' for more information.\n", PROGRAM_NAME); | ||
| 707 | return(1); | ||
| 708 | |||
| 709 | default: | ||
| 710 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 711 | fprintf(stderr, "Try `%s info --help' for more information.\n", PROGRAM_NAME); | ||
| 712 | return(1); | ||
| 713 | }; | ||
| 714 | }; | ||
| 715 | |||
| 716 | if ((argc - optind) < 1) | ||
| 717 | { | ||
| 718 | fprintf(stderr, "%s: missing MAC address\n", PROGRAM_NAME); | ||
| 719 | fprintf(stderr, "Try `%s info --help' for more information.\n", PROGRAM_NAME); | ||
| 720 | return(1); | ||
| 721 | }; | ||
| 722 | if ((optind+1) != argc) | ||
| 723 | { | ||
| 724 | fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, argv[optind+1]); | ||
| 725 | fprintf(stderr, "Try `%s info --help' for more information.\n", PROGRAM_NAME); | ||
| 726 | return(1); | ||
| 727 | }; | ||
| 728 | |||
| 729 | if ((mau_conv_str2mac(cnf, argv[optind], addr))) | ||
| 730 | return(1); | 602 | return(1); |
| 731 | 603 | ||
| 732 | mau_conv_mac2eui(cnf, addr, eui); | 604 | mau_conv_mac2eui(cnf, addr, eui); |
| @@ -739,56 +611,12 @@ int mau_cmd_link_local(mau_config * cnf, int argc, char * argv[]) | |||
| 739 | } | 611 | } |
| 740 | 612 | ||
| 741 | 613 | ||
| 742 | int mau_cmd_macaddress(mau_config * cnf, int argc, char * argv[]) | 614 | int mau_cmd_macaddress(mau_config * cnf) |
| 743 | { | 615 | { |
| 744 | int c; | ||
| 745 | int opt_index; | ||
| 746 | maustr_t str; | 616 | maustr_t str; |
| 747 | mauaddr_t addr; | 617 | mauaddr_t addr; |
| 748 | 618 | ||
| 749 | // getopt options | 619 | if ((mau_conv_str2mac(cnf, cnf->cmd_argv[optind], addr))) |
| 750 | static struct option long_opt[] = { MAU_GETOPT_LONG }; | ||
| 751 | |||
| 752 | |||
| 753 | while((c = mau_getopt(cnf, argc, argv, cnf->cmd->cmd_shortopts, long_opt, &opt_index)) != -1) | ||
| 754 | { | ||
| 755 | switch(c) | ||
| 756 | { | ||
| 757 | case -2: /* captured by common options */ | ||
| 758 | case -1: /* no more arguments */ | ||
| 759 | case 0: /* long options toggles */ | ||
| 760 | break; | ||
| 761 | |||
| 762 | case 1: | ||
| 763 | return(0); | ||
| 764 | case 2: | ||
| 765 | return(1); | ||
| 766 | |||
| 767 | case '?': | ||
| 768 | fprintf(stderr, "Try `%s format --help' for more information.\n", PROGRAM_NAME); | ||
| 769 | return(1); | ||
| 770 | |||
| 771 | default: | ||
| 772 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 773 | fprintf(stderr, "Try `%s format --help' for more information.\n", PROGRAM_NAME); | ||
| 774 | return(1); | ||
| 775 | }; | ||
| 776 | }; | ||
| 777 | |||
| 778 | if ((argc - optind) < 1) | ||
| 779 | { | ||
| 780 | fprintf(stderr, "%s: missing MAC address\n", PROGRAM_NAME); | ||
| 781 | fprintf(stderr, "Try `%s format --help' for more information.\n", PROGRAM_NAME); | ||
| 782 | return(1); | ||
| 783 | }; | ||
| 784 | if ((optind+1) != argc) | ||
| 785 | { | ||
| 786 | fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, argv[optind+1]); | ||
| 787 | fprintf(stderr, "Try `%s format --help' for more information.\n", PROGRAM_NAME); | ||
| 788 | return(1); | ||
| 789 | }; | ||
| 790 | |||
| 791 | if ((mau_conv_str2mac(cnf, argv[optind], addr))) | ||
| 792 | return(1); | 620 | return(1); |
| 793 | 621 | ||
| 794 | mau_conv_mac2str(cnf, addr, str); | 622 | mau_conv_mac2str(cnf, addr, str); |
| @@ -799,59 +627,16 @@ int mau_cmd_macaddress(mau_config * cnf, int argc, char * argv[]) | |||
| 799 | } | 627 | } |
| 800 | 628 | ||
| 801 | 629 | ||
| 802 | int mau_cmd_test(mau_config * cnf, int argc, char * argv[]) | 630 | int mau_cmd_test(mau_config * cnf) |
| 803 | { | 631 | { |
| 804 | int c; | 632 | printf("%s\n", cnf->cmd_name); |
| 805 | int opt_index; | 633 | return(0); |
| 806 | 634 | } | |
| 807 | // getopt options | ||
| 808 | static struct option long_opt[] = { MAU_GETOPT_LONG }; | ||
| 809 | |||
| 810 | |||
| 811 | while((c = mau_getopt(cnf, argc, argv, cnf->cmd->cmd_shortopts, long_opt, &opt_index)) != -1) | ||
| 812 | { | ||
| 813 | switch(c) | ||
| 814 | { | ||
| 815 | case -2: /* captured by common options */ | ||
| 816 | case -1: /* no more arguments */ | ||
| 817 | case 0: /* long options toggles */ | ||
| 818 | break; | ||
| 819 | |||
| 820 | case 1: | ||
| 821 | return(0); | ||
| 822 | case 2: | ||
| 823 | return(1); | ||
| 824 | |||
| 825 | case '?': | ||
| 826 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, argv[0]); | ||
| 827 | return(1); | ||
| 828 | |||
| 829 | default: | ||
| 830 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 831 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, argv[0]); | ||
| 832 | return(1); | ||
| 833 | }; | ||
| 834 | }; | ||
| 835 | 635 | ||
| 836 | if ((argc - optind) < 1) | ||
| 837 | { | ||
| 838 | fprintf(stderr, "%s: missing file name\n", PROGRAM_NAME); | ||
| 839 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, argv[0]); | ||
| 840 | return(1); | ||
| 841 | }; | ||
| 842 | if ((argc - optind) < 2) | ||
| 843 | { | ||
| 844 | fprintf(stderr, "%s: missing URL\n", PROGRAM_NAME); | ||
| 845 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, argv[0]); | ||
| 846 | return(1); | ||
| 847 | }; | ||
| 848 | if ((optind+2) != argc) | ||
| 849 | { | ||
| 850 | fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, argv[optind+2]); | ||
| 851 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, argv[0]); | ||
| 852 | return(1); | ||
| 853 | }; | ||
| 854 | 636 | ||
| 637 | int mau_cmd_update(mau_config * cnf) | ||
| 638 | { | ||
| 639 | printf("%s\n", cnf->cmd_name); | ||
| 855 | return(0); | 640 | return(0); |
| 856 | } | 641 | } |
| 857 | 642 | ||
| @@ -1105,6 +890,23 @@ int mau_getopt(mau_config * cnf, int argc, char * const * argv, | |||
| 1105 | 890 | ||
| 1106 | switch(c) | 891 | switch(c) |
| 1107 | { | 892 | { |
| 893 | case 0: | ||
| 894 | if (!(strcmp(long_opt[*opt_index].name, "vmware"))) | ||
| 895 | { | ||
| 896 | cnf->use_oui = 1; | ||
| 897 | cnf->oui[0] = 0x00; | ||
| 898 | cnf->oui[1] = 0x50; | ||
| 899 | cnf->oui[2] = 0x56; | ||
| 900 | }; | ||
| 901 | if (!(strcmp(long_opt[*opt_index].name, "xen"))) | ||
| 902 | { | ||
| 903 | cnf->use_oui = 1; | ||
| 904 | cnf->oui[0] = 0x00; | ||
| 905 | cnf->oui[1] = 0x16; | ||
| 906 | cnf->oui[2] = 0x3e; | ||
| 907 | }; | ||
| 908 | return(-2); | ||
| 909 | |||
| 1108 | case 'c': | 910 | case 'c': |
| 1109 | cnf->notation = MAU_SET_NOTATION(cnf->notation, MAU_NOTATION_COLON); | 911 | cnf->notation = MAU_SET_NOTATION(cnf->notation, MAU_NOTATION_COLON); |
| 1110 | return(-2); | 912 | return(-2); |
| @@ -1133,6 +935,10 @@ int mau_getopt(mau_config * cnf, int argc, char * const * argv, | |||
| 1133 | cnf->notation = MAU_SET_NOTATION(cnf->notation, MAU_NOTATION_RAW); | 935 | cnf->notation = MAU_SET_NOTATION(cnf->notation, MAU_NOTATION_RAW); |
| 1134 | return(-2); | 936 | return(-2); |
| 1135 | 937 | ||
| 938 | case 'r': | ||
| 939 | cnf->rnd_file = optarg; | ||
| 940 | return(-2); | ||
| 941 | |||
| 1136 | case 'u': | 942 | case 'u': |
| 1137 | cnf->notation = MAU_SET_CASE(cnf->notation, MAU_CASE_UPPER); | 943 | cnf->notation = MAU_SET_CASE(cnf->notation, MAU_CASE_UPPER); |
| 1138 | return(-2); | 944 | return(-2); |
| @@ -1145,6 +951,20 @@ int mau_getopt(mau_config * cnf, int argc, char * const * argv, | |||
| 1145 | cnf->verbose++; | 951 | cnf->verbose++; |
| 1146 | return(-2); | 952 | return(-2); |
| 1147 | 953 | ||
| 954 | case 'W': | ||
| 955 | cnf->use_oui = 1; | ||
| 956 | cnf->oui[0] = 0x00; | ||
| 957 | cnf->oui[1] = 0x50; | ||
| 958 | cnf->oui[2] = 0x56; | ||
| 959 | return(-2); | ||
| 960 | |||
| 961 | case 'X': | ||
| 962 | cnf->use_oui = 1; | ||
| 963 | cnf->oui[0] = 0x00; | ||
| 964 | cnf->oui[1] = 0x16; | ||
| 965 | cnf->oui[2] = 0x3e; | ||
| 966 | return(-2); | ||
| 967 | |||
| 1148 | default: | 968 | default: |
| 1149 | break; | 969 | break; |
| 1150 | }; | 970 | }; |
| @@ -1230,11 +1050,12 @@ void mau_usage(mau_config * cnf) | |||
| 1230 | if ((strchr(shortopts, 'q'))) printf(" -q, --quiet, --silent do not print messages\n"); | 1050 | if ((strchr(shortopts, 'q'))) printf(" -q, --quiet, --silent do not print messages\n"); |
| 1231 | if ((strchr(shortopts, 'R'))) printf(" -R, --raw print MAC addresses in raw hex notation\n"); | 1051 | if ((strchr(shortopts, 'R'))) printf(" -R, --raw print MAC addresses in raw hex notation\n"); |
| 1232 | if ((strchr(shortopts, 'r'))) printf(" -r randomdev a file containing random data\n"); | 1052 | if ((strchr(shortopts, 'r'))) printf(" -r randomdev a file containing random data\n"); |
| 1053 | if ((strchr(shortopts, 'U'))) printf(" -U url URL of MA-L Public Listing\n"); | ||
| 1233 | if ((strchr(shortopts, 'u'))) printf(" -u, --upper print HEX digits in upper case\n"); | 1054 | if ((strchr(shortopts, 'u'))) printf(" -u, --upper print HEX digits in upper case\n"); |
| 1234 | if ((strchr(shortopts, 'V'))) printf(" -V, --version print version number and exit\n"); | 1055 | if ((strchr(shortopts, 'V'))) printf(" -V, --version print version number and exit\n"); |
| 1235 | if ((strchr(shortopts, 'v'))) printf(" -v, --verbose print verbose messages\n"); | 1056 | if ((strchr(shortopts, 'v'))) printf(" -v, --verbose print verbose messages\n"); |
| 1236 | if ((strchr(shortopts, 'X'))) printf(" --vmware generate MAC address for VMWare\n"); | 1057 | if ((strchr(shortopts, 'W'))) printf(" --vmware generate MAC address for VMWare\n"); |
| 1237 | if ((strchr(shortopts, 'W'))) printf(" --xen generate MAC address for Xen\n"); | 1058 | if ((strchr(shortopts, 'X'))) printf(" --xen generate MAC address for Xen\n"); |
| 1238 | if (!(cnf->cmd)) | 1059 | if (!(cnf->cmd)) |
| 1239 | { | 1060 | { |
| 1240 | printf("COMMANDS:\n"); | 1061 | printf("COMMANDS:\n"); |
