check_dhcp.c 39 KB

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