check_dhcp.c 39 KB

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