check_dhcp.c 39 KB

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