1 /******************************************************************************
2 *
3 * CHECK_DHCP.C
4 *
5 * Program: DHCP plugin for Nagios
6 * License: GPL
7 * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)
8 *
9 * License Information:
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *****************************************************************************/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <sys/time.h>
33 #include <sys/ioctl.h>
34 #include <fcntl.h>
35 #include <features.h>
36 #include <linux/if_ether.h>
37 #include <getopt.h>
38 #include <net/if.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <netdb.h>
45 const char *progname = "check_dhcp";
47 /*#define DEBUG*/
48 #define HAVE_GETOPT_H
51 /**** Common definitions ****/
53 #define STATE_OK 0
54 #define STATE_WARNING 1
55 #define STATE_CRITICAL 2
56 #define STATE_UNKNOWN -1
58 #define OK 0
59 #define ERROR -1
61 #define FALSE 0
62 #define TRUE 1
65 /**** DHCP definitions ****/
67 #define MAX_DHCP_CHADDR_LENGTH 16
68 #define MAX_DHCP_SNAME_LENGTH 64
69 #define MAX_DHCP_FILE_LENGTH 128
70 #define MAX_DHCP_OPTIONS_LENGTH 312
73 typedef struct dhcp_packet_struct{
74 u_int8_t op; /* packet type */
75 u_int8_t htype; /* type of hardware address for this machine (Ethernet, etc) */
76 u_int8_t hlen; /* length of hardware address (of this machine) */
77 u_int8_t hops; /* hops */
78 u_int32_t xid; /* random transaction id number - chosen by this machine */
79 u_int16_t secs; /* seconds used in timing */
80 u_int16_t flags; /* flags */
81 struct in_addr ciaddr; /* IP address of this machine (if we already have one) */
82 struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */
83 struct in_addr siaddr; /* IP address of DHCP server */
84 struct in_addr giaddr; /* IP address of DHCP relay */
85 unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */
86 char sname [MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */
87 char file [MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */
88 char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */
89 }dhcp_packet;
92 typedef struct dhcp_offer_struct{
93 struct in_addr server_address; /* address of DHCP server that sent this offer */
94 struct in_addr offered_address; /* the IP address that was offered to us */
95 u_int32_t lease_time; /* lease time in seconds */
96 u_int32_t renewal_time; /* renewal time in seconds */
97 u_int32_t rebinding_time; /* rebinding time in seconds */
98 struct dhcp_offer_struct *next;
99 }dhcp_offer;
102 typedef struct requested_server_struct{
103 struct in_addr server_address;
104 struct requested_server_struct *next;
105 }requested_server;
108 #define BOOTREQUEST 1
109 #define BOOTREPLY 2
111 #define DHCPDISCOVER 1
112 #define DHCPOFFER 2
113 #define DHCPREQUEST 3
114 #define DHCPDECLINE 4
115 #define DHCPACK 5
116 #define DHCPNACK 6
117 #define DHCPRELEASE 7
119 #define DHCP_OPTION_MESSAGE_TYPE 53
120 #define DHCP_OPTION_HOST_NAME 12
121 #define DHCP_OPTION_BROADCAST_ADDRESS 28
122 #define DHCP_OPTION_REQUESTED_ADDRESS 50
123 #define DHCP_OPTION_LEASE_TIME 51
124 #define DHCP_OPTION_RENEWAL_TIME 58
125 #define DHCP_OPTION_REBINDING_TIME 59
127 #define DHCP_INFINITE_TIME 0xFFFFFFFF
129 #define DHCP_BROADCAST_FLAG 32768
131 #define DHCP_SERVER_PORT 67
132 #define DHCP_CLIENT_PORT 68
134 #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */
135 #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */
137 unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]="";
139 char network_interface_name[8]="eth0";
141 u_int32_t packet_xid=0;
143 u_int32_t dhcp_lease_time=0;
144 u_int32_t dhcp_renewal_time=0;
145 u_int32_t dhcp_rebinding_time=0;
147 int dhcpoffer_timeout=2;
149 dhcp_offer *dhcp_offer_list=NULL;
150 requested_server *requested_server_list=NULL;
152 int valid_responses=0; /* number of valid DHCPOFFERs we received */
153 int requested_servers=0;
154 int requested_responses=0;
156 int request_specific_address=FALSE;
157 int received_requested_address=FALSE;
158 struct in_addr requested_address;
161 int process_arguments(int, char **);
162 int call_getopt(int, char **);
163 int validate_arguments(void);
164 void print_usage(void);
165 void print_help(void);
167 int get_hardware_address(int,char *);
169 int send_dhcp_discover(int);
170 int get_dhcp_offer(int);
172 int get_results(void);
174 int add_dhcp_offer(struct in_addr,dhcp_packet *);
175 int free_dhcp_offer_list(void);
176 int free_requested_server_list(void);
178 int create_dhcp_socket(void);
179 int close_dhcp_socket(int);
180 int send_dhcp_packet(void *,int,int,struct sockaddr_in *);
181 int receive_dhcp_packet(void *,int,int,int,struct sockaddr_in *);
185 int main(int argc, char **argv){
186 int dhcp_socket;
187 int result;
189 if(process_arguments(argc,argv)!=OK){
190 /*usage("Invalid command arguments supplied\n");*/
191 printf("Invalid command arguments supplied\n");
192 exit(STATE_UNKNOWN);
193 }
196 /* create socket for DHCP communications */
197 dhcp_socket=create_dhcp_socket();
199 /* get hardware address of client machine */
200 get_hardware_address(dhcp_socket,network_interface_name);
202 /* send DHCPDISCOVER packet */
203 send_dhcp_discover(dhcp_socket);
205 /* wait for a DHCPOFFER packet */
206 get_dhcp_offer(dhcp_socket);
208 /* close socket we created */
209 close_dhcp_socket(dhcp_socket);
211 /* determine state/plugin output to return */
212 result=get_results();
214 /* free allocated memory */
215 free_dhcp_offer_list();
216 free_requested_server_list();
218 return result;
219 }
223 /* determines hardware address on client machine */
224 int get_hardware_address(int sock,char *interface_name){
225 struct ifreq ifr;
227 strncpy((char *)&ifr.ifr_name,interface_name,sizeof(ifr.ifr_name));
229 /* try and grab hardware address of requested interface */
230 if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){
231 printf("Error: Could not get hardware address of interface '%s'\n",interface_name);
232 exit(STATE_UNKNOWN);
233 }
235 memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6);
237 #ifdef DEBUG
238 printf("Hardware address: %02x:%02x:%02x:",client_hardware_address[0],client_hardware_address[1],client_hardware_address[2]);
239 printf("%02x:",client_hardware_address[3]);
240 printf("%02x:%02x\n",client_hardware_address[4],client_hardware_address[5]);
241 printf("\n");
242 #endif
244 return OK;
245 }
248 /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
249 int send_dhcp_discover(int sock){
250 dhcp_packet discover_packet;
251 struct sockaddr_in sockaddr_broadcast;
254 /* clear the packet data structure */
255 bzero(&discover_packet,sizeof(discover_packet));
258 /* boot request flag (backward compatible with BOOTP servers) */
259 discover_packet.op=BOOTREQUEST;
261 /* hardware address type */
262 discover_packet.htype=ETHERNET_HARDWARE_ADDRESS;
264 /* length of our hardware address */
265 discover_packet.hlen=ETHERNET_HARDWARE_ADDRESS_LENGTH;
267 discover_packet.hops=0;
269 /* transaction id is supposed to be random */
270 srand(time(NULL));
271 packet_xid=random();
272 discover_packet.xid=htonl(packet_xid);
274 /**** WHAT THE HECK IS UP WITH THIS?!? IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/
275 /* downright bizzarre... */
276 ntohl(discover_packet.xid);
278 /*discover_packet.secs=htons(65535);*/
279 discover_packet.secs=0xFF;
281 /* tell server it should broadcast its response */
282 discover_packet.flags=htons(DHCP_BROADCAST_FLAG);
284 /* our hardware address */
285 memcpy(discover_packet.chaddr,client_hardware_address,ETHERNET_HARDWARE_ADDRESS_LENGTH);
287 /* first four bytes of options field is magic cookie (as per RFC 2132) */
288 discover_packet.options[0]='\x63';
289 discover_packet.options[1]='\x82';
290 discover_packet.options[2]='\x53';
291 discover_packet.options[3]='\x63';
293 /* DHCP message type is embedded in options field */
294 discover_packet.options[4]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
295 discover_packet.options[5]='\x01'; /* DHCP message option length in bytes */
296 discover_packet.options[6]=DHCPDISCOVER;
298 /* the IP address we're requesting */
299 if(request_specific_address==TRUE){
300 discover_packet.options[7]=DHCP_OPTION_REQUESTED_ADDRESS;
301 discover_packet.options[8]='\x04';
302 memcpy(&discover_packet.options[9],&requested_address,sizeof(requested_address));
303 }
305 /* send the DHCPDISCOVER packet to broadcast address */
306 sockaddr_broadcast.sin_family=AF_INET;
307 sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT);
308 sockaddr_broadcast.sin_addr.s_addr=INADDR_BROADCAST;
309 bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero));
312 #ifdef DEBUG
313 printf("DHCPDISCOVER to %s port %d\n",inet_ntoa(sockaddr_broadcast.sin_addr),ntohs(sockaddr_broadcast.sin_port));
314 printf("DHCPDISCOVER XID: %lu (0x%X)\n",ntohl(discover_packet.xid),ntohl(discover_packet.xid));
315 printf("DHCDISCOVER ciaddr: %s\n",inet_ntoa(discover_packet.ciaddr));
316 printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr));
317 printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr));
318 printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr));
319 #endif
321 /* send the DHCPDISCOVER packet out */
322 send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast);
324 #ifdef DEBUG
325 printf("\n\n");
326 #endif
328 return OK;
329 }
334 /* waits for a DHCPOFFER message from one or more DHCP servers */
335 int get_dhcp_offer(int sock){
336 dhcp_packet offer_packet;
337 struct sockaddr_in source;
338 int result=OK;
339 int timeout=1;
340 int responses=0;
341 int x;
342 time_t start_time;
343 time_t current_time;
345 time(&start_time);
347 /* receive as many responses as we can */
348 for(responses=0,valid_responses=0;;){
350 time(¤t_time);
351 if((current_time-start_time)>=dhcpoffer_timeout)
352 break;
354 #ifdef DEBUG
355 printf("\n\n");
356 #endif
358 bzero(&source,sizeof(source));
359 bzero(&offer_packet,sizeof(offer_packet));
361 result=OK;
362 result=receive_dhcp_packet(&offer_packet,sizeof(offer_packet),sock,dhcpoffer_timeout,&source);
364 if(result!=OK){
365 #ifdef DEBUG
366 printf("Result=ERROR\n");
367 #endif
368 continue;
369 }
370 else{
371 #ifdef DEBUG
372 printf("Result=OK\n");
373 #endif
374 responses++;
375 }
377 #ifdef DEBUG
378 printf("DHCPOFFER from IP address %s\n",inet_ntoa(source.sin_addr));
379 printf("DHCPOFFER XID: %lu (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid));
380 #endif
382 /* check packet xid to see if its the same as the one we used in the discover packet */
383 if(ntohl(offer_packet.xid)!=packet_xid){
384 #ifdef DEBUG
385 printf("DHCPOFFER XID (%lu) did not match DHCPDISCOVER XID (%lu) - ignoring packet\n",ntohl(offer_packet.xid),packet_xid);
386 #endif
387 continue;
388 }
390 /* check hardware address */
391 result=OK;
392 #ifdef DEBUG
393 printf("DHCPOFFER chaddr: ");
394 #endif
395 for(x=0;x<ETHERNET_HARDWARE_ADDRESS_LENGTH;x++){
396 #ifdef DEBUG
397 printf("%02X",(unsigned char)offer_packet.chaddr[x]);
398 #endif
399 if(offer_packet.chaddr[x]!=client_hardware_address[x]){
400 result=ERROR;
401 }
402 }
403 #ifdef DEBUG
404 printf("\n");
405 #endif
406 if(result==ERROR){
407 #ifdef DEBUG
408 printf("DHCPOFFER hardware address did not match our own - ignoring packet\n");
409 #endif
410 continue;
411 }
413 #ifdef DEBUG
414 printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr));
415 printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr));
416 printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr));
417 printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr));
418 #endif
420 add_dhcp_offer(source.sin_addr,&offer_packet);
422 valid_responses++;
423 }
425 #ifdef DEBUG
426 printf("Total responses seen on the wire: %d\n",responses);
427 printf("Valid responses for this machine: %d\n",valid_responses);
428 #endif
430 return OK;
431 }
435 /* sends a DHCP packet */
436 int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in *dest){
437 struct sockaddr_in myname;
438 int result;
440 result=sendto(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)dest,sizeof(*dest));
442 #ifdef DEBUG
443 printf("send_dhcp_packet result: %d\n",result);
444 #endif
446 if(result<0)
447 return ERROR;
449 return OK;
450 }
454 /* receives a DHCP packet */
455 int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){
456 struct timeval tv;
457 fd_set readfds;
458 int recv_result;
459 socklen_t address_size;
460 struct sockaddr_in source_address;
463 /* wait for data to arrive (up time timeout) */
464 tv.tv_sec=timeout;
465 tv.tv_usec=0;
466 FD_ZERO(&readfds);
467 FD_SET(sock,&readfds);
468 select(sock+1,&readfds,NULL,NULL,&tv);
470 /* make sure some data has arrived */
471 if(!FD_ISSET(sock,&readfds)){
472 #ifdef DEBUG
473 printf("No (more) data received\n");
474 #endif
475 return ERROR;
476 }
478 else{
480 /* why do we need to peek first? i don't know, its a hack. without it, the source address of the first packet received was
481 not being interpreted correctly. sigh... */
482 bzero(&source_address,sizeof(source_address));
483 address_size=sizeof(source_address);
484 recv_result=recvfrom(sock,(char *)buffer,buffer_size,MSG_PEEK,(struct sockaddr *)&source_address,&address_size);
485 #ifdef DEBUG
486 printf("recv_result_1: %d\n",recv_result);
487 #endif
488 recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size);
489 #ifdef DEBUG
490 printf("recv_result_2: %d\n",recv_result);
491 #endif
493 if(recv_result==-1){
494 #ifdef DEBUG
495 printf("recvfrom() failed, ");
496 printf("errno: (%d) -> %s\n",errno,strerror(errno));
497 #endif
498 return ERROR;
499 }
500 else{
501 #ifdef DEBUG
502 printf("receive_dhcp_packet() result: %d\n",recv_result);
503 printf("receive_dhcp_packet() source: %s\n",inet_ntoa(source_address.sin_addr));
504 #endif
506 memcpy(address,&source_address,sizeof(source_address));
507 return OK;
508 }
509 }
511 return OK;
512 }
516 /* creates a socket for DHCP communication */
517 int create_dhcp_socket(void){
518 struct sockaddr_in myname;
519 struct ifreq interface;
520 int sock;
521 int flag=1;
523 /* Set up the address we're going to bind to. */
524 bzero(&myname,sizeof(myname));
525 myname.sin_family=AF_INET;
526 myname.sin_port=htons(DHCP_CLIENT_PORT);
527 myname.sin_addr.s_addr=INADDR_ANY; /* listen on any address */
528 bzero(&myname.sin_zero,sizeof(myname.sin_zero));
530 /* create a socket for DHCP communications */
531 sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
532 if(sock<0){
533 printf("Error: Could not create socket!\n");
534 exit(STATE_UNKNOWN);
535 }
537 #ifdef DEBUG
538 printf("DHCP socket: %d\n",sock);
539 #endif
541 /* set the reuse address flag so we don't get errors when restarting */
542 flag=1;
543 if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
544 printf("Error: Could not set reuse address option on DHCP socket!\n");
545 exit(STATE_UNKNOWN);
546 }
548 /* set the broadcast option - we need this to listen to DHCP broadcast messages */
549 if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){
550 printf("Error: Could not set broadcast option on DHCP socket!\n");
551 exit(STATE_UNKNOWN);
552 }
554 /* bind socket to interface */
555 strncpy(interface.ifr_ifrn.ifrn_name,network_interface_name,IFNAMSIZ);
556 if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){
557 printf("Error: Could not bind socket to interface %s. Check your privileges...\n",network_interface_name);
558 exit(STATE_UNKNOWN);
559 }
561 /* bind the socket */
562 if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
563 printf("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n",DHCP_CLIENT_PORT);
564 exit(STATE_UNKNOWN);
565 }
567 return sock;
568 }
574 /* closes DHCP socket */
575 int close_dhcp_socket(int sock){
577 close(sock);
579 return OK;
580 }
585 /* adds a requested server address to list in memory */
586 int add_requested_server(struct in_addr server_address){
587 requested_server *new_server;
589 new_server=(requested_server *)malloc(sizeof(requested_server));
590 if(new_server==NULL)
591 return ERROR;
593 new_server->server_address=server_address;
595 new_server->next=requested_server_list;
596 requested_server_list=new_server;
598 requested_servers++;
600 #ifdef DEBUG
601 printf("Requested server address: %s\n",inet_ntoa(new_server->server_address));
602 #endif
604 return OK;
605 }
610 /* adds a DHCP OFFER to list in memory */
611 int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
612 dhcp_offer *new_offer;
613 int x;
614 int y;
615 unsigned option_type;
616 unsigned option_length;
618 if(offer_packet==NULL)
619 return ERROR;
621 /* process all DHCP options present in the packet */
622 for(x=4;x<MAX_DHCP_OPTIONS_LENGTH;){
624 /* end of options (0 is really just a pad, but bail out anyway) */
625 if((int)offer_packet->options[x]==-1 || (int)offer_packet->options[x]==0)
626 break;
628 /* get option type */
629 option_type=offer_packet->options[x++];
631 /* get option length */
632 option_length=offer_packet->options[x++];
634 #ifdef DEBUG
635 printf("Option: %d (0x%02X)\n",option_type,option_length);
636 #endif
638 /* get option data */
639 if(option_type==DHCP_OPTION_LEASE_TIME)
640 dhcp_lease_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
641 if(option_type==DHCP_OPTION_RENEWAL_TIME)
642 dhcp_renewal_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
643 if(option_type==DHCP_OPTION_REBINDING_TIME)
644 dhcp_rebinding_time=ntohl(*((u_int32_t *)&offer_packet->options[x]));
646 /* skip option data we're ignoring */
647 else
648 for(y=0;y<option_length;y++,x++);
649 }
651 #ifdef DEBUG
652 if(dhcp_lease_time==DHCP_INFINITE_TIME)
653 printf("Lease Time: Infinite\n");
654 else
655 printf("Lease Time: %lu seconds\n",(unsigned long)dhcp_lease_time);
656 if(dhcp_renewal_time==DHCP_INFINITE_TIME)
657 printf("Renewal Time: Infinite\n");
658 else
659 printf("Renewal Time: %lu seconds\n",(unsigned long)dhcp_renewal_time);
660 if(dhcp_rebinding_time==DHCP_INFINITE_TIME)
661 printf("Rebinding Time: Infinite\n");
662 printf("Rebinding Time: %lu seconds\n",(unsigned long)dhcp_rebinding_time);
663 #endif
665 new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer));
667 if(new_offer==NULL)
668 return ERROR;
671 new_offer->server_address=source;
672 new_offer->offered_address=offer_packet->yiaddr;
673 new_offer->lease_time=dhcp_lease_time;
674 new_offer->renewal_time=dhcp_renewal_time;
675 new_offer->rebinding_time=dhcp_rebinding_time;
678 #ifdef DEBUG
679 printf("Added offer from server @ %s",inet_ntoa(new_offer->server_address));
680 printf(" of IP address %s\n",inet_ntoa(new_offer->offered_address));
681 #endif
683 /* add new offer to head of list */
684 new_offer->next=dhcp_offer_list;
685 dhcp_offer_list=new_offer;
687 return OK;
688 }
693 /* frees memory allocated to DHCP OFFER list */
694 int free_dhcp_offer_list(void){
695 dhcp_offer *this_offer;
696 dhcp_offer *next_offer;
698 for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){
699 next_offer=this_offer->next;
700 free(this_offer);
701 }
703 return OK;
704 }
709 /* frees memory allocated to requested server list */
710 int free_requested_server_list(void){
711 requested_server *this_server;
712 requested_server *next_server;
714 for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){
715 next_server=this_server->next;
716 free(this_server);
717 }
719 return OK;
720 }
723 /* gets state and plugin output to return */
724 int get_results(void){
725 dhcp_offer *temp_offer;
726 requested_server *temp_server;
727 int result;
728 u_int32_t max_lease_time=0;
730 received_requested_address=FALSE;
732 /* checks responses from requested servers */
733 requested_responses=0;
734 if(requested_servers>0){
736 for(temp_server=requested_server_list;temp_server!=NULL;temp_server=temp_server->next){
738 for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
740 /* get max lease time we were offered */
741 if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
742 max_lease_time=temp_offer->lease_time;
744 /* see if we got the address we requested */
745 if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
746 received_requested_address=TRUE;
748 /* see if the servers we wanted a response from talked to us or not */
749 if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){
750 #ifdef DEBUG
751 printf("DHCP Server Match: Offerer=%s",inet_ntoa(temp_offer->server_address));
752 printf(" Requested=%s\n",inet_ntoa(temp_server->server_address));
753 #endif
754 requested_responses++;
755 }
756 }
757 }
759 }
761 /* else check and see if we got our requested address from any server */
762 else{
764 for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
766 /* get max lease time we were offered */
767 if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
768 max_lease_time=temp_offer->lease_time;
770 /* see if we got the address we requested */
771 if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
772 received_requested_address=TRUE;
773 }
774 }
776 result=STATE_OK;
777 if(valid_responses==0)
778 result=STATE_CRITICAL;
779 else if(requested_servers>0 && requested_responses==0)
780 result=STATE_CRITICAL;
781 else if(requested_responses<requested_servers)
782 result=STATE_WARNING;
783 else if(request_specific_address==TRUE && received_requested_address==FALSE)
784 result=STATE_WARNING;
787 printf("DHCP %s: ",(result==STATE_OK)?"ok":"problem");
789 /* we didn't receive any DHCPOFFERs */
790 if(dhcp_offer_list==NULL){
791 printf("No DHCPOFFERs were received.\n");
792 return result;
793 }
795 printf("Received %d DHCPOFFER(s)",valid_responses);
797 if(requested_servers>0)
798 printf(", %s%d of %d requested servers responded",((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers);
800 if(request_specific_address==TRUE)
801 printf(", requested address (%s) was %soffered",inet_ntoa(requested_address),(received_requested_address==TRUE)?"":"not ");
803 printf(", max lease time = ");
804 if(max_lease_time==DHCP_INFINITE_TIME)
805 printf("Infinity");
806 else
807 printf("%lu sec",(unsigned long)max_lease_time);
809 printf(".\n");
811 return result;
812 }
819 /* print usage help */
820 void print_help(void){
822 /*print_revision(progname,"$Revision$");*/
824 printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n\n");
825 printf("This plugin tests the availability of DHCP servers on a network.\n\n");
827 print_usage();
829 printf
830 ("\nOptions:\n"
831 " -s, --serverip=IPADDRESS\n"
832 " IP address of DHCP server that we must hear from\n"
833 " -r, --requestedip=IPADDRESS\n"
834 " IP address that should be offered by at least one DHCP server\n"
835 " -t, --timeout=INTEGER\n"
836 " Seconds to wait for DHCPOFFER before timeout occurs\n"
837 " -i, --interface=STRING\n"
838 " Interface to to use for listening (i.e. eth0)\n"
839 " -v, --verbose\n"
840 " Print extra information (command-line use only)\n"
841 " -h, --help\n"
842 " Print detailed help screen\n"
843 " -V, --version\n"
844 " Print version information\n\n"
845 );
847 /*support();*/
849 return;
850 }
853 /* prints usage information */
854 void print_usage(void){
856 printf("Usage: %s [-s serverip] [-r requestedip] [-t timeout] [-i interface]\n",progname);
857 printf(" %s --help\n",progname);
858 printf(" %s --version\n",progname);
860 return;
861 }
866 /* process command-line arguments */
867 int process_arguments(int argc, char **argv){
868 int c;
870 if(argc<1)
871 return ERROR;
873 c=0;
874 while((c+=(call_getopt(argc-c,&argv[c])))<argc){
876 /*
877 if(is_option(argv[c]))
878 continue;
879 */
880 }
882 return validate_arguments();
883 }
887 int call_getopt(int argc, char **argv){
888 int c=0;
889 int i=0;
890 struct in_addr ipaddress;
892 #ifdef HAVE_GETOPT_H
893 int option_index = 0;
894 static struct option long_options[] =
895 {
896 {"serverip", required_argument,0,'s'},
897 {"requestedip", required_argument,0,'r'},
898 {"timeout", required_argument,0,'t'},
899 {"interface", required_argument,0,'i'},
900 {"verbose", no_argument, 0,'v'},
901 {"version", no_argument, 0,'V'},
902 {"help", no_argument, 0,'h'},
903 {0,0,0,0}
904 };
905 #endif
907 while(1){
908 #ifdef HAVE_GETOPT_H
909 c=getopt_long(argc,argv,"+hVvt:s:r:t:i:",long_options,&option_index);
910 #else
911 c=getopt(argc,argv,"+?hVvt:s:r:t:i:");
912 #endif
914 i++;
916 if(c==-1||c==EOF||c==1)
917 break;
919 switch(c){
920 case 'w':
921 case 'r':
922 case 't':
923 case 'i':
924 i++;
925 break;
926 default:
927 break;
928 }
930 switch(c){
932 case 's': /* DHCP server address */
933 if(inet_aton(optarg,&ipaddress))
934 add_requested_server(ipaddress);
935 /*
936 else
937 usage("Invalid server IP address\n");
938 */
939 break;
941 case 'r': /* address we are requested from DHCP servers */
942 if(inet_aton(optarg,&ipaddress)){
943 requested_address=ipaddress;
944 request_specific_address=TRUE;
945 }
946 /*
947 else
948 usage("Invalid requested IP address\n");
949 */
950 break;
952 case 't': /* timeout */
954 /*
955 if(is_intnonneg(optarg))
956 */
957 if(atoi(optarg)>0)
958 dhcpoffer_timeout=atoi(optarg);
959 /*
960 else
961 usage("Time interval must be a nonnegative integer\n");
962 */
963 break;
965 case 'i': /* interface name */
967 strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
968 network_interface_name[sizeof(network_interface_name)-1]='\x0';
970 break;
972 case 'V': /* version */
974 /*print_revision(progname,"$Revision$");*/
975 exit(STATE_OK);
977 case 'h': /* help */
979 print_help();
980 exit(STATE_OK);
982 case '?': /* help */
984 /*usage("Invalid argument\n");*/
985 break;
987 default:
988 break;
989 }
990 }
992 return i;
993 }
997 int validate_arguments(void){
999 return OK;
1000 }