check_dhcp.c 38 KB

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