add --random-mac option

changes/54/3254/1
Richard Fuchs 12 years ago
parent 755071b47e
commit 412ee1d691

@ -70,7 +70,8 @@ u_int32_t listen_timeout = 3600;
void print_help(char *cmd)
{
fprintf(stdout, "Usage: %s [ options ]\n", cmd);
fprintf(stdout, " -m mac_address\n");
fprintf(stdout, " -m MAC_address\n");
fprintf(stdout, " -R, --random-mac\t\t\tUse a randomly generated MAC address\n");
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");
@ -107,9 +108,12 @@ int main(int argc, char *argv[])
exit(2);
}
init_rand();
int option_index = 0;
static struct option long_options[] = {
{ "mac", required_argument, 0, 'm' },
{ "random-mac", no_argument, 0, 'R' },
{ "interface", required_argument, 0, 'i' },
{ "vlan", required_argument, 0, 'v' },
{ "dhcp_xid", required_argument, 0, 'x' },
@ -138,7 +142,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:bfVrpansu::T:P:g:S:I:o:k:L:h:d:",\
get_cmd = getopt_long(argc, argv, "m:Ri:v:t:bfVrpansu::T:P:g:S:I:o:k:L:h:d:",\
long_options, &option_index);
if(get_cmd == -1 ) {
break;
@ -162,6 +166,21 @@ int main(int argc, char *argv[])
}
break;
case 'R':
{
int i;
for (i = 0; i < ETHER_ADDR_LEN; i++)
dhmac[i] = rand() & 0xff;
/* clear multicast bit, set the L bit, clear MSB */
dhmac[0] &= ~0x81;
dhmac[0] |= 0x02;
dhmac_flag = 1;
}
break;
case 'i':
iface_name = optarg;
break;

@ -313,15 +313,17 @@ int reset_dhopt_size()
return 0;
}
void init_rand() {
srand(time(NULL) ^ (getpid() << 16));
}
/*
* Sets a random DHCP xid
*/
int set_rand_dhcp_xid()
{
if(dhcp_xid == 0) {
srand(time(NULL) ^ (getpid() << 16));
dhcp_xid = rand() % 0xffffffff;
}
if(dhcp_xid == 0)
dhcp_xid = (rand() % 0xfffffff0) + 1;
return 0;
}

@ -20,6 +20,7 @@ int send_packet(int pkt_type); /* Sends DHCP packet socket*/
int recv_packet(int pkt_type); /* Receives DHCP packet on socket*/
int reset_dhopt_size(); /* Resets the dhopt_size to zero */
void init_rand();
int set_rand_dhcp_xid(); /* Sets a random DHCP xid */
int build_option53(int msg_type); /* Option53: MSGTYPE. Builds option53*/
int build_option55(); /* Requested parameters list */

Loading…
Cancel
Save