check_dhcp.c 39 KB

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