diff options
| -rw-r--r-- | plugins-root/check_dhcp.c | 610 |
1 files changed, 305 insertions, 305 deletions
diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c index 312de546..99df3d23 100644 --- a/plugins-root/check_dhcp.c +++ b/plugins-root/check_dhcp.c | |||
| @@ -1,37 +1,37 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * Monitoring check_dhcp plugin | 3 | * Monitoring check_dhcp plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org) | 6 | * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org) |
| 7 | * Copyright (c) 2001-2007 Monitoring Plugins Development Team | 7 | * Copyright (c) 2001-2007 Monitoring Plugins Development Team |
| 8 | * | 8 | * |
| 9 | * Description: | 9 | * Description: |
| 10 | * | 10 | * |
| 11 | * This file contains the check_dhcp plugin | 11 | * This file contains the check_dhcp plugin |
| 12 | * | 12 | * |
| 13 | * This plugin tests the availability of DHCP servers on a network. | 13 | * This plugin tests the availability of DHCP servers on a network. |
| 14 | * | 14 | * |
| 15 | * Unicast mode was originally implemented by Heiti of Boras Kommun with | 15 | * Unicast mode was originally implemented by Heiti of Boras Kommun with |
| 16 | * general improvements as well as usability fixes and "forward"-porting by | 16 | * general improvements as well as usability fixes and "forward"-porting by |
| 17 | * Andreas Ericsson of OP5 AB. | 17 | * Andreas Ericsson of OP5 AB. |
| 18 | * | 18 | * |
| 19 | * | 19 | * |
| 20 | * This program is free software: you can redistribute it and/or modify | 20 | * This program is free software: you can redistribute it and/or modify |
| 21 | * it under the terms of the GNU General Public License as published by | 21 | * it under the terms of the GNU General Public License as published by |
| 22 | * the Free Software Foundation, either version 3 of the License, or | 22 | * the Free Software Foundation, either version 3 of the License, or |
| 23 | * (at your option) any later version. | 23 | * (at your option) any later version. |
| 24 | * | 24 | * |
| 25 | * This program is distributed in the hope that it will be useful, | 25 | * This program is distributed in the hope that it will be useful, |
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 | * GNU General Public License for more details. | 28 | * GNU General Public License for more details. |
| 29 | * | 29 | * |
| 30 | * You should have received a copy of the GNU General Public License | 30 | * You should have received a copy of the GNU General Public License |
| 31 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 31 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 32 | * | 32 | * |
| 33 | * | 33 | * |
| 34 | *****************************************************************************/ | 34 | *****************************************************************************/ |
| 35 | 35 | ||
| 36 | const char *progname = "check_dhcp"; | 36 | const char *progname = "check_dhcp"; |
| 37 | const char *copyright = "2001-2007"; | 37 | const char *copyright = "2001-2007"; |
| @@ -128,22 +128,22 @@ long mac_addr_dlpi( const char *, int, u_char *); | |||
| 128 | 128 | ||
| 129 | 129 | ||
| 130 | typedef struct dhcp_packet_struct{ | 130 | typedef struct dhcp_packet_struct{ |
| 131 | uint8_t op; /* packet type */ | 131 | uint8_t op; /* packet type */ |
| 132 | uint8_t htype; /* type of hardware address for this machine (Ethernet, etc) */ | 132 | uint8_t htype; /* type of hardware address for this machine (Ethernet, etc) */ |
| 133 | uint8_t hlen; /* length of hardware address (of this machine) */ | 133 | uint8_t hlen; /* length of hardware address (of this machine) */ |
| 134 | uint8_t hops; /* hops */ | 134 | uint8_t hops; /* hops */ |
| 135 | uint32_t xid; /* random transaction id number - chosen by this machine */ | 135 | uint32_t xid; /* random transaction id number - chosen by this machine */ |
| 136 | uint16_t secs; /* seconds used in timing */ | 136 | uint16_t secs; /* seconds used in timing */ |
| 137 | uint16_t flags; /* flags */ | 137 | uint16_t flags; /* flags */ |
| 138 | struct in_addr ciaddr; /* IP address of this machine (if we already have one) */ | 138 | struct in_addr ciaddr; /* IP address of this machine (if we already have one) */ |
| 139 | struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */ | 139 | struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */ |
| 140 | struct in_addr siaddr; /* IP address of next server */ | 140 | struct in_addr siaddr; /* IP address of next server */ |
| 141 | struct in_addr giaddr; /* IP address of DHCP relay */ | 141 | struct in_addr giaddr; /* IP address of DHCP relay */ |
| 142 | unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */ | 142 | unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */ |
| 143 | char sname [MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */ | 143 | char sname [MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */ |
| 144 | char file [MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */ | 144 | char file [MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */ |
| 145 | char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */ | 145 | char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */ |
| 146 | }dhcp_packet; | 146 | }dhcp_packet; |
| 147 | 147 | ||
| 148 | 148 | ||
| 149 | typedef struct dhcp_offer_struct{ | 149 | typedef struct dhcp_offer_struct{ |
| @@ -153,14 +153,14 @@ typedef struct dhcp_offer_struct{ | |||
| 153 | uint32_t renewal_time; /* renewal time in seconds */ | 153 | uint32_t renewal_time; /* renewal time in seconds */ |
| 154 | uint32_t rebinding_time; /* rebinding time in seconds */ | 154 | uint32_t rebinding_time; /* rebinding time in seconds */ |
| 155 | struct dhcp_offer_struct *next; | 155 | struct dhcp_offer_struct *next; |
| 156 | }dhcp_offer; | 156 | }dhcp_offer; |
| 157 | 157 | ||
| 158 | 158 | ||
| 159 | typedef struct requested_server_struct{ | 159 | typedef struct requested_server_struct{ |
| 160 | struct in_addr server_address; | 160 | struct in_addr server_address; |
| 161 | int answered; | 161 | int answered; |
| 162 | struct requested_server_struct *next; | 162 | struct requested_server_struct *next; |
| 163 | }requested_server; | 163 | }requested_server; |
| 164 | 164 | ||
| 165 | 165 | ||
| 166 | #define BOOTREQUEST 1 | 166 | #define BOOTREQUEST 1 |
| @@ -264,7 +264,7 @@ int main(int argc, char **argv){ | |||
| 264 | 264 | ||
| 265 | if(process_arguments(argc,argv)!=OK){ | 265 | if(process_arguments(argc,argv)!=OK){ |
| 266 | usage4 (_("Could not parse arguments")); | 266 | usage4 (_("Could not parse arguments")); |
| 267 | } | 267 | } |
| 268 | 268 | ||
| 269 | /* create socket for DHCP communications */ | 269 | /* create socket for DHCP communications */ |
| 270 | dhcp_socket=create_dhcp_socket(); | 270 | dhcp_socket=create_dhcp_socket(); |
| @@ -295,7 +295,7 @@ int main(int argc, char **argv){ | |||
| 295 | free_requested_server_list(); | 295 | free_requested_server_list(); |
| 296 | 296 | ||
| 297 | return result; | 297 | return result; |
| 298 | } | 298 | } |
| 299 | 299 | ||
| 300 | 300 | ||
| 301 | 301 | ||
| @@ -310,83 +310,83 @@ int get_hardware_address(int sock,char *interface_name){ | |||
| 310 | 310 | ||
| 311 | /* try and grab hardware address of requested interface */ | 311 | /* try and grab hardware address of requested interface */ |
| 312 | if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){ | 312 | if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){ |
| 313 | printf(_("Error: Could not get hardware address of interface '%s'\n"),interface_name); | 313 | printf(_("Error: Could not get hardware address of interface '%s'\n"),interface_name); |
| 314 | exit(STATE_UNKNOWN); | 314 | exit(STATE_UNKNOWN); |
| 315 | } | 315 | } |
| 316 | 316 | ||
| 317 | memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6); | 317 | memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6); |
| 318 | 318 | ||
| 319 | #elif defined(__bsd__) | 319 | #elif defined(__bsd__) |
| 320 | /* King 2004 see ACKNOWLEDGEMENTS */ | 320 | /* King 2004 see ACKNOWLEDGEMENTS */ |
| 321 | 321 | ||
| 322 | size_t len; | 322 | size_t len; |
| 323 | int mib[6]; | 323 | int mib[6]; |
| 324 | char *buf; | 324 | char *buf; |
| 325 | unsigned char *ptr; | 325 | unsigned char *ptr; |
| 326 | struct if_msghdr *ifm; | 326 | struct if_msghdr *ifm; |
| 327 | struct sockaddr_dl *sdl; | 327 | struct sockaddr_dl *sdl; |
| 328 | 328 | ||
| 329 | mib[0] = CTL_NET; | 329 | mib[0] = CTL_NET; |
| 330 | mib[1] = AF_ROUTE; | 330 | mib[1] = AF_ROUTE; |
| 331 | mib[2] = 0; | 331 | mib[2] = 0; |
| 332 | mib[3] = AF_LINK; | 332 | mib[3] = AF_LINK; |
| 333 | mib[4] = NET_RT_IFLIST; | 333 | mib[4] = NET_RT_IFLIST; |
| 334 | 334 | ||
| 335 | if((mib[5] = if_nametoindex(interface_name)) == 0){ | 335 | if((mib[5] = if_nametoindex(interface_name)) == 0){ |
| 336 | printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno)); | 336 | printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno)); |
| 337 | exit(STATE_UNKNOWN); | 337 | exit(STATE_UNKNOWN); |
| 338 | } | 338 | } |
| 339 | 339 | ||
| 340 | if(sysctl(mib, 6, NULL, &len, NULL, 0) < 0){ | 340 | if(sysctl(mib, 6, NULL, &len, NULL, 0) < 0){ |
| 341 | printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name, strerror(errno)); | 341 | printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name, strerror(errno)); |
| 342 | exit(STATE_UNKNOWN); | 342 | exit(STATE_UNKNOWN); |
| 343 | } | 343 | } |
| 344 | 344 | ||
| 345 | if((buf = malloc(len)) == NULL){ | 345 | if((buf = malloc(len)) == NULL){ |
| 346 | printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name, strerror(errno)); | 346 | printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name, strerror(errno)); |
| 347 | exit(4); | 347 | exit(4); |
| 348 | } | 348 | } |
| 349 | 349 | ||
| 350 | if(sysctl(mib, 6, buf, &len, NULL, 0) < 0){ | 350 | if(sysctl(mib, 6, buf, &len, NULL, 0) < 0){ |
| 351 | printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name, strerror(errno)); | 351 | printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name, strerror(errno)); |
| 352 | exit(STATE_UNKNOWN); | 352 | exit(STATE_UNKNOWN); |
| 353 | } | 353 | } |
| 354 | 354 | ||
| 355 | ifm = (struct if_msghdr *)buf; | 355 | ifm = (struct if_msghdr *)buf; |
| 356 | sdl = (struct sockaddr_dl *)(ifm + 1); | 356 | sdl = (struct sockaddr_dl *)(ifm + 1); |
| 357 | ptr = (unsigned char *)LLADDR(sdl); | 357 | ptr = (unsigned char *)LLADDR(sdl); |
| 358 | memcpy(&client_hardware_address[0], ptr, 6) ; | 358 | memcpy(&client_hardware_address[0], ptr, 6) ; |
| 359 | /* King 2004 */ | 359 | /* King 2004 */ |
| 360 | 360 | ||
| 361 | #elif defined(__sun__) || defined(__solaris__) | 361 | #elif defined(__sun__) || defined(__solaris__) |
| 362 | 362 | ||
| 363 | /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */ | 363 | /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */ |
| 364 | long stat; | 364 | long stat; |
| 365 | char dev[20] = "/dev/"; | 365 | char dev[20] = "/dev/"; |
| 366 | char *p; | 366 | char *p; |
| 367 | int unit; | 367 | int unit; |
| 368 | 368 | ||
| 369 | /* get last number from interfacename, eg lnc0, e1000g0*/ | 369 | /* get last number from interfacename, eg lnc0, e1000g0*/ |
| 370 | int i; | 370 | int i; |
| 371 | p = interface_name + strlen(interface_name) -1; | 371 | p = interface_name + strlen(interface_name) -1; |
| 372 | for(i = strlen(interface_name) -1; i > 0; p--) { | 372 | for(i = strlen(interface_name) -1; i > 0; p--) { |
| 373 | if(isalpha(*p)) | 373 | if(isalpha(*p)) |
| 374 | break; | 374 | break; |
| 375 | } | 375 | } |
| 376 | p++; | 376 | p++; |
| 377 | if( p != interface_name ){ | 377 | if( p != interface_name ){ |
| 378 | unit = atoi(p) ; | 378 | unit = atoi(p) ; |
| 379 | strncat(dev, interface_name, 6) ; | 379 | strncat(dev, interface_name, 6) ; |
| 380 | } | 380 | } |
| 381 | else{ | 381 | else{ |
| 382 | printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name); | 382 | printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name); |
| 383 | exit(STATE_UNKNOWN); | 383 | exit(STATE_UNKNOWN); |
| 384 | } | 384 | } |
| 385 | stat = mac_addr_dlpi(dev, unit, client_hardware_address); | 385 | stat = mac_addr_dlpi(dev, unit, client_hardware_address); |
| 386 | if(stat != 0){ | 386 | if(stat != 0){ |
| 387 | printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit); | 387 | printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit); |
| 388 | exit(STATE_UNKNOWN); | 388 | exit(STATE_UNKNOWN); |
| 389 | } | 389 | } |
| 390 | 390 | ||
| 391 | #elif defined(__hpux__) | 391 | #elif defined(__hpux__) |
| 392 | 392 | ||
| @@ -398,8 +398,8 @@ int get_hardware_address(int sock,char *interface_name){ | |||
| 398 | if(stat != 0){ | 398 | if(stat != 0){ |
| 399 | printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit); | 399 | printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit); |
| 400 | exit(STATE_UNKNOWN); | 400 | exit(STATE_UNKNOWN); |
| 401 | } | 401 | } |
| 402 | /* Kompf 2000-2003 */ | 402 | /* Kompf 2000-2003 */ |
| 403 | 403 | ||
| 404 | #else | 404 | #else |
| 405 | printf(_("Error: can't get MAC address for this architecture. Use the --mac option.\n")); | 405 | printf(_("Error: can't get MAC address for this architecture. Use the --mac option.\n")); |
| @@ -410,7 +410,7 @@ int get_hardware_address(int sock,char *interface_name){ | |||
| 410 | print_hardware_address(client_hardware_address); | 410 | print_hardware_address(client_hardware_address); |
| 411 | 411 | ||
| 412 | return OK; | 412 | return OK; |
| 413 | } | 413 | } |
| 414 | 414 | ||
| 415 | /* determines IP address of the client interface */ | 415 | /* determines IP address of the client interface */ |
| 416 | int get_ip_address(int sock,char *interface_name){ | 416 | int get_ip_address(int sock,char *interface_name){ |
| @@ -422,9 +422,9 @@ int get_ip_address(int sock,char *interface_name){ | |||
| 422 | 422 | ||
| 423 | if(ioctl(sock,SIOCGIFADDR,&ifr)<0){ | 423 | if(ioctl(sock,SIOCGIFADDR,&ifr)<0){ |
| 424 | printf(_("Error: Cannot determine IP address of interface %s\n"), | 424 | printf(_("Error: Cannot determine IP address of interface %s\n"), |
| 425 | interface_name); | 425 | interface_name); |
| 426 | exit(STATE_UNKNOWN); | 426 | exit(STATE_UNKNOWN); |
| 427 | } | 427 | } |
| 428 | 428 | ||
| 429 | my_ip=((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; | 429 | my_ip=((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; |
| 430 | 430 | ||
| @@ -437,13 +437,13 @@ int get_ip_address(int sock,char *interface_name){ | |||
| 437 | printf(_("Pretending to be relay client %s\n"),inet_ntoa(my_ip)); | 437 | printf(_("Pretending to be relay client %s\n"),inet_ntoa(my_ip)); |
| 438 | 438 | ||
| 439 | return OK; | 439 | return OK; |
| 440 | } | 440 | } |
| 441 | 441 | ||
| 442 | /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */ | 442 | /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */ |
| 443 | int send_dhcp_discover(int sock){ | 443 | int send_dhcp_discover(int sock){ |
| 444 | dhcp_packet discover_packet; | 444 | dhcp_packet discover_packet; |
| 445 | struct sockaddr_in sockaddr_broadcast; | 445 | struct sockaddr_in sockaddr_broadcast; |
| 446 | unsigned short opts; | 446 | unsigned short opts; |
| 447 | 447 | ||
| 448 | 448 | ||
| 449 | /* clear the packet data structure */ | 449 | /* clear the packet data structure */ |
| @@ -484,7 +484,7 @@ int send_dhcp_discover(int sock){ | |||
| 484 | discover_packet.options[2]='\x53'; | 484 | discover_packet.options[2]='\x53'; |
| 485 | discover_packet.options[3]='\x63'; | 485 | discover_packet.options[3]='\x63'; |
| 486 | 486 | ||
| 487 | opts = 4; | 487 | opts = 4; |
| 488 | /* DHCP message type is embedded in options field */ | 488 | /* DHCP message type is embedded in options field */ |
| 489 | discover_packet.options[opts++]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */ | 489 | discover_packet.options[opts++]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */ |
| 490 | discover_packet.options[opts++]='\x01'; /* DHCP message option length in bytes */ | 490 | discover_packet.options[opts++]='\x01'; /* DHCP message option length in bytes */ |
| @@ -496,7 +496,7 @@ int send_dhcp_discover(int sock){ | |||
| 496 | discover_packet.options[opts++]='\x04'; | 496 | discover_packet.options[opts++]='\x04'; |
| 497 | memcpy(&discover_packet.options[opts],&requested_address,sizeof(requested_address)); | 497 | memcpy(&discover_packet.options[opts],&requested_address,sizeof(requested_address)); |
| 498 | opts += sizeof(requested_address); | 498 | opts += sizeof(requested_address); |
| 499 | } | 499 | } |
| 500 | discover_packet.options[opts++]=DHCP_OPTION_END; | 500 | discover_packet.options[opts++]=DHCP_OPTION_END; |
| 501 | 501 | ||
| 502 | /* unicast fields */ | 502 | /* unicast fields */ |
| @@ -507,8 +507,8 @@ int send_dhcp_discover(int sock){ | |||
| 507 | discover_packet.hops = unicast ? 1 : 0; | 507 | discover_packet.hops = unicast ? 1 : 0; |
| 508 | 508 | ||
| 509 | /* send the DHCPDISCOVER packet to broadcast address */ | 509 | /* send the DHCPDISCOVER packet to broadcast address */ |
| 510 | sockaddr_broadcast.sin_family=AF_INET; | 510 | sockaddr_broadcast.sin_family=AF_INET; |
| 511 | sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT); | 511 | sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT); |
| 512 | sockaddr_broadcast.sin_addr.s_addr = unicast ? dhcp_ip.s_addr : INADDR_BROADCAST; | 512 | sockaddr_broadcast.sin_addr.s_addr = unicast ? dhcp_ip.s_addr : INADDR_BROADCAST; |
| 513 | bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero)); | 513 | bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero)); |
| 514 | 514 | ||
| @@ -520,7 +520,7 @@ int send_dhcp_discover(int sock){ | |||
| 520 | printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr)); | 520 | printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr)); |
| 521 | printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr)); | 521 | printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr)); |
| 522 | printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr)); | 522 | printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr)); |
| 523 | } | 523 | } |
| 524 | 524 | ||
| 525 | /* send the DHCPDISCOVER packet out */ | 525 | /* send the DHCPDISCOVER packet out */ |
| 526 | send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast); | 526 | send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast); |
| @@ -529,7 +529,7 @@ int send_dhcp_discover(int sock){ | |||
| 529 | printf("\n\n"); | 529 | printf("\n\n"); |
| 530 | 530 | ||
| 531 | return OK; | 531 | return OK; |
| 532 | } | 532 | } |
| 533 | 533 | ||
| 534 | 534 | ||
| 535 | 535 | ||
| @@ -569,13 +569,13 @@ int get_dhcp_offer(int sock){ | |||
| 569 | printf(_("Result=ERROR\n")); | 569 | printf(_("Result=ERROR\n")); |
| 570 | 570 | ||
| 571 | continue; | 571 | continue; |
| 572 | } | 572 | } |
| 573 | else{ | 573 | else{ |
| 574 | if(verbose) | 574 | if(verbose) |
| 575 | printf(_("Result=OK\n")); | 575 | printf(_("Result=OK\n")); |
| 576 | 576 | ||
| 577 | responses++; | 577 | responses++; |
| 578 | } | 578 | } |
| 579 | 579 | ||
| 580 | /* The "source" is either a server or a relay. */ | 580 | /* The "source" is either a server or a relay. */ |
| 581 | /* Save a copy of "source" into "via" even if it's via itself */ | 581 | /* Save a copy of "source" into "via" even if it's via itself */ |
| @@ -585,7 +585,7 @@ int get_dhcp_offer(int sock){ | |||
| 585 | printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source.sin_addr)); | 585 | printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source.sin_addr)); |
| 586 | printf(_(" via %s\n"),inet_ntoa(via.sin_addr)); | 586 | printf(_(" via %s\n"),inet_ntoa(via.sin_addr)); |
| 587 | printf("DHCPOFFER XID: %u (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid)); | 587 | printf("DHCPOFFER XID: %u (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid)); |
| 588 | } | 588 | } |
| 589 | 589 | ||
| 590 | /* check packet xid to see if its the same as the one we used in the discover packet */ | 590 | /* check packet xid to see if its the same as the one we used in the discover packet */ |
| 591 | if(ntohl(offer_packet.xid)!=packet_xid){ | 591 | if(ntohl(offer_packet.xid)!=packet_xid){ |
| @@ -593,7 +593,7 @@ int get_dhcp_offer(int sock){ | |||
| 593 | printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"),ntohl(offer_packet.xid),packet_xid); | 593 | printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"),ntohl(offer_packet.xid),packet_xid); |
| 594 | 594 | ||
| 595 | continue; | 595 | continue; |
| 596 | } | 596 | } |
| 597 | 597 | ||
| 598 | /* check hardware address */ | 598 | /* check hardware address */ |
| 599 | result=OK; | 599 | result=OK; |
| @@ -606,7 +606,7 @@ int get_dhcp_offer(int sock){ | |||
| 606 | 606 | ||
| 607 | if(offer_packet.chaddr[x]!=client_hardware_address[x]) | 607 | if(offer_packet.chaddr[x]!=client_hardware_address[x]) |
| 608 | result=ERROR; | 608 | result=ERROR; |
| 609 | } | 609 | } |
| 610 | if(verbose) | 610 | if(verbose) |
| 611 | printf("\n"); | 611 | printf("\n"); |
| 612 | 612 | ||
| @@ -615,27 +615,27 @@ int get_dhcp_offer(int sock){ | |||
| 615 | printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n")); | 615 | printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n")); |
| 616 | 616 | ||
| 617 | continue; | 617 | continue; |
| 618 | } | 618 | } |
| 619 | 619 | ||
| 620 | if(verbose){ | 620 | if(verbose){ |
| 621 | printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr)); | 621 | printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr)); |
| 622 | printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr)); | 622 | printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr)); |
| 623 | printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr)); | 623 | printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr)); |
| 624 | printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr)); | 624 | printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr)); |
| 625 | } | 625 | } |
| 626 | 626 | ||
| 627 | add_dhcp_offer(source.sin_addr,&offer_packet); | 627 | add_dhcp_offer(source.sin_addr,&offer_packet); |
| 628 | 628 | ||
| 629 | valid_responses++; | 629 | valid_responses++; |
| 630 | } | 630 | } |
| 631 | 631 | ||
| 632 | if(verbose){ | 632 | if(verbose){ |
| 633 | printf(_("Total responses seen on the wire: %d\n"),responses); | 633 | printf(_("Total responses seen on the wire: %d\n"),responses); |
| 634 | printf(_("Valid responses for this machine: %d\n"),valid_responses); | 634 | printf(_("Valid responses for this machine: %d\n"),valid_responses); |
| 635 | } | 635 | } |
| 636 | 636 | ||
| 637 | return OK; | 637 | return OK; |
| 638 | } | 638 | } |
| 639 | 639 | ||
| 640 | 640 | ||
| 641 | 641 | ||
| @@ -652,14 +652,14 @@ int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in | |||
| 652 | return ERROR; | 652 | return ERROR; |
| 653 | 653 | ||
| 654 | return OK; | 654 | return OK; |
| 655 | } | 655 | } |
| 656 | 656 | ||
| 657 | 657 | ||
| 658 | 658 | ||
| 659 | /* receives a DHCP packet */ | 659 | /* receives a DHCP packet */ |
| 660 | int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){ | 660 | int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){ |
| 661 | struct timeval tv; | 661 | struct timeval tv; |
| 662 | fd_set readfds; | 662 | fd_set readfds; |
| 663 | fd_set oobfds; | 663 | fd_set oobfds; |
| 664 | int recv_result; | 664 | int recv_result; |
| 665 | socklen_t address_size; | 665 | socklen_t address_size; |
| @@ -667,88 +667,88 @@ int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, st | |||
| 667 | int nfound; | 667 | int nfound; |
| 668 | 668 | ||
| 669 | 669 | ||
| 670 | /* wait for data to arrive (up time timeout) */ | 670 | /* wait for data to arrive (up time timeout) */ |
| 671 | tv.tv_sec=timeout; | 671 | tv.tv_sec=timeout; |
| 672 | tv.tv_usec=0; | 672 | tv.tv_usec=0; |
| 673 | FD_ZERO(&readfds); | 673 | FD_ZERO(&readfds); |
| 674 | FD_ZERO(&oobfds); | 674 | FD_ZERO(&oobfds); |
| 675 | FD_SET(sock,&readfds); | 675 | FD_SET(sock,&readfds); |
| 676 | FD_SET(sock,&oobfds); | 676 | FD_SET(sock,&oobfds); |
| 677 | nfound = select(sock+1,&readfds,NULL,&oobfds,&tv); | 677 | nfound = select(sock+1,&readfds,NULL,&oobfds,&tv); |
| 678 | 678 | ||
| 679 | /* make sure some data has arrived */ | 679 | /* make sure some data has arrived */ |
| 680 | if(!FD_ISSET(sock,&readfds)){ | 680 | if(!FD_ISSET(sock,&readfds)){ |
| 681 | if(verbose) | 681 | if(verbose) |
| 682 | printf(_("No (more) data received (nfound: %d)\n"), nfound); | 682 | printf(_("No (more) data received (nfound: %d)\n"), nfound); |
| 683 | return ERROR; | 683 | return ERROR; |
| 684 | } | 684 | } |
| 685 | 685 | ||
| 686 | else{ | 686 | else{ |
| 687 | bzero(&source_address,sizeof(source_address)); | 687 | bzero(&source_address,sizeof(source_address)); |
| 688 | address_size=sizeof(source_address); | 688 | address_size=sizeof(source_address); |
| 689 | recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size); | 689 | recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size); |
| 690 | if(verbose) | 690 | if(verbose) |
| 691 | printf("recv_result: %d\n",recv_result); | 691 | printf("recv_result: %d\n",recv_result); |
| 692 | 692 | ||
| 693 | if(recv_result==-1){ | 693 | if(recv_result==-1){ |
| 694 | if(verbose){ | 694 | if(verbose){ |
| 695 | printf(_("recvfrom() failed, ")); | 695 | printf(_("recvfrom() failed, ")); |
| 696 | printf("errno: (%d) -> %s\n",errno,strerror(errno)); | 696 | printf("errno: (%d) -> %s\n",errno,strerror(errno)); |
| 697 | } | 697 | } |
| 698 | return ERROR; | 698 | return ERROR; |
| 699 | } | 699 | } |
| 700 | else{ | 700 | else{ |
| 701 | if(verbose){ | 701 | if(verbose){ |
| 702 | printf(_("receive_dhcp_packet() result: %d\n"),recv_result); | 702 | printf(_("receive_dhcp_packet() result: %d\n"),recv_result); |
| 703 | printf(_("receive_dhcp_packet() source: %s\n"),inet_ntoa(source_address.sin_addr)); | 703 | printf(_("receive_dhcp_packet() source: %s\n"),inet_ntoa(source_address.sin_addr)); |
| 704 | } | 704 | } |
| 705 | 705 | ||
| 706 | memcpy(address,&source_address,sizeof(source_address)); | 706 | memcpy(address,&source_address,sizeof(source_address)); |
| 707 | return OK; | 707 | return OK; |
| 708 | } | 708 | } |
| 709 | } | 709 | } |
| 710 | 710 | ||
| 711 | return OK; | 711 | return OK; |
| 712 | } | 712 | } |
| 713 | 713 | ||
| 714 | 714 | ||
| 715 | /* creates a socket for DHCP communication */ | 715 | /* creates a socket for DHCP communication */ |
| 716 | int create_dhcp_socket(void){ | 716 | int create_dhcp_socket(void){ |
| 717 | struct sockaddr_in myname; | 717 | struct sockaddr_in myname; |
| 718 | struct ifreq interface; | 718 | struct ifreq interface; |
| 719 | int sock; | 719 | int sock; |
| 720 | int flag=1; | 720 | int flag=1; |
| 721 | 721 | ||
| 722 | /* Set up the address we're going to bind to. */ | 722 | /* Set up the address we're going to bind to. */ |
| 723 | bzero(&myname,sizeof(myname)); | 723 | bzero(&myname,sizeof(myname)); |
| 724 | myname.sin_family=AF_INET; | 724 | myname.sin_family=AF_INET; |
| 725 | /* listen to DHCP server port if we're in unicast mode */ | 725 | /* listen to DHCP server port if we're in unicast mode */ |
| 726 | myname.sin_port = htons(unicast ? DHCP_SERVER_PORT : DHCP_CLIENT_PORT); | 726 | myname.sin_port = htons(unicast ? DHCP_SERVER_PORT : DHCP_CLIENT_PORT); |
| 727 | myname.sin_addr.s_addr = unicast ? my_ip.s_addr : INADDR_ANY; | 727 | myname.sin_addr.s_addr = unicast ? my_ip.s_addr : INADDR_ANY; |
| 728 | bzero(&myname.sin_zero,sizeof(myname.sin_zero)); | 728 | bzero(&myname.sin_zero,sizeof(myname.sin_zero)); |
| 729 | 729 | ||
| 730 | /* create a socket for DHCP communications */ | 730 | /* create a socket for DHCP communications */ |
| 731 | sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); | 731 | sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); |
| 732 | if(sock<0){ | 732 | if(sock<0){ |
| 733 | printf(_("Error: Could not create socket!\n")); | 733 | printf(_("Error: Could not create socket!\n")); |
| 734 | exit(STATE_UNKNOWN); | 734 | exit(STATE_UNKNOWN); |
| 735 | } | 735 | } |
| 736 | 736 | ||
| 737 | if(verbose) | 737 | if(verbose) |
| 738 | printf("DHCP socket: %d\n",sock); | 738 | printf("DHCP socket: %d\n",sock); |
| 739 | 739 | ||
| 740 | /* set the reuse address flag so we don't get errors when restarting */ | 740 | /* set the reuse address flag so we don't get errors when restarting */ |
| 741 | flag=1; | 741 | flag=1; |
| 742 | if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){ | 742 | if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){ |
| 743 | printf(_("Error: Could not set reuse address option on DHCP socket!\n")); | 743 | printf(_("Error: Could not set reuse address option on DHCP socket!\n")); |
| 744 | exit(STATE_UNKNOWN); | 744 | exit(STATE_UNKNOWN); |
| 745 | } | 745 | } |
| 746 | 746 | ||
| 747 | /* set the broadcast option - we need this to listen to DHCP broadcast messages */ | 747 | /* set the broadcast option - we need this to listen to DHCP broadcast messages */ |
| 748 | if(!unicast && setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){ | 748 | if(!unicast && setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){ |
| 749 | printf(_("Error: Could not set broadcast option on DHCP socket!\n")); | 749 | printf(_("Error: Could not set broadcast option on DHCP socket!\n")); |
| 750 | exit(STATE_UNKNOWN); | 750 | exit(STATE_UNKNOWN); |
| 751 | } | 751 | } |
| 752 | 752 | ||
| 753 | /* bind socket to interface */ | 753 | /* bind socket to interface */ |
| 754 | #if defined(__linux__) | 754 | #if defined(__linux__) |
| @@ -757,21 +757,21 @@ int create_dhcp_socket(void){ | |||
| 757 | if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){ | 757 | if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){ |
| 758 | printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"),network_interface_name); | 758 | printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"),network_interface_name); |
| 759 | exit(STATE_UNKNOWN); | 759 | exit(STATE_UNKNOWN); |
| 760 | } | 760 | } |
| 761 | 761 | ||
| 762 | #else | 762 | #else |
| 763 | strncpy(interface.ifr_name,network_interface_name,IFNAMSIZ-1); | 763 | strncpy(interface.ifr_name,network_interface_name,IFNAMSIZ-1); |
| 764 | interface.ifr_name[IFNAMSIZ-1]='\0'; | 764 | interface.ifr_name[IFNAMSIZ-1]='\0'; |
| 765 | #endif | 765 | #endif |
| 766 | 766 | ||
| 767 | /* bind the socket */ | 767 | /* bind the socket */ |
| 768 | if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){ | 768 | if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){ |
| 769 | printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"),DHCP_CLIENT_PORT); | 769 | printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"),DHCP_CLIENT_PORT); |
| 770 | exit(STATE_UNKNOWN); | 770 | exit(STATE_UNKNOWN); |
| 771 | } | 771 | } |
| 772 | 772 | ||
| 773 | return sock; | 773 | return sock; |
| 774 | } | 774 | } |
| 775 | 775 | ||
| 776 | 776 | ||
| 777 | /* closes DHCP socket */ | 777 | /* closes DHCP socket */ |
| @@ -780,7 +780,7 @@ int close_dhcp_socket(int sock){ | |||
| 780 | close(sock); | 780 | close(sock); |
| 781 | 781 | ||
| 782 | return OK; | 782 | return OK; |
| 783 | } | 783 | } |
| 784 | 784 | ||
| 785 | 785 | ||
| 786 | /* adds a requested server address to list in memory */ | 786 | /* adds a requested server address to list in memory */ |
| @@ -803,7 +803,7 @@ int add_requested_server(struct in_addr server_address){ | |||
| 803 | printf(_("Requested server address: %s\n"),inet_ntoa(new_server->server_address)); | 803 | printf(_("Requested server address: %s\n"),inet_ntoa(new_server->server_address)); |
| 804 | 804 | ||
| 805 | return OK; | 805 | return OK; |
| 806 | } | 806 | } |
| 807 | 807 | ||
| 808 | 808 | ||
| 809 | 809 | ||
| @@ -836,29 +836,29 @@ int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){ | |||
| 836 | 836 | ||
| 837 | /* get option data */ | 837 | /* get option data */ |
| 838 | switch(option_type){ | 838 | switch(option_type){ |
| 839 | case DHCP_OPTION_LEASE_TIME: | 839 | case DHCP_OPTION_LEASE_TIME: |
| 840 | memcpy(&dhcp_lease_time, &offer_packet->options[x],sizeof(dhcp_lease_time)); | 840 | memcpy(&dhcp_lease_time, &offer_packet->options[x],sizeof(dhcp_lease_time)); |
| 841 | dhcp_lease_time = ntohl(dhcp_lease_time); | 841 | dhcp_lease_time = ntohl(dhcp_lease_time); |
| 842 | break; | 842 | break; |
| 843 | case DHCP_OPTION_RENEWAL_TIME: | 843 | case DHCP_OPTION_RENEWAL_TIME: |
| 844 | memcpy(&dhcp_renewal_time, &offer_packet->options[x],sizeof(dhcp_renewal_time)); | 844 | memcpy(&dhcp_renewal_time, &offer_packet->options[x],sizeof(dhcp_renewal_time)); |
| 845 | dhcp_renewal_time = ntohl(dhcp_renewal_time); | 845 | dhcp_renewal_time = ntohl(dhcp_renewal_time); |
| 846 | break; | 846 | break; |
| 847 | case DHCP_OPTION_REBINDING_TIME: | 847 | case DHCP_OPTION_REBINDING_TIME: |
| 848 | memcpy(&dhcp_rebinding_time, &offer_packet->options[x],sizeof(dhcp_rebinding_time)); | 848 | memcpy(&dhcp_rebinding_time, &offer_packet->options[x],sizeof(dhcp_rebinding_time)); |
| 849 | dhcp_rebinding_time = ntohl(dhcp_rebinding_time); | 849 | dhcp_rebinding_time = ntohl(dhcp_rebinding_time); |
| 850 | break; | 850 | break; |
| 851 | case DHCP_OPTION_SERVER_IDENTIFIER: | 851 | case DHCP_OPTION_SERVER_IDENTIFIER: |
| 852 | memcpy(&serv_ident.s_addr, &offer_packet->options[x],sizeof(serv_ident.s_addr)); | 852 | memcpy(&serv_ident.s_addr, &offer_packet->options[x],sizeof(serv_ident.s_addr)); |
| 853 | break; | 853 | break; |
| 854 | } | 854 | } |
| 855 | 855 | ||
| 856 | /* skip option data we're ignoring */ | 856 | /* skip option data we're ignoring */ |
| 857 | if(option_type==0) /* "pad" option, see RFC 2132 (3.1) */ | 857 | if(option_type==0) /* "pad" option, see RFC 2132 (3.1) */ |
| 858 | x+=1; | 858 | x+=1; |
| 859 | else | 859 | else |
| 860 | x+=option_length; | 860 | x+=option_length; |
| 861 | } | 861 | } |
| 862 | 862 | ||
| 863 | if(verbose){ | 863 | if(verbose){ |
| 864 | if(dhcp_lease_time==DHCP_INFINITE_TIME) | 864 | if(dhcp_lease_time==DHCP_INFINITE_TIME) |
| @@ -872,7 +872,7 @@ int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){ | |||
| 872 | if(dhcp_rebinding_time==DHCP_INFINITE_TIME) | 872 | if(dhcp_rebinding_time==DHCP_INFINITE_TIME) |
| 873 | printf(_("Rebinding Time: Infinite\n")); | 873 | printf(_("Rebinding Time: Infinite\n")); |
| 874 | printf(_("Rebinding Time: %lu seconds\n"),(unsigned long)dhcp_rebinding_time); | 874 | printf(_("Rebinding Time: %lu seconds\n"),(unsigned long)dhcp_rebinding_time); |
| 875 | } | 875 | } |
| 876 | 876 | ||
| 877 | new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer)); | 877 | new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer)); |
| 878 | 878 | ||
| @@ -901,14 +901,14 @@ int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){ | |||
| 901 | if(verbose){ | 901 | if(verbose){ |
| 902 | printf(_("Added offer from server @ %s"),inet_ntoa(new_offer->server_address)); | 902 | printf(_("Added offer from server @ %s"),inet_ntoa(new_offer->server_address)); |
| 903 | printf(_(" of IP address %s\n"),inet_ntoa(new_offer->offered_address)); | 903 | printf(_(" of IP address %s\n"),inet_ntoa(new_offer->offered_address)); |
| 904 | } | 904 | } |
| 905 | 905 | ||
| 906 | /* add new offer to head of list */ | 906 | /* add new offer to head of list */ |
| 907 | new_offer->next=dhcp_offer_list; | 907 | new_offer->next=dhcp_offer_list; |
| 908 | dhcp_offer_list=new_offer; | 908 | dhcp_offer_list=new_offer; |
| 909 | 909 | ||
| 910 | return OK; | 910 | return OK; |
| 911 | } | 911 | } |
| 912 | 912 | ||
| 913 | 913 | ||
| 914 | /* frees memory allocated to DHCP OFFER list */ | 914 | /* frees memory allocated to DHCP OFFER list */ |
| @@ -919,10 +919,10 @@ int free_dhcp_offer_list(void){ | |||
| 919 | for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){ | 919 | for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){ |
| 920 | next_offer=this_offer->next; | 920 | next_offer=this_offer->next; |
| 921 | free(this_offer); | 921 | free(this_offer); |
| 922 | } | 922 | } |
| 923 | 923 | ||
| 924 | return OK; | 924 | return OK; |
| 925 | } | 925 | } |
| 926 | 926 | ||
| 927 | 927 | ||
| 928 | /* frees memory allocated to requested server list */ | 928 | /* frees memory allocated to requested server list */ |
| @@ -933,10 +933,10 @@ int free_requested_server_list(void){ | |||
| 933 | for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){ | 933 | for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){ |
| 934 | next_server=this_server->next; | 934 | next_server=this_server->next; |
| 935 | free(this_server); | 935 | free(this_server); |
| 936 | } | 936 | } |
| 937 | 937 | ||
| 938 | return OK; | 938 | return OK; |
| 939 | } | 939 | } |
| 940 | 940 | ||
| 941 | 941 | ||
| 942 | /* gets state and plugin output to return */ | 942 | /* gets state and plugin output to return */ |
| @@ -972,16 +972,16 @@ int get_results(void){ | |||
| 972 | if(temp_server->answered) | 972 | if(temp_server->answered) |
| 973 | printf(_(" (duplicate)")); | 973 | printf(_(" (duplicate)")); |
| 974 | printf(_("\n")); | 974 | printf(_("\n")); |
| 975 | } | 975 | } |
| 976 | if(temp_server->answered == FALSE){ | 976 | if(temp_server->answered == FALSE){ |
| 977 | requested_responses++; | 977 | requested_responses++; |
| 978 | temp_server->answered=TRUE; | 978 | temp_server->answered=TRUE; |
| 979 | } | 979 | } |
| 980 | } | 980 | } |
| 981 | } | 981 | } |
| 982 | } | 982 | } |
| 983 | 983 | ||
| 984 | } | 984 | } |
| 985 | 985 | ||
| 986 | /* else check and see if we got our requested address from any server */ | 986 | /* else check and see if we got our requested address from any server */ |
| 987 | else{ | 987 | else{ |
| @@ -995,8 +995,8 @@ int get_results(void){ | |||
| 995 | /* see if we got the address we requested */ | 995 | /* see if we got the address we requested */ |
| 996 | if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address))) | 996 | if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address))) |
| 997 | received_requested_address=TRUE; | 997 | received_requested_address=TRUE; |
| 998 | } | 998 | } |
| 999 | } | 999 | } |
| 1000 | 1000 | ||
| 1001 | result=STATE_OK; | 1001 | result=STATE_OK; |
| 1002 | if(valid_responses==0) | 1002 | if(valid_responses==0) |
| @@ -1021,7 +1021,7 @@ int get_results(void){ | |||
| 1021 | if(dhcp_offer_list==NULL){ | 1021 | if(dhcp_offer_list==NULL){ |
| 1022 | printf(_("No DHCPOFFERs were received.\n")); | 1022 | printf(_("No DHCPOFFERs were received.\n")); |
| 1023 | return result; | 1023 | return result; |
| 1024 | } | 1024 | } |
| 1025 | 1025 | ||
| 1026 | printf(_("Received %d DHCPOFFER(s)"),valid_responses); | 1026 | printf(_("Received %d DHCPOFFER(s)"),valid_responses); |
| 1027 | 1027 | ||
| @@ -1040,7 +1040,7 @@ int get_results(void){ | |||
| 1040 | printf(".\n"); | 1040 | printf(".\n"); |
| 1041 | 1041 | ||
| 1042 | return result; | 1042 | return result; |
| 1043 | } | 1043 | } |
| 1044 | 1044 | ||
| 1045 | 1045 | ||
| 1046 | /* process command-line arguments */ | 1046 | /* process command-line arguments */ |
| @@ -1083,71 +1083,71 @@ int call_getopt(int argc, char **argv){ | |||
| 1083 | 1083 | ||
| 1084 | switch(c){ | 1084 | switch(c){ |
| 1085 | 1085 | ||
| 1086 | case 's': /* DHCP server address */ | 1086 | case 's': /* DHCP server address */ |
| 1087 | resolve_host(optarg,&dhcp_ip); | 1087 | resolve_host(optarg,&dhcp_ip); |
| 1088 | add_requested_server(dhcp_ip); | 1088 | add_requested_server(dhcp_ip); |
| 1089 | break; | 1089 | break; |
| 1090 | 1090 | ||
| 1091 | case 'r': /* address we are requested from DHCP servers */ | 1091 | case 'r': /* address we are requested from DHCP servers */ |
| 1092 | resolve_host(optarg,&requested_address); | 1092 | resolve_host(optarg,&requested_address); |
| 1093 | request_specific_address=TRUE; | 1093 | request_specific_address=TRUE; |
| 1094 | break; | 1094 | break; |
| 1095 | 1095 | ||
| 1096 | case 't': /* timeout */ | 1096 | case 't': /* timeout */ |
| 1097 | |||
| 1098 | /* | ||
| 1099 | if(is_intnonneg(optarg)) | ||
| 1100 | */ | ||
| 1101 | if(atoi(optarg)>0) | ||
| 1102 | dhcpoffer_timeout=atoi(optarg); | ||
| 1103 | /* | ||
| 1104 | else | ||
| 1105 | usage("Time interval must be a nonnegative integer\n"); | ||
| 1106 | */ | ||
| 1107 | break; | ||
| 1108 | 1097 | ||
| 1109 | case 'm': /* MAC address */ | 1098 | /* |
| 1099 | if(is_intnonneg(optarg)) | ||
| 1100 | */ | ||
| 1101 | if(atoi(optarg)>0) | ||
| 1102 | dhcpoffer_timeout=atoi(optarg); | ||
| 1103 | /* | ||
| 1104 | else | ||
| 1105 | usage("Time interval must be a nonnegative integer\n"); | ||
| 1106 | */ | ||
| 1107 | break; | ||
| 1110 | 1108 | ||
| 1111 | if((user_specified_mac=mac_aton(optarg)) == NULL) | 1109 | case 'm': /* MAC address */ |
| 1112 | usage("Cannot parse MAC address.\n"); | ||
| 1113 | if(verbose) | ||
| 1114 | print_hardware_address(user_specified_mac); | ||
| 1115 | 1110 | ||
| 1116 | break; | 1111 | if((user_specified_mac=mac_aton(optarg)) == NULL) |
| 1112 | usage("Cannot parse MAC address.\n"); | ||
| 1113 | if(verbose) | ||
| 1114 | print_hardware_address(user_specified_mac); | ||
| 1117 | 1115 | ||
| 1118 | case 'i': /* interface name */ | 1116 | break; |
| 1119 | 1117 | ||
| 1120 | strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1); | 1118 | case 'i': /* interface name */ |
| 1121 | network_interface_name[sizeof(network_interface_name)-1]='\x0'; | ||
| 1122 | 1119 | ||
| 1123 | break; | 1120 | strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1); |
| 1121 | network_interface_name[sizeof(network_interface_name)-1]='\x0'; | ||
| 1124 | 1122 | ||
| 1125 | case 'u': /* unicast testing */ | 1123 | break; |
| 1126 | unicast=1; | ||
| 1127 | break; | ||
| 1128 | 1124 | ||
| 1129 | case 'V': /* version */ | 1125 | case 'u': /* unicast testing */ |
| 1130 | print_revision(progname, NP_VERSION); | 1126 | unicast=1; |
| 1131 | exit(STATE_UNKNOWN); | 1127 | break; |
| 1132 | 1128 | ||
| 1133 | case 'h': /* help */ | 1129 | case 'V': /* version */ |
| 1134 | print_help(); | 1130 | print_revision(progname, NP_VERSION); |
| 1135 | exit(STATE_UNKNOWN); | 1131 | exit(STATE_UNKNOWN); |
| 1136 | 1132 | ||
| 1137 | case 'v': /* verbose */ | 1133 | case 'h': /* help */ |
| 1138 | verbose=1; | 1134 | print_help(); |
| 1139 | break; | 1135 | exit(STATE_UNKNOWN); |
| 1140 | 1136 | ||
| 1141 | case '?': /* help */ | 1137 | case 'v': /* verbose */ |
| 1142 | usage5 (); | 1138 | verbose=1; |
| 1143 | break; | 1139 | break; |
| 1144 | 1140 | ||
| 1145 | default: | 1141 | case '?': /* help */ |
| 1146 | break; | 1142 | usage5 (); |
| 1147 | } | 1143 | break; |
| 1148 | } | 1144 | |
| 1145 | default: | ||
| 1146 | break; | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | return optind; | 1149 | return optind; |
| 1150 | } | 1150 | } |
| 1151 | 1151 | ||
| 1152 | 1152 | ||
| 1153 | int validate_arguments(int argc){ | 1153 | int validate_arguments(int argc){ |
| @@ -1174,21 +1174,21 @@ static int get_msg(int fd){ | |||
| 1174 | if(res < 0){ | 1174 | if(res < 0){ |
| 1175 | if(errno == EINTR){ | 1175 | if(errno == EINTR){ |
| 1176 | return(GOT_INTR); | 1176 | return(GOT_INTR); |
| 1177 | } | 1177 | } |
| 1178 | else{ | 1178 | else{ |
| 1179 | printf("%s\n", "get_msg FAILED."); | 1179 | printf("%s\n", "get_msg FAILED."); |
| 1180 | return(GOT_ERR); | 1180 | return(GOT_ERR); |
| 1181 | } | ||
| 1182 | } | 1181 | } |
| 1182 | } | ||
| 1183 | if(ctl.len > 0){ | 1183 | if(ctl.len > 0){ |
| 1184 | ret |= GOT_CTRL; | 1184 | ret |= GOT_CTRL; |
| 1185 | } | 1185 | } |
| 1186 | if(dat.len > 0){ | 1186 | if(dat.len > 0){ |
| 1187 | ret |= GOT_DATA; | 1187 | ret |= GOT_DATA; |
| 1188 | } | 1188 | } |
| 1189 | 1189 | ||
| 1190 | return(ret); | 1190 | return(ret); |
| 1191 | } | 1191 | } |
| 1192 | 1192 | ||
| 1193 | /* verify that dl_primitive in ctl_area = prim */ | 1193 | /* verify that dl_primitive in ctl_area = prim */ |
| 1194 | static int check_ctrl(int prim){ | 1194 | static int check_ctrl(int prim){ |
| @@ -1197,10 +1197,10 @@ static int check_ctrl(int prim){ | |||
| 1197 | if(err_ack->dl_primitive != prim){ | 1197 | if(err_ack->dl_primitive != prim){ |
| 1198 | printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno)); | 1198 | printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno)); |
| 1199 | exit(STATE_UNKNOWN); | 1199 | exit(STATE_UNKNOWN); |
| 1200 | } | 1200 | } |
| 1201 | 1201 | ||
| 1202 | return 0; | 1202 | return 0; |
| 1203 | } | 1203 | } |
| 1204 | 1204 | ||
| 1205 | /* put a control message on a stream */ | 1205 | /* put a control message on a stream */ |
| 1206 | static int put_ctrl(int fd, int len, int pri){ | 1206 | static int put_ctrl(int fd, int len, int pri){ |
| @@ -1209,10 +1209,10 @@ static int put_ctrl(int fd, int len, int pri){ | |||
| 1209 | if(putmsg(fd, &ctl, 0, pri) < 0){ | 1209 | if(putmsg(fd, &ctl, 0, pri) < 0){ |
| 1210 | printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno)); | 1210 | printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno)); |
| 1211 | exit(STATE_UNKNOWN); | 1211 | exit(STATE_UNKNOWN); |
| 1212 | } | 1212 | } |
| 1213 | 1213 | ||
| 1214 | return 0; | 1214 | return 0; |
| 1215 | } | 1215 | } |
| 1216 | 1216 | ||
| 1217 | /* put a control + data message on a stream */ | 1217 | /* put a control + data message on a stream */ |
| 1218 | static int put_both(int fd, int clen, int dlen, int pri){ | 1218 | static int put_both(int fd, int clen, int dlen, int pri){ |
| @@ -1222,10 +1222,10 @@ static int put_both(int fd, int clen, int dlen, int pri){ | |||
| 1222 | if(putmsg(fd, &ctl, &dat, pri) < 0){ | 1222 | if(putmsg(fd, &ctl, &dat, pri) < 0){ |
| 1223 | printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno)); | 1223 | printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno)); |
| 1224 | exit(STATE_UNKNOWN); | 1224 | exit(STATE_UNKNOWN); |
| 1225 | } | 1225 | } |
| 1226 | 1226 | ||
| 1227 | return 0; | 1227 | return 0; |
| 1228 | } | 1228 | } |
| 1229 | 1229 | ||
| 1230 | /* open file descriptor and attach */ | 1230 | /* open file descriptor and attach */ |
| 1231 | static int dl_open(const char *dev, int unit, int *fd){ | 1231 | static int dl_open(const char *dev, int unit, int *fd){ |
| @@ -1234,13 +1234,13 @@ static int dl_open(const char *dev, int unit, int *fd){ | |||
| 1234 | if((*fd = open(dev, O_RDWR)) == -1){ | 1234 | if((*fd = open(dev, O_RDWR)) == -1){ |
| 1235 | printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev, strerror(errno)); | 1235 | printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev, strerror(errno)); |
| 1236 | exit(STATE_UNKNOWN); | 1236 | exit(STATE_UNKNOWN); |
| 1237 | } | 1237 | } |
| 1238 | attach_req->dl_primitive = DL_ATTACH_REQ; | 1238 | attach_req->dl_primitive = DL_ATTACH_REQ; |
| 1239 | attach_req->dl_ppa = unit; | 1239 | attach_req->dl_ppa = unit; |
| 1240 | put_ctrl(*fd, sizeof(dl_attach_req_t), 0); | 1240 | put_ctrl(*fd, sizeof(dl_attach_req_t), 0); |
| 1241 | get_msg(*fd); | 1241 | get_msg(*fd); |
| 1242 | return check_ctrl(DL_OK_ACK); | 1242 | return check_ctrl(DL_OK_ACK); |
| 1243 | } | 1243 | } |
| 1244 | 1244 | ||
| 1245 | /* send DL_BIND_REQ */ | 1245 | /* send DL_BIND_REQ */ |
| 1246 | static int dl_bind(int fd, int sap, u_char *addr){ | 1246 | static int dl_bind(int fd, int sap, u_char *addr){ |
| @@ -1258,12 +1258,12 @@ static int dl_bind(int fd, int sap, u_char *addr){ | |||
| 1258 | if (GOT_ERR == check_ctrl(DL_BIND_ACK)){ | 1258 | if (GOT_ERR == check_ctrl(DL_BIND_ACK)){ |
| 1259 | printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno)); | 1259 | printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno)); |
| 1260 | exit(STATE_UNKNOWN); | 1260 | exit(STATE_UNKNOWN); |
| 1261 | } | 1261 | } |
| 1262 | bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr, | 1262 | bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr, |
| 1263 | bind_ack->dl_addr_length); | 1263 | bind_ack->dl_addr_length); |
| 1264 | 1264 | ||
| 1265 | return 0; | 1265 | return 0; |
| 1266 | } | 1266 | } |
| 1267 | 1267 | ||
| 1268 | /*********************************************************************** | 1268 | /*********************************************************************** |
| 1269 | * interface: | 1269 | * interface: |
| @@ -1282,15 +1282,15 @@ long mac_addr_dlpi( const char *dev, int unit, u_char *addr){ | |||
| 1282 | u_char mac_addr[25]; | 1282 | u_char mac_addr[25]; |
| 1283 | 1283 | ||
| 1284 | if(GOT_ERR != dl_open(dev, unit, &fd)){ | 1284 | if(GOT_ERR != dl_open(dev, unit, &fd)){ |
| 1285 | if(GOT_ERR != dl_bind(fd, INSAP, mac_addr)){ | 1285 | if(GOT_ERR != dl_bind(fd, INSAP, mac_addr)){ |
| 1286 | bcopy( mac_addr, addr, 6); | 1286 | bcopy( mac_addr, addr, 6); |
| 1287 | return 0; | 1287 | return 0; |
| 1288 | } | ||
| 1289 | } | 1288 | } |
| 1290 | close(fd); | 1289 | } |
| 1290 | close(fd); | ||
| 1291 | 1291 | ||
| 1292 | return -1; | 1292 | return -1; |
| 1293 | } | 1293 | } |
| 1294 | 1294 | ||
| 1295 | /* Kompf 2000-2003 */ | 1295 | /* Kompf 2000-2003 */ |
| 1296 | #endif | 1296 | #endif |
| @@ -1307,7 +1307,7 @@ void resolve_host(const char *in,struct in_addr *out){ | |||
| 1307 | 1307 | ||
| 1308 | memcpy(out,&((struct sockaddr_in *)ai->ai_addr)->sin_addr,sizeof(*out)); | 1308 | memcpy(out,&((struct sockaddr_in *)ai->ai_addr)->sin_addr,sizeof(*out)); |
| 1309 | freeaddrinfo(ai); | 1309 | freeaddrinfo(ai); |
| 1310 | } | 1310 | } |
| 1311 | 1311 | ||
| 1312 | 1312 | ||
| 1313 | /* parse MAC address string, return 6 bytes (unterminated) or NULL */ | 1313 | /* parse MAC address string, return 6 bytes (unterminated) or NULL */ |
| @@ -1326,10 +1326,10 @@ unsigned char *mac_aton(const char *string){ | |||
| 1326 | result[j]=strtol(tmp,(char **)NULL,16); | 1326 | result[j]=strtol(tmp,(char **)NULL,16); |
| 1327 | i++; | 1327 | i++; |
| 1328 | j++; | 1328 | j++; |
| 1329 | } | 1329 | } |
| 1330 | 1330 | ||
| 1331 | return (j==6) ? result : NULL; | 1331 | return (j==6) ? result : NULL; |
| 1332 | } | 1332 | } |
| 1333 | 1333 | ||
| 1334 | 1334 | ||
| 1335 | void print_hardware_address(const unsigned char *address){ | 1335 | void print_hardware_address(const unsigned char *address){ |
| @@ -1340,7 +1340,7 @@ void print_hardware_address(const unsigned char *address){ | |||
| 1340 | printf("%2.2x:", address[i]); | 1340 | printf("%2.2x:", address[i]); |
| 1341 | printf("%2.2x", address[i]); | 1341 | printf("%2.2x", address[i]); |
| 1342 | putchar('\n'); | 1342 | putchar('\n'); |
| 1343 | } | 1343 | } |
| 1344 | 1344 | ||
| 1345 | 1345 | ||
| 1346 | /* print usage help */ | 1346 | /* print usage help */ |
| @@ -1353,7 +1353,7 @@ void print_help(void){ | |||
| 1353 | 1353 | ||
| 1354 | printf("%s\n", _("This plugin tests the availability of DHCP servers on a network.")); | 1354 | printf("%s\n", _("This plugin tests the availability of DHCP servers on a network.")); |
| 1355 | 1355 | ||
| 1356 | printf ("\n\n"); | 1356 | printf ("\n\n"); |
| 1357 | 1357 | ||
| 1358 | print_usage(); | 1358 | print_usage(); |
| 1359 | 1359 | ||
| @@ -1363,32 +1363,32 @@ void print_help(void){ | |||
| 1363 | printf (UT_VERBOSE); | 1363 | printf (UT_VERBOSE); |
| 1364 | 1364 | ||
| 1365 | printf (" %s\n", "-s, --serverip=IPADDRESS"); | 1365 | printf (" %s\n", "-s, --serverip=IPADDRESS"); |
| 1366 | printf (" %s\n", _("IP address of DHCP server that we must hear from")); | 1366 | printf (" %s\n", _("IP address of DHCP server that we must hear from")); |
| 1367 | printf (" %s\n", "-r, --requestedip=IPADDRESS"); | 1367 | printf (" %s\n", "-r, --requestedip=IPADDRESS"); |
| 1368 | printf (" %s\n", _("IP address that should be offered by at least one DHCP server")); | 1368 | printf (" %s\n", _("IP address that should be offered by at least one DHCP server")); |
| 1369 | printf (" %s\n", "-t, --timeout=INTEGER"); | 1369 | printf (" %s\n", "-t, --timeout=INTEGER"); |
| 1370 | printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs")); | 1370 | printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs")); |
| 1371 | printf (" %s\n", "-i, --interface=STRING"); | 1371 | printf (" %s\n", "-i, --interface=STRING"); |
| 1372 | printf (" %s\n", _("Interface to to use for listening (i.e. eth0)")); | 1372 | printf (" %s\n", _("Interface to to use for listening (i.e. eth0)")); |
| 1373 | printf (" %s\n", "-m, --mac=STRING"); | 1373 | printf (" %s\n", "-m, --mac=STRING"); |
| 1374 | printf (" %s\n", _("MAC address to use in the DHCP request")); | 1374 | printf (" %s\n", _("MAC address to use in the DHCP request")); |
| 1375 | printf (" %s\n", "-u, --unicast"); | 1375 | printf (" %s\n", "-u, --unicast"); |
| 1376 | printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s")); | 1376 | printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s")); |
| 1377 | 1377 | ||
| 1378 | printf (UT_SUPPORT); | 1378 | printf (UT_SUPPORT); |
| 1379 | return; | 1379 | return; |
| 1380 | } | 1380 | } |
| 1381 | 1381 | ||
| 1382 | 1382 | ||
| 1383 | void | 1383 | void |
| 1384 | print_usage(void){ | 1384 | print_usage(void){ |
| 1385 | 1385 | ||
| 1386 | printf ("%s\n", _("Usage:")); | 1386 | printf ("%s\n", _("Usage:")); |
| 1387 | printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname); | 1387 | printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname); |
| 1388 | printf (" [-i interface] [-m mac]\n"); | 1388 | printf (" [-i interface] [-m mac]\n"); |
| 1389 | 1389 | ||
| 1390 | return; | 1390 | return; |
| 1391 | } | 1391 | } |
| 1392 | 1392 | ||
| 1393 | 1393 | ||
| 1394 | 1394 | ||
