check_dhcp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /******************************************************************************
  2. *
  3. * Nagios check_dhcp plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)
  7. * Copyright (c) 2001-2006 Nagios Plugin Development Team
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Description:
  12. *
  13. * This file contains the check_dhcp plugin
  14. *
  15. * This plugin tests the availability of DHCP servers on a network.
  16. *
  17. *
  18. * License Information:
  19. *
  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
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. *
  34. * $Id$
  35. *
  36. * ------------------------------------------------------------------------
  37. * Unicast mode was originally implemented by Heiti of Boras Kommun with
  38. * general improvements as well as usability fixes and "forward"-porting by
  39. * Andreas Ericsson of OP5 AB.
  40. * ------------------------------------------------------------------------
  41. *
  42. *****************************************************************************/
  43. const char *progname = "check_dhcp";
  44. const char *revision = "$Revision$";
  45. const char *copyright = "2001-2006";
  46. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  47. #include "common.h"
  48. #include "netutils.h"
  49. #include "utils.h"
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <errno.h>
  54. #include <unistd.h>
  55. #include <sys/time.h>
  56. #include <sys/ioctl.h>
  57. #include <fcntl.h>
  58. #include <getopt.h>
  59. #include <sys/socket.h>
  60. #include <sys/types.h>
  61. #include <netdb.h>
  62. #include <netinet/in.h>
  63. #include <net/if.h>
  64. #include <arpa/inet.h>
  65. #if HAVE_SYS_SOCKIO_H
  66. #include <sys/sockio.h>
  67. #endif
  68. #if defined( __linux__ )
  69. #include <linux/if_ether.h>
  70. #include <features.h>
  71. #elif defined (__bsd__)
  72. #include <netinet/if_ether.h>
  73. #include <sys/param.h>
  74. #include <sys/sysctl.h>
  75. #include <net/if_dl.h>
  76. #elif defined(__sun__) || defined(__solaris__) || defined(__hpux__)
  77. #define INSAP 22
  78. #define OUTSAP 24
  79. #include <signal.h>
  80. #include <ctype.h>
  81. #include <sys/stropts.h>
  82. #include <sys/poll.h>
  83. #include <sys/dlpi.h>
  84. #define bcopy(source, destination, length) memcpy(destination, source, length)
  85. #define AREA_SZ 5000 /* buffer length in bytes */
  86. static u_long ctl_area[AREA_SZ];
  87. static u_long dat_area[AREA_SZ];
  88. static struct strbuf ctl = {AREA_SZ, 0, (char *)ctl_area};
  89. static struct strbuf dat = {AREA_SZ, 0, (char *)dat_area};
  90. #define GOT_CTRL 1
  91. #define GOT_DATA 2
  92. #define GOT_BOTH 3
  93. #define GOT_INTR 4
  94. #define GOT_ERR 128
  95. #define u_int8_t uint8_t
  96. #define u_int16_t uint16_t
  97. #define u_int32_t uint32_t
  98. static int get_msg(int);
  99. static int check_ctrl(int);
  100. static int put_ctrl(int, int, int);
  101. static int put_both(int, int, int, int);
  102. static int dl_open(const char *, int, int *);
  103. static int dl_bind(int, int, u_char *);
  104. long mac_addr_dlpi( const char *, int, u_char *);
  105. #endif
  106. /**** Common definitions ****/
  107. #define OK 0
  108. #define ERROR -1
  109. #define FALSE 0
  110. #define TRUE 1
  111. /**** DHCP definitions ****/
  112. #define MAX_DHCP_CHADDR_LENGTH 16
  113. #define MAX_DHCP_SNAME_LENGTH 64
  114. #define MAX_DHCP_FILE_LENGTH 128
  115. #define MAX_DHCP_OPTIONS_LENGTH 312
  116. typedef struct dhcp_packet_struct{
  117. u_int8_t op; /* packet type */
  118. u_int8_t htype; /* type of hardware address for this machine (Ethernet, etc) */
  119. u_int8_t hlen; /* length of hardware address (of this machine) */
  120. u_int8_t hops; /* hops */
  121. u_int32_t xid; /* random transaction id number - chosen by this machine */
  122. u_int16_t secs; /* seconds used in timing */
  123. u_int16_t flags; /* flags */
  124. struct in_addr ciaddr; /* IP address of this machine (if we already have one) */
  125. struct in_addr yiaddr; /* IP address of this machine (offered by the DHCP server) */
  126. struct in_addr siaddr; /* IP address of DHCP server */
  127. struct in_addr giaddr; /* IP address of DHCP relay */
  128. unsigned char chaddr [MAX_DHCP_CHADDR_LENGTH]; /* hardware address of this machine */
  129. char sname [MAX_DHCP_SNAME_LENGTH]; /* name of DHCP server */
  130. char file [MAX_DHCP_FILE_LENGTH]; /* boot file name (used for diskless booting?) */
  131. char options[MAX_DHCP_OPTIONS_LENGTH]; /* options */
  132. }dhcp_packet;
  133. typedef struct dhcp_offer_struct{
  134. struct in_addr server_address; /* address of DHCP server that sent this offer */
  135. struct in_addr offered_address; /* the IP address that was offered to us */
  136. u_int32_t lease_time; /* lease time in seconds */
  137. u_int32_t renewal_time; /* renewal time in seconds */
  138. u_int32_t rebinding_time; /* rebinding time in seconds */
  139. struct dhcp_offer_struct *next;
  140. }dhcp_offer;
  141. typedef struct requested_server_struct{
  142. struct in_addr server_address;
  143. int answered;
  144. struct requested_server_struct *next;
  145. }requested_server;
  146. #define BOOTREQUEST 1
  147. #define BOOTREPLY 2
  148. #define DHCPDISCOVER 1
  149. #define DHCPOFFER 2
  150. #define DHCPREQUEST 3
  151. #define DHCPDECLINE 4
  152. #define DHCPACK 5
  153. #define DHCPNACK 6
  154. #define DHCPRELEASE 7
  155. #define DHCP_OPTION_MESSAGE_TYPE 53
  156. #define DHCP_OPTION_HOST_NAME 12
  157. #define DHCP_OPTION_BROADCAST_ADDRESS 28
  158. #define DHCP_OPTION_REQUESTED_ADDRESS 50
  159. #define DHCP_OPTION_LEASE_TIME 51
  160. #define DHCP_OPTION_SERVER_IDENTIFIER 54
  161. #define DHCP_OPTION_RENEWAL_TIME 58
  162. #define DHCP_OPTION_REBINDING_TIME 59
  163. #define DHCP_OPTION_END 255
  164. #define DHCP_INFINITE_TIME 0xFFFFFFFF
  165. #define DHCP_BROADCAST_FLAG 32768
  166. #define DHCP_UNICAST_FLAG 0
  167. #define DHCP_SERVER_PORT 67
  168. #define DHCP_CLIENT_PORT 68
  169. #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */
  170. #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */
  171. u_int8_t unicast = 0; /* unicast mode: mimic a DHCP relay */
  172. struct in_addr my_ip; /* our address (required for relay) */
  173. struct in_addr dhcp_ip; /* server to query (if in unicast mode) */
  174. unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]="";
  175. char network_interface_name[IFNAMSIZ]="eth0";
  176. u_int32_t packet_xid=0;
  177. u_int32_t dhcp_lease_time=0;
  178. u_int32_t dhcp_renewal_time=0;
  179. u_int32_t dhcp_rebinding_time=0;
  180. int dhcpoffer_timeout=2;
  181. dhcp_offer *dhcp_offer_list=NULL;
  182. requested_server *requested_server_list=NULL;
  183. int valid_responses=0; /* number of valid DHCPOFFERs we received */
  184. int requested_servers=0;
  185. int requested_responses=0;
  186. int request_specific_address=FALSE;
  187. int received_requested_address=FALSE;
  188. int verbose=0;
  189. struct in_addr requested_address;
  190. int process_arguments(int, char **);
  191. int call_getopt(int, char **);
  192. int validate_arguments(void);
  193. void print_usage(void);
  194. void print_help(void);
  195. int get_hardware_address(int,char *);
  196. int get_ip_address(int,char *);
  197. int send_dhcp_discover(int);
  198. int get_dhcp_offer(int);
  199. int get_results(void);
  200. int add_dhcp_offer(struct in_addr,dhcp_packet *);
  201. int free_dhcp_offer_list(void);
  202. int free_requested_server_list(void);
  203. int create_dhcp_socket(void);
  204. int close_dhcp_socket(int);
  205. int send_dhcp_packet(void *,int,int,struct sockaddr_in *);
  206. int receive_dhcp_packet(void *,int,int,int,struct sockaddr_in *);
  207. int main(int argc, char **argv){
  208. int dhcp_socket;
  209. int result = STATE_UNKNOWN;
  210. /* this plugin almost certainly needs root permissions. */
  211. np_warn_if_not_root();
  212. setlocale (LC_ALL, "");
  213. bindtextdomain (PACKAGE, LOCALEDIR);
  214. textdomain (PACKAGE);
  215. if(process_arguments(argc,argv)!=OK){
  216. usage4 (_("Could not parse arguments"));
  217. }
  218. /* create socket for DHCP communications */
  219. dhcp_socket=create_dhcp_socket();
  220. /* get hardware address of client machine */
  221. get_hardware_address(dhcp_socket,network_interface_name);
  222. if(unicast) /* get IP address of client machine */
  223. get_ip_address(dhcp_socket,network_interface_name);
  224. /* send DHCPDISCOVER packet */
  225. send_dhcp_discover(dhcp_socket);
  226. /* wait for a DHCPOFFER packet */
  227. get_dhcp_offer(dhcp_socket);
  228. /* close socket we created */
  229. close_dhcp_socket(dhcp_socket);
  230. /* determine state/plugin output to return */
  231. result=get_results();
  232. /* free allocated memory */
  233. free_dhcp_offer_list();
  234. free_requested_server_list();
  235. return result;
  236. }
  237. /* determines hardware address on client machine */
  238. int get_hardware_address(int sock,char *interface_name){
  239. int i;
  240. #if defined(__linux__)
  241. struct ifreq ifr;
  242. strncpy((char *)&ifr.ifr_name,interface_name,sizeof(ifr.ifr_name));
  243. /* try and grab hardware address of requested interface */
  244. if(ioctl(sock,SIOCGIFHWADDR,&ifr)<0){
  245. printf(_("Error: Could not get hardware address of interface '%s'\n"),interface_name);
  246. exit(STATE_UNKNOWN);
  247. }
  248. memcpy(&client_hardware_address[0],&ifr.ifr_hwaddr.sa_data,6);
  249. #elif defined(__bsd__)
  250. /* King 2004 see ACKNOWLEDGEMENTS */
  251. int mib[6], len;
  252. char *buf;
  253. unsigned char *ptr;
  254. struct if_msghdr *ifm;
  255. struct sockaddr_dl *sdl;
  256. mib[0] = CTL_NET;
  257. mib[1] = AF_ROUTE;
  258. mib[2] = 0;
  259. mib[3] = AF_LINK;
  260. mib[4] = NET_RT_IFLIST;
  261. if((mib[5] = if_nametoindex(interface_name)) == 0){
  262. printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno));
  263. exit(STATE_UNKNOWN);
  264. }
  265. if(sysctl(mib, 6, NULL, &len, NULL, 0) < 0){
  266. printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name, strerror(errno));
  267. exit(STATE_UNKNOWN);
  268. }
  269. if((buf = malloc(len)) == NULL){
  270. printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name, strerror(errno));
  271. exit(4);
  272. }
  273. if(sysctl(mib, 6, buf, &len, NULL, 0) < 0){
  274. printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name, strerror(errno));
  275. exit(STATE_UNKNOWN);
  276. }
  277. ifm = (struct if_msghdr *)buf;
  278. sdl = (struct sockaddr_dl *)(ifm + 1);
  279. ptr = (unsigned char *)LLADDR(sdl);
  280. memcpy(&client_hardware_address[0], ptr, 6) ;
  281. /* King 2004 */
  282. #elif defined(__sun__) || defined(__solaris__)
  283. /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */
  284. long stat;
  285. char dev[20] = "/dev/";
  286. char *p;
  287. int unit;
  288. for(p = interface_name; *p && isalpha(*p); p++)
  289. /* no-op */ ;
  290. if( p != '\0' ){
  291. unit = atoi(p) ;
  292. *p = '\0' ;
  293. strncat(dev, interface_name, 6) ;
  294. }
  295. else{
  296. printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name);
  297. exit(STATE_UNKNOWN);
  298. }
  299. stat = mac_addr_dlpi(dev, unit, client_hardware_address);
  300. if(stat != 0){
  301. printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit);
  302. exit(STATE_UNKNOWN);
  303. }
  304. #elif defined(__hpux__)
  305. long stat;
  306. char dev[20] = "/dev/dlpi" ;
  307. int unit = 0;
  308. stat = mac_addr_dlpi(dev, unit, client_hardware_address);
  309. if(stat != 0){
  310. printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev, unit);
  311. exit(STATE_UNKNOWN);
  312. }
  313. /* Kompf 2000-2003 */
  314. #else
  315. printf(_("Error: can't get MAC address for this architecture.\n"));
  316. exit(STATE_UNKNOWN);
  317. #endif
  318. if(verbose){
  319. printf(_("Hardware address: "));
  320. for (i=0; i<6; ++i)
  321. printf("%2.2x", client_hardware_address[i]);
  322. printf( "\n");
  323. }
  324. return OK;
  325. }
  326. /* determines IP address of the client interface */
  327. int get_ip_address(int sock,char *interface_name){
  328. #if defined(SIOCGIFADDR)
  329. struct ifreq ifr;
  330. strncpy((char *)&ifr.ifr_name,interface_name,sizeof(ifr.ifr_name)-1);
  331. ifr.ifr_name[sizeof(ifr.ifr_name)-1]='\0';
  332. if(ioctl(sock,SIOCGIFADDR,&ifr)<0){
  333. printf(_("Error: Cannot determine IP address of interface %s\n"),
  334. interface_name);
  335. exit(STATE_UNKNOWN);
  336. }
  337. my_ip=((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
  338. #else
  339. printf(_("Error: Cannot get interface IP address on this platform.\n"));
  340. exit(STATE_UNKNOWN);
  341. #endif
  342. if(verbose)
  343. printf(_("Pretending to be relay client %s\n"),inet_ntoa(my_ip));
  344. return OK;
  345. }
  346. /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
  347. int send_dhcp_discover(int sock){
  348. dhcp_packet discover_packet;
  349. struct sockaddr_in sockaddr_broadcast;
  350. unsigned short opts;
  351. /* clear the packet data structure */
  352. bzero(&discover_packet,sizeof(discover_packet));
  353. /* boot request flag (backward compatible with BOOTP servers) */
  354. discover_packet.op=BOOTREQUEST;
  355. /* hardware address type */
  356. discover_packet.htype=ETHERNET_HARDWARE_ADDRESS;
  357. /* length of our hardware address */
  358. discover_packet.hlen=ETHERNET_HARDWARE_ADDRESS_LENGTH;
  359. /* transaction id is supposed to be random */
  360. srand(time(NULL));
  361. packet_xid=random();
  362. discover_packet.xid=htonl(packet_xid);
  363. /**** WHAT THE HECK IS UP WITH THIS?!? IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/
  364. /* downright bizzarre... */
  365. ntohl(discover_packet.xid);
  366. /*discover_packet.secs=htons(65535);*/
  367. discover_packet.secs=0xFF;
  368. /*
  369. * server needs to know if it should broadcast or unicast its response:
  370. * 0x8000L == 32768 == 1 << 15 == broadcast, 0 == unicast
  371. */
  372. discover_packet.flags = unicast ? 0 : htons(DHCP_BROADCAST_FLAG);
  373. /* our hardware address */
  374. memcpy(discover_packet.chaddr,client_hardware_address,ETHERNET_HARDWARE_ADDRESS_LENGTH);
  375. /* first four bytes of options field is magic cookie (as per RFC 2132) */
  376. discover_packet.options[0]='\x63';
  377. discover_packet.options[1]='\x82';
  378. discover_packet.options[2]='\x53';
  379. discover_packet.options[3]='\x63';
  380. opts = 4;
  381. /* DHCP message type is embedded in options field */
  382. discover_packet.options[opts++]=DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
  383. discover_packet.options[opts++]='\x01'; /* DHCP message option length in bytes */
  384. discover_packet.options[opts++]=DHCPDISCOVER;
  385. /* the IP address we're requesting */
  386. if(request_specific_address==TRUE){
  387. discover_packet.options[opts++]=DHCP_OPTION_REQUESTED_ADDRESS;
  388. discover_packet.options[opts++]='\x04';
  389. memcpy(&discover_packet.options[opts],&requested_address,sizeof(requested_address));
  390. opts += sizeof(requested_address);
  391. }
  392. discover_packet.options[opts++]=DHCP_OPTION_END;
  393. /* unicast fields */
  394. if(unicast)
  395. discover_packet.giaddr.s_addr = my_ip.s_addr;
  396. /* see RFC 1542, 4.1.1 */
  397. discover_packet.hops = unicast ? 1 : 0;
  398. /* send the DHCPDISCOVER packet to broadcast address */
  399. sockaddr_broadcast.sin_family=AF_INET;
  400. sockaddr_broadcast.sin_port=htons(DHCP_SERVER_PORT);
  401. sockaddr_broadcast.sin_addr.s_addr = unicast ? dhcp_ip.s_addr : INADDR_BROADCAST;
  402. bzero(&sockaddr_broadcast.sin_zero,sizeof(sockaddr_broadcast.sin_zero));
  403. if(verbose){
  404. printf(_("DHCPDISCOVER to %s port %d\n"),inet_ntoa(sockaddr_broadcast.sin_addr),ntohs(sockaddr_broadcast.sin_port));
  405. printf("DHCPDISCOVER XID: %lu (0x%X)\n",ntohl(discover_packet.xid),ntohl(discover_packet.xid));
  406. printf("DHCDISCOVER ciaddr: %s\n",inet_ntoa(discover_packet.ciaddr));
  407. printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet.yiaddr));
  408. printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet.siaddr));
  409. printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet.giaddr));
  410. }
  411. /* send the DHCPDISCOVER packet out */
  412. send_dhcp_packet(&discover_packet,sizeof(discover_packet),sock,&sockaddr_broadcast);
  413. if(verbose)
  414. printf("\n\n");
  415. return OK;
  416. }
  417. /* waits for a DHCPOFFER message from one or more DHCP servers */
  418. int get_dhcp_offer(int sock){
  419. dhcp_packet offer_packet;
  420. struct sockaddr_in source;
  421. struct sockaddr_in via;
  422. int result=OK;
  423. int responses=0;
  424. int x;
  425. time_t start_time;
  426. time_t current_time;
  427. time(&start_time);
  428. /* receive as many responses as we can */
  429. for(responses=0,valid_responses=0;;){
  430. time(&current_time);
  431. if((current_time-start_time)>=dhcpoffer_timeout)
  432. break;
  433. if(verbose)
  434. printf("\n\n");
  435. bzero(&source,sizeof(source));
  436. bzero(&via,sizeof(via));
  437. bzero(&offer_packet,sizeof(offer_packet));
  438. result=OK;
  439. result=receive_dhcp_packet(&offer_packet,sizeof(offer_packet),sock,dhcpoffer_timeout,&source);
  440. if(result!=OK){
  441. if(verbose)
  442. printf(_("Result=ERROR\n"));
  443. continue;
  444. }
  445. else{
  446. if(verbose)
  447. printf(_("Result=OK\n"));
  448. responses++;
  449. }
  450. /* The "source" is either a server or a relay. */
  451. /* Save a copy of "source" into "via" even if it's via itself */
  452. memcpy(&via,&source,sizeof(source)) ;
  453. /* If siaddr is non-zero, set "source" to siaddr */
  454. if(offer_packet.siaddr.s_addr != 0L){
  455. source.sin_addr.s_addr = offer_packet.siaddr.s_addr ;
  456. }
  457. if(verbose){
  458. printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source.sin_addr));
  459. printf(_(" via %s\n"),inet_ntoa(via.sin_addr));
  460. printf("DHCPOFFER XID: %lu (0x%X)\n",ntohl(offer_packet.xid),ntohl(offer_packet.xid));
  461. }
  462. /* check packet xid to see if its the same as the one we used in the discover packet */
  463. if(ntohl(offer_packet.xid)!=packet_xid){
  464. if(verbose)
  465. printf(_("DHCPOFFER XID (%lu) did not match DHCPDISCOVER XID (%lu) - ignoring packet\n"),ntohl(offer_packet.xid),packet_xid);
  466. continue;
  467. }
  468. /* check hardware address */
  469. result=OK;
  470. if(verbose)
  471. printf("DHCPOFFER chaddr: ");
  472. for(x=0;x<ETHERNET_HARDWARE_ADDRESS_LENGTH;x++){
  473. if(verbose)
  474. printf("%02X",(unsigned char)offer_packet.chaddr[x]);
  475. if(offer_packet.chaddr[x]!=client_hardware_address[x])
  476. result=ERROR;
  477. }
  478. if(verbose)
  479. printf("\n");
  480. if(result==ERROR){
  481. if(verbose)
  482. printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n"));
  483. continue;
  484. }
  485. if(verbose){
  486. printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet.ciaddr));
  487. printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet.yiaddr));
  488. printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet.siaddr));
  489. printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet.giaddr));
  490. }
  491. add_dhcp_offer(source.sin_addr,&offer_packet);
  492. valid_responses++;
  493. }
  494. if(verbose){
  495. printf(_("Total responses seen on the wire: %d\n"),responses);
  496. printf(_("Valid responses for this machine: %d\n"),valid_responses);
  497. }
  498. return OK;
  499. }
  500. /* sends a DHCP packet */
  501. int send_dhcp_packet(void *buffer, int buffer_size, int sock, struct sockaddr_in *dest){
  502. int result;
  503. result=sendto(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)dest,sizeof(*dest));
  504. if(verbose)
  505. printf(_("send_dhcp_packet result: %d\n"),result);
  506. if(result<0)
  507. return ERROR;
  508. return OK;
  509. }
  510. /* receives a DHCP packet */
  511. int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout, struct sockaddr_in *address){
  512. struct timeval tv;
  513. fd_set readfds;
  514. int recv_result;
  515. socklen_t address_size;
  516. struct sockaddr_in source_address;
  517. /* wait for data to arrive (up time timeout) */
  518. tv.tv_sec=timeout;
  519. tv.tv_usec=0;
  520. FD_ZERO(&readfds);
  521. FD_SET(sock,&readfds);
  522. select(sock+1,&readfds,NULL,NULL,&tv);
  523. /* make sure some data has arrived */
  524. if(!FD_ISSET(sock,&readfds)){
  525. if(verbose)
  526. printf(_("No (more) data received\n"));
  527. return ERROR;
  528. }
  529. else{
  530. /* 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
  531. not being interpreted correctly. sigh... */
  532. bzero(&source_address,sizeof(source_address));
  533. address_size=sizeof(source_address);
  534. recv_result=recvfrom(sock,(char *)buffer,buffer_size,MSG_PEEK,(struct sockaddr *)&source_address,&address_size);
  535. if(verbose)
  536. printf("recv_result_1: %d\n",recv_result);
  537. recv_result=recvfrom(sock,(char *)buffer,buffer_size,0,(struct sockaddr *)&source_address,&address_size);
  538. if(verbose)
  539. printf("recv_result_2: %d\n",recv_result);
  540. if(recv_result==-1){
  541. if(verbose){
  542. printf(_("recvfrom() failed, "));
  543. printf("errno: (%d) -> %s\n",errno,strerror(errno));
  544. }
  545. return ERROR;
  546. }
  547. else{
  548. if(verbose){
  549. printf(_("receive_dhcp_packet() result: %d\n"),recv_result);
  550. printf(_("receive_dhcp_packet() source: %s\n"),inet_ntoa(source_address.sin_addr));
  551. }
  552. memcpy(address,&source_address,sizeof(source_address));
  553. return OK;
  554. }
  555. }
  556. return OK;
  557. }
  558. /* creates a socket for DHCP communication */
  559. int create_dhcp_socket(void){
  560. struct sockaddr_in myname;
  561. struct ifreq interface;
  562. int sock;
  563. int flag=1;
  564. /* Set up the address we're going to bind to. */
  565. bzero(&myname,sizeof(myname));
  566. myname.sin_family=AF_INET;
  567. /* listen to DHCP server port if we're in unicast mode */
  568. myname.sin_port = htons(unicast ? DHCP_SERVER_PORT : DHCP_CLIENT_PORT);
  569. myname.sin_addr.s_addr = unicast ? my_ip.s_addr : INADDR_ANY;
  570. bzero(&myname.sin_zero,sizeof(myname.sin_zero));
  571. /* create a socket for DHCP communications */
  572. sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
  573. if(sock<0){
  574. printf(_("Error: Could not create socket!\n"));
  575. exit(STATE_UNKNOWN);
  576. }
  577. if(verbose)
  578. printf("DHCP socket: %d\n",sock);
  579. /* set the reuse address flag so we don't get errors when restarting */
  580. flag=1;
  581. if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
  582. printf(_("Error: Could not set reuse address option on DHCP socket!\n"));
  583. exit(STATE_UNKNOWN);
  584. }
  585. /* set the broadcast option - we need this to listen to DHCP broadcast messages */
  586. if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&flag,sizeof flag)<0){
  587. printf(_("Error: Could not set broadcast option on DHCP socket!\n"));
  588. exit(STATE_UNKNOWN);
  589. }
  590. /* bind socket to interface */
  591. #if defined(__linux__)
  592. strncpy(interface.ifr_ifrn.ifrn_name,network_interface_name,IFNAMSIZ);
  593. if(setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,(char *)&interface,sizeof(interface))<0){
  594. printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"),network_interface_name);
  595. exit(STATE_UNKNOWN);
  596. }
  597. #else
  598. strncpy(interface.ifr_name,network_interface_name,IFNAMSIZ);
  599. #endif
  600. /* bind the socket */
  601. if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
  602. printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"),DHCP_CLIENT_PORT);
  603. exit(STATE_UNKNOWN);
  604. }
  605. return sock;
  606. }
  607. /* closes DHCP socket */
  608. int close_dhcp_socket(int sock){
  609. close(sock);
  610. return OK;
  611. }
  612. /* adds a requested server address to list in memory */
  613. int add_requested_server(struct in_addr server_address){
  614. requested_server *new_server;
  615. new_server=(requested_server *)malloc(sizeof(requested_server));
  616. if(new_server==NULL)
  617. return ERROR;
  618. new_server->server_address=server_address;
  619. new_server->answered=FALSE;
  620. new_server->next=requested_server_list;
  621. requested_server_list=new_server;
  622. requested_servers++;
  623. if(verbose)
  624. printf(_("Requested server address: %s\n"),inet_ntoa(new_server->server_address));
  625. return OK;
  626. }
  627. /* adds a DHCP OFFER to list in memory */
  628. int add_dhcp_offer(struct in_addr source,dhcp_packet *offer_packet){
  629. dhcp_offer *new_offer;
  630. int x;
  631. unsigned option_type;
  632. unsigned option_length;
  633. struct in_addr serv_ident = {0};
  634. if(offer_packet==NULL)
  635. return ERROR;
  636. /* process all DHCP options present in the packet */
  637. for(x=4;x<MAX_DHCP_OPTIONS_LENGTH;){
  638. /* end of options (0 is really just a pad, but bail out anyway) */
  639. if((int)offer_packet->options[x]==-1 || (int)offer_packet->options[x]==0)
  640. break;
  641. /* get option type */
  642. option_type=offer_packet->options[x++];
  643. /* get option length */
  644. option_length=offer_packet->options[x++];
  645. if(verbose)
  646. printf("Option: %d (0x%02X)\n",option_type,option_length);
  647. /* get option data */
  648. switch(option_type){
  649. case DHCP_OPTION_LEASE_TIME:
  650. memcpy(&dhcp_lease_time, &offer_packet->options[x],sizeof(dhcp_lease_time));
  651. dhcp_lease_time = ntohl(dhcp_lease_time);
  652. break;
  653. case DHCP_OPTION_RENEWAL_TIME:
  654. memcpy(&dhcp_renewal_time, &offer_packet->options[x],sizeof(dhcp_renewal_time));
  655. dhcp_renewal_time = ntohl(dhcp_renewal_time);
  656. break;
  657. case DHCP_OPTION_REBINDING_TIME:
  658. memcpy(&dhcp_rebinding_time, &offer_packet->options[x],sizeof(dhcp_rebinding_time));
  659. dhcp_rebinding_time = ntohl(dhcp_rebinding_time);
  660. break;
  661. case DHCP_OPTION_SERVER_IDENTIFIER:
  662. memcpy(&serv_ident.s_addr, &offer_packet->options[x],sizeof(serv_ident.s_addr));
  663. break;
  664. }
  665. /* skip option data we're ignoring */
  666. if(option_type!=DHCP_OPTION_REBINDING_TIME)
  667. x+=option_length;
  668. }
  669. if(verbose){
  670. if(dhcp_lease_time==DHCP_INFINITE_TIME)
  671. printf(_("Lease Time: Infinite\n"));
  672. else
  673. printf(_("Lease Time: %lu seconds\n"),(unsigned long)dhcp_lease_time);
  674. if(dhcp_renewal_time==DHCP_INFINITE_TIME)
  675. printf(_("Renewal Time: Infinite\n"));
  676. else
  677. printf(_("Renewal Time: %lu seconds\n"),(unsigned long)dhcp_renewal_time);
  678. if(dhcp_rebinding_time==DHCP_INFINITE_TIME)
  679. printf(_("Rebinding Time: Infinite\n"));
  680. printf(_("Rebinding Time: %lu seconds\n"),(unsigned long)dhcp_rebinding_time);
  681. }
  682. new_offer=(dhcp_offer *)malloc(sizeof(dhcp_offer));
  683. if(new_offer==NULL)
  684. return ERROR;
  685. /*
  686. * RFC 2131 (2.) says: "DHCP clarifies the interpretation of the
  687. * 'siaddr' field as the address of the server to use in the next step
  688. * of the client's bootstrap process. A DHCP server may return its own
  689. * address in the 'siaddr' field, if the server is prepared to supply
  690. * the next bootstrap service (e.g., delivery of an operating system
  691. * executable image). A DHCP server always returns its own address in
  692. * the 'server identifier' option." 'serv_ident' is the 'server
  693. * identifier' option, 'source' is the 'siaddr' field or (if 'siaddr'
  694. * wasn't available) the IP address we received the DHCPOFFER from. If
  695. * 'serv_ident' isn't available for some reason, we use 'source'.
  696. */
  697. new_offer->server_address=serv_ident.s_addr?serv_ident:source;
  698. new_offer->offered_address=offer_packet->yiaddr;
  699. new_offer->lease_time=dhcp_lease_time;
  700. new_offer->renewal_time=dhcp_renewal_time;
  701. new_offer->rebinding_time=dhcp_rebinding_time;
  702. if(verbose){
  703. printf(_("Added offer from server @ %s"),inet_ntoa(new_offer->server_address));
  704. printf(_(" of IP address %s\n"),inet_ntoa(new_offer->offered_address));
  705. }
  706. /* add new offer to head of list */
  707. new_offer->next=dhcp_offer_list;
  708. dhcp_offer_list=new_offer;
  709. return OK;
  710. }
  711. /* frees memory allocated to DHCP OFFER list */
  712. int free_dhcp_offer_list(void){
  713. dhcp_offer *this_offer;
  714. dhcp_offer *next_offer;
  715. for(this_offer=dhcp_offer_list;this_offer!=NULL;this_offer=next_offer){
  716. next_offer=this_offer->next;
  717. free(this_offer);
  718. }
  719. return OK;
  720. }
  721. /* frees memory allocated to requested server list */
  722. int free_requested_server_list(void){
  723. requested_server *this_server;
  724. requested_server *next_server;
  725. for(this_server=requested_server_list;this_server!=NULL;this_server=next_server){
  726. next_server=this_server->next;
  727. free(this_server);
  728. }
  729. return OK;
  730. }
  731. /* gets state and plugin output to return */
  732. int get_results(void){
  733. dhcp_offer *temp_offer;
  734. requested_server *temp_server;
  735. int result;
  736. u_int32_t max_lease_time=0;
  737. received_requested_address=FALSE;
  738. /* checks responses from requested servers */
  739. requested_responses=0;
  740. if(requested_servers>0){
  741. for(temp_server=requested_server_list;temp_server!=NULL;temp_server=temp_server->next){
  742. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  743. /* get max lease time we were offered */
  744. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  745. max_lease_time=temp_offer->lease_time;
  746. /* see if we got the address we requested */
  747. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  748. received_requested_address=TRUE;
  749. /* see if the servers we wanted a response from talked to us or not */
  750. if(!memcmp(&temp_offer->server_address,&temp_server->server_address,sizeof(temp_server->server_address))){
  751. if(verbose){
  752. printf(_("DHCP Server Match: Offerer=%s"),inet_ntoa(temp_offer->server_address));
  753. printf(_(" Requested=%s"),inet_ntoa(temp_server->server_address));
  754. if(temp_server->answered)
  755. printf(_(" (duplicate)"));
  756. printf(_("\n"));
  757. }
  758. if(temp_server->answered == FALSE){
  759. requested_responses++;
  760. temp_server->answered=TRUE;
  761. }
  762. }
  763. }
  764. }
  765. }
  766. /* else check and see if we got our requested address from any server */
  767. else{
  768. for(temp_offer=dhcp_offer_list;temp_offer!=NULL;temp_offer=temp_offer->next){
  769. /* get max lease time we were offered */
  770. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME)
  771. max_lease_time=temp_offer->lease_time;
  772. /* see if we got the address we requested */
  773. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address)))
  774. received_requested_address=TRUE;
  775. }
  776. }
  777. result=STATE_OK;
  778. if(valid_responses==0)
  779. result=STATE_CRITICAL;
  780. else if(requested_servers>0 && requested_responses==0)
  781. result=STATE_CRITICAL;
  782. else if(requested_responses<requested_servers)
  783. result=STATE_WARNING;
  784. else if(request_specific_address==TRUE && received_requested_address==FALSE)
  785. result=STATE_WARNING;
  786. if(result==0) /* garrett honeycutt 2005 */
  787. printf("OK: ");
  788. else if(result==1)
  789. printf("WARNING: ");
  790. else if(result==2)
  791. printf("CRITICAL: ");
  792. else if(result==3)
  793. printf("UNKNOWN: ");
  794. /* we didn't receive any DHCPOFFERs */
  795. if(dhcp_offer_list==NULL){
  796. printf(_("No DHCPOFFERs were received.\n"));
  797. return result;
  798. }
  799. printf(_("Received %d DHCPOFFER(s)"),valid_responses);
  800. if(requested_servers>0)
  801. printf(_(", %s%d of %d requested servers responded"),((requested_responses<requested_servers) && requested_responses>0)?"only ":"",requested_responses,requested_servers);
  802. if(request_specific_address==TRUE)
  803. printf(_(", requested address (%s) was %soffered"),inet_ntoa(requested_address),(received_requested_address==TRUE)?"":_("not "));
  804. printf(_(", max lease time = "));
  805. if(max_lease_time==DHCP_INFINITE_TIME)
  806. printf(_("Infinity"));
  807. else
  808. printf("%lu sec",(unsigned long)max_lease_time);
  809. printf(".\n");
  810. return result;
  811. }
  812. /* process command-line arguments */
  813. int process_arguments(int argc, char **argv){
  814. int c;
  815. if(argc<1)
  816. return ERROR;
  817. c=0;
  818. while((c+=(call_getopt(argc-c,&argv[c])))<argc){
  819. /*
  820. if(is_option(argv[c]))
  821. continue;
  822. */
  823. }
  824. return validate_arguments();
  825. }
  826. int call_getopt(int argc, char **argv){
  827. int c=0;
  828. int i=0;
  829. struct in_addr ipaddress;
  830. int option_index = 0;
  831. static struct option long_options[] =
  832. {
  833. {"serverip", required_argument,0,'s'},
  834. {"requestedip", required_argument,0,'r'},
  835. {"timeout", required_argument,0,'t'},
  836. {"interface", required_argument,0,'i'},
  837. {"unicast", no_argument, 0,'u'},
  838. {"verbose", no_argument, 0,'v'},
  839. {"version", no_argument, 0,'V'},
  840. {"help", no_argument, 0,'h'},
  841. {0,0,0,0}
  842. };
  843. while(1){
  844. c=getopt_long(argc,argv,"+hVvt:s:r:t:i:u",long_options,&option_index);
  845. i++;
  846. if(c==-1||c==EOF||c==1)
  847. break;
  848. switch(c){
  849. case 'w':
  850. case 'r':
  851. case 't':
  852. case 'i':
  853. i++;
  854. break;
  855. default:
  856. break;
  857. }
  858. switch(c){
  859. case 's': /* DHCP server address */
  860. if(inet_aton(optarg,&ipaddress)){
  861. add_requested_server(ipaddress);
  862. inet_aton(optarg, &dhcp_ip);
  863. if (verbose)
  864. printf("querying %s\n",inet_ntoa(dhcp_ip));
  865. }
  866. /*
  867. else
  868. usage("Invalid server IP address\n");
  869. */
  870. break;
  871. case 'r': /* address we are requested from DHCP servers */
  872. if(inet_aton(optarg,&ipaddress)){
  873. requested_address=ipaddress;
  874. request_specific_address=TRUE;
  875. }
  876. /*
  877. else
  878. usage("Invalid requested IP address\n");
  879. */
  880. break;
  881. case 't': /* timeout */
  882. /*
  883. if(is_intnonneg(optarg))
  884. */
  885. if(atoi(optarg)>0)
  886. dhcpoffer_timeout=atoi(optarg);
  887. /*
  888. else
  889. usage("Time interval must be a nonnegative integer\n");
  890. */
  891. break;
  892. case 'i': /* interface name */
  893. strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
  894. network_interface_name[sizeof(network_interface_name)-1]='\x0';
  895. break;
  896. case 'u': /* unicast testing */
  897. unicast=1;
  898. break;
  899. case 'V': /* version */
  900. print_revision(progname,revision);
  901. exit(STATE_OK);
  902. case 'h': /* help */
  903. print_help();
  904. exit(STATE_OK);
  905. case 'v': /* verbose */
  906. verbose=1;
  907. break;
  908. case '?': /* help */
  909. usage5 ();
  910. break;
  911. default:
  912. break;
  913. }
  914. }
  915. return i;
  916. }
  917. int validate_arguments(void){
  918. return OK;
  919. }
  920. #if defined(__sun__) || defined(__solaris__) || defined(__hpux__)
  921. /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */
  922. /* get a message from a stream; return type of message */
  923. static int get_msg(int fd){
  924. int flags = 0;
  925. int res, ret;
  926. ctl_area[0] = 0;
  927. dat_area[0] = 0;
  928. ret = 0;
  929. res = getmsg(fd, &ctl, &dat, &flags);
  930. if(res < 0){
  931. if(errno == EINTR){
  932. return(GOT_INTR);
  933. }
  934. else{
  935. printf("%s\n", "get_msg FAILED.");
  936. return(GOT_ERR);
  937. }
  938. }
  939. if(ctl.len > 0){
  940. ret |= GOT_CTRL;
  941. }
  942. if(dat.len > 0){
  943. ret |= GOT_DATA;
  944. }
  945. return(ret);
  946. }
  947. /* verify that dl_primitive in ctl_area = prim */
  948. static int check_ctrl(int prim){
  949. dl_error_ack_t *err_ack = (dl_error_ack_t *)ctl_area;
  950. if(err_ack->dl_primitive != prim){
  951. printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno));
  952. exit(STATE_UNKNOWN);
  953. }
  954. return 0;
  955. }
  956. /* put a control message on a stream */
  957. static int put_ctrl(int fd, int len, int pri){
  958. ctl.len = len;
  959. if(putmsg(fd, &ctl, 0, pri) < 0){
  960. printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno));
  961. exit(STATE_UNKNOWN);
  962. }
  963. return 0;
  964. }
  965. /* put a control + data message on a stream */
  966. static int put_both(int fd, int clen, int dlen, int pri){
  967. ctl.len = clen;
  968. dat.len = dlen;
  969. if(putmsg(fd, &ctl, &dat, pri) < 0){
  970. printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno));
  971. exit(STATE_UNKNOWN);
  972. }
  973. return 0;
  974. }
  975. /* open file descriptor and attach */
  976. static int dl_open(const char *dev, int unit, int *fd){
  977. dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area;
  978. if((*fd = open(dev, O_RDWR)) == -1){
  979. printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev, strerror(errno));
  980. exit(STATE_UNKNOWN);
  981. }
  982. attach_req->dl_primitive = DL_ATTACH_REQ;
  983. attach_req->dl_ppa = unit;
  984. put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
  985. get_msg(*fd);
  986. return check_ctrl(DL_OK_ACK);
  987. }
  988. /* send DL_BIND_REQ */
  989. static int dl_bind(int fd, int sap, u_char *addr){
  990. dl_bind_req_t *bind_req = (dl_bind_req_t *)ctl_area;
  991. dl_bind_ack_t *bind_ack = (dl_bind_ack_t *)ctl_area;
  992. bind_req->dl_primitive = DL_BIND_REQ;
  993. bind_req->dl_sap = sap;
  994. bind_req->dl_max_conind = 1;
  995. bind_req->dl_service_mode = DL_CLDLS;
  996. bind_req->dl_conn_mgmt = 0;
  997. bind_req->dl_xidtest_flg = 0;
  998. put_ctrl(fd, sizeof(dl_bind_req_t), 0);
  999. get_msg(fd);
  1000. if (GOT_ERR == check_ctrl(DL_BIND_ACK)){
  1001. printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno));
  1002. exit(STATE_UNKNOWN);
  1003. }
  1004. bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr,
  1005. bind_ack->dl_addr_length);
  1006. return 0;
  1007. }
  1008. /***********************************************************************
  1009. * interface:
  1010. * function mac_addr_dlpi - get the mac address of the interface with
  1011. * type dev (eg lnc, hme) and unit (0, 1 ..)
  1012. *
  1013. * parameter: addr: an array of six bytes, has to be allocated by the caller
  1014. *
  1015. * return: 0 if OK, -1 if the address could not be determined
  1016. *
  1017. *
  1018. ***********************************************************************/
  1019. long mac_addr_dlpi( const char *dev, int unit, u_char *addr){
  1020. int fd;
  1021. u_char mac_addr[25];
  1022. if(GOT_ERR != dl_open(dev, unit, &fd)){
  1023. if(GOT_ERR != dl_bind(fd, INSAP, mac_addr)){
  1024. bcopy( mac_addr, addr, 6);
  1025. return 0;
  1026. }
  1027. }
  1028. close(fd);
  1029. return -1;
  1030. }
  1031. /* Kompf 2000-2003 */
  1032. #endif
  1033. /* print usage help */
  1034. void print_help(void){
  1035. print_revision(progname,revision);
  1036. printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n");
  1037. printf (COPYRIGHT, copyright, email);
  1038. printf("%s\n", _("This plugin tests the availability of DHCP servers on a network."));
  1039. printf ("\n\n");
  1040. print_usage();
  1041. printf (_(UT_HELP_VRSN));
  1042. printf (_(UT_VERBOSE));
  1043. printf (" %s\n", "-s, --serverip=IPADDRESS");
  1044. printf (" %s\n", _("IP address of DHCP server that we must hear from"));
  1045. printf (" %s\n", "-r, --requestedip=IPADDRESS");
  1046. printf (" %s\n", _("IP address that should be offered by at least one DHCP server"));
  1047. printf (" %s\n", "-t, --timeout=INTEGER");
  1048. printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs"));
  1049. printf (" %s\n", "-i, --interface=STRING");
  1050. printf (" %s\n", _("Interface to to use for listening (i.e. eth0)"));
  1051. printf (" %s\n", "-u, --unicast");
  1052. printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s"));
  1053. return;
  1054. }
  1055. void
  1056. print_usage(void){
  1057. printf (_("Usage:"));
  1058. printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname);
  1059. printf (" [-i interface]\n");
  1060. return;
  1061. }