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