check_dhcp.c 39 KB

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