Back to index

nagios-plugins  1.4.16
check_dhcp.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_dhcp plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)
00007 * Copyright (c) 2001-2007 Nagios Plugin Development Team
00008 * 
00009 * Description:
00010 * 
00011 * This file contains the check_dhcp plugin
00012 * 
00013 * This plugin tests the availability of DHCP servers on a network.
00014 * 
00015 * Unicast mode was originally implemented by Heiti of Boras Kommun with
00016 * general improvements as well as usability fixes and "forward"-porting by
00017 * Andreas Ericsson of OP5 AB.
00018 * 
00019 * 
00020 * This program is free software: you can redistribute it and/or modify
00021 * it under the terms of the GNU General Public License as published by
00022 * the Free Software Foundation, either version 3 of the License, or
00023 * (at your option) any later version.
00024 * 
00025 * This program is distributed in the hope that it will be useful,
00026 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00028 * GNU General Public License for more details.
00029 * 
00030 * You should have received a copy of the GNU General Public License
00031 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00032 * 
00033 * 
00034 *****************************************************************************/
00035 
00036 const char *progname = "check_dhcp";
00037 const char *copyright = "2001-2007";
00038 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00039 
00040 #include "common.h"
00041 #include "netutils.h"
00042 #include "utils.h"
00043 
00044 #include <ctype.h>
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <errno.h>
00049 #include <unistd.h>
00050 #include <sys/time.h>
00051 #include <sys/ioctl.h>
00052 #include <fcntl.h>
00053 #include <getopt.h>
00054 #include <sys/socket.h>
00055 #include <sys/types.h>
00056 #include <netdb.h>
00057 #include <netinet/in.h>
00058 #include <net/if.h>
00059 #include <arpa/inet.h>
00060 #if HAVE_SYS_SOCKIO_H
00061 #include <sys/sockio.h>
00062 #endif
00063 
00064 #if defined( __linux__ )
00065 
00066 #include <linux/if_ether.h>
00067 #include <features.h>
00068 
00069 #elif defined (__bsd__)
00070 
00071 #include <netinet/if_ether.h>
00072 #include <sys/param.h>
00073 #include <sys/sysctl.h>
00074 #include <net/if_dl.h>
00075 
00076 #elif defined(__sun__) || defined(__solaris__) || defined(__hpux__)
00077 
00078 #define INSAP 22
00079 #define OUTSAP 24
00080 
00081 #include <signal.h>
00082 #include <ctype.h>
00083 #include <sys/stropts.h>
00084 #include <sys/poll.h>
00085 #include <sys/dlpi.h>
00086 
00087 #define bcopy(source, destination, length) memcpy(destination, source, length)
00088 
00089 #define AREA_SZ 5000        /* buffer length in bytes */
00090 static u_long ctl_area[AREA_SZ];
00091 static u_long dat_area[AREA_SZ];
00092 static struct strbuf ctl = {AREA_SZ, 0, (char *)ctl_area};
00093 static struct strbuf dat = {AREA_SZ, 0, (char *)dat_area};
00094 
00095 #define GOT_CTRL 1
00096 #define GOT_DATA 2
00097 #define GOT_BOTH 3
00098 #define GOT_INTR 4
00099 #define GOT_ERR 128
00100 
00101 #define u_int8_t      uint8_t
00102 #define u_int16_t    uint16_t
00103 #define u_int32_t    uint32_t
00104 
00105 static int get_msg(int);
00106 static int check_ctrl(int);
00107 static int put_ctrl(int, int, int);
00108 static int put_both(int, int, int, int);
00109 static int dl_open(const char *, int, int *);
00110 static int dl_bind(int, int, u_char *);
00111 long mac_addr_dlpi( const char *, int, u_char *);
00112 
00113 #endif
00114 
00115 
00116 
00117 /**** Common definitions ****/
00118 
00119 #define OK                0
00120 #define ERROR             -1
00121 
00122 #define FALSE             0
00123 #define TRUE              1
00124 
00125 
00126 /**** DHCP definitions ****/
00127 
00128 #define MAX_DHCP_CHADDR_LENGTH           16
00129 #define MAX_DHCP_SNAME_LENGTH            64
00130 #define MAX_DHCP_FILE_LENGTH             128
00131 #define MAX_DHCP_OPTIONS_LENGTH          312
00132 
00133 
00134 typedef struct dhcp_packet_struct{
00135         u_int8_t  op;                   /* packet type */
00136         u_int8_t  htype;                /* type of hardware address for this machine (Ethernet, etc) */
00137         u_int8_t  hlen;                 /* length of hardware address (of this machine) */
00138         u_int8_t  hops;                 /* hops */
00139         u_int32_t xid;                  /* random transaction id number - chosen by this machine */
00140         u_int16_t secs;                 /* seconds used in timing */
00141         u_int16_t flags;                /* flags */
00142         struct in_addr ciaddr;          /* IP address of this machine (if we already have one) */
00143         struct in_addr yiaddr;          /* IP address of this machine (offered by the DHCP server) */
00144         struct in_addr siaddr;          /* IP address of next server */
00145         struct in_addr giaddr;          /* IP address of DHCP relay */
00146         unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH];      /* hardware address of this machine */
00147         char sname [MAX_DHCP_SNAME_LENGTH];    /* name of DHCP server */
00148         char file [MAX_DHCP_FILE_LENGTH];      /* boot file name (used for diskless booting?) */
00149        char options[MAX_DHCP_OPTIONS_LENGTH];  /* options */
00150         }dhcp_packet;
00151 
00152 
00153 typedef struct dhcp_offer_struct{
00154        struct in_addr server_address;   /* address of DHCP server that sent this offer */
00155        struct in_addr offered_address;  /* the IP address that was offered to us */
00156        u_int32_t lease_time;            /* lease time in seconds */
00157        u_int32_t renewal_time;          /* renewal time in seconds */
00158        u_int32_t rebinding_time;        /* rebinding time in seconds */
00159        struct dhcp_offer_struct *next;
00160         }dhcp_offer;
00161 
00162 
00163 typedef struct requested_server_struct{
00164        struct in_addr server_address;
00165        int answered;
00166        struct requested_server_struct *next;
00167         }requested_server;
00168 
00169 
00170 #define BOOTREQUEST     1
00171 #define BOOTREPLY       2
00172 
00173 #define DHCPDISCOVER    1
00174 #define DHCPOFFER       2
00175 #define DHCPREQUEST     3
00176 #define DHCPDECLINE     4
00177 #define DHCPACK         5
00178 #define DHCPNACK        6
00179 #define DHCPRELEASE     7
00180 
00181 #define DHCP_OPTION_MESSAGE_TYPE        53
00182 #define DHCP_OPTION_HOST_NAME           12
00183 #define DHCP_OPTION_BROADCAST_ADDRESS   28
00184 #define DHCP_OPTION_REQUESTED_ADDRESS   50
00185 #define DHCP_OPTION_LEASE_TIME          51
00186 #define DHCP_OPTION_SERVER_IDENTIFIER   54
00187 #define DHCP_OPTION_RENEWAL_TIME        58
00188 #define DHCP_OPTION_REBINDING_TIME      59
00189 #define DHCP_OPTION_END                 255
00190 
00191 #define DHCP_INFINITE_TIME              0xFFFFFFFF
00192 
00193 #define DHCP_BROADCAST_FLAG 32768
00194 
00195 #define DHCP_SERVER_PORT   67
00196 #define DHCP_CLIENT_PORT   68
00197 
00198 #define ETHERNET_HARDWARE_ADDRESS            1     /* used in htype field of dhcp packet */
00199 #define ETHERNET_HARDWARE_ADDRESS_LENGTH     6     /* length of Ethernet hardware addresses */
00200 
00201 u_int8_t unicast = 0;        /* unicast mode: mimic a DHCP relay */
00202 struct in_addr my_ip;        /* our address (required for relay) */
00203 struct in_addr dhcp_ip;      /* server to query (if in unicast mode) */
00204 unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]="";
00205 unsigned char *user_specified_mac=NULL;
00206 
00207 char network_interface_name[IFNAMSIZ]="eth0";
00208 
00209 u_int32_t packet_xid=0;
00210 
00211 u_int32_t dhcp_lease_time=0;
00212 u_int32_t dhcp_renewal_time=0;
00213 u_int32_t dhcp_rebinding_time=0;
00214 
00215 int dhcpoffer_timeout=2;
00216 
00217 dhcp_offer *dhcp_offer_list=NULL;
00218 requested_server *requested_server_list=NULL;
00219 
00220 int valid_responses=0;     /* number of valid DHCPOFFERs we received */
00221 int requested_servers=0;
00222 int requested_responses=0;
00223 
00224 int request_specific_address=FALSE;
00225 int received_requested_address=FALSE;
00226 int verbose=0;
00227 struct in_addr requested_address;
00228 
00229 
00230 int process_arguments(int, char **);
00231 int call_getopt(int, char **);
00232 int validate_arguments(void);
00233 void print_usage(void);
00234 void print_help(void);
00235 
00236 void resolve_host(const char *in,struct in_addr *out);
00237 unsigned char *mac_aton(const char *);
00238 void print_hardware_address(const unsigned char *);
00239 int get_hardware_address(int,char *);
00240 int get_ip_address(int,char *);
00241 
00242 int send_dhcp_discover(int);
00243 int get_dhcp_offer(int);
00244 
00245 int get_results(void);
00246 
00247 int add_dhcp_offer(struct in_addr,dhcp_packet *);
00248 int free_dhcp_offer_list(void);
00249 int free_requested_server_list(void);
00250 
00251 int create_dhcp_socket(void);
00252 int close_dhcp_socket(int);
00253 int send_dhcp_packet(void *,int,int,struct sockaddr_in *);
00254 int receive_dhcp_packet(void *,int,int,int,struct sockaddr_in *);
00255 
00256 
00257 
00258 int main(int argc, char **argv){
00259        int dhcp_socket;
00260        int result = STATE_UNKNOWN;
00261 
00262        setlocale (LC_ALL, "");
00263        bindtextdomain (PACKAGE, LOCALEDIR);
00264        textdomain (PACKAGE);
00265 
00266        /* Parse extra opts if any */
00267        argv=np_extra_opts(&argc, argv, progname);
00268 
00269        if(process_arguments(argc,argv)!=OK){
00270               usage4 (_("Could not parse arguments"));
00271               }
00272 
00273        /* this plugin almost certainly needs root permissions. */
00274        np_warn_if_not_root();
00275 
00276        /* create socket for DHCP communications */
00277        dhcp_socket=create_dhcp_socket();
00278 
00279        /* get hardware address of client machine */
00280        if(user_specified_mac!=NULL)
00281               memcpy(client_hardware_address,user_specified_mac,6);
00282        else
00283               get_hardware_address(dhcp_socket,network_interface_name);
00284 
00285        if(unicast) /* get IP address of client machine */
00286               get_ip_address(dhcp_socket,network_interface_name);
00287 
00288        /* send DHCPDISCOVER packet */
00289        send_dhcp_discover(dhcp_socket);
00290 
00291        /* wait for a DHCPOFFER packet */
00292        get_dhcp_offer(dhcp_socket);
00293 
00294        /* close socket we created */
00295        close_dhcp_socket(dhcp_socket);
00296 
00297        /* determine state/plugin output to return */
00298        result=get_results();
00299 
00300        /* free allocated memory */
00301        free_dhcp_offer_list();
00302        free_requested_server_list();
00303 
00304        return result;
00305         }
00306 
00307 
00308 
00309 /* determines hardware address on client machine */
00310 int get_hardware_address(int sock,char *interface_name){
00311 
00312 #if defined(__linux__)
00313        struct ifreq ifr;
00314 
00315        strncpy((char *)&ifr.ifr_name,interface_name,sizeof(ifr.ifr_name)-1);
00316        ifr.ifr_name[sizeof(ifr.ifr_name)-1]='\0';
00317 
00318        /* try and grab hardware address of requested interface */
00319        if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){
00320                 printf(_("Error: Could not get hardware address of interface '%s'\n"),interface_name);
00321               exit(STATE_UNKNOWN);
00322                }
00323 
00324        memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6);
00325 
00326 #elif defined(__bsd__)
00327                                           /* King 2004  see ACKNOWLEDGEMENTS */
00328 
00329         int                     mib[6], len;
00330         char                    *buf;
00331         unsigned char           *ptr;
00332         struct if_msghdr        *ifm;
00333         struct sockaddr_dl      *sdl;
00334 
00335         mib[0] = CTL_NET;
00336         mib[1] = AF_ROUTE;
00337         mib[2] = 0;
00338         mib[3] = AF_LINK;
00339         mib[4] = NET_RT_IFLIST;
00340 
00341         if((mib[5] = if_nametoindex(interface_name)) == 0){
00342                 printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno));
00343                 exit(STATE_UNKNOWN);
00344               }
00345 
00346         if(sysctl(mib, 6, NULL, &len, NULL, 0) < 0){
00347                 printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name, strerror(errno));
00348                 exit(STATE_UNKNOWN);
00349               }
00350 
00351         if((buf = malloc(len)) == NULL){
00352                 printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name, strerror(errno));
00353                 exit(4);
00354               }
00355 
00356         if(sysctl(mib, 6, buf, &len, NULL, 0) < 0){
00357                 printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name, strerror(errno));
00358                 exit(STATE_UNKNOWN);
00359               }
00360 
00361         ifm = (struct if_msghdr *)buf;
00362         sdl = (struct sockaddr_dl *)(ifm + 1);
00363         ptr = (unsigned char *)LLADDR(sdl);
00364         memcpy(&client_hardware_address[0], ptr, 6) ;
00365                                           /* King 2004 */
00366 
00367 #elif defined(__sun__) || defined(__solaris__)
00368 
00369                                           /* Kompf 2000-2003   see ACKNOWLEDGEMENTS */
00370        long stat;
00371        char dev[20] = "/dev/";
00372        char *p;
00373        int unit;
00374 
00375        for(p = interface_name; *p && isalpha(*p); p++)
00376               /* no-op */ ;
00377        if( p != '\0' ){
00378               unit = atoi(p) ;
00379               *p = '\0' ;
00380               strncat(dev, interface_name, 6) ;
00381               }
00382        else{
00383               printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name);
00384               exit(STATE_UNKNOWN);
00385               }
00386        stat = mac_addr_dlpi(dev, unit, client_hardware_address);
00387        if(stat != 0){
00388               printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit);
00389               exit(STATE_UNKNOWN);
00390               }
00391 
00392 #elif defined(__hpux__)
00393 
00394        long stat;
00395        char dev[20] = "/dev/dlpi" ;
00396        int unit = 0;
00397 
00398        stat = mac_addr_dlpi(dev, unit, client_hardware_address);
00399        if(stat != 0){
00400               printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit);
00401               exit(STATE_UNKNOWN);
00402               }
00403                                           /* Kompf 2000-2003 */
00404 
00405 #else
00406        printf(_("Error: can't get MAC address for this architecture.  Use the --mac option.\n"));
00407        exit(STATE_UNKNOWN);
00408 #endif
00409 
00410        if(verbose)
00411               print_hardware_address(client_hardware_address);
00412 
00413        return OK;
00414         }
00415 
00416 /* determines IP address of the client interface */
00417 int get_ip_address(int sock,char *interface_name){
00418 #if defined(SIOCGIFADDR)
00419        struct ifreq ifr;
00420 
00421        strncpy((char *)&ifr.ifr_name,interface_name,sizeof(ifr.ifr_name)-1);
00422        ifr.ifr_name[sizeof(ifr.ifr_name)-1]='\0';
00423 
00424        if(ioctl(sock,SIOCGIFADDR,&ifr)<0){
00425               printf(_("Error: Cannot determine IP address of interface %s\n"),
00426                      interface_name);
00427               exit(STATE_UNKNOWN);
00428               }
00429 
00430        my_ip=((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
00431 
00432 #else
00433        printf(_("Error: Cannot get interface IP address on this platform.\n"));
00434        exit(STATE_UNKNOWN);
00435 #endif
00436 
00437        if(verbose)
00438               printf(_("Pretending to be relay client %s\n"),inet_ntoa(my_ip));
00439 
00440        return OK;
00441        }
00442 
00443 /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
00444 int send_dhcp_discover(int sock){
00445        dhcp_packet discover_packet;
00446        struct sockaddr_in sockaddr_broadcast;
00447     unsigned short opts;
00448 
00449 
00450        /* clear the packet data structure */
00451        bzero(&discover_packet,sizeof(discover_packet));
00452 
00453 
00454        /* boot request flag (backward compatible with BOOTP servers) */
00455        discover_packet.op=BOOTREQUEST;
00456 
00457        /* hardware address type */
00458        discover_packet.htype=ETHERNET_HARDWARE_ADDRESS;
00459 
00460        /* length of our hardware address */
00461        discover_packet.hlen=ETHERNET_HARDWARE_ADDRESS_LENGTH;
00462 
00463        /*
00464         * transaction ID is supposed to be random.  We won't use the address so
00465         * we don't care about high entropy here.  time(2) is good enough.
00466         */
00467        srand(time(NULL));
00468        packet_xid=random();
00469        discover_packet.xid=htonl(packet_xid);
00470 
00471        /**** WHAT THE HECK IS UP WITH THIS?!?  IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/
00472        /* downright bizzarre... */
00473        ntohl(discover_packet.xid);
00474 
00475        /*discover_packet.secs=htons(65535);*/
00476        discover_packet.secs=0xFF;
00477 
00478        /*
00479         * server needs to know if it should broadcast or unicast its response:
00480         * 0x8000L == 32768 == 1 << 15 == broadcast, 0 == unicast
00481         */
00482        discover_packet.flags = unicast ? 0 : htons(DHCP_BROADCAST_FLAG);
00483 
00484        /* our hardware address */
00485        memcpy(discover_packet.chaddr,client_hardware_address,ETHERNET_HARDWARE_ADDRESS_LENGTH);
00486 
00487        /* first four bytes of options field is magic cookie (as per RFC 2132) */
00488        discover_packet.options[0]='\x63';
00489        discover_packet.options[1]='\x82';
00490        discover_packet.options[2]='\x53';
00491        discover_packet.options[3]='\x63';
00492 
00493     opts = 4;
00494        /* DHCP message type is embedded in options field */
00495        discover_packet.options[opts++]=DHCP_OPTION_MESSAGE_TYPE;    /* DHCP message type option identifier */
00496        discover_packet.options[opts++]='\x01';               /* DHCP message option length in bytes */
00497        discover_packet.options[opts++]=DHCPDISCOVER;
00498 
00499        /* the IP address we're requesting */
00500        if(request_specific_address==TRUE){
00501               discover_packet.options[opts++]=DHCP_OPTION_REQUESTED_ADDRESS;
00502               discover_packet.options[opts++]='\x04';
00503               memcpy(&discover_packet.options[opts],&requested_address,sizeof(requested_address));
00504               opts += sizeof(requested_address);
00505                }
00506        discover_packet.options[opts++]=DHCP_OPTION_END;
00507 
00508        /* unicast fields */
00509        if(unicast)
00510               discover_packet.giaddr.s_addr = my_ip.s_addr;
00511 
00512        /* see RFC 1542, 4.1.1 */
00513        discover_packet.hops = unicast ? 1 : 0;
00514 
00515        /* send the DHCPDISCOVER packet to broadcast address */
00516         sockaddr_broadcast.sin_family=AF_INET;
00517         sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT);
00518        sockaddr_broadcast.sin_addr.s_addr = unicast ? dhcp_ip.s_addr : INADDR_BROADCAST;
00519        bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero));
00520 
00521 
00522        if(verbose){
00523               printf(_("DHCPDISCOVER to %s port %d\n"),inet_ntoa(sockaddr_broadcast.sin_addr),ntohs(sockaddr_broadcast.sin_port));
00524               printf("DHCPDISCOVER XID: %u (0x%X)\n",ntohl(discover_packet.xid),ntohl(discover_packet.xid));
00525               printf("DHCDISCOVER ciaddr:  %s\n",inet_ntoa(discover_packet.ciaddr));
00526               printf("DHCDISCOVER yiaddr:  %s\n",inet_ntoa(discover_packet.yiaddr));
00527               printf("DHCDISCOVER siaddr:  %s\n",inet_ntoa(discover_packet.siaddr));
00528               printf("DHCDISCOVER giaddr:  %s\n",inet_ntoa(discover_packet.giaddr));
00529               }
00530 
00531        /* send the DHCPDISCOVER packet out */
00532        send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast);
00533 
00534        if(verbose)
00535               printf("\n\n");
00536 
00537        return OK;
00538         }
00539 
00540 
00541 
00542 
00543 /* waits for a DHCPOFFER message from one or more DHCP servers */
00544 int get_dhcp_offer(int sock){
00545        dhcp_packet offer_packet;
00546        struct sockaddr_in source;
00547        struct sockaddr_in via;
00548        int result=OK;
00549        int responses=0;
00550        int x;
00551        time_t start_time;
00552        time_t current_time;
00553 
00554        time(&start_time);
00555 
00556        /* receive as many responses as we can */
00557        for(responses=0,valid_responses=0;;){
00558 
00559               time(&current_time);
00560               if((current_time-start_time)>=dhcpoffer_timeout)
00561                      break;
00562 
00563               if(verbose)
00564                      printf("\n\n");
00565 
00566               bzero(&source,sizeof(source));
00567               bzero(&via,sizeof(via));
00568               bzero(&offer_packet,sizeof(offer_packet));
00569 
00570               result=OK;
00571               result=receive_dhcp_packet(&offer_packet,sizeof(offer_packet),sock,dhcpoffer_timeout,&source);
00572 
00573               if(result!=OK){
00574                      if(verbose)
00575                             printf(_("Result=ERROR\n"));
00576 
00577                      continue;
00578                       }
00579               else{
00580                      if(verbose)
00581                             printf(_("Result=OK\n"));
00582 
00583                      responses++;
00584                       }
00585 
00586               /* The "source" is either a server or a relay. */
00587               /* Save a copy of "source" into "via" even if it's via itself */
00588               memcpy(&via,&source,sizeof(source)) ;
00589 
00590               if(verbose){
00591                      printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source.sin_addr));
00592                      printf(_(" via %s\n"),inet_ntoa(via.sin_addr));
00593                      printf("DHCPOFFER XID: %u (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid));
00594                      }
00595 
00596               /* check packet xid to see if its the same as the one we used in the discover packet */
00597               if(ntohl(offer_packet.xid)!=packet_xid){
00598                      if(verbose)
00599                             printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"),ntohl(offer_packet.xid),packet_xid);
00600 
00601                      continue;
00602                       }
00603 
00604               /* check hardware address */
00605               result=OK;
00606               if(verbose)
00607                      printf("DHCPOFFER chaddr: ");
00608 
00609               for(x=0;x<ETHERNET_HARDWARE_ADDRESS_LENGTH;x++){
00610                      if(verbose)
00611                             printf("%02X",(unsigned char)offer_packet.chaddr[x]);
00612 
00613                      if(offer_packet.chaddr[x]!=client_hardware_address[x])
00614                             result=ERROR;
00615                      }
00616               if(verbose)
00617                      printf("\n");
00618 
00619               if(result==ERROR){
00620                      if(verbose)
00621                             printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n"));
00622 
00623                      continue;
00624                       }
00625 
00626               if(verbose){
00627                      printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr));
00628                      printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr));
00629                      printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr));
00630                      printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr));
00631                      }
00632 
00633               add_dhcp_offer(source.sin_addr,&offer_packet);
00634 
00635               valid_responses++;
00636                }
00637 
00638        if(verbose){
00639               printf(_("Total responses seen on the wire: %d\n"),responses);
00640               printf(_("Valid responses for this machine: %d\n"),valid_responses);
00641               }
00642 
00643        return OK;
00644         }
00645 
00646 
00647 
00648 /* sends a DHCP packet */
00649 int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in *dest){
00650        int result;
00651 
00652        result=sendto(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)dest,sizeof(*dest));
00653 
00654        if(verbose)
00655               printf(_("send_dhcp_packet result: %d\n"),result);
00656 
00657        if(result<0)
00658               return ERROR;
00659 
00660        return OK;
00661         }
00662 
00663 
00664 
00665 /* receives a DHCP packet */
00666 int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){
00667         struct timeval tv;
00668         fd_set readfds;
00669        fd_set oobfds;
00670        int recv_result;
00671        socklen_t address_size;
00672        struct sockaddr_in source_address;
00673        int nfound;
00674 
00675 
00676         /* wait for data to arrive (up time timeout) */
00677         tv.tv_sec=timeout;
00678         tv.tv_usec=0;
00679         FD_ZERO(&readfds);
00680         FD_ZERO(&oobfds);
00681         FD_SET(sock,&readfds);
00682         FD_SET(sock,&oobfds);
00683         nfound = select(sock+1,&readfds,NULL,&oobfds,&tv);
00684 
00685         /* make sure some data has arrived */
00686         if(!FD_ISSET(sock,&readfds)){
00687               if(verbose)
00688                      printf(_("No (more) data received (nfound: %d)\n"), nfound);
00689                 return ERROR;
00690                 }
00691 
00692         else{
00693 
00694               /* 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
00695                  not being interpreted correctly.  sigh... */
00696               bzero(&source_address,sizeof(source_address));
00697               address_size=sizeof(source_address);
00698                 recv_result=recvfrom(sock,(char *)buffer,buffer_size,MSG_PEEK,(struct sockaddr *)&source_address,&address_size);
00699               if(verbose)
00700                      printf("recv_result_1: %d\n",recv_result);
00701                 recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size);
00702               if(verbose)
00703                      printf("recv_result_2: %d\n",recv_result);
00704 
00705                 if(recv_result==-1){
00706                      if(verbose){
00707                             printf(_("recvfrom() failed, "));
00708                             printf("errno: (%d) -> %s\n",errno,strerror(errno));
00709                             }
00710                         return ERROR;
00711                         }
00712               else{
00713                      if(verbose){
00714                             printf(_("receive_dhcp_packet() result: %d\n"),recv_result);
00715                             printf(_("receive_dhcp_packet() source: %s\n"),inet_ntoa(source_address.sin_addr));
00716                             }
00717 
00718                      memcpy(address,&source_address,sizeof(source_address));
00719                      return OK;
00720                       }
00721                 }
00722 
00723        return OK;
00724         }
00725 
00726 
00727 /* creates a socket for DHCP communication */
00728 int create_dhcp_socket(void){
00729         struct sockaddr_in myname;
00730        struct ifreq interface;
00731         int sock;
00732         int flag=1;
00733 
00734         /* Set up the address we're going to bind to. */
00735        bzero(&myname,sizeof(myname));
00736         myname.sin_family=AF_INET;
00737         /* listen to DHCP server port if we're in unicast mode */
00738         myname.sin_port = htons(unicast ? DHCP_SERVER_PORT : DHCP_CLIENT_PORT);
00739         myname.sin_addr.s_addr = unicast ? my_ip.s_addr : INADDR_ANY;
00740         bzero(&myname.sin_zero,sizeof(myname.sin_zero));
00741 
00742         /* create a socket for DHCP communications */
00743        sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
00744         if(sock<0){
00745               printf(_("Error: Could not create socket!\n"));
00746               exit(STATE_UNKNOWN);
00747                }
00748 
00749        if(verbose)
00750               printf("DHCP socket: %d\n",sock);
00751 
00752         /* set the reuse address flag so we don't get errors when restarting */
00753         flag=1;
00754         if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
00755               printf(_("Error: Could not set reuse address option on DHCP socket!\n"));
00756               exit(STATE_UNKNOWN);
00757                }
00758 
00759         /* set the broadcast option - we need this to listen to DHCP broadcast messages */
00760         if(!unicast && setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){
00761               printf(_("Error: Could not set broadcast option on DHCP socket!\n"));
00762               exit(STATE_UNKNOWN);
00763                }
00764 
00765        /* bind socket to interface */
00766 #if defined(__linux__)
00767        strncpy(interface.ifr_ifrn.ifrn_name,network_interface_name,IFNAMSIZ-1);
00768        interface.ifr_ifrn.ifrn_name[IFNAMSIZ-1]='\0';
00769        if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){
00770               printf(_("Error: Could not bind socket to interface %s.  Check your privileges...\n"),network_interface_name);
00771               exit(STATE_UNKNOWN);
00772                }
00773 
00774 #else
00775        strncpy(interface.ifr_name,network_interface_name,IFNAMSIZ-1);
00776        interface.ifr_name[IFNAMSIZ-1]='\0';
00777 #endif
00778 
00779         /* bind the socket */
00780         if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
00781               printf(_("Error: Could not bind to DHCP socket (port %d)!  Check your privileges...\n"),DHCP_CLIENT_PORT);
00782               exit(STATE_UNKNOWN);
00783                }
00784 
00785         return sock;
00786         }
00787 
00788 
00789 /* closes DHCP socket */
00790 int close_dhcp_socket(int sock){
00791 
00792        close(sock);
00793 
00794        return OK;
00795         }
00796 
00797 
00798 /* adds a requested server address to list in memory */
00799 int add_requested_server(struct in_addr server_address){
00800        requested_server *new_server;
00801 
00802        new_server=(requested_server *)malloc(sizeof(requested_server));
00803        if(new_server==NULL)
00804               return ERROR;
00805 
00806        new_server->server_address=server_address;
00807        new_server->answered=FALSE;
00808 
00809        new_server->next=requested_server_list;
00810        requested_server_list=new_server;
00811 
00812        requested_servers++;
00813 
00814        if(verbose)
00815               printf(_("Requested server address: %s\n"),inet_ntoa(new_server->server_address));
00816 
00817        return OK;
00818         }
00819 
00820 
00821 
00822 
00823 /* adds a DHCP OFFER to list in memory */
00824 int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
00825        dhcp_offer *new_offer;
00826        int x;
00827        unsigned option_type;
00828        unsigned option_length;
00829        struct in_addr serv_ident = {0};
00830 
00831        if(offer_packet==NULL)
00832               return ERROR;
00833 
00834        /* process all DHCP options present in the packet */
00835        for(x=4;x<MAX_DHCP_OPTIONS_LENGTH;){
00836 
00837               if((int)offer_packet->options[x]==-1)
00838                      break;
00839 
00840               /* get option type */
00841               option_type=offer_packet->options[x++];
00842 
00843               /* get option length */
00844               option_length=offer_packet->options[x++];
00845 
00846               if(verbose)
00847                      printf("Option: %d (0x%02X)\n",option_type,option_length);
00848 
00849               /* get option data */
00850               switch(option_type){
00851               case DHCP_OPTION_LEASE_TIME:
00852                      memcpy(&dhcp_lease_time, &offer_packet->options[x],sizeof(dhcp_lease_time));
00853                      dhcp_lease_time = ntohl(dhcp_lease_time);
00854                      break;
00855               case DHCP_OPTION_RENEWAL_TIME:
00856                      memcpy(&dhcp_renewal_time, &offer_packet->options[x],sizeof(dhcp_renewal_time));
00857                      dhcp_renewal_time = ntohl(dhcp_renewal_time);
00858                      break;
00859               case DHCP_OPTION_REBINDING_TIME:
00860                      memcpy(&dhcp_rebinding_time, &offer_packet->options[x],sizeof(dhcp_rebinding_time));
00861                      dhcp_rebinding_time = ntohl(dhcp_rebinding_time);
00862                      break;
00863               case DHCP_OPTION_SERVER_IDENTIFIER:
00864                      memcpy(&serv_ident.s_addr, &offer_packet->options[x],sizeof(serv_ident.s_addr));
00865                      break;
00866                      }
00867 
00868               /* skip option data we're ignoring */
00869               if(option_type==0) /* "pad" option, see RFC 2132 (3.1) */
00870                      x+=1;
00871               else
00872                      x+=option_length;
00873               }
00874 
00875        if(verbose){
00876               if(dhcp_lease_time==DHCP_INFINITE_TIME)
00877                      printf(_("Lease Time: Infinite\n"));
00878               else
00879                      printf(_("Lease Time: %lu seconds\n"),(unsigned long)dhcp_lease_time);
00880               if(dhcp_renewal_time==DHCP_INFINITE_TIME)
00881                      printf(_("Renewal Time: Infinite\n"));
00882               else
00883                      printf(_("Renewal Time: %lu seconds\n"),(unsigned long)dhcp_renewal_time);
00884               if(dhcp_rebinding_time==DHCP_INFINITE_TIME)
00885                      printf(_("Rebinding Time: Infinite\n"));
00886               printf(_("Rebinding Time: %lu seconds\n"),(unsigned long)dhcp_rebinding_time);
00887               }
00888 
00889        new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer));
00890 
00891        if(new_offer==NULL)
00892               return ERROR;
00893 
00894        /*
00895         * RFC 2131 (2.) says: "DHCP clarifies the interpretation of the
00896         * 'siaddr' field as the address of the server to use in the next step
00897         * of the client's bootstrap process.  A DHCP server may return its own
00898         * address in the 'siaddr' field, if the server is prepared to supply
00899         * the next bootstrap service (e.g., delivery of an operating system
00900         * executable image).  A DHCP server always returns its own address in
00901         * the 'server identifier' option."  'serv_ident' is the 'server
00902         * identifier' option, 'source' is the IP address we received the
00903         * DHCPOFFER from.  If 'serv_ident' isn't available for some reason, we
00904         * use 'source'.
00905         */
00906        new_offer->server_address=serv_ident.s_addr?serv_ident:source;
00907        new_offer->offered_address=offer_packet->yiaddr;
00908        new_offer->lease_time=dhcp_lease_time;
00909        new_offer->renewal_time=dhcp_renewal_time;
00910        new_offer->rebinding_time=dhcp_rebinding_time;
00911 
00912 
00913        if(verbose){
00914               printf(_("Added offer from server @ %s"),inet_ntoa(new_offer->server_address));
00915               printf(_(" of IP address %s\n"),inet_ntoa(new_offer->offered_address));
00916               }
00917 
00918        /* add new offer to head of list */
00919        new_offer->next=dhcp_offer_list;
00920        dhcp_offer_list=new_offer;
00921 
00922        return OK;
00923         }
00924 
00925 
00926 /* frees memory allocated to DHCP OFFER list */
00927 int free_dhcp_offer_list(void){
00928        dhcp_offer *this_offer;
00929        dhcp_offer *next_offer;
00930 
00931        for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){
00932               next_offer=this_offer->next;
00933               free(this_offer);
00934                }
00935 
00936        return OK;
00937         }
00938 
00939 
00940 /* frees memory allocated to requested server list */
00941 int free_requested_server_list(void){
00942        requested_server *this_server;
00943        requested_server *next_server;
00944 
00945        for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){
00946               next_server=this_server->next;
00947               free(this_server);
00948                }
00949 
00950        return OK;
00951         }
00952 
00953 
00954 /* gets state and plugin output to return */
00955 int get_results(void){
00956        dhcp_offer *temp_offer;
00957        requested_server *temp_server;
00958        int result;
00959        u_int32_t max_lease_time=0;
00960 
00961        received_requested_address=FALSE;
00962 
00963        /* checks responses from requested servers */
00964        requested_responses=0;
00965        if(requested_servers>0){
00966 
00967               for(temp_server=requested_server_list;temp_server!=NULL;temp_server=temp_server->next){
00968 
00969                      for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
00970 
00971                             /* get max lease time we were offered */
00972                             if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
00973                                    max_lease_time=temp_offer->lease_time;
00974 
00975                             /* see if we got the address we requested */
00976                             if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
00977                                    received_requested_address=TRUE;
00978 
00979                             /* see if the servers we wanted a response from talked to us or not */
00980                             if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){
00981                                    if(verbose){
00982                                           printf(_("DHCP Server Match: Offerer=%s"),inet_ntoa(temp_offer->server_address));
00983                                           printf(_(" Requested=%s"),inet_ntoa(temp_server->server_address));
00984                                           if(temp_server->answered)
00985                                                  printf(_(" (duplicate)"));
00986                                           printf(_("\n"));
00987                                           }
00988                                    if(temp_server->answered == FALSE){
00989                                           requested_responses++;
00990                                           temp_server->answered=TRUE;
00991                                           }
00992                                     }
00993                               }
00994                       }
00995 
00996                }
00997 
00998        /* else check and see if we got our requested address from any server */
00999        else{
01000 
01001               for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
01002 
01003                      /* get max lease time we were offered */
01004                      if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
01005                             max_lease_time=temp_offer->lease_time;
01006 
01007                      /* see if we got the address we requested */
01008                      if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
01009                             received_requested_address=TRUE;
01010                        }
01011                }
01012 
01013        result=STATE_OK;
01014        if(valid_responses==0)
01015               result=STATE_CRITICAL;
01016        else if(requested_servers>0 && requested_responses==0)
01017               result=STATE_CRITICAL;
01018        else if(requested_responses<requested_servers)
01019               result=STATE_WARNING;
01020        else if(request_specific_address==TRUE && received_requested_address==FALSE)
01021               result=STATE_WARNING;
01022 
01023        if(result==0)               /* garrett honeycutt 2005 */
01024               printf("OK: ");
01025        else if(result==1)
01026               printf("WARNING: ");
01027        else if(result==2)
01028               printf("CRITICAL: ");
01029        else if(result==3)
01030               printf("UNKNOWN: ");
01031 
01032        /* we didn't receive any DHCPOFFERs */
01033        if(dhcp_offer_list==NULL){
01034               printf(_("No DHCPOFFERs were received.\n"));
01035               return result;
01036                }
01037 
01038        printf(_("Received %d DHCPOFFER(s)"),valid_responses);
01039 
01040        if(requested_servers>0)
01041               printf(_(", %s%d of %d requested servers responded"),((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers);
01042 
01043        if(request_specific_address==TRUE)
01044               printf(_(", requested address (%s) was %soffered"),inet_ntoa(requested_address),(received_requested_address==TRUE)?"":_("not "));
01045 
01046        printf(_(", max lease time = "));
01047        if(max_lease_time==DHCP_INFINITE_TIME)
01048               printf(_("Infinity"));
01049        else
01050               printf("%lu sec",(unsigned long)max_lease_time);
01051 
01052        printf(".\n");
01053 
01054        return result;
01055         }
01056 
01057 
01058 /* process command-line arguments */
01059 int process_arguments(int argc, char **argv){
01060        int c;
01061 
01062        if(argc<1)
01063               return ERROR;
01064 
01065        c=0;
01066        while((c+=(call_getopt(argc-c,&argv[c])))<argc){
01067 
01068               /*
01069               if(is_option(argv[c]))
01070                      continue;
01071               */
01072               }
01073 
01074        return validate_arguments();
01075         }
01076 
01077 
01078 
01079 int call_getopt(int argc, char **argv){
01080        int c=0;
01081        int i=0;
01082 
01083        int option_index = 0;
01084        static struct option long_options[] =
01085        {
01086               {"serverip",       required_argument,0,'s'},
01087               {"requestedip",    required_argument,0,'r'},
01088               {"timeout",        required_argument,0,'t'},
01089               {"interface",      required_argument,0,'i'},
01090               {"mac",            required_argument,0,'m'},
01091               {"unicast",        no_argument,      0,'u'},
01092               {"verbose",        no_argument,      0,'v'},
01093               {"version",        no_argument,      0,'V'},
01094               {"help",           no_argument,      0,'h'},
01095               {0,0,0,0}
01096        };
01097 
01098        while(1){
01099               c=getopt_long(argc,argv,"+hVvt:s:r:t:i:m:u",long_options,&option_index);
01100 
01101               i++;
01102 
01103               if(c==-1||c==EOF||c==1)
01104                      break;
01105 
01106               switch(c){
01107               case 'w':
01108               case 'r':
01109               case 't':
01110               case 'i':
01111                      i++;
01112                      break;
01113               default:
01114                      break;
01115                       }
01116 
01117               switch(c){
01118 
01119               case 's': /* DHCP server address */
01120                      resolve_host(optarg,&dhcp_ip);
01121                      add_requested_server(dhcp_ip);
01122                      break;
01123 
01124               case 'r': /* address we are requested from DHCP servers */
01125                      resolve_host(optarg,&requested_address);
01126                      request_specific_address=TRUE;
01127                      break;
01128 
01129               case 't': /* timeout */
01130 
01131                      /*
01132                      if(is_intnonneg(optarg))
01133                      */
01134                      if(atoi(optarg)>0)
01135                             dhcpoffer_timeout=atoi(optarg);
01136                      /*
01137                      else
01138                             usage("Time interval must be a nonnegative integer\n");
01139                      */
01140                      break;
01141 
01142               case 'm': /* MAC address */
01143 
01144                      if((user_specified_mac=mac_aton(optarg)) == NULL)
01145                             usage("Cannot parse MAC address.\n");
01146                      if(verbose)
01147                             print_hardware_address(user_specified_mac);
01148 
01149                      break;
01150 
01151               case 'i': /* interface name */
01152 
01153                      strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
01154                      network_interface_name[sizeof(network_interface_name)-1]='\x0';
01155 
01156                      break;
01157 
01158               case 'u': /* unicast testing */
01159                      unicast=1;
01160                      break;
01161 
01162               case 'V': /* version */
01163                      print_revision(progname, NP_VERSION);
01164                      exit(STATE_OK);
01165 
01166               case 'h': /* help */
01167                      print_help();
01168                      exit(STATE_OK);
01169 
01170               case 'v': /* verbose */
01171                      verbose=1;
01172                      break;
01173 
01174               case '?': /* help */
01175                      usage5 ();
01176                      break;
01177 
01178               default:
01179                      break;
01180                       }
01181                }
01182 
01183        return i;
01184         }
01185 
01186 
01187 int validate_arguments(void){
01188 
01189        return OK;
01190         }
01191 
01192 
01193 #if defined(__sun__) || defined(__solaris__) || defined(__hpux__)
01194 /* Kompf 2000-2003   see ACKNOWLEDGEMENTS */
01195 
01196 /* get a message from a stream; return type of message */
01197 static int get_msg(int fd){
01198        int flags = 0;
01199        int res, ret;
01200        ctl_area[0] = 0;
01201        dat_area[0] = 0;
01202        ret = 0;
01203        res = getmsg(fd, &ctl, &dat, &flags);
01204 
01205        if(res < 0){
01206               if(errno == EINTR){
01207                      return(GOT_INTR);
01208                      }
01209               else{
01210                      printf("%s\n", "get_msg FAILED.");
01211                      return(GOT_ERR);
01212                      }
01213               }
01214        if(ctl.len > 0){
01215               ret |= GOT_CTRL;
01216               }
01217        if(dat.len > 0){
01218               ret |= GOT_DATA;
01219               }
01220 
01221        return(ret);
01222        }
01223 
01224 /* verify that dl_primitive in ctl_area = prim */
01225 static int check_ctrl(int prim){
01226        dl_error_ack_t *err_ack = (dl_error_ack_t *)ctl_area;
01227 
01228        if(err_ack->dl_primitive != prim){
01229               printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno));
01230               exit(STATE_UNKNOWN);
01231               }
01232 
01233        return 0;
01234        }
01235 
01236 /* put a control message on a stream */
01237 static int put_ctrl(int fd, int len, int pri){
01238 
01239        ctl.len = len;
01240        if(putmsg(fd, &ctl, 0, pri) < 0){
01241               printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno));
01242               exit(STATE_UNKNOWN);
01243               }
01244 
01245        return  0;
01246        }
01247 
01248 /* put a control + data message on a stream */
01249 static int put_both(int fd, int clen, int dlen, int pri){
01250 
01251        ctl.len = clen;
01252        dat.len = dlen;
01253        if(putmsg(fd, &ctl, &dat, pri) < 0){
01254               printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno));
01255               exit(STATE_UNKNOWN);
01256               }
01257 
01258        return  0;
01259        }
01260 
01261 /* open file descriptor and attach */
01262 static int dl_open(const char *dev, int unit, int *fd){
01263        dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area;
01264 
01265        if((*fd = open(dev, O_RDWR)) == -1){
01266               printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev, strerror(errno));
01267               exit(STATE_UNKNOWN);
01268               }
01269        attach_req->dl_primitive = DL_ATTACH_REQ;
01270        attach_req->dl_ppa = unit;
01271        put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
01272        get_msg(*fd);
01273        return check_ctrl(DL_OK_ACK);
01274        }
01275 
01276 /* send DL_BIND_REQ */
01277 static int dl_bind(int fd, int sap, u_char *addr){
01278        dl_bind_req_t *bind_req = (dl_bind_req_t *)ctl_area;
01279        dl_bind_ack_t *bind_ack = (dl_bind_ack_t *)ctl_area;
01280 
01281        bind_req->dl_primitive = DL_BIND_REQ;
01282        bind_req->dl_sap = sap;
01283        bind_req->dl_max_conind = 1;
01284        bind_req->dl_service_mode = DL_CLDLS;
01285        bind_req->dl_conn_mgmt = 0;
01286        bind_req->dl_xidtest_flg = 0;
01287        put_ctrl(fd, sizeof(dl_bind_req_t), 0);
01288        get_msg(fd);
01289        if (GOT_ERR == check_ctrl(DL_BIND_ACK)){
01290               printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno));
01291               exit(STATE_UNKNOWN);
01292               }
01293        bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr,
01294              bind_ack->dl_addr_length);
01295 
01296        return 0;
01297        }
01298 
01299 /***********************************************************************
01300  * interface:
01301  * function mac_addr_dlpi - get the mac address of the interface with
01302  *                          type dev (eg lnc, hme) and unit (0, 1 ..)
01303  *
01304  * parameter: addr: an array of six bytes, has to be allocated by the caller
01305  *
01306  * return: 0 if OK, -1 if the address could not be determined
01307  *
01308  *
01309  ***********************************************************************/
01310 
01311 long mac_addr_dlpi( const char *dev, int unit, u_char  *addr){
01312        int fd;
01313        u_char mac_addr[25];
01314 
01315        if(GOT_ERR != dl_open(dev, unit, &fd)){
01316               if(GOT_ERR != dl_bind(fd, INSAP, mac_addr)){
01317                      bcopy( mac_addr, addr, 6);
01318                      return 0;
01319                      }
01320               }
01321         close(fd);
01322 
01323        return -1;
01324        }
01325 
01326 /* Kompf 2000-2003 */
01327 #endif
01328 
01329 
01330 /* resolve host name or die (TODO: move this to netutils.c!) */
01331 void resolve_host(const char *in,struct in_addr *out){
01332        struct addrinfo hints, *ai;
01333 
01334        memset(&hints,0,sizeof(hints));
01335        hints.ai_family=PF_INET;
01336        if (getaddrinfo(in,NULL,&hints,&ai) != 0)
01337               usage_va(_("Invalid hostname/address - %s"),optarg);
01338 
01339        memcpy(out,&((struct sockaddr_in *)ai->ai_addr)->sin_addr,sizeof(*out));
01340        freeaddrinfo(ai);
01341        }
01342 
01343 
01344 /* parse MAC address string, return 6 bytes (unterminated) or NULL */
01345 unsigned char *mac_aton(const char *string){
01346        static unsigned char result[6];
01347        char tmp[3];
01348        unsigned i, j;
01349 
01350        for(i=0, j=0; string[i] != '\0' && j < sizeof(result); i++){
01351               /* ignore ':' and any other non-hex character */
01352               if(!isxdigit(string[i]) || !isxdigit(string[i+1]))
01353                      continue;
01354               tmp[0]=string[i];
01355               tmp[1]=string[i+1];
01356               tmp[2]='\0';
01357               result[j]=strtol(tmp,(char **)NULL,16);
01358               i++;
01359               j++;
01360               }
01361 
01362        return (j==6) ? result : NULL;
01363        }
01364 
01365 
01366 void print_hardware_address(const unsigned char *address){
01367        int i;
01368 
01369        printf(_("Hardware address: "));
01370        for (i=0; i<5; i++)
01371               printf("%2.2x:", address[i]);
01372        printf("%2.2x", address[i]);
01373        putchar('\n');
01374        }
01375 
01376 
01377 /* print usage help */
01378 void print_help(void){
01379 
01380        print_revision(progname, NP_VERSION);
01381 
01382        printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n");
01383        printf (COPYRIGHT, copyright, email);
01384 
01385        printf("%s\n", _("This plugin tests the availability of DHCP servers on a network."));
01386 
01387   printf ("\n\n");
01388 
01389        print_usage();
01390 
01391        printf (UT_HELP_VRSN);
01392        printf (UT_EXTRA_OPTS);
01393 
01394        printf (UT_VERBOSE);
01395 
01396        printf (" %s\n", "-s, --serverip=IPADDRESS");
01397   printf ("    %s\n", _("IP address of DHCP server that we must hear from"));
01398   printf (" %s\n", "-r, --requestedip=IPADDRESS");
01399   printf ("    %s\n", _("IP address that should be offered by at least one DHCP server"));
01400   printf (" %s\n", "-t, --timeout=INTEGER");
01401   printf ("    %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs"));
01402   printf (" %s\n", "-i, --interface=STRING");
01403   printf ("    %s\n", _("Interface to to use for listening (i.e. eth0)"));
01404   printf (" %s\n", "-m, --mac=STRING");
01405   printf ("    %s\n", _("MAC address to use in the DHCP request"));
01406   printf (" %s\n", "-u, --unicast");
01407   printf ("    %s\n", _("Unicast testing: mimic a DHCP relay, requires -s"));
01408 
01409   printf (UT_SUPPORT);
01410        return;
01411        }
01412 
01413 
01414 void
01415 print_usage(void){
01416 
01417   printf ("%s\n", _("Usage:"));
01418   printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname);
01419   printf ("                  [-i interface] [-m mac]\n");
01420 
01421        return;
01422        }
01423 
01424 
01425