summaryrefslogtreecommitdiffstats
path: root/plugins-root/check_dhcp.c
diff options
context:
space:
mode:
authorHolger Weiss <hweiss@users.sourceforge.net>2007-07-26 12:38:53 +0000
committerHolger Weiss <hweiss@users.sourceforge.net>2007-07-26 12:38:53 +0000
commitee33124028a0a5adb13123c0683b04323d26c3b3 (patch)
treea04e7870452b4088eecb4847dd5863b111cbe414 /plugins-root/check_dhcp.c
parent6f60c0ac81933976fdc2e7e763fc1d03312e3b3f (diff)
downloadmonitoring-plugins-ee33124028a0a5adb13123c0683b04323d26c3b3.tar.gz
New "-m, --mac" option which allows for specifying the MAC address to
use in the DHCP request. git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1765 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins-root/check_dhcp.c')
-rw-r--r--plugins-root/check_dhcp.c70
1 files changed, 58 insertions, 12 deletions
diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c
index db673893..a0f150fb 100644
--- a/plugins-root/check_dhcp.c
+++ b/plugins-root/check_dhcp.c
@@ -50,6 +50,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
50#include "netutils.h" 50#include "netutils.h"
51#include "utils.h" 51#include "utils.h"
52 52
53#include <ctype.h>
53#include <stdio.h> 54#include <stdio.h>
54#include <stdlib.h> 55#include <stdlib.h>
55#include <string.h> 56#include <string.h>
@@ -211,6 +212,7 @@ u_int8_t unicast = 0; /* unicast mode: mimic a DHCP relay */
211struct in_addr my_ip; /* our address (required for relay) */ 212struct in_addr my_ip; /* our address (required for relay) */
212struct in_addr dhcp_ip; /* server to query (if in unicast mode) */ 213struct in_addr dhcp_ip; /* server to query (if in unicast mode) */
213unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]=""; 214unsigned char client_hardware_address[MAX_DHCP_CHADDR_LENGTH]="";
215unsigned char *user_specified_mac=NULL;
214 216
215char network_interface_name[IFNAMSIZ]="eth0"; 217char network_interface_name[IFNAMSIZ]="eth0";
216 218
@@ -241,6 +243,8 @@ int validate_arguments(void);
241void print_usage(void); 243void print_usage(void);
242void print_help(void); 244void print_help(void);
243 245
246unsigned char *mac_aton(const char *);
247void print_hardware_address(const unsigned char *);
244int get_hardware_address(int,char *); 248int get_hardware_address(int,char *);
245int get_ip_address(int,char *); 249int get_ip_address(int,char *);
246 250
@@ -279,7 +283,10 @@ int main(int argc, char **argv){
279 dhcp_socket=create_dhcp_socket(); 283 dhcp_socket=create_dhcp_socket();
280 284
281 /* get hardware address of client machine */ 285 /* get hardware address of client machine */
282 get_hardware_address(dhcp_socket,network_interface_name); 286 if(user_specified_mac!=NULL)
287 memcpy(client_hardware_address,user_specified_mac,6);
288 else
289 get_hardware_address(dhcp_socket,network_interface_name);
283 290
284 if(unicast) /* get IP address of client machine */ 291 if(unicast) /* get IP address of client machine */
285 get_ip_address(dhcp_socket,network_interface_name); 292 get_ip_address(dhcp_socket,network_interface_name);
@@ -308,8 +315,6 @@ int main(int argc, char **argv){
308/* determines hardware address on client machine */ 315/* determines hardware address on client machine */
309int get_hardware_address(int sock,char *interface_name){ 316int get_hardware_address(int sock,char *interface_name){
310 317
311 int i;
312
313#if defined(__linux__) 318#if defined(__linux__)
314 struct ifreq ifr; 319 struct ifreq ifr;
315 320
@@ -404,16 +409,12 @@ int get_hardware_address(int sock,char *interface_name){
404 /* Kompf 2000-2003 */ 409 /* Kompf 2000-2003 */
405 410
406#else 411#else
407 printf(_("Error: can't get MAC address for this architecture.\n")); 412 printf(_("Error: can't get MAC address for this architecture. Use the --mac option.\n"));
408 exit(STATE_UNKNOWN); 413 exit(STATE_UNKNOWN);
409#endif 414#endif
410 415
411 if(verbose){ 416 if(verbose)
412 printf(_("Hardware address: ")); 417 print_hardware_address(client_hardware_address);
413 for (i=0; i<6; ++i)
414 printf("%2.2x", client_hardware_address[i]);
415 printf( "\n");
416 }
417 418
418 return OK; 419 return OK;
419 } 420 }
@@ -1097,6 +1098,7 @@ int call_getopt(int argc, char **argv){
1097 {"requestedip", required_argument,0,'r'}, 1098 {"requestedip", required_argument,0,'r'},
1098 {"timeout", required_argument,0,'t'}, 1099 {"timeout", required_argument,0,'t'},
1099 {"interface", required_argument,0,'i'}, 1100 {"interface", required_argument,0,'i'},
1101 {"mac", required_argument,0,'m'},
1100 {"unicast", no_argument, 0,'u'}, 1102 {"unicast", no_argument, 0,'u'},
1101 {"verbose", no_argument, 0,'v'}, 1103 {"verbose", no_argument, 0,'v'},
1102 {"version", no_argument, 0,'V'}, 1104 {"version", no_argument, 0,'V'},
@@ -1105,7 +1107,7 @@ int call_getopt(int argc, char **argv){
1105 }; 1107 };
1106 1108
1107 while(1){ 1109 while(1){
1108 c=getopt_long(argc,argv,"+hVvt:s:r:t:i:u",long_options,&option_index); 1110 c=getopt_long(argc,argv,"+hVvt:s:r:t:i:m:u",long_options,&option_index);
1109 1111
1110 i++; 1112 i++;
1111 1113
@@ -1162,6 +1164,15 @@ int call_getopt(int argc, char **argv){
1162 */ 1164 */
1163 break; 1165 break;
1164 1166
1167 case 'm': /* MAC address */
1168
1169 if((user_specified_mac=mac_aton(optarg)) == NULL)
1170 usage("Cannot parse MAC address.\n");
1171 if(verbose)
1172 print_hardware_address(user_specified_mac);
1173
1174 break;
1175
1165 case 'i': /* interface name */ 1176 case 'i': /* interface name */
1166 1177
1167 strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1); 1178 strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
@@ -1341,6 +1352,39 @@ long mac_addr_dlpi( const char *dev, int unit, u_char *addr){
1341#endif 1352#endif
1342 1353
1343 1354
1355/* parse MAC address string, return 6 bytes (unterminated) or NULL */
1356unsigned char *mac_aton(const char *string){
1357 static unsigned char result[6];
1358 char tmp[3];
1359 unsigned i, j;
1360
1361 for(i=0, j=0; string[i] != '\0' && j < sizeof(result); i++){
1362 /* ignore ':' and any other non-hex character */
1363 if(!isxdigit(string[i]) || !isxdigit(string[i+1]))
1364 continue;
1365 tmp[0]=string[i];
1366 tmp[1]=string[i+1];
1367 tmp[2]='\0';
1368 result[j]=strtol(tmp,(char **)NULL,16);
1369 i++;
1370 j++;
1371 }
1372
1373 return (j==6) ? result : NULL;
1374 }
1375
1376
1377void print_hardware_address(const unsigned char *address){
1378 int i;
1379
1380 printf(_("Hardware address: "));
1381 for (i=0; i<5; i++)
1382 printf("%2.2x:", address[i]);
1383 printf("%2.2x", address[i]);
1384 putchar('\n');
1385 }
1386
1387
1344/* print usage help */ 1388/* print usage help */
1345void print_help(void){ 1389void print_help(void){
1346 1390
@@ -1367,6 +1411,8 @@ void print_help(void){
1367 printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs")); 1411 printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs"));
1368 printf (" %s\n", "-i, --interface=STRING"); 1412 printf (" %s\n", "-i, --interface=STRING");
1369 printf (" %s\n", _("Interface to to use for listening (i.e. eth0)")); 1413 printf (" %s\n", _("Interface to to use for listening (i.e. eth0)"));
1414 printf (" %s\n", "-m, --mac=STRING");
1415 printf (" %s\n", _("MAC address to use in the DHCP request"));
1370 printf (" %s\n", "-u, --unicast"); 1416 printf (" %s\n", "-u, --unicast");
1371 printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s")); 1417 printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s"));
1372 1418
@@ -1379,7 +1425,7 @@ print_usage(void){
1379 1425
1380 printf (_("Usage:")); 1426 printf (_("Usage:"));
1381 printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname); 1427 printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname);
1382 printf (" [-i interface]\n"); 1428 printf (" [-i interface] [-m mac]\n");
1383 1429
1384 return; 1430 return;
1385 } 1431 }