4
0

check_dhcp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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. }
  814. }
  815. }
  816. }
  817. }
  818. /* else check and see if we got our requested address from any server */
  819. else {
  820. for(temp_offer=dhcp_offer_list; temp_offer!=NULL; temp_offer=temp_offer->next) {
  821. /* get max lease time we were offered */
  822. if(temp_offer->lease_time>max_lease_time || temp_offer->lease_time==DHCP_INFINITE_TIME) {
  823. max_lease_time=temp_offer->lease_time;
  824. }
  825. /* see if we got the address we requested */
  826. if(!memcmp(&requested_address,&temp_offer->offered_address,sizeof(requested_address))) {
  827. received_requested_address=TRUE;
  828. }
  829. }
  830. }
  831. result=STATE_OK;
  832. if(valid_responses==0) {
  833. result=STATE_CRITICAL;
  834. }
  835. else if(requested_servers>0 && requested_responses==0) {
  836. result=STATE_CRITICAL;
  837. }
  838. else if(requested_responses<requested_servers) {
  839. result=STATE_WARNING;
  840. }
  841. else if(request_specific_address==TRUE && received_requested_address==FALSE) {
  842. result=STATE_WARNING;
  843. }
  844. if(result==0) {
  845. printf("OK: ");
  846. }
  847. else if(result==1) {
  848. printf("WARNING: ");
  849. }
  850. else if(result==2) {
  851. printf("CRITICAL: ");
  852. }
  853. else if(result==3) {
  854. printf("UNKNOWN: ");
  855. }
  856. /* we didn't receive any DHCPOFFERs */
  857. if(dhcp_offer_list==NULL) {
  858. printf(_("No DHCPOFFERs were received.\n"));
  859. return result;
  860. }
  861. printf(_("Received %d DHCPOFFER(s)"),valid_responses);
  862. if(requested_servers>0) {
  863. printf(_(", %s%d of %d requested servers responded"),
  864. ((requested_responses<requested_servers) && requested_responses>0)?"only ":"",
  865. requested_responses,requested_servers);
  866. }
  867. if(request_specific_address==TRUE) {
  868. printf(_(", requested address (%s) was %soffered"),
  869. inet_ntoa(requested_address),
  870. (received_requested_address==TRUE)?"":_("not "));
  871. }
  872. printf(_(", max lease time = "));
  873. if(max_lease_time==DHCP_INFINITE_TIME) {
  874. printf(_("Infinity"));
  875. }
  876. else {
  877. printf("%lu sec",(unsigned long)max_lease_time);
  878. }
  879. printf(".\n");
  880. return result;
  881. }
  882. /* process command-line arguments */
  883. int process_arguments(int argc, char **argv) {
  884. int c = 0;
  885. int option_index = 0;
  886. static struct option long_options[] =
  887. {
  888. {"serverip", required_argument,0,'s'},
  889. {"requestedip", required_argument,0,'r'},
  890. {"timeout", required_argument,0,'t'},
  891. {"interface", required_argument,0,'i'},
  892. {"mac", required_argument,0,'m'},
  893. {"unicast", no_argument, 0,'u'},
  894. {"verbose", no_argument, 0,'v'},
  895. {"version", no_argument, 0,'V'},
  896. {"help", no_argument, 0,'h'},
  897. {0,0,0,0}
  898. };
  899. if(argc<1) {
  900. return ERROR;
  901. }
  902. while(1) {
  903. c=getopt_long(argc,argv,"+hVvt:s:r:t:i:m:u",long_options,&option_index);
  904. if(c==-1||c==EOF||c==1) {
  905. break;
  906. }
  907. switch(c) {
  908. case 'w':
  909. case 'r':
  910. case 't':
  911. case 'i':
  912. break;
  913. default:
  914. break;
  915. }
  916. switch(c) {
  917. case 's': /* DHCP server address */
  918. resolve_host(optarg,&dhcp_ip);
  919. add_requested_server(dhcp_ip);
  920. break;
  921. case 'r': /* address we are requested from DHCP servers */
  922. resolve_host(optarg,&requested_address);
  923. request_specific_address=TRUE;
  924. break;
  925. case 't': /* timeout */
  926. /*
  927. if(is_intnonneg(optarg))
  928. */
  929. if(atoi(optarg)>0) {
  930. dhcpoffer_timeout=atoi(optarg);
  931. }
  932. /*
  933. else
  934. usage("Time interval must be a nonnegative integer\n");
  935. */
  936. break;
  937. case 'm': /* MAC address */
  938. if((user_specified_mac=mac_aton(optarg)) == NULL) {
  939. usage("Cannot parse MAC address.\n");
  940. }
  941. if(verbose) {
  942. print_hardware_address(user_specified_mac);
  943. }
  944. break;
  945. case 'i': /* interface name */
  946. strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
  947. network_interface_name[sizeof(network_interface_name)-1]='\x0';
  948. break;
  949. case 'u': /* unicast testing */
  950. unicast=1;
  951. break;
  952. case 'V': /* version */
  953. print_revision(progname, NP_VERSION);
  954. exit(STATE_OK);
  955. case 'h': /* help */
  956. print_help();
  957. exit(STATE_OK);
  958. case 'v': /* verbose */
  959. verbose=1;
  960. break;
  961. case '?': /* help */
  962. usage5 ();
  963. break;
  964. default:
  965. break;
  966. }
  967. }
  968. return validate_arguments();
  969. }
  970. int validate_arguments(void){
  971. return OK;
  972. }
  973. #if defined(__sun__) || defined(__solaris__) || defined(__hpux__)
  974. /* get a message from a stream; return type of message */
  975. static int get_msg(int fd) {
  976. int flags = 0;
  977. int res, ret;
  978. ctl_area[0] = 0;
  979. dat_area[0] = 0;
  980. ret = 0;
  981. res = getmsg(fd, &ctl, &dat, &flags);
  982. if(res < 0) {
  983. if(errno == EINTR) {
  984. return(GOT_INTR);
  985. }
  986. else {
  987. printf("%s\n", "get_msg FAILED.");
  988. return(GOT_ERR);
  989. }
  990. }
  991. if(ctl.len > 0) {
  992. ret |= GOT_CTRL;
  993. }
  994. if(dat.len > 0) {
  995. ret |= GOT_DATA;
  996. }
  997. return(ret);
  998. }
  999. /* verify that dl_primitive in ctl_area = prim */
  1000. static int check_ctrl(int prim) {
  1001. dl_error_ack_t *err_ack = (dl_error_ack_t *)ctl_area;
  1002. if(err_ack->dl_primitive != prim) {
  1003. printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno));
  1004. exit(STATE_UNKNOWN);
  1005. }
  1006. return 0;
  1007. }
  1008. /* put a control message on a stream */
  1009. static int put_ctrl(int fd, int len, int pri) {
  1010. ctl.len = len;
  1011. if(putmsg(fd, &ctl, 0, pri) < 0) {
  1012. printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno));
  1013. exit(STATE_UNKNOWN);
  1014. }
  1015. return 0;
  1016. }
  1017. /* put a control + data message on a stream */
  1018. static int put_both(int fd, int clen, int dlen, int pri) {
  1019. ctl.len = clen;
  1020. dat.len = dlen;
  1021. if(putmsg(fd, &ctl, &dat, pri) < 0) {
  1022. printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno));
  1023. exit(STATE_UNKNOWN);
  1024. }
  1025. return 0;
  1026. }
  1027. /* open file descriptor and attach */
  1028. static int dl_open(const char *dev, int unit, int *fd) {
  1029. dl_attach_req_t *attach_req = (dl_attach_req_t *)ctl_area;
  1030. if((*fd = open(dev, O_RDWR)) == -1) {
  1031. printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev, strerror(errno));
  1032. exit(STATE_UNKNOWN);
  1033. }
  1034. attach_req->dl_primitive = DL_ATTACH_REQ;
  1035. attach_req->dl_ppa = unit;
  1036. put_ctrl(*fd, sizeof(dl_attach_req_t), 0);
  1037. get_msg(*fd);
  1038. return check_ctrl(DL_OK_ACK);
  1039. }
  1040. /* send DL_BIND_REQ */
  1041. static int dl_bind(int fd, int sap, u_char *addr) {
  1042. dl_bind_req_t *bind_req = (dl_bind_req_t *)ctl_area;
  1043. dl_bind_ack_t *bind_ack = (dl_bind_ack_t *)ctl_area;
  1044. bind_req->dl_primitive = DL_BIND_REQ;
  1045. bind_req->dl_sap = sap;
  1046. bind_req->dl_max_conind = 1;
  1047. bind_req->dl_service_mode = DL_CLDLS;
  1048. bind_req->dl_conn_mgmt = 0;
  1049. bind_req->dl_xidtest_flg = 0;
  1050. put_ctrl(fd, sizeof(dl_bind_req_t), 0);
  1051. get_msg(fd);
  1052. if (GOT_ERR == check_ctrl(DL_BIND_ACK)) {
  1053. printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno));
  1054. exit(STATE_UNKNOWN);
  1055. }
  1056. bcopy((u_char *)bind_ack + bind_ack->dl_addr_offset, addr, bind_ack->dl_addr_length);
  1057. return 0;
  1058. }
  1059. /***********************************************************************
  1060. * interface:
  1061. * function mac_addr_dlpi - get the mac address of the interface with
  1062. * type dev (eg lnc, hme) and unit (0, 1 ..)
  1063. *
  1064. * parameter: addr: an array of six bytes, has to be allocated by the caller
  1065. *
  1066. * return: 0 if OK, -1 if the address could not be determined
  1067. *
  1068. *
  1069. ***********************************************************************/
  1070. long mac_addr_dlpi( const char *dev, int unit, u_char *addr) {
  1071. int fd;
  1072. u_char mac_addr[25];
  1073. if(GOT_ERR != dl_open(dev, unit, &fd)) {
  1074. if(GOT_ERR != dl_bind(fd, INSAP, mac_addr)) {
  1075. bcopy(mac_addr, addr, 6);
  1076. return 0;
  1077. }
  1078. }
  1079. close(fd);
  1080. return -1;
  1081. }
  1082. #endif
  1083. /* resolve host name or die (TODO: move this to netutils.c!) */
  1084. void resolve_host(const char *in,struct in_addr *out) {
  1085. struct addrinfo hints, *ai;
  1086. memset(&hints,0,sizeof(hints));
  1087. hints.ai_family=PF_INET;
  1088. if (getaddrinfo(in,NULL,&hints,&ai) != 0) {
  1089. usage_va(_("Invalid hostname/address - %s"),optarg);
  1090. }
  1091. memcpy(out,&((struct sockaddr_in *)ai->ai_addr)->sin_addr,sizeof(*out));
  1092. freeaddrinfo(ai);
  1093. }
  1094. /* parse MAC address string, return 6 bytes (unterminated) or NULL */
  1095. unsigned char *mac_aton(const char *string) {
  1096. static unsigned char result[6];
  1097. char tmp[3];
  1098. unsigned i, j;
  1099. for(i=0, j=0; string[i] != '\0' && j < sizeof(result); i++) {
  1100. /* ignore ':' and any other non-hex character */
  1101. if(!isxdigit(string[i]) || !isxdigit(string[i+1])) {
  1102. continue;
  1103. }
  1104. tmp[0]=string[i];
  1105. tmp[1]=string[i+1];
  1106. tmp[2]='\0';
  1107. result[j]=strtol(tmp,(char **)NULL,16);
  1108. i++;
  1109. j++;
  1110. }
  1111. return (j==6) ? result : NULL;
  1112. }
  1113. void print_hardware_address(const unsigned char *address) {
  1114. int i;
  1115. printf(_("Hardware address: "));
  1116. for (i=0; i<5; i++) {
  1117. printf("%2.2x:", address[i]);
  1118. }
  1119. printf("%2.2x", address[i]);
  1120. putchar('\n');
  1121. }
  1122. /* print usage help */
  1123. void print_help(void) {
  1124. print_revision(progname, NP_VERSION);
  1125. printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n");
  1126. printf (COPYRIGHT, copyright, email);
  1127. printf("%s\n", _("This plugin tests the availability of DHCP servers on a network."));
  1128. printf ("\n\n");
  1129. print_usage();
  1130. printf (UT_HELP_VRSN);
  1131. printf (UT_EXTRA_OPTS);
  1132. printf (UT_VERBOSE);
  1133. printf (" %s\n", "-s, --serverip=IPADDRESS");
  1134. printf (" %s\n", _("IP address of DHCP server that we must hear from"));
  1135. printf (" %s\n", "-r, --requestedip=IPADDRESS");
  1136. printf (" %s\n", _("IP address that should be offered by at least one DHCP server"));
  1137. printf (" %s\n", "-t, --timeout=INTEGER");
  1138. printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs"));
  1139. printf (" %s\n", "-i, --interface=STRING");
  1140. printf (" %s\n", _("Interface to to use for listening (i.e. eth0)"));
  1141. printf (" %s\n", "-m, --mac=STRING");
  1142. printf (" %s\n", _("MAC address to use in the DHCP request"));
  1143. printf (" %s\n", "-u, --unicast");
  1144. printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s"));
  1145. printf (UT_SUPPORT);
  1146. return;
  1147. }
  1148. void
  1149. print_usage(void) {
  1150. printf ("%s\n", _("Usage:"));
  1151. printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname);
  1152. printf (" [-i interface] [-m mac]\n");
  1153. return;
  1154. }