check_dhcp.c 39 KB

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