diff --git a/README.txt b/README.txt index f6f97cd..2a6c008 100644 --- a/README.txt +++ b/README.txt @@ -29,6 +29,7 @@ dhtest version 1.1 supports the following 2. Support of bind timout with -k flag on command line +3. Added DHCP option 51 - Requested lease time from server Authour: Saravanakumar.G Send your comments, enhancements and bugs to saravana815@gmail.com diff --git a/dhtest.c b/dhtest.c index 79552a8..a1f9471 100644 --- a/dhtest.c +++ b/dhtest.c @@ -45,6 +45,7 @@ u_int32_t dhcp_xid = 0; u_int16_t bcast_flag = 0; /* DHCP broadcast flag */ u_int8_t vci_buff[256] = { 0 }; /* VCI buffer*/ u_int16_t vci_flag = 0; +u_int32_t option51_lease_time = 0; /* Pointers for all layer data structures */ struct ethernet_hdr *eth_hg = { 0 }; @@ -77,9 +78,10 @@ void print_help(char *cmd) { fprintf(stdout, "Usage: %s [ options ] -m mac_address\n", cmd); fprintf(stdout, " -r, --release\t\t\t\t# Releases obtained DHCP IP for corresponding MAC\n"); + fprintf(stdout, " -L, --option51-lease_time [ Lease_time ] # Option 51. Requested lease time in secondes\n"); fprintf(stdout, " -I, --option50-ip\t[ IP_address ]\t# Option 50 IP address on DHCP discover\n"); fprintf(stdout, " -o, --option60-vci\t[ VCI_string ]\t# Vendor Class Idendifier string\n"); - fprintf(stdout, " -v, --vlan\t\t[ vlan_id ]\t# VLAN ID. Range(2 - 4094)\n"); + fprintf(stdout, " -v, --vlan\t\t[ vlan_id ]\t# VLAN ID. Range(1 - 4094)\n"); /* fprintf(stdout, " -x, --dhcp_xid\t[ dhcp_xid ]\n"); */ fprintf(stdout, " -t, --tos\t\t[ TOS_value ]\t# IP header TOS value\n"); fprintf(stdout, " -i, --interface\t[ interface ]\t# Interface to use. Default eth0\n"); @@ -108,6 +110,7 @@ int main(int argc, char *argv[]) { "vlan", required_argument, 0, 'v' }, { "dhcp_xid", required_argument, 0, 'x' }, { "tos", required_argument, 0, 't' }, + { "option51-lease_time", required_argument, 0, 'L' }, { "option50-ip", required_argument, 0, 'I' }, { "option60-vci", required_argument, 0, 'o' }, { "timeout", required_argument, 0, 'T' }, @@ -121,7 +124,7 @@ int main(int argc, char *argv[]) /*getopt routine to get command line arguments*/ while(get_tmp < argc) { - get_cmd = getopt_long(argc, argv, "m:i:v:t:bfVrT:I:o:k:",\ + get_cmd = getopt_long(argc, argv, "m:i:v:t:bfVrT:I:o:k:L:",\ long_options, &option_index); if(get_cmd == -1 ) { break; @@ -148,9 +151,9 @@ int main(int argc, char *argv[]) break; case 'v': - if(atoi(optarg) <= 1 || atoi(optarg) >= 4095) + if(atoi(optarg) < 1 || atoi(optarg) > 4095) { - fprintf(stdout, "VLAN ID is not valid. Range 2 to 4094\n"); + fprintf(stdout, "VLAN ID is not valid. Range 1 to 4095\n"); exit(1); } vlan = atoi(optarg); @@ -181,7 +184,11 @@ int main(int argc, char *argv[]) } l3_tos = atoi(optarg); break; - + + case 'L': + option51_lease_time = atoi(optarg); + break; + case 'I': option50_ip = inet_addr(optarg); break; @@ -260,6 +267,9 @@ int main(int argc, char *argv[]) if(option50_ip) { build_option50(); /* Option50 - req. IP */ } + if(option51_lease_time) { + build_option51(); /* Option51 - DHCP lease time requested */ + } if(vci_flag == 1) { build_option60_vci(); /* Option60 - VCI */ @@ -302,6 +312,9 @@ int main(int argc, char *argv[]) if(vci_flag == 1) { build_option60_vci(); } + if(option51_lease_time) { + build_option51(); /* Option51 - DHCP lease time requested */ + } build_option55(); build_optioneof(); build_dhpacket(DHCP_MSGREQUEST); /* Builds specified packet */ diff --git a/functions.c b/functions.c index 0969f4f..67c773f 100644 --- a/functions.c +++ b/functions.c @@ -37,6 +37,7 @@ extern u_int32_t dhcp_xid; extern u_int32_t bcast_flag; extern u_int8_t timeout; extern u_int8_t vci_buff[256]; +extern u_int32_t option51_lease_time; extern struct ethernet_hdr *eth_hg; extern struct vlan_hdr *vlan_hg; @@ -459,6 +460,21 @@ int build_option50() return 0; } +/* + * Builds DHCP option51 on dhopt_buff - DHCP lease time requested + */ +int build_option51() +{ + u_int8_t msgtype = DHCP_LEASETIME; + u_int8_t msglen = 4; + u_int32_t msg = htonl(option51_lease_time); + + memcpy((dhopt_buff + dhopt_size), &msgtype, 1); + memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1); + memcpy((dhopt_buff + dhopt_size + 2), &msg, 4); + dhopt_size = dhopt_size + 6; + return 0; +} /* * Builds DHCP option54 on dhopt_buff */ diff --git a/headers.h b/headers.h index bfc7fa3..958f261 100644 --- a/headers.h +++ b/headers.h @@ -24,6 +24,7 @@ int build_option53(int msg_type); /* Option53: MSGTYPE. Builds option53*/ int build_option55(); /* Requested parameters list */ int build_option54(); /* Builds server identifier on DHCP request */ int build_option50(); /* Option50: Rqstd IP. Builds option50*/ +int build_option51(); /* Option51: Rqstd lease time. Builds option51*/ int build_option60_vci(); /* Vendor class identifier */ int build_optioneof(); /* End of option */