summaryrefslogtreecommitdiffstats
path: root/plugins-root
diff options
context:
space:
mode:
Diffstat (limited to 'plugins-root')
-rw-r--r--plugins-root/check_dhcp.c671
1 files changed, 331 insertions, 340 deletions
diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c
index 2d22619..0ddace5 100644
--- a/plugins-root/check_dhcp.c
+++ b/plugins-root/check_dhcp.c
@@ -1,40 +1,40 @@
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
36const char *progname = "check_dhcp"; 36const char *progname = "check_dhcp";
37const char *copyright = "2001-2007"; 37const char *copyright = "2001-2023";
38const char *email = "devel@monitoring-plugins.org"; 38const char *email = "devel@monitoring-plugins.org";
39 39
40#include "common.h" 40#include "common.h"
@@ -57,9 +57,10 @@ const char *email = "devel@monitoring-plugins.org";
57#include <netinet/in.h> 57#include <netinet/in.h>
58#include <net/if.h> 58#include <net/if.h>
59#include <arpa/inet.h> 59#include <arpa/inet.h>
60
60#if HAVE_SYS_SOCKIO_H 61#if HAVE_SYS_SOCKIO_H
61#include <sys/sockio.h> 62#include <sys/sockio.h>
62#endif 63#endif // HAVE_SYS_SOCKIO_H
63 64
64#if defined( __linux__ ) 65#if defined( __linux__ )
65 66
@@ -98,10 +99,6 @@ static struct strbuf dat = {AREA_SZ, 0, (char *)dat_area};
98#define GOT_INTR 4 99#define GOT_INTR 4
99#define GOT_ERR 128 100#define GOT_ERR 128
100 101
101#define u_int8_t uint8_t
102#define u_int16_t uint16_t
103#define u_int32_t uint32_t
104
105static int get_msg(int); 102static int get_msg(int);
106static int check_ctrl(int); 103static int check_ctrl(int);
107static int put_ctrl(int, int, int); 104static int put_ctrl(int, int, int);
@@ -110,7 +107,7 @@ static int dl_open(const char *, int, int *);
110static int dl_bind(int, int, u_char *); 107static int dl_bind(int, int, u_char *);
111long mac_addr_dlpi( const char *, int, u_char *); 108long mac_addr_dlpi( const char *, int, u_char *);
112 109
113#endif 110#endif // __sun__ || __solaris__ || __hpux
114 111
115 112
116 113
@@ -119,9 +116,6 @@ long mac_addr_dlpi( const char *, int, u_char *);
119#define OK 0 116#define OK 0
120#define ERROR -1 117#define ERROR -1
121 118
122#define FALSE 0
123#define TRUE 1
124
125 119
126/**** DHCP definitions ****/ 120/**** DHCP definitions ****/
127 121
@@ -132,39 +126,39 @@ long mac_addr_dlpi( const char *, int, u_char *);
132 126
133 127
134typedef struct dhcp_packet_struct{ 128typedef struct dhcp_packet_struct{
135 u_int8_t op; /* packet type */ 129 uint8_t op; /* packet type */
136 u_int8_t htype; /* type of hardware address for this machine (Ethernet, etc) */ 130 uint8_t htype; /* type of hardware address for this machine (Ethernet, etc) */
137 u_int8_t hlen; /* length of hardware address (of this machine) */ 131 uint8_t hlen; /* length of hardware address (of this machine) */
138 u_int8_t hops; /* hops */ 132 uint8_t hops; /* hops */
139 u_int32_t xid; /* random transaction id number - chosen by this machine */ 133 uint32_t xid; /* random transaction id number - chosen by this machine */
140 u_int16_t secs; /* seconds used in timing */ 134 uint16_t secs; /* seconds used in timing */
141 u_int16_t flags; /* flags */ 135 uint16_t flags; /* flags */
142 struct in_addr ciaddr; /* IP address of this machine (if we already have one) */ 136 struct in_addr ciaddr; /* IP address of this machine (if we already have one) */
143 struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */ 137 struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */
144 struct in_addr siaddr; /* IP address of next server */ 138 struct in_addr siaddr; /* IP address of next server */
145 struct in_addr giaddr; /* IP address of DHCP relay */ 139 struct in_addr giaddr; /* IP address of DHCP relay */
146 unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */ 140 unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */
147 char sname [MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */ 141 char sname [MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */
148 char file [MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */ 142 char file [MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */
149 char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */ 143 char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */
150 }dhcp_packet; 144}dhcp_packet;
151 145
152 146
153typedef struct dhcp_offer_struct{ 147typedef struct dhcp_offer_struct{
154 struct in_addr server_address; /* address of DHCP server that sent this offer */ 148 struct in_addr server_address; /* address of DHCP server that sent this offer */
155 struct in_addr offered_address; /* the IP address that was offered to us */ 149 struct in_addr offered_address; /* the IP address that was offered to us */
156 u_int32_t lease_time; /* lease time in seconds */ 150 uint32_t lease_time; /* lease time in seconds */
157 u_int32_t renewal_time; /* renewal time in seconds */ 151 uint32_t renewal_time; /* renewal time in seconds */
158 u_int32_t rebinding_time; /* rebinding time in seconds */ 152 uint32_t rebinding_time; /* rebinding time in seconds */
159 struct dhcp_offer_struct *next; 153 struct dhcp_offer_struct *next;
160 }dhcp_offer; 154}dhcp_offer;
161 155
162 156
163typedef struct requested_server_struct{ 157typedef struct requested_server_struct{
164 struct in_addr server_address; 158 struct in_addr server_address;
165 int answered; 159 bool answered;
166 struct requested_server_struct *next; 160 struct requested_server_struct *next;
167 }requested_server; 161}requested_server;
168 162
169 163
170#define BOOTREQUEST 1 164#define BOOTREQUEST 1
@@ -198,7 +192,7 @@ typedef struct requested_server_struct{
198#define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */ 192#define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */
199#define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */ 193#define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */
200 194
201u_int8_t unicast = 0; /* unicast mode: mimic a DHCP relay */ 195uint8_t unicast = 0; /* unicast mode: mimic a DHCP relay */
202struct in_addr my_ip; /* our address (required for relay) */ 196struct in_addr my_ip; /* our address (required for relay) */
203struct in_addr dhcp_ip; /* server to query (if in unicast mode) */ 197struct in_addr dhcp_ip; /* server to query (if in unicast mode) */
204unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]=""; 198unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]="";
@@ -206,11 +200,11 @@ unsigned char *user_specified_mac=NULL;
206 200
207char network_interface_name[IFNAMSIZ]="eth0"; 201char network_interface_name[IFNAMSIZ]="eth0";
208 202
209u_int32_t packet_xid=0; 203uint32_t packet_xid=0;
210 204
211u_int32_t dhcp_lease_time=0; 205uint32_t dhcp_lease_time=0;
212u_int32_t dhcp_renewal_time=0; 206uint32_t dhcp_renewal_time=0;
213u_int32_t dhcp_rebinding_time=0; 207uint32_t dhcp_rebinding_time=0;
214 208
215int dhcpoffer_timeout=2; 209int dhcpoffer_timeout=2;
216 210
@@ -221,8 +215,8 @@ int valid_responses=0; /* number of valid DHCPOFFERs we received */
221int requested_servers=0; 215int requested_servers=0;
222int requested_responses=0; 216int requested_responses=0;
223 217
224int request_specific_address=FALSE; 218bool request_specific_address=false;
225int received_requested_address=FALSE; 219bool received_requested_address=false;
226int verbose=0; 220int verbose=0;
227struct in_addr requested_address; 221struct in_addr requested_address;
228 222
@@ -268,7 +262,7 @@ int main(int argc, char **argv){
268 262
269 if(process_arguments(argc,argv)!=OK){ 263 if(process_arguments(argc,argv)!=OK){
270 usage4 (_("Could not parse arguments")); 264 usage4 (_("Could not parse arguments"));
271 } 265 }
272 266
273 /* create socket for DHCP communications */ 267 /* create socket for DHCP communications */
274 dhcp_socket=create_dhcp_socket(); 268 dhcp_socket=create_dhcp_socket();
@@ -299,7 +293,7 @@ int main(int argc, char **argv){
299 free_requested_server_list(); 293 free_requested_server_list();
300 294
301 return result; 295 return result;
302 } 296}
303 297
304 298
305 299
@@ -314,83 +308,83 @@ int get_hardware_address(int sock,char *interface_name){
314 308
315 /* try and grab hardware address of requested interface */ 309 /* try and grab hardware address of requested interface */
316 if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){ 310 if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){
317 printf(_("Error: Could not get hardware address of interface '%s'\n"),interface_name); 311 printf(_("Error: Could not get hardware address of interface '%s'\n"),interface_name);
318 exit(STATE_UNKNOWN); 312 exit(STATE_UNKNOWN);
319 } 313 }
320 314
321 memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6); 315 memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6);
322 316
323#elif defined(__bsd__) 317#elif defined(__bsd__)
324 /* King 2004 see ACKNOWLEDGEMENTS */ 318 /* King 2004 see ACKNOWLEDGEMENTS */
325 319
326 size_t len; 320 size_t len;
327 int mib[6]; 321 int mib[6];
328 char *buf; 322 char *buf;
329 unsigned char *ptr; 323 unsigned char *ptr;
330 struct if_msghdr *ifm; 324 struct if_msghdr *ifm;
331 struct sockaddr_dl *sdl; 325 struct sockaddr_dl *sdl;
332 326
333 mib[0] = CTL_NET; 327 mib[0] = CTL_NET;
334 mib[1] = AF_ROUTE; 328 mib[1] = AF_ROUTE;
335 mib[2] = 0; 329 mib[2] = 0;
336 mib[3] = AF_LINK; 330 mib[3] = AF_LINK;
337 mib[4] = NET_RT_IFLIST; 331 mib[4] = NET_RT_IFLIST;
338 332
339 if((mib[5] = if_nametoindex(interface_name)) == 0){ 333 if((mib[5] = if_nametoindex(interface_name)) == 0){
340 printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno)); 334 printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno));
341 exit(STATE_UNKNOWN); 335 exit(STATE_UNKNOWN);
342 } 336 }
343 337
344 if(sysctl(mib, 6, NULL, &len, NULL, 0) < 0){ 338 if(sysctl(mib, 6, NULL, &len, NULL, 0) < 0){
345 printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name, strerror(errno)); 339 printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name, strerror(errno));
346 exit(STATE_UNKNOWN); 340 exit(STATE_UNKNOWN);
347 } 341 }
348 342
349 if((buf = malloc(len)) == NULL){ 343 if((buf = malloc(len)) == NULL){
350 printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name, strerror(errno)); 344 printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name, strerror(errno));
351 exit(4); 345 exit(4);
352 } 346 }
353 347
354 if(sysctl(mib, 6, buf, &len, NULL, 0) < 0){ 348 if(sysctl(mib, 6, buf, &len, NULL, 0) < 0){
355 printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name, strerror(errno)); 349 printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name, strerror(errno));
356 exit(STATE_UNKNOWN); 350 exit(STATE_UNKNOWN);
357 } 351 }
358 352
359 ifm = (struct if_msghdr *)buf; 353 ifm = (struct if_msghdr *)buf;
360 sdl = (struct sockaddr_dl *)(ifm + 1); 354 sdl = (struct sockaddr_dl *)(ifm + 1);
361 ptr = (unsigned char *)LLADDR(sdl); 355 ptr = (unsigned char *)LLADDR(sdl);
362 memcpy(&client_hardware_address[0], ptr, 6) ; 356 memcpy(&client_hardware_address[0], ptr, 6) ;
363 /* King 2004 */ 357 /* King 2004 */
364 358
365#elif defined(__sun__) || defined(__solaris__) 359#elif defined(__sun__) || defined(__solaris__)
366 360
367 /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */ 361 /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */
368 long stat; 362 long stat;
369 char dev[20] = "/dev/"; 363 char dev[20] = "/dev/";
370 char *p; 364 char *p;
371 int unit; 365 int unit;
372 366
373 /* get last number from interfacename, eg lnc0, e1000g0*/ 367 /* get last number from interfacename, eg lnc0, e1000g0*/
374 int i; 368 int i;
375 p = interface_name + strlen(interface_name) -1; 369 p = interface_name + strlen(interface_name) -1;
376 for(i = strlen(interface_name) -1; i > 0; p--) { 370 for(i = strlen(interface_name) -1; i > 0; p--) {
377 if(isalpha(*p)) 371 if(isalpha(*p))
378 break; 372 break;
379 } 373 }
380 p++; 374 p++;
381 if( p != interface_name ){ 375 if( p != interface_name ){
382 unit = atoi(p) ; 376 unit = atoi(p) ;
383 strncat(dev, interface_name, 6) ; 377 strncat(dev, interface_name, 6) ;
384 } 378 }
385 else{ 379 else{
386 printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name); 380 printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name);
387 exit(STATE_UNKNOWN); 381 exit(STATE_UNKNOWN);
388 } 382 }
389 stat = mac_addr_dlpi(dev, unit, client_hardware_address); 383 stat = mac_addr_dlpi(dev, unit, client_hardware_address);
390 if(stat != 0){ 384 if(stat != 0){
391 printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit); 385 printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit);
392 exit(STATE_UNKNOWN); 386 exit(STATE_UNKNOWN);
393 } 387 }
394 388
395#elif defined(__hpux__) 389#elif defined(__hpux__)
396 390
@@ -402,8 +396,8 @@ int get_hardware_address(int sock,char *interface_name){
402 if(stat != 0){ 396 if(stat != 0){
403 printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit); 397 printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit);
404 exit(STATE_UNKNOWN); 398 exit(STATE_UNKNOWN);
405 } 399 }
406 /* Kompf 2000-2003 */ 400 /* Kompf 2000-2003 */
407 401
408#else 402#else
409 printf(_("Error: can't get MAC address for this architecture. Use the --mac option.\n")); 403 printf(_("Error: can't get MAC address for this architecture. Use the --mac option.\n"));
@@ -414,7 +408,7 @@ int get_hardware_address(int sock,char *interface_name){
414 print_hardware_address(client_hardware_address); 408 print_hardware_address(client_hardware_address);
415 409
416 return OK; 410 return OK;
417 } 411}
418 412
419/* determines IP address of the client interface */ 413/* determines IP address of the client interface */
420int get_ip_address(int sock,char *interface_name){ 414int get_ip_address(int sock,char *interface_name){
@@ -426,9 +420,9 @@ int get_ip_address(int sock,char *interface_name){
426 420
427 if(ioctl(sock,SIOCGIFADDR,&ifr)<0){ 421 if(ioctl(sock,SIOCGIFADDR,&ifr)<0){
428 printf(_("Error: Cannot determine IP address of interface %s\n"), 422 printf(_("Error: Cannot determine IP address of interface %s\n"),
429 interface_name); 423 interface_name);
430 exit(STATE_UNKNOWN); 424 exit(STATE_UNKNOWN);
431 } 425 }
432 426
433 my_ip=((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; 427 my_ip=((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
434 428
@@ -441,13 +435,13 @@ int get_ip_address(int sock,char *interface_name){
441 printf(_("Pretending to be relay client %s\n"),inet_ntoa(my_ip)); 435 printf(_("Pretending to be relay client %s\n"),inet_ntoa(my_ip));
442 436
443 return OK; 437 return OK;
444 } 438}
445 439
446/* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */ 440/* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
447int send_dhcp_discover(int sock){ 441int send_dhcp_discover(int sock){
448 dhcp_packet discover_packet; 442 dhcp_packet discover_packet;
449 struct sockaddr_in sockaddr_broadcast; 443 struct sockaddr_in sockaddr_broadcast;
450 unsigned short opts; 444 unsigned short opts;
451 445
452 446
453 /* clear the packet data structure */ 447 /* clear the packet data structure */
@@ -488,19 +482,19 @@ int send_dhcp_discover(int sock){
488 discover_packet.options[2]='\x53'; 482 discover_packet.options[2]='\x53';
489 discover_packet.options[3]='\x63'; 483 discover_packet.options[3]='\x63';
490 484
491 opts = 4; 485 opts = 4;
492 /* DHCP message type is embedded in options field */ 486 /* DHCP message type is embedded in options field */
493 discover_packet.options[opts++]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */ 487 discover_packet.options[opts++]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
494 discover_packet.options[opts++]='\x01'; /* DHCP message option length in bytes */ 488 discover_packet.options[opts++]='\x01'; /* DHCP message option length in bytes */
495 discover_packet.options[opts++]=DHCPDISCOVER; 489 discover_packet.options[opts++]=DHCPDISCOVER;
496 490
497 /* the IP address we're requesting */ 491 /* the IP address we're requesting */
498 if(request_specific_address==TRUE){ 492 if(request_specific_address){
499 discover_packet.options[opts++]=DHCP_OPTION_REQUESTED_ADDRESS; 493 discover_packet.options[opts++]=DHCP_OPTION_REQUESTED_ADDRESS;
500 discover_packet.options[opts++]='\x04'; 494 discover_packet.options[opts++]='\x04';
501 memcpy(&discover_packet.options[opts],&requested_address,sizeof(requested_address)); 495 memcpy(&discover_packet.options[opts],&requested_address,sizeof(requested_address));
502 opts += sizeof(requested_address); 496 opts += sizeof(requested_address);
503 } 497 }
504 discover_packet.options[opts++]=DHCP_OPTION_END; 498 discover_packet.options[opts++]=DHCP_OPTION_END;
505 499
506 /* unicast fields */ 500 /* unicast fields */
@@ -511,8 +505,8 @@ int send_dhcp_discover(int sock){
511 discover_packet.hops = unicast ? 1 : 0; 505 discover_packet.hops = unicast ? 1 : 0;
512 506
513 /* send the DHCPDISCOVER packet to broadcast address */ 507 /* send the DHCPDISCOVER packet to broadcast address */
514 sockaddr_broadcast.sin_family=AF_INET; 508 sockaddr_broadcast.sin_family=AF_INET;
515 sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT); 509 sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT);
516 sockaddr_broadcast.sin_addr.s_addr = unicast ? dhcp_ip.s_addr : INADDR_BROADCAST; 510 sockaddr_broadcast.sin_addr.s_addr = unicast ? dhcp_ip.s_addr : INADDR_BROADCAST;
517 bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero)); 511 bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero));
518 512
@@ -524,7 +518,7 @@ int send_dhcp_discover(int sock){
524 printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr)); 518 printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr));
525 printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr)); 519 printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr));
526 printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr)); 520 printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr));
527 } 521 }
528 522
529 /* send the DHCPDISCOVER packet out */ 523 /* send the DHCPDISCOVER packet out */
530 send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast); 524 send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast);
@@ -533,7 +527,7 @@ int send_dhcp_discover(int sock){
533 printf("\n\n"); 527 printf("\n\n");
534 528
535 return OK; 529 return OK;
536 } 530}
537 531
538 532
539 533
@@ -573,13 +567,13 @@ int get_dhcp_offer(int sock){
573 printf(_("Result=ERROR\n")); 567 printf(_("Result=ERROR\n"));
574 568
575 continue; 569 continue;
576 } 570 }
577 else{ 571 else{
578 if(verbose) 572 if(verbose)
579 printf(_("Result=OK\n")); 573 printf(_("Result=OK\n"));
580 574
581 responses++; 575 responses++;
582 } 576 }
583 577
584 /* The "source" is either a server or a relay. */ 578 /* The "source" is either a server or a relay. */
585 /* Save a copy of "source" into "via" even if it's via itself */ 579 /* Save a copy of "source" into "via" even if it's via itself */
@@ -589,7 +583,7 @@ int get_dhcp_offer(int sock){
589 printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source.sin_addr)); 583 printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source.sin_addr));
590 printf(_(" via %s\n"),inet_ntoa(via.sin_addr)); 584 printf(_(" via %s\n"),inet_ntoa(via.sin_addr));
591 printf("DHCPOFFER XID: %u (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid)); 585 printf("DHCPOFFER XID: %u (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid));
592 } 586 }
593 587
594 /* check packet xid to see if its the same as the one we used in the discover packet */ 588 /* check packet xid to see if its the same as the one we used in the discover packet */
595 if(ntohl(offer_packet.xid)!=packet_xid){ 589 if(ntohl(offer_packet.xid)!=packet_xid){
@@ -597,7 +591,7 @@ int get_dhcp_offer(int sock){
597 printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"),ntohl(offer_packet.xid),packet_xid); 591 printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"),ntohl(offer_packet.xid),packet_xid);
598 592
599 continue; 593 continue;
600 } 594 }
601 595
602 /* check hardware address */ 596 /* check hardware address */
603 result=OK; 597 result=OK;
@@ -610,7 +604,7 @@ int get_dhcp_offer(int sock){
610 604
611 if(offer_packet.chaddr[x]!=client_hardware_address[x]) 605 if(offer_packet.chaddr[x]!=client_hardware_address[x])
612 result=ERROR; 606 result=ERROR;
613 } 607 }
614 if(verbose) 608 if(verbose)
615 printf("\n"); 609 printf("\n");
616 610
@@ -619,27 +613,27 @@ int get_dhcp_offer(int sock){
619 printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n")); 613 printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n"));
620 614
621 continue; 615 continue;
622 } 616 }
623 617
624 if(verbose){ 618 if(verbose){
625 printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr)); 619 printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr));
626 printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr)); 620 printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr));
627 printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr)); 621 printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr));
628 printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr)); 622 printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr));
629 } 623 }
630 624
631 add_dhcp_offer(source.sin_addr,&offer_packet); 625 add_dhcp_offer(source.sin_addr,&offer_packet);
632 626
633 valid_responses++; 627 valid_responses++;
634 } 628 }
635 629
636 if(verbose){ 630 if(verbose){
637 printf(_("Total responses seen on the wire: %d\n"),responses); 631 printf(_("Total responses seen on the wire: %d\n"),responses);
638 printf(_("Valid responses for this machine: %d\n"),valid_responses); 632 printf(_("Valid responses for this machine: %d\n"),valid_responses);
639 } 633 }
640 634
641 return OK; 635 return OK;
642 } 636}
643 637
644 638
645 639
@@ -656,14 +650,14 @@ int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in
656 return ERROR; 650 return ERROR;
657 651
658 return OK; 652 return OK;
659 } 653}
660 654
661 655
662 656
663/* receives a DHCP packet */ 657/* receives a DHCP packet */
664int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){ 658int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){
665 struct timeval tv; 659 struct timeval tv;
666 fd_set readfds; 660 fd_set readfds;
667 fd_set oobfds; 661 fd_set oobfds;
668 int recv_result; 662 int recv_result;
669 socklen_t address_size; 663 socklen_t address_size;
@@ -671,88 +665,88 @@ int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, st
671 int nfound; 665 int nfound;
672 666
673 667
674 /* wait for data to arrive (up time timeout) */ 668 /* wait for data to arrive (up time timeout) */
675 tv.tv_sec=timeout; 669 tv.tv_sec=timeout;
676 tv.tv_usec=0; 670 tv.tv_usec=0;
677 FD_ZERO(&readfds); 671 FD_ZERO(&readfds);
678 FD_ZERO(&oobfds); 672 FD_ZERO(&oobfds);
679 FD_SET(sock,&readfds); 673 FD_SET(sock,&readfds);
680 FD_SET(sock,&oobfds); 674 FD_SET(sock,&oobfds);
681 nfound = select(sock+1,&readfds,NULL,&oobfds,&tv); 675 nfound = select(sock+1,&readfds,NULL,&oobfds,&tv);
682 676
683 /* make sure some data has arrived */ 677 /* make sure some data has arrived */
684 if(!FD_ISSET(sock,&readfds)){ 678 if(!FD_ISSET(sock,&readfds)){
685 if(verbose) 679 if(verbose)
686 printf(_("No (more) data received (nfound: %d)\n"), nfound); 680 printf(_("No (more) data received (nfound: %d)\n"), nfound);
687 return ERROR; 681 return ERROR;
688 } 682 }
689 683
690 else{ 684 else{
691 bzero(&source_address,sizeof(source_address)); 685 bzero(&source_address,sizeof(source_address));
692 address_size=sizeof(source_address); 686 address_size=sizeof(source_address);
693 recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size); 687 recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size);
694 if(verbose) 688 if(verbose)
695 printf("recv_result: %d\n",recv_result); 689 printf("recv_result: %d\n",recv_result);
696 690
697 if(recv_result==-1){ 691 if(recv_result==-1){
698 if(verbose){ 692 if(verbose){
699 printf(_("recvfrom() failed, ")); 693 printf(_("recvfrom() failed, "));
700 printf("errno: (%d) -> %s\n",errno,strerror(errno)); 694 printf("errno: (%d) -> %s\n",errno,strerror(errno));
701 } 695 }
702 return ERROR; 696 return ERROR;
703 } 697 }
704 else{ 698 else{
705 if(verbose){ 699 if(verbose){
706 printf(_("receive_dhcp_packet() result: %d\n"),recv_result); 700 printf(_("receive_dhcp_packet() result: %d\n"),recv_result);
707 printf(_("receive_dhcp_packet() source: %s\n"),inet_ntoa(source_address.sin_addr)); 701 printf(_("receive_dhcp_packet() source: %s\n"),inet_ntoa(source_address.sin_addr));
708 } 702 }
709 703
710 memcpy(address,&source_address,sizeof(source_address)); 704 memcpy(address,&source_address,sizeof(source_address));
711 return OK; 705 return OK;
712 } 706 }
713 } 707 }
714 708
715 return OK; 709 return OK;
716 } 710}
717 711
718 712
719/* creates a socket for DHCP communication */ 713/* creates a socket for DHCP communication */
720int create_dhcp_socket(void){ 714int create_dhcp_socket(void){
721 struct sockaddr_in myname; 715 struct sockaddr_in myname;
722 struct ifreq interface; 716 struct ifreq interface;
723 int sock; 717 int sock;
724 int flag=1; 718 int flag=1;
725 719
726 /* Set up the address we're going to bind to. */ 720 /* Set up the address we're going to bind to. */
727 bzero(&myname,sizeof(myname)); 721 bzero(&myname,sizeof(myname));
728 myname.sin_family=AF_INET; 722 myname.sin_family=AF_INET;
729 /* listen to DHCP server port if we're in unicast mode */ 723 /* listen to DHCP server port if we're in unicast mode */
730 myname.sin_port = htons(unicast ? DHCP_SERVER_PORT : DHCP_CLIENT_PORT); 724 myname.sin_port = htons(unicast ? DHCP_SERVER_PORT : DHCP_CLIENT_PORT);
731 myname.sin_addr.s_addr = unicast ? my_ip.s_addr : INADDR_ANY; 725 myname.sin_addr.s_addr = unicast ? my_ip.s_addr : INADDR_ANY;
732 bzero(&myname.sin_zero,sizeof(myname.sin_zero)); 726 bzero(&myname.sin_zero,sizeof(myname.sin_zero));
733 727
734 /* create a socket for DHCP communications */ 728 /* create a socket for DHCP communications */
735 sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); 729 sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
736 if(sock<0){ 730 if(sock<0){
737 printf(_("Error: Could not create socket!\n")); 731 printf(_("Error: Could not create socket!\n"));
738 exit(STATE_UNKNOWN); 732 exit(STATE_UNKNOWN);
739 } 733 }
740 734
741 if(verbose) 735 if(verbose)
742 printf("DHCP socket: %d\n",sock); 736 printf("DHCP socket: %d\n",sock);
743 737
744 /* set the reuse address flag so we don't get errors when restarting */ 738 /* set the reuse address flag so we don't get errors when restarting */
745 flag=1; 739 flag=1;
746 if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){ 740 if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
747 printf(_("Error: Could not set reuse address option on DHCP socket!\n")); 741 printf(_("Error: Could not set reuse address option on DHCP socket!\n"));
748 exit(STATE_UNKNOWN); 742 exit(STATE_UNKNOWN);
749 } 743 }
750 744
751 /* set the broadcast option - we need this to listen to DHCP broadcast messages */ 745 /* set the broadcast option - we need this to listen to DHCP broadcast messages */
752 if(!unicast && setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){ 746 if(!unicast && setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){
753 printf(_("Error: Could not set broadcast option on DHCP socket!\n")); 747 printf(_("Error: Could not set broadcast option on DHCP socket!\n"));
754 exit(STATE_UNKNOWN); 748 exit(STATE_UNKNOWN);
755 } 749 }
756 750
757 /* bind socket to interface */ 751 /* bind socket to interface */
758#if defined(__linux__) 752#if defined(__linux__)
@@ -761,21 +755,21 @@ int create_dhcp_socket(void){
761 if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){ 755 if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){
762 printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"),network_interface_name); 756 printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"),network_interface_name);
763 exit(STATE_UNKNOWN); 757 exit(STATE_UNKNOWN);
764 } 758 }
765 759
766#else 760#else
767 strncpy(interface.ifr_name,network_interface_name,IFNAMSIZ-1); 761 strncpy(interface.ifr_name,network_interface_name,IFNAMSIZ-1);
768 interface.ifr_name[IFNAMSIZ-1]='\0'; 762 interface.ifr_name[IFNAMSIZ-1]='\0';
769#endif 763#endif
770 764
771 /* bind the socket */ 765 /* bind the socket */
772 if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){ 766 if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
773 printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"),DHCP_CLIENT_PORT); 767 printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"),DHCP_CLIENT_PORT);
774 exit(STATE_UNKNOWN); 768 exit(STATE_UNKNOWN);
775 } 769 }
776 770
777 return sock; 771 return sock;
778 } 772}
779 773
780 774
781/* closes DHCP socket */ 775/* closes DHCP socket */
@@ -784,7 +778,7 @@ int close_dhcp_socket(int sock){
784 close(sock); 778 close(sock);
785 779
786 return OK; 780 return OK;
787 } 781}
788 782
789 783
790/* adds a requested server address to list in memory */ 784/* adds a requested server address to list in memory */
@@ -796,7 +790,7 @@ int add_requested_server(struct in_addr server_address){
796 return ERROR; 790 return ERROR;
797 791
798 new_server->server_address=server_address; 792 new_server->server_address=server_address;
799 new_server->answered=FALSE; 793 new_server->answered=false;
800 794
801 new_server->next=requested_server_list; 795 new_server->next=requested_server_list;
802 requested_server_list=new_server; 796 requested_server_list=new_server;
@@ -807,7 +801,7 @@ int add_requested_server(struct in_addr server_address){
807 printf(_("Requested server address: %s\n"),inet_ntoa(new_server->server_address)); 801 printf(_("Requested server address: %s\n"),inet_ntoa(new_server->server_address));
808 802
809 return OK; 803 return OK;
810 } 804}
811 805
812 806
813 807
@@ -840,29 +834,29 @@ int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
840 834
841 /* get option data */ 835 /* get option data */
842 switch(option_type){ 836 switch(option_type){
843 case DHCP_OPTION_LEASE_TIME: 837 case DHCP_OPTION_LEASE_TIME:
844 memcpy(&dhcp_lease_time, &offer_packet->options[x],sizeof(dhcp_lease_time)); 838 memcpy(&dhcp_lease_time, &offer_packet->options[x],sizeof(dhcp_lease_time));
845 dhcp_lease_time = ntohl(dhcp_lease_time); 839 dhcp_lease_time = ntohl(dhcp_lease_time);
846 break; 840 break;
847 case DHCP_OPTION_RENEWAL_TIME: 841 case DHCP_OPTION_RENEWAL_TIME:
848 memcpy(&dhcp_renewal_time, &offer_packet->options[x],sizeof(dhcp_renewal_time)); 842 memcpy(&dhcp_renewal_time, &offer_packet->options[x],sizeof(dhcp_renewal_time));
849 dhcp_renewal_time = ntohl(dhcp_renewal_time); 843 dhcp_renewal_time = ntohl(dhcp_renewal_time);
850 break; 844 break;
851 case DHCP_OPTION_REBINDING_TIME: 845 case DHCP_OPTION_REBINDING_TIME:
852 memcpy(&dhcp_rebinding_time, &offer_packet->options[x],sizeof(dhcp_rebinding_time)); 846 memcpy(&dhcp_rebinding_time, &offer_packet->options[x],sizeof(dhcp_rebinding_time));
853 dhcp_rebinding_time = ntohl(dhcp_rebinding_time); 847 dhcp_rebinding_time = ntohl(dhcp_rebinding_time);
854 break; 848 break;
855 case DHCP_OPTION_SERVER_IDENTIFIER: 849 case DHCP_OPTION_SERVER_IDENTIFIER:
856 memcpy(&serv_ident.s_addr, &offer_packet->options[x],sizeof(serv_ident.s_addr)); 850 memcpy(&serv_ident.s_addr, &offer_packet->options[x],sizeof(serv_ident.s_addr));
857 break; 851 break;
858 } 852 }
859 853
860 /* skip option data we're ignoring */ 854 /* skip option data we're ignoring */
861 if(option_type==0) /* "pad" option, see RFC 2132 (3.1) */ 855 if(option_type==0) /* "pad" option, see RFC 2132 (3.1) */
862 x+=1; 856 x+=1;
863 else 857 else
864 x+=option_length; 858 x+=option_length;
865 } 859 }
866 860
867 if(verbose){ 861 if(verbose){
868 if(dhcp_lease_time==DHCP_INFINITE_TIME) 862 if(dhcp_lease_time==DHCP_INFINITE_TIME)
@@ -876,7 +870,7 @@ int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
876 if(dhcp_rebinding_time==DHCP_INFINITE_TIME) 870 if(dhcp_rebinding_time==DHCP_INFINITE_TIME)
877 printf(_("Rebinding Time: Infinite\n")); 871 printf(_("Rebinding Time: Infinite\n"));
878 printf(_("Rebinding Time: %lu seconds\n"),(unsigned long)dhcp_rebinding_time); 872 printf(_("Rebinding Time: %lu seconds\n"),(unsigned long)dhcp_rebinding_time);
879 } 873 }
880 874
881 new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer)); 875 new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer));
882 876
@@ -905,14 +899,14 @@ int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
905 if(verbose){ 899 if(verbose){
906 printf(_("Added offer from server @ %s"),inet_ntoa(new_offer->server_address)); 900 printf(_("Added offer from server @ %s"),inet_ntoa(new_offer->server_address));
907 printf(_(" of IP address %s\n"),inet_ntoa(new_offer->offered_address)); 901 printf(_(" of IP address %s\n"),inet_ntoa(new_offer->offered_address));
908 } 902 }
909 903
910 /* add new offer to head of list */ 904 /* add new offer to head of list */
911 new_offer->next=dhcp_offer_list; 905 new_offer->next=dhcp_offer_list;
912 dhcp_offer_list=new_offer; 906 dhcp_offer_list=new_offer;
913 907
914 return OK; 908 return OK;
915 } 909}
916 910
917 911
918/* frees memory allocated to DHCP OFFER list */ 912/* frees memory allocated to DHCP OFFER list */
@@ -923,10 +917,10 @@ int free_dhcp_offer_list(void){
923 for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){ 917 for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){
924 next_offer=this_offer->next; 918 next_offer=this_offer->next;
925 free(this_offer); 919 free(this_offer);
926 } 920 }
927 921
928 return OK; 922 return OK;
929 } 923}
930 924
931 925
932/* frees memory allocated to requested server list */ 926/* frees memory allocated to requested server list */
@@ -937,10 +931,10 @@ int free_requested_server_list(void){
937 for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){ 931 for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){
938 next_server=this_server->next; 932 next_server=this_server->next;
939 free(this_server); 933 free(this_server);
940 } 934 }
941 935
942 return OK; 936 return OK;
943 } 937}
944 938
945 939
946/* gets state and plugin output to return */ 940/* gets state and plugin output to return */
@@ -948,9 +942,9 @@ int get_results(void){
948 dhcp_offer *temp_offer; 942 dhcp_offer *temp_offer;
949 requested_server *temp_server; 943 requested_server *temp_server;
950 int result; 944 int result;
951 u_int32_t max_lease_time=0; 945 uint32_t max_lease_time=0;
952 946
953 received_requested_address=FALSE; 947 received_requested_address=false;
954 948
955 /* checks responses from requested servers */ 949 /* checks responses from requested servers */
956 requested_responses=0; 950 requested_responses=0;
@@ -966,7 +960,7 @@ int get_results(void){
966 960
967 /* see if we got the address we requested */ 961 /* see if we got the address we requested */
968 if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address))) 962 if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
969 received_requested_address=TRUE; 963 received_requested_address=true;
970 964
971 /* see if the servers we wanted a response from talked to us or not */ 965 /* see if the servers we wanted a response from talked to us or not */
972 if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){ 966 if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){
@@ -976,16 +970,16 @@ int get_results(void){
976 if(temp_server->answered) 970 if(temp_server->answered)
977 printf(_(" (duplicate)")); 971 printf(_(" (duplicate)"));
978 printf(_("\n")); 972 printf(_("\n"));
979 } 973 }
980 if(temp_server->answered == FALSE){ 974 if(!temp_server->answered){
981 requested_responses++; 975 requested_responses++;
982 temp_server->answered=TRUE; 976 temp_server->answered=true;
983 } 977 }
984 } 978 }
985 } 979 }
986 } 980 }
987 981
988 } 982 }
989 983
990 /* else check and see if we got our requested address from any server */ 984 /* else check and see if we got our requested address from any server */
991 else{ 985 else{
@@ -998,9 +992,9 @@ int get_results(void){
998 992
999 /* see if we got the address we requested */ 993 /* see if we got the address we requested */
1000 if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address))) 994 if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
1001 received_requested_address=TRUE; 995 received_requested_address=true;
1002 } 996 }
1003 } 997 }
1004 998
1005 result=STATE_OK; 999 result=STATE_OK;
1006 if(valid_responses==0) 1000 if(valid_responses==0)
@@ -1009,7 +1003,7 @@ int get_results(void){
1009 result=STATE_CRITICAL; 1003 result=STATE_CRITICAL;
1010 else if(requested_responses<requested_servers) 1004 else if(requested_responses<requested_servers)
1011 result=STATE_WARNING; 1005 result=STATE_WARNING;
1012 else if(request_specific_address==TRUE && received_requested_address==FALSE) 1006 else if(request_specific_address && !received_requested_address)
1013 result=STATE_WARNING; 1007 result=STATE_WARNING;
1014 1008
1015 if(result==0) /* garrett honeycutt 2005 */ 1009 if(result==0) /* garrett honeycutt 2005 */
@@ -1025,15 +1019,15 @@ int get_results(void){
1025 if(dhcp_offer_list==NULL){ 1019 if(dhcp_offer_list==NULL){
1026 printf(_("No DHCPOFFERs were received.\n")); 1020 printf(_("No DHCPOFFERs were received.\n"));
1027 return result; 1021 return result;
1028 } 1022 }
1029 1023
1030 printf(_("Received %d DHCPOFFER(s)"),valid_responses); 1024 printf(_("Received %d DHCPOFFER(s)"),valid_responses);
1031 1025
1032 if(requested_servers>0) 1026 if(requested_servers>0)
1033 printf(_(", %s%d of %d requested servers responded"),((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers); 1027 printf(_(", %s%d of %d requested servers responded"),((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers);
1034 1028
1035 if(request_specific_address==TRUE) 1029 if(request_specific_address)
1036 printf(_(", requested address (%s) was %soffered"),inet_ntoa(requested_address),(received_requested_address==TRUE)?"":_("not ")); 1030 printf(_(", requested address (%s) was %soffered"),inet_ntoa(requested_address),(received_requested_address)?"":_("not "));
1037 1031
1038 printf(_(", max lease time = ")); 1032 printf(_(", max lease time = "));
1039 if(max_lease_time==DHCP_INFINITE_TIME) 1033 if(max_lease_time==DHCP_INFINITE_TIME)
@@ -1044,7 +1038,7 @@ int get_results(void){
1044 printf(".\n"); 1038 printf(".\n");
1045 1039
1046 return result; 1040 return result;
1047 } 1041}
1048 1042
1049 1043
1050/* process command-line arguments */ 1044/* process command-line arguments */
@@ -1087,71 +1081,71 @@ int call_getopt(int argc, char **argv){
1087 1081
1088 switch(c){ 1082 switch(c){
1089 1083
1090 case 's': /* DHCP server address */ 1084 case 's': /* DHCP server address */
1091 resolve_host(optarg,&dhcp_ip); 1085 resolve_host(optarg,&dhcp_ip);
1092 add_requested_server(dhcp_ip); 1086 add_requested_server(dhcp_ip);
1093 break; 1087 break;
1094 1088
1095 case 'r': /* address we are requested from DHCP servers */ 1089 case 'r': /* address we are requested from DHCP servers */
1096 resolve_host(optarg,&requested_address); 1090 resolve_host(optarg,&requested_address);
1097 request_specific_address=TRUE; 1091 request_specific_address=true;
1098 break; 1092 break;
1099 1093
1100 case 't': /* timeout */ 1094 case 't': /* timeout */
1101
1102 /*
1103 if(is_intnonneg(optarg))
1104 */
1105 if(atoi(optarg)>0)
1106 dhcpoffer_timeout=atoi(optarg);
1107 /*
1108 else
1109 usage("Time interval must be a nonnegative integer\n");
1110 */
1111 break;
1112 1095
1113 case 'm': /* MAC address */ 1096 /*
1097 if(is_intnonneg(optarg))
1098 */
1099 if(atoi(optarg)>0)
1100 dhcpoffer_timeout=atoi(optarg);
1101 /*
1102 else
1103 usage("Time interval must be a nonnegative integer\n");
1104 */
1105 break;
1114 1106
1115 if((user_specified_mac=mac_aton(optarg)) == NULL) 1107 case 'm': /* MAC address */
1116 usage("Cannot parse MAC address.\n");
1117 if(verbose)
1118 print_hardware_address(user_specified_mac);
1119 1108
1120 break; 1109 if((user_specified_mac=mac_aton(optarg)) == NULL)
1110 usage("Cannot parse MAC address.\n");
1111 if(verbose)
1112 print_hardware_address(user_specified_mac);
1121 1113
1122 case 'i': /* interface name */ 1114 break;
1123 1115
1124 strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1); 1116 case 'i': /* interface name */
1125 network_interface_name[sizeof(network_interface_name)-1]='\x0';
1126 1117
1127 break; 1118 strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
1119 network_interface_name[sizeof(network_interface_name)-1]='\x0';
1128 1120
1129 case 'u': /* unicast testing */ 1121 break;
1130 unicast=1;
1131 break;
1132 1122
1133 case 'V': /* version */ 1123 case 'u': /* unicast testing */
1134 print_revision(progname, NP_VERSION); 1124 unicast=1;
1135 exit(STATE_UNKNOWN); 1125 break;
1136 1126
1137 case 'h': /* help */ 1127 case 'V': /* version */
1138 print_help(); 1128 print_revision(progname, NP_VERSION);
1139 exit(STATE_UNKNOWN); 1129 exit(STATE_UNKNOWN);
1140 1130
1141 case 'v': /* verbose */ 1131 case 'h': /* help */
1142 verbose=1; 1132 print_help();
1143 break; 1133 exit(STATE_UNKNOWN);
1144 1134
1145 case '?': /* help */ 1135 case 'v': /* verbose */
1146 usage5 (); 1136 verbose=1;
1147 break; 1137 break;
1148 1138
1149 default: 1139 case '?': /* help */
1150 break; 1140 usage5 ();
1151 } 1141 break;
1152 } 1142
1143 default:
1144 break;
1145 }
1146 }
1153 return optind; 1147 return optind;
1154 } 1148}
1155 1149
1156 1150
1157int validate_arguments(int argc){ 1151int validate_arguments(int argc){
@@ -1178,21 +1172,21 @@ static int get_msg(int fd){
1178 if(res < 0){ 1172 if(res < 0){
1179 if(errno == EINTR){ 1173 if(errno == EINTR){
1180 return(GOT_INTR); 1174 return(GOT_INTR);
1181 } 1175 }
1182 else{ 1176 else{
1183 printf("%s\n", "get_msg FAILED."); 1177 printf("%s\n", "get_msg FAILED.");
1184 return(GOT_ERR); 1178 return(GOT_ERR);
1185 }
1186 } 1179 }
1180 }
1187 if(ctl.len > 0){ 1181 if(ctl.len > 0){
1188 ret |= GOT_CTRL; 1182 ret |= GOT_CTRL;
1189 } 1183 }
1190 if(dat.len > 0){ 1184 if(dat.len > 0){
1191 ret |= GOT_DATA; 1185 ret |= GOT_DATA;
1192 } 1186 }
1193 1187
1194 return(ret); 1188 return(ret);
1195 } 1189}
1196 1190
1197/* verify that dl_primitive in ctl_area = prim */ 1191/* verify that dl_primitive in ctl_area = prim */
1198static int check_ctrl(int prim){ 1192static int check_ctrl(int prim){
@@ -1201,10 +1195,10 @@ static int check_ctrl(int prim){
1201 if(err_ack->dl_primitive != prim){ 1195 if(err_ack->dl_primitive != prim){
1202 printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno)); 1196 printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno));
1203 exit(STATE_UNKNOWN); 1197 exit(STATE_UNKNOWN);
1204 } 1198 }
1205 1199
1206 return 0; 1200 return 0;
1207 } 1201}
1208 1202
1209/* put a control message on a stream */ 1203/* put a control message on a stream */
1210static int put_ctrl(int fd, int len, int pri){ 1204static int put_ctrl(int fd, int len, int pri){
@@ -1213,10 +1207,10 @@ static int put_ctrl(int fd, int len, int pri){
1213 if(putmsg(fd, &ctl, 0, pri) < 0){ 1207 if(putmsg(fd, &ctl, 0, pri) < 0){
1214 printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno)); 1208 printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno));
1215 exit(STATE_UNKNOWN); 1209 exit(STATE_UNKNOWN);
1216 } 1210 }
1217 1211
1218 return 0; 1212 return 0;
1219 } 1213}
1220 1214
1221/* put a control + data message on a stream */ 1215/* put a control + data message on a stream */
1222static int put_both(int fd, int clen, int dlen, int pri){ 1216static int put_both(int fd, int clen, int dlen, int pri){
@@ -1226,10 +1220,10 @@ static int put_both(int fd, int clen, int dlen, int pri){
1226 if(putmsg(fd, &ctl, &dat, pri) < 0){ 1220 if(putmsg(fd, &ctl, &dat, pri) < 0){
1227 printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno)); 1221 printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno));
1228 exit(STATE_UNKNOWN); 1222 exit(STATE_UNKNOWN);
1229 } 1223 }
1230 1224
1231 return 0; 1225 return 0;
1232 } 1226}
1233 1227
1234/* open file descriptor and attach */ 1228/* open file descriptor and attach */
1235static int dl_open(const char *dev, int unit, int *fd){ 1229static int dl_open(const char *dev, int unit, int *fd){
@@ -1238,13 +1232,13 @@ static int dl_open(const char *dev, int unit, int *fd){
1238 if((*fd = open(dev, O_RDWR)) == -1){ 1232 if((*fd = open(dev, O_RDWR)) == -1){
1239 printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev, strerror(errno)); 1233 printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev, strerror(errno));
1240 exit(STATE_UNKNOWN); 1234 exit(STATE_UNKNOWN);
1241 } 1235 }
1242 attach_req->dl_primitive = DL_ATTACH_REQ; 1236 attach_req->dl_primitive = DL_ATTACH_REQ;
1243 attach_req->dl_ppa = unit; 1237 attach_req->dl_ppa = unit;
1244 put_ctrl(*fd, sizeof(dl_attach_req_t), 0); 1238 put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
1245 get_msg(*fd); 1239 get_msg(*fd);
1246 return check_ctrl(DL_OK_ACK); 1240 return check_ctrl(DL_OK_ACK);
1247 } 1241}
1248 1242
1249/* send DL_BIND_REQ */ 1243/* send DL_BIND_REQ */
1250static int dl_bind(int fd, int sap, u_char *addr){ 1244static int dl_bind(int fd, int sap, u_char *addr){
@@ -1262,12 +1256,12 @@ static int dl_bind(int fd, int sap, u_char *addr){
1262 if (GOT_ERR == check_ctrl(DL_BIND_ACK)){ 1256 if (GOT_ERR == check_ctrl(DL_BIND_ACK)){
1263 printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno)); 1257 printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno));
1264 exit(STATE_UNKNOWN); 1258 exit(STATE_UNKNOWN);
1265 } 1259 }
1266 bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr, 1260 bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr,
1267 bind_ack->dl_addr_length); 1261 bind_ack->dl_addr_length);
1268 1262
1269 return 0; 1263 return 0;
1270 } 1264}
1271 1265
1272/*********************************************************************** 1266/***********************************************************************
1273 * interface: 1267 * interface:
@@ -1286,15 +1280,15 @@ long mac_addr_dlpi( const char *dev, int unit, u_char *addr){
1286 u_char mac_addr[25]; 1280 u_char mac_addr[25];
1287 1281
1288 if(GOT_ERR != dl_open(dev, unit, &fd)){ 1282 if(GOT_ERR != dl_open(dev, unit, &fd)){
1289 if(GOT_ERR != dl_bind(fd, INSAP, mac_addr)){ 1283 if(GOT_ERR != dl_bind(fd, INSAP, mac_addr)){
1290 bcopy( mac_addr, addr, 6); 1284 bcopy( mac_addr, addr, 6);
1291 return 0; 1285 return 0;
1292 }
1293 } 1286 }
1294 close(fd); 1287 }
1288 close(fd);
1295 1289
1296 return -1; 1290 return -1;
1297 } 1291}
1298 1292
1299/* Kompf 2000-2003 */ 1293/* Kompf 2000-2003 */
1300#endif 1294#endif
@@ -1311,7 +1305,7 @@ void resolve_host(const char *in,struct in_addr *out){
1311 1305
1312 memcpy(out,&((struct sockaddr_in *)ai->ai_addr)->sin_addr,sizeof(*out)); 1306 memcpy(out,&((struct sockaddr_in *)ai->ai_addr)->sin_addr,sizeof(*out));
1313 freeaddrinfo(ai); 1307 freeaddrinfo(ai);
1314 } 1308}
1315 1309
1316 1310
1317/* parse MAC address string, return 6 bytes (unterminated) or NULL */ 1311/* parse MAC address string, return 6 bytes (unterminated) or NULL */
@@ -1330,10 +1324,10 @@ unsigned char *mac_aton(const char *string){
1330 result[j]=strtol(tmp,(char **)NULL,16); 1324 result[j]=strtol(tmp,(char **)NULL,16);
1331 i++; 1325 i++;
1332 j++; 1326 j++;
1333 } 1327 }
1334 1328
1335 return (j==6) ? result : NULL; 1329 return (j==6) ? result : NULL;
1336 } 1330}
1337 1331
1338 1332
1339void print_hardware_address(const unsigned char *address){ 1333void print_hardware_address(const unsigned char *address){
@@ -1344,7 +1338,7 @@ void print_hardware_address(const unsigned char *address){
1344 printf("%2.2x:", address[i]); 1338 printf("%2.2x:", address[i]);
1345 printf("%2.2x", address[i]); 1339 printf("%2.2x", address[i]);
1346 putchar('\n'); 1340 putchar('\n');
1347 } 1341}
1348 1342
1349 1343
1350/* print usage help */ 1344/* print usage help */
@@ -1357,7 +1351,7 @@ void print_help(void){
1357 1351
1358 printf("%s\n", _("This plugin tests the availability of DHCP servers on a network.")); 1352 printf("%s\n", _("This plugin tests the availability of DHCP servers on a network."));
1359 1353
1360 printf ("\n\n"); 1354 printf ("\n\n");
1361 1355
1362 print_usage(); 1356 print_usage();
1363 1357
@@ -1367,32 +1361,29 @@ void print_help(void){
1367 printf (UT_VERBOSE); 1361 printf (UT_VERBOSE);
1368 1362
1369 printf (" %s\n", "-s, --serverip=IPADDRESS"); 1363 printf (" %s\n", "-s, --serverip=IPADDRESS");
1370 printf (" %s\n", _("IP address of DHCP server that we must hear from")); 1364 printf (" %s\n", _("IP address of DHCP server that we must hear from"));
1371 printf (" %s\n", "-r, --requestedip=IPADDRESS"); 1365 printf (" %s\n", "-r, --requestedip=IPADDRESS");
1372 printf (" %s\n", _("IP address that should be offered by at least one DHCP server")); 1366 printf (" %s\n", _("IP address that should be offered by at least one DHCP server"));
1373 printf (" %s\n", "-t, --timeout=INTEGER"); 1367 printf (" %s\n", "-t, --timeout=INTEGER");
1374 printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs")); 1368 printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs"));
1375 printf (" %s\n", "-i, --interface=STRING"); 1369 printf (" %s\n", "-i, --interface=STRING");
1376 printf (" %s\n", _("Interface to to use for listening (i.e. eth0)")); 1370 printf (" %s\n", _("Interface to to use for listening (i.e. eth0)"));
1377 printf (" %s\n", "-m, --mac=STRING"); 1371 printf (" %s\n", "-m, --mac=STRING");
1378 printf (" %s\n", _("MAC address to use in the DHCP request")); 1372 printf (" %s\n", _("MAC address to use in the DHCP request"));
1379 printf (" %s\n", "-u, --unicast"); 1373 printf (" %s\n", "-u, --unicast");
1380 printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s")); 1374 printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s"));
1381 1375
1382 printf (UT_SUPPORT); 1376 printf (UT_SUPPORT);
1383 return; 1377 return;
1384 } 1378}
1385 1379
1386 1380
1387void 1381void
1388print_usage(void){ 1382print_usage(void){
1389 1383
1390 printf ("%s\n", _("Usage:")); 1384 printf ("%s\n", _("Usage:"));
1391 printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname); 1385 printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname);
1392 printf (" [-i interface] [-m mac]\n"); 1386 printf (" [-i interface] [-m mac]\n");
1393 1387
1394 return; 1388 return;
1395 } 1389}
1396
1397
1398