From a61ebf552f71d8639b1a7584913c60fd49b8de90 Mon Sep 17 00:00:00 2001 From: Oscar Weiner-Wong Date: Sun, 15 Jan 2012 21:12:36 +0100 Subject: [PATCH] Outsource creation of random MAC address to a new function create_random_mac. --- src/layer2.c | 2 +- src/mz.h | 4 ++++ src/tools.c | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/layer2.c b/src/layer2.c index ebbc7d8..0c0f177 100644 --- a/src/layer2.c +++ b/src/layer2.c @@ -206,7 +206,7 @@ int send_arp () if ( (getarg(tx.arg_string,"sendermac", argval)==1) || (getarg(tx.arg_string,"smac", argval)==1) ) { - //TODO: Allow 'rand' as sendermac + //TODO: Allow 'rand' as sendermac str2hex(argval,sendermac,6); } else diff --git a/src/mz.h b/src/mz.h index 083cffb..91b7ea0 100644 --- a/src/mz.h +++ b/src/mz.h @@ -552,6 +552,10 @@ int create_syslog_packet(); // // ************************************ +// Sets eth_mac to a random unicast Ethernet MAC address. +// Usage: create_random_mac(tx.eth_src); +void create_random_mac(u_int8_t eth_mac[6]); + // Converts MAC address specified in str into u_int8_t array // Usage: str2hex_mac ( "00:01:02:aa:ff:ee", src_addr ) int str2hex_mac (char* str, u_int8_t *addr); diff --git a/src/tools.c b/src/tools.c index d95317c..c69a5fe 100644 --- a/src/tools.c +++ b/src/tools.c @@ -49,6 +49,7 @@ // mz_tok .............. Decomposes a string into tokens and maps them to args // Example usage: IPv6-addresses, user input for MPLS-tags // delay_parse ......... Parses one or two strings for a delay specification and sets a struct timespec +// create_random_mac ... Creates a random unicast Ethernet MAC address // //////////////////////////////////////////////////////////////////////////////////////////// @@ -1340,3 +1341,24 @@ int delay_parse (struct timespec *t, char *a, char *b) return 0; } +// PURPOSE: Create random MAC address. +// Create a random MAC address and store it in the array +// passed in the function argument. +// +// NOTE: The MAC address returned is a unicast address. +// Unicast addresses have the LSB always set to zero. +// At least that's what thay say on the Internet.. ;-) +// +// RETURN VALUE: none +// ToDo: This function should have a return value. +// +void create_random_mac(u_int8_t eth_mac[6]) +{ + printf("I'm right here!\n"); + int i; + for (i = 0; i < 6; i++) + { + tx.eth_src[i] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); + } + tx.eth_src[0] &= 0xFE; // keeps b-cast bit zero +} -- 2.11.4.GIT