diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-04-01 00:05:18 +0200 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-06-23 12:09:58 +0200 |
commit | a0d3493f9dbe02f4cf1d578129581c4b45994ca2 (patch) | |
tree | fce1364068acb7b5a6c44c3e64a2ea03f3d6713d | |
parent | 1150082f6042e50c6fe109d15b4e68ea27e0c890 (diff) | |
download | monitoring-plugins-a0d3493f9dbe02f4cf1d578129581c4b45994ca2.tar.gz |
check_dhcp: clang-format
-rw-r--r-- | plugins-root/check_dhcp.c | 184 |
1 files changed, 119 insertions, 65 deletions
diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c index 6802232e..70809956 100644 --- a/plugins-root/check_dhcp.c +++ b/plugins-root/check_dhcp.c | |||
@@ -260,13 +260,15 @@ int main(int argc, char **argv) { | |||
260 | dhcp_socket = create_dhcp_socket(); | 260 | dhcp_socket = create_dhcp_socket(); |
261 | 261 | ||
262 | /* get hardware address of client machine */ | 262 | /* get hardware address of client machine */ |
263 | if (user_specified_mac != NULL) | 263 | if (user_specified_mac != NULL) { |
264 | memcpy(client_hardware_address, user_specified_mac, 6); | 264 | memcpy(client_hardware_address, user_specified_mac, 6); |
265 | else | 265 | } else { |
266 | get_hardware_address(dhcp_socket, network_interface_name); | 266 | get_hardware_address(dhcp_socket, network_interface_name); |
267 | } | ||
267 | 268 | ||
268 | if (unicast) /* get IP address of client machine */ | 269 | if (unicast) { /* get IP address of client machine */ |
269 | get_ip_address(dhcp_socket, network_interface_name); | 270 | get_ip_address(dhcp_socket, network_interface_name); |
271 | } | ||
270 | 272 | ||
271 | /* send DHCPDISCOVER packet */ | 273 | /* send DHCPDISCOVER packet */ |
272 | send_dhcp_discover(dhcp_socket); | 274 | send_dhcp_discover(dhcp_socket); |
@@ -358,8 +360,9 @@ static int get_hardware_address(int sock, char *interface_name) { | |||
358 | int i; | 360 | int i; |
359 | p = interface_name + strlen(interface_name) - 1; | 361 | p = interface_name + strlen(interface_name) - 1; |
360 | for (i = strlen(interface_name) - 1; i > 0; p--) { | 362 | for (i = strlen(interface_name) - 1; i > 0; p--) { |
361 | if (isalpha(*p)) | 363 | if (isalpha(*p)) { |
362 | break; | 364 | break; |
365 | } | ||
363 | } | 366 | } |
364 | p++; | 367 | p++; |
365 | if (p != interface_name) { | 368 | if (p != interface_name) { |
@@ -393,8 +396,9 @@ static int get_hardware_address(int sock, char *interface_name) { | |||
393 | exit(STATE_UNKNOWN); | 396 | exit(STATE_UNKNOWN); |
394 | #endif | 397 | #endif |
395 | 398 | ||
396 | if (verbose) | 399 | if (verbose) { |
397 | print_hardware_address(client_hardware_address); | 400 | print_hardware_address(client_hardware_address); |
401 | } | ||
398 | 402 | ||
399 | return OK; | 403 | return OK; |
400 | } | 404 | } |
@@ -419,8 +423,9 @@ static int get_ip_address(int sock, char *interface_name) { | |||
419 | exit(STATE_UNKNOWN); | 423 | exit(STATE_UNKNOWN); |
420 | #endif | 424 | #endif |
421 | 425 | ||
422 | if (verbose) | 426 | if (verbose) { |
423 | printf(_("Pretending to be relay client %s\n"), inet_ntoa(my_ip)); | 427 | printf(_("Pretending to be relay client %s\n"), inet_ntoa(my_ip)); |
428 | } | ||
424 | 429 | ||
425 | return OK; | 430 | return OK; |
426 | } | 431 | } |
@@ -484,8 +489,9 @@ static int send_dhcp_discover(int sock) { | |||
484 | discover_packet.options[opts++] = (char)DHCP_OPTION_END; | 489 | discover_packet.options[opts++] = (char)DHCP_OPTION_END; |
485 | 490 | ||
486 | /* unicast fields */ | 491 | /* unicast fields */ |
487 | if (unicast) | 492 | if (unicast) { |
488 | discover_packet.giaddr.s_addr = my_ip.s_addr; | 493 | discover_packet.giaddr.s_addr = my_ip.s_addr; |
494 | } | ||
489 | 495 | ||
490 | /* see RFC 1542, 4.1.1 */ | 496 | /* see RFC 1542, 4.1.1 */ |
491 | discover_packet.hops = unicast ? 1 : 0; | 497 | discover_packet.hops = unicast ? 1 : 0; |
@@ -508,8 +514,9 @@ static int send_dhcp_discover(int sock) { | |||
508 | /* send the DHCPDISCOVER packet out */ | 514 | /* send the DHCPDISCOVER packet out */ |
509 | send_dhcp_packet(&discover_packet, sizeof(discover_packet), sock, &sockaddr_broadcast); | 515 | send_dhcp_packet(&discover_packet, sizeof(discover_packet), sock, &sockaddr_broadcast); |
510 | 516 | ||
511 | if (verbose) | 517 | if (verbose) { |
512 | printf("\n\n"); | 518 | printf("\n\n"); |
519 | } | ||
513 | 520 | ||
514 | return OK; | 521 | return OK; |
515 | } | 522 | } |
@@ -531,11 +538,13 @@ static int get_dhcp_offer(int sock) { | |||
531 | for (responses = 0, valid_responses = 0;;) { | 538 | for (responses = 0, valid_responses = 0;;) { |
532 | 539 | ||
533 | time(¤t_time); | 540 | time(¤t_time); |
534 | if ((current_time - start_time) >= dhcpoffer_timeout) | 541 | if ((current_time - start_time) >= dhcpoffer_timeout) { |
535 | break; | 542 | break; |
543 | } | ||
536 | 544 | ||
537 | if (verbose) | 545 | if (verbose) { |
538 | printf("\n\n"); | 546 | printf("\n\n"); |
547 | } | ||
539 | 548 | ||
540 | bzero(&source, sizeof(source)); | 549 | bzero(&source, sizeof(source)); |
541 | bzero(&via, sizeof(via)); | 550 | bzero(&via, sizeof(via)); |
@@ -545,13 +554,15 @@ static int get_dhcp_offer(int sock) { | |||
545 | result = receive_dhcp_packet(&offer_packet, sizeof(offer_packet), sock, dhcpoffer_timeout, &source); | 554 | result = receive_dhcp_packet(&offer_packet, sizeof(offer_packet), sock, dhcpoffer_timeout, &source); |
546 | 555 | ||
547 | if (result != OK) { | 556 | if (result != OK) { |
548 | if (verbose) | 557 | if (verbose) { |
549 | printf(_("Result=ERROR\n")); | 558 | printf(_("Result=ERROR\n")); |
559 | } | ||
550 | 560 | ||
551 | continue; | 561 | continue; |
552 | } else { | 562 | } else { |
553 | if (verbose) | 563 | if (verbose) { |
554 | printf(_("Result=OK\n")); | 564 | printf(_("Result=OK\n")); |
565 | } | ||
555 | 566 | ||
556 | responses++; | 567 | responses++; |
557 | } | 568 | } |
@@ -568,30 +579,37 @@ static int get_dhcp_offer(int sock) { | |||
568 | 579 | ||
569 | /* check packet xid to see if its the same as the one we used in the discover packet */ | 580 | /* check packet xid to see if its the same as the one we used in the discover packet */ |
570 | if (ntohl(offer_packet.xid) != packet_xid) { | 581 | if (ntohl(offer_packet.xid) != packet_xid) { |
571 | if (verbose) | 582 | if (verbose) { |
572 | printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"), ntohl(offer_packet.xid), packet_xid); | 583 | printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"), ntohl(offer_packet.xid), |
584 | packet_xid); | ||
585 | } | ||
573 | 586 | ||
574 | continue; | 587 | continue; |
575 | } | 588 | } |
576 | 589 | ||
577 | /* check hardware address */ | 590 | /* check hardware address */ |
578 | result = OK; | 591 | result = OK; |
579 | if (verbose) | 592 | if (verbose) { |
580 | printf("DHCPOFFER chaddr: "); | 593 | printf("DHCPOFFER chaddr: "); |
594 | } | ||
581 | 595 | ||
582 | for (x = 0; x < ETHERNET_HARDWARE_ADDRESS_LENGTH; x++) { | 596 | for (x = 0; x < ETHERNET_HARDWARE_ADDRESS_LENGTH; x++) { |
583 | if (verbose) | 597 | if (verbose) { |
584 | printf("%02X", (unsigned char)offer_packet.chaddr[x]); | 598 | printf("%02X", (unsigned char)offer_packet.chaddr[x]); |
599 | } | ||
585 | 600 | ||
586 | if (offer_packet.chaddr[x] != client_hardware_address[x]) | 601 | if (offer_packet.chaddr[x] != client_hardware_address[x]) { |
587 | result = ERROR; | 602 | result = ERROR; |
603 | } | ||
588 | } | 604 | } |
589 | if (verbose) | 605 | if (verbose) { |
590 | printf("\n"); | 606 | printf("\n"); |
607 | } | ||
591 | 608 | ||
592 | if (result == ERROR) { | 609 | if (result == ERROR) { |
593 | if (verbose) | 610 | if (verbose) { |
594 | printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n")); | 611 | printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n")); |
612 | } | ||
595 | 613 | ||
596 | continue; | 614 | continue; |
597 | } | 615 | } |
@@ -622,11 +640,13 @@ static int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sock | |||
622 | 640 | ||
623 | result = sendto(sock, (char *)buffer, buffer_size, 0, (struct sockaddr *)dest, sizeof(*dest)); | 641 | result = sendto(sock, (char *)buffer, buffer_size, 0, (struct sockaddr *)dest, sizeof(*dest)); |
624 | 642 | ||
625 | if (verbose) | 643 | if (verbose) { |
626 | printf(_("send_dhcp_packet result: %d\n"), result); | 644 | printf(_("send_dhcp_packet result: %d\n"), result); |
645 | } | ||
627 | 646 | ||
628 | if (result < 0) | 647 | if (result < 0) { |
629 | return ERROR; | 648 | return ERROR; |
649 | } | ||
630 | 650 | ||
631 | return OK; | 651 | return OK; |
632 | } | 652 | } |
@@ -652,8 +672,9 @@ static int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int time | |||
652 | 672 | ||
653 | /* make sure some data has arrived */ | 673 | /* make sure some data has arrived */ |
654 | if (!FD_ISSET(sock, &readfds)) { | 674 | if (!FD_ISSET(sock, &readfds)) { |
655 | if (verbose) | 675 | if (verbose) { |
656 | printf(_("No (more) data received (nfound: %d)\n"), nfound); | 676 | printf(_("No (more) data received (nfound: %d)\n"), nfound); |
677 | } | ||
657 | return ERROR; | 678 | return ERROR; |
658 | } | 679 | } |
659 | 680 | ||
@@ -661,8 +682,9 @@ static int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int time | |||
661 | bzero(&source_address, sizeof(source_address)); | 682 | bzero(&source_address, sizeof(source_address)); |
662 | address_size = sizeof(source_address); | 683 | address_size = sizeof(source_address); |
663 | recv_result = recvfrom(sock, (char *)buffer, buffer_size, 0, (struct sockaddr *)&source_address, &address_size); | 684 | recv_result = recvfrom(sock, (char *)buffer, buffer_size, 0, (struct sockaddr *)&source_address, &address_size); |
664 | if (verbose) | 685 | if (verbose) { |
665 | printf("recv_result: %d\n", recv_result); | 686 | printf("recv_result: %d\n", recv_result); |
687 | } | ||
666 | 688 | ||
667 | if (recv_result == -1) { | 689 | if (recv_result == -1) { |
668 | if (verbose) { | 690 | if (verbose) { |
@@ -706,8 +728,9 @@ static int create_dhcp_socket(void) { | |||
706 | exit(STATE_UNKNOWN); | 728 | exit(STATE_UNKNOWN); |
707 | } | 729 | } |
708 | 730 | ||
709 | if (verbose) | 731 | if (verbose) { |
710 | printf("DHCP socket: %d\n", sock); | 732 | printf("DHCP socket: %d\n", sock); |
733 | } | ||
711 | 734 | ||
712 | /* set the reuse address flag so we don't get errors when restarting */ | 735 | /* set the reuse address flag so we don't get errors when restarting */ |
713 | flag = 1; | 736 | flag = 1; |
@@ -758,8 +781,9 @@ static int add_requested_server(struct in_addr server_address) { | |||
758 | requested_server *new_server; | 781 | requested_server *new_server; |
759 | 782 | ||
760 | new_server = (requested_server *)malloc(sizeof(requested_server)); | 783 | new_server = (requested_server *)malloc(sizeof(requested_server)); |
761 | if (new_server == NULL) | 784 | if (new_server == NULL) { |
762 | return ERROR; | 785 | return ERROR; |
786 | } | ||
763 | 787 | ||
764 | new_server->server_address = server_address; | 788 | new_server->server_address = server_address; |
765 | new_server->answered = false; | 789 | new_server->answered = false; |
@@ -769,8 +793,9 @@ static int add_requested_server(struct in_addr server_address) { | |||
769 | 793 | ||
770 | requested_servers++; | 794 | requested_servers++; |
771 | 795 | ||
772 | if (verbose) | 796 | if (verbose) { |
773 | printf(_("Requested server address: %s\n"), inet_ntoa(new_server->server_address)); | 797 | printf(_("Requested server address: %s\n"), inet_ntoa(new_server->server_address)); |
798 | } | ||
774 | 799 | ||
775 | return OK; | 800 | return OK; |
776 | } | 801 | } |
@@ -783,14 +808,16 @@ static int add_dhcp_offer(struct in_addr source, dhcp_packet *offer_packet) { | |||
783 | unsigned option_length; | 808 | unsigned option_length; |
784 | struct in_addr serv_ident = {0}; | 809 | struct in_addr serv_ident = {0}; |
785 | 810 | ||
786 | if (offer_packet == NULL) | 811 | if (offer_packet == NULL) { |
787 | return ERROR; | 812 | return ERROR; |
813 | } | ||
788 | 814 | ||
789 | /* process all DHCP options present in the packet */ | 815 | /* process all DHCP options present in the packet */ |
790 | for (x = 4; x < MAX_DHCP_OPTIONS_LENGTH - 1;) { | 816 | for (x = 4; x < MAX_DHCP_OPTIONS_LENGTH - 1;) { |
791 | 817 | ||
792 | if ((int)offer_packet->options[x] == -1) | 818 | if ((int)offer_packet->options[x] == -1) { |
793 | break; | 819 | break; |
820 | } | ||
794 | 821 | ||
795 | /* get option type */ | 822 | /* get option type */ |
796 | option_type = offer_packet->options[x++]; | 823 | option_type = offer_packet->options[x++]; |
@@ -798,8 +825,9 @@ static int add_dhcp_offer(struct in_addr source, dhcp_packet *offer_packet) { | |||
798 | /* get option length */ | 825 | /* get option length */ |
799 | option_length = offer_packet->options[x++]; | 826 | option_length = offer_packet->options[x++]; |
800 | 827 | ||
801 | if (verbose) | 828 | if (verbose) { |
802 | printf("Option: %d (0x%02X)\n", option_type, option_length); | 829 | printf("Option: %d (0x%02X)\n", option_type, option_length); |
830 | } | ||
803 | 831 | ||
804 | /* get option data */ | 832 | /* get option data */ |
805 | switch (option_type) { | 833 | switch (option_type) { |
@@ -821,30 +849,35 @@ static int add_dhcp_offer(struct in_addr source, dhcp_packet *offer_packet) { | |||
821 | } | 849 | } |
822 | 850 | ||
823 | /* skip option data we're ignoring */ | 851 | /* skip option data we're ignoring */ |
824 | if (option_type == 0) /* "pad" option, see RFC 2132 (3.1) */ | 852 | if (option_type == 0) { /* "pad" option, see RFC 2132 (3.1) */ |
825 | x += 1; | 853 | x += 1; |
826 | else | 854 | } else { |
827 | x += option_length; | 855 | x += option_length; |
856 | } | ||
828 | } | 857 | } |
829 | 858 | ||
830 | if (verbose) { | 859 | if (verbose) { |
831 | if (dhcp_lease_time == DHCP_INFINITE_TIME) | 860 | if (dhcp_lease_time == DHCP_INFINITE_TIME) { |
832 | printf(_("Lease Time: Infinite\n")); | 861 | printf(_("Lease Time: Infinite\n")); |
833 | else | 862 | } else { |
834 | printf(_("Lease Time: %lu seconds\n"), (unsigned long)dhcp_lease_time); | 863 | printf(_("Lease Time: %lu seconds\n"), (unsigned long)dhcp_lease_time); |
835 | if (dhcp_renewal_time == DHCP_INFINITE_TIME) | 864 | } |
865 | if (dhcp_renewal_time == DHCP_INFINITE_TIME) { | ||
836 | printf(_("Renewal Time: Infinite\n")); | 866 | printf(_("Renewal Time: Infinite\n")); |
837 | else | 867 | } else { |
838 | printf(_("Renewal Time: %lu seconds\n"), (unsigned long)dhcp_renewal_time); | 868 | printf(_("Renewal Time: %lu seconds\n"), (unsigned long)dhcp_renewal_time); |
839 | if (dhcp_rebinding_time == DHCP_INFINITE_TIME) | 869 | } |
870 | if (dhcp_rebinding_time == DHCP_INFINITE_TIME) { | ||
840 | printf(_("Rebinding Time: Infinite\n")); | 871 | printf(_("Rebinding Time: Infinite\n")); |
872 | } | ||
841 | printf(_("Rebinding Time: %lu seconds\n"), (unsigned long)dhcp_rebinding_time); | 873 | printf(_("Rebinding Time: %lu seconds\n"), (unsigned long)dhcp_rebinding_time); |
842 | } | 874 | } |
843 | 875 | ||
844 | new_offer = (dhcp_offer *)malloc(sizeof(dhcp_offer)); | 876 | new_offer = (dhcp_offer *)malloc(sizeof(dhcp_offer)); |
845 | 877 | ||
846 | if (new_offer == NULL) | 878 | if (new_offer == NULL) { |
847 | return ERROR; | 879 | return ERROR; |
880 | } | ||
848 | 881 | ||
849 | /* | 882 | /* |
850 | * RFC 2131 (2.) says: "DHCP clarifies the interpretation of the | 883 | * RFC 2131 (2.) says: "DHCP clarifies the interpretation of the |
@@ -921,20 +954,23 @@ static int get_results(void) { | |||
921 | for (temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) { | 954 | for (temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) { |
922 | 955 | ||
923 | /* get max lease time we were offered */ | 956 | /* get max lease time we were offered */ |
924 | if (temp_offer->lease_time > max_lease_time || temp_offer->lease_time == DHCP_INFINITE_TIME) | 957 | if (temp_offer->lease_time > max_lease_time || temp_offer->lease_time == DHCP_INFINITE_TIME) { |
925 | max_lease_time = temp_offer->lease_time; | 958 | max_lease_time = temp_offer->lease_time; |
959 | } | ||
926 | 960 | ||
927 | /* see if we got the address we requested */ | 961 | /* see if we got the address we requested */ |
928 | if (!memcmp(&requested_address, &temp_offer->offered_address, sizeof(requested_address))) | 962 | if (!memcmp(&requested_address, &temp_offer->offered_address, sizeof(requested_address))) { |
929 | received_requested_address = true; | 963 | received_requested_address = true; |
964 | } | ||
930 | 965 | ||
931 | /* see if the servers we wanted a response from talked to us or not */ | 966 | /* see if the servers we wanted a response from talked to us or not */ |
932 | if (!memcmp(&temp_offer->server_address, &temp_server->server_address, sizeof(temp_server->server_address))) { | 967 | if (!memcmp(&temp_offer->server_address, &temp_server->server_address, sizeof(temp_server->server_address))) { |
933 | if (verbose) { | 968 | if (verbose) { |
934 | printf(_("DHCP Server Match: Offerer=%s"), inet_ntoa(temp_offer->server_address)); | 969 | printf(_("DHCP Server Match: Offerer=%s"), inet_ntoa(temp_offer->server_address)); |
935 | printf(_(" Requested=%s"), inet_ntoa(temp_server->server_address)); | 970 | printf(_(" Requested=%s"), inet_ntoa(temp_server->server_address)); |
936 | if (temp_server->answered) | 971 | if (temp_server->answered) { |
937 | printf(_(" (duplicate)")); | 972 | printf(_(" (duplicate)")); |
973 | } | ||
938 | printf(_("\n")); | 974 | printf(_("\n")); |
939 | } | 975 | } |
940 | if (!temp_server->answered) { | 976 | if (!temp_server->answered) { |
@@ -961,36 +997,41 @@ static int get_results(void) { | |||
961 | for (temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) { | 997 | for (temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) { |
962 | 998 | ||
963 | /* get max lease time we were offered */ | 999 | /* get max lease time we were offered */ |
964 | if (temp_offer->lease_time > max_lease_time || temp_offer->lease_time == DHCP_INFINITE_TIME) | 1000 | if (temp_offer->lease_time > max_lease_time || temp_offer->lease_time == DHCP_INFINITE_TIME) { |
965 | max_lease_time = temp_offer->lease_time; | 1001 | max_lease_time = temp_offer->lease_time; |
1002 | } | ||
966 | 1003 | ||
967 | /* see if we got the address we requested */ | 1004 | /* see if we got the address we requested */ |
968 | if (!memcmp(&requested_address, &temp_offer->offered_address, sizeof(requested_address))) | 1005 | if (!memcmp(&requested_address, &temp_offer->offered_address, sizeof(requested_address))) { |
969 | received_requested_address = true; | 1006 | received_requested_address = true; |
1007 | } | ||
970 | } | 1008 | } |
971 | } | 1009 | } |
972 | 1010 | ||
973 | result = STATE_OK; | 1011 | result = STATE_OK; |
974 | if (valid_responses == 0) | 1012 | if (valid_responses == 0) { |
975 | result = STATE_CRITICAL; | 1013 | result = STATE_CRITICAL; |
976 | else if (requested_servers > 0 && requested_responses == 0) | 1014 | } else if (requested_servers > 0 && requested_responses == 0) { |
977 | result = STATE_CRITICAL; | 1015 | result = STATE_CRITICAL; |
978 | else if (requested_responses < requested_servers) | 1016 | } else if (requested_responses < requested_servers) { |
979 | result = STATE_WARNING; | 1017 | result = STATE_WARNING; |
980 | else if (request_specific_address && !received_requested_address) | 1018 | } else if (request_specific_address && !received_requested_address) { |
981 | result = STATE_WARNING; | 1019 | result = STATE_WARNING; |
1020 | } | ||
982 | 1021 | ||
983 | if (exclusive && undesired_offer) | 1022 | if (exclusive && undesired_offer) { |
984 | result = STATE_CRITICAL; | 1023 | result = STATE_CRITICAL; |
1024 | } | ||
985 | 1025 | ||
986 | if (result == 0) /* garrett honeycutt 2005 */ | 1026 | if (result == 0) { /* garrett honeycutt 2005 */ |
987 | printf("OK: "); | 1027 | printf("OK: "); |
988 | else if (result == 1) | 1028 | } else if (result == 1) { |
989 | printf("WARNING: "); | 1029 | printf("WARNING: "); |
990 | else if (result == 2) | 1030 | } else if (result == 2) { |
991 | printf("CRITICAL: "); | 1031 | printf("CRITICAL: "); |
992 | else if (result == 3) | 1032 | } else if (result == 3) { |
993 | printf("UNKNOWN: "); | 1033 | printf("UNKNOWN: "); |
1034 | } | ||
994 | 1035 | ||
995 | /* we didn't receive any DHCPOFFERs */ | 1036 | /* we didn't receive any DHCPOFFERs */ |
996 | if (dhcp_offer_list == NULL) { | 1037 | if (dhcp_offer_list == NULL) { |
@@ -1006,18 +1047,22 @@ static int get_results(void) { | |||
1006 | return result; | 1047 | return result; |
1007 | } | 1048 | } |
1008 | 1049 | ||
1009 | if (requested_servers > 0) | 1050 | if (requested_servers > 0) { |
1010 | printf(_(", %s%d of %d requested servers responded"), ((requested_responses < requested_servers) && requested_responses > 0) ? "only " : "", requested_responses, | 1051 | printf(_(", %s%d of %d requested servers responded"), |
1052 | ((requested_responses < requested_servers) && requested_responses > 0) ? "only " : "", requested_responses, | ||
1011 | requested_servers); | 1053 | requested_servers); |
1054 | } | ||
1012 | 1055 | ||
1013 | if (request_specific_address) | 1056 | if (request_specific_address) { |
1014 | printf(_(", requested address (%s) was %soffered"), inet_ntoa(requested_address), (received_requested_address) ? "" : _("not ")); | 1057 | printf(_(", requested address (%s) was %soffered"), inet_ntoa(requested_address), (received_requested_address) ? "" : _("not ")); |
1058 | } | ||
1015 | 1059 | ||
1016 | printf(_(", max lease time = ")); | 1060 | printf(_(", max lease time = ")); |
1017 | if (max_lease_time == DHCP_INFINITE_TIME) | 1061 | if (max_lease_time == DHCP_INFINITE_TIME) { |
1018 | printf(_("Infinity")); | 1062 | printf(_("Infinity")); |
1019 | else | 1063 | } else { |
1020 | printf("%lu sec", (unsigned long)max_lease_time); | 1064 | printf("%lu sec", (unsigned long)max_lease_time); |
1065 | } | ||
1021 | 1066 | ||
1022 | printf(".\n"); | 1067 | printf(".\n"); |
1023 | 1068 | ||
@@ -1026,8 +1071,9 @@ static int get_results(void) { | |||
1026 | 1071 | ||
1027 | /* process command-line arguments */ | 1072 | /* process command-line arguments */ |
1028 | static int process_arguments(int argc, char **argv) { | 1073 | static int process_arguments(int argc, char **argv) { |
1029 | if (argc < 1) | 1074 | if (argc < 1) { |
1030 | return ERROR; | 1075 | return ERROR; |
1076 | } | ||
1031 | 1077 | ||
1032 | call_getopt(argc, argv); | 1078 | call_getopt(argc, argv); |
1033 | return validate_arguments(argc); | 1079 | return validate_arguments(argc); |
@@ -1052,8 +1098,9 @@ static int call_getopt(int argc, char **argv) { | |||
1052 | while (true) { | 1098 | while (true) { |
1053 | c = getopt_long(argc, argv, "+hVvxt:s:r:t:i:m:u", long_options, &option_index); | 1099 | c = getopt_long(argc, argv, "+hVvxt:s:r:t:i:m:u", long_options, &option_index); |
1054 | 1100 | ||
1055 | if (c == -1 || c == EOF || c == 1) | 1101 | if (c == -1 || c == EOF || c == 1) { |
1056 | break; | 1102 | break; |
1103 | } | ||
1057 | 1104 | ||
1058 | switch (c) { | 1105 | switch (c) { |
1059 | 1106 | ||
@@ -1072,8 +1119,9 @@ static int call_getopt(int argc, char **argv) { | |||
1072 | /* | 1119 | /* |
1073 | if(is_intnonneg(optarg)) | 1120 | if(is_intnonneg(optarg)) |
1074 | */ | 1121 | */ |
1075 | if (atoi(optarg) > 0) | 1122 | if (atoi(optarg) > 0) { |
1076 | dhcpoffer_timeout = atoi(optarg); | 1123 | dhcpoffer_timeout = atoi(optarg); |
1124 | } | ||
1077 | /* | 1125 | /* |
1078 | else | 1126 | else |
1079 | usage("Time interval must be a nonnegative integer\n"); | 1127 | usage("Time interval must be a nonnegative integer\n"); |
@@ -1082,10 +1130,12 @@ static int call_getopt(int argc, char **argv) { | |||
1082 | 1130 | ||
1083 | case 'm': /* MAC address */ | 1131 | case 'm': /* MAC address */ |
1084 | 1132 | ||
1085 | if ((user_specified_mac = mac_aton(optarg)) == NULL) | 1133 | if ((user_specified_mac = mac_aton(optarg)) == NULL) { |
1086 | usage("Cannot parse MAC address.\n"); | 1134 | usage("Cannot parse MAC address.\n"); |
1087 | if (verbose) | 1135 | } |
1136 | if (verbose) { | ||
1088 | print_hardware_address(user_specified_mac); | 1137 | print_hardware_address(user_specified_mac); |
1138 | } | ||
1089 | 1139 | ||
1090 | break; | 1140 | break; |
1091 | 1141 | ||
@@ -1127,8 +1177,9 @@ static int call_getopt(int argc, char **argv) { | |||
1127 | 1177 | ||
1128 | static int validate_arguments(int argc) { | 1178 | static int validate_arguments(int argc) { |
1129 | 1179 | ||
1130 | if (argc - optind > 0) | 1180 | if (argc - optind > 0) { |
1131 | usage(_("Got unexpected non-option argument")); | 1181 | usage(_("Got unexpected non-option argument")); |
1182 | } | ||
1132 | 1183 | ||
1133 | return OK; | 1184 | return OK; |
1134 | } | 1185 | } |
@@ -1273,8 +1324,9 @@ static void resolve_host(const char *in, struct in_addr *out) { | |||
1273 | 1324 | ||
1274 | memset(&hints, 0, sizeof(hints)); | 1325 | memset(&hints, 0, sizeof(hints)); |
1275 | hints.ai_family = PF_INET; | 1326 | hints.ai_family = PF_INET; |
1276 | if (getaddrinfo(in, NULL, &hints, &ai) != 0) | 1327 | if (getaddrinfo(in, NULL, &hints, &ai) != 0) { |
1277 | usage_va(_("Invalid hostname/address - %s"), optarg); | 1328 | usage_va(_("Invalid hostname/address - %s"), optarg); |
1329 | } | ||
1278 | 1330 | ||
1279 | memcpy(out, &((struct sockaddr_in *)ai->ai_addr)->sin_addr, sizeof(*out)); | 1331 | memcpy(out, &((struct sockaddr_in *)ai->ai_addr)->sin_addr, sizeof(*out)); |
1280 | freeaddrinfo(ai); | 1332 | freeaddrinfo(ai); |
@@ -1288,8 +1340,9 @@ static unsigned char *mac_aton(const char *string) { | |||
1288 | 1340 | ||
1289 | for (i = 0, j = 0; string[i] != '\0' && j < sizeof(result); i++) { | 1341 | for (i = 0, j = 0; string[i] != '\0' && j < sizeof(result); i++) { |
1290 | /* ignore ':' and any other non-hex character */ | 1342 | /* ignore ':' and any other non-hex character */ |
1291 | if (!isxdigit(string[i]) || !isxdigit(string[i + 1])) | 1343 | if (!isxdigit(string[i]) || !isxdigit(string[i + 1])) { |
1292 | continue; | 1344 | continue; |
1345 | } | ||
1293 | tmp[0] = string[i]; | 1346 | tmp[0] = string[i]; |
1294 | tmp[1] = string[i + 1]; | 1347 | tmp[1] = string[i + 1]; |
1295 | tmp[2] = '\0'; | 1348 | tmp[2] = '\0'; |
@@ -1305,8 +1358,9 @@ static void print_hardware_address(const unsigned char *address) { | |||
1305 | int i; | 1358 | int i; |
1306 | 1359 | ||
1307 | printf(_("Hardware address: ")); | 1360 | printf(_("Hardware address: ")); |
1308 | for (i = 0; i < 5; i++) | 1361 | for (i = 0; i < 5; i++) { |
1309 | printf("%2.2x:", address[i]); | 1362 | printf("%2.2x:", address[i]); |
1363 | } | ||
1310 | printf("%2.2x", address[i]); | 1364 | printf("%2.2x", address[i]); |
1311 | putchar('\n'); | 1365 | putchar('\n'); |
1312 | } | 1366 | } |