summaryrefslogtreecommitdiff
path: root/staging/modifications.c
diff options
context:
space:
mode:
authorMatteo Croce <mcroce@redhat.com>2018-08-21 16:38:09 +0200
committerTobias Klauser <tklauser@distanz.ch>2018-08-22 10:21:37 +0200
commitc7c176394ad2b87f56b0aed2731eb9dde29750f0 (patch)
tree1c423f98bc7ac52feb134044efeed38ccc6d0507 /staging/modifications.c
parent01f339a7bb170310f9dd6a389d9c89b6c62032d0 (diff)
mausezahn: improve random mac address generation
Modify -b option to generate all random MAC addresses. Improve the random generation algorithm, use nrand48() which fills the ethernet address in two calls instead of six calls to rand() and six floating point calculations. Set the locally administered bit of generated MAC addresses. Signed-off-by: Matteo Croce <mcroce@redhat.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'staging/modifications.c')
-rw-r--r--staging/modifications.c39
1 files changed, 11 insertions, 28 deletions
diff --git a/staging/modifications.c b/staging/modifications.c
index 1affbbb..b105db0 100644
--- a/staging/modifications.c
+++ b/staging/modifications.c
@@ -24,7 +24,7 @@
// This sections contains functions to manipulate headers of
// Eth, MPLS, 802.1Q, IP, UDP, and TCP:
//
-// int update_Eth_SA (libnet_t *l, libnet_ptag_t t)
+// int rand_addr (u_int8_t *addr)
// int update_IP_SA (libnet_t *l, libnet_ptag_t t)
// int update_IP_DA (libnet_t *l, libnet_ptag_t t)
// int update_IP6_SA (libnet_t *l, libnet_ptag_t t)
@@ -42,36 +42,19 @@
#include "mz.h"
#include "mops.h"
-///////////////////////////////////////////////////////////////////////////
-// Applies another random Ethernet source address to a given Ethernet-PTAG.
-// (The calling function should check 'tx.eth_src_rand' whether the SA
+// Applies another random address to a given buffer.
+// (The calling function should check 'tx.eth_(dst|src)_rand' whether the address
// should be randomized.)
-//
-int update_Eth_SA(libnet_t *l, libnet_ptag_t t)
+void rand_addr(u_int8_t *addr)
{
- tx.eth_src[0] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256) & 0xFE; // keeps bcast-bit zero
- tx.eth_src[1] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
- tx.eth_src[2] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
- tx.eth_src[3] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
- tx.eth_src[4] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
- tx.eth_src[5] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
-
- t = libnet_build_ethernet (tx.eth_dst,
- tx.eth_src,
- tx.eth_type,
- NULL, // the payload
- 0,
- l,
- t);
+ nrand48((unsigned short *)addr);
+ nrand48((unsigned short *)(addr + 3));
- if (t == -1)
- {
- fprintf(stderr, " mz/update_Eth_SA: Can't build Ethernet header: %s\n",
- libnet_geterror(l));
- exit(EXIT_FAILURE);
- }
-
- return 0;
+ // Get rid of multicast addresses
+ addr[0] &= 0xfe;
+
+ // Set the locally administered bit
+ addr[0] |= 2;
}