diff --git a/include/n2n.h b/include/n2n.h index c725360..cd6fa1a 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -211,6 +211,7 @@ void hexdump (const uint8_t * buf, size_t len); void print_n2n_version (); int is_empty_ip_address (const n2n_sock_t * sock); void print_edge_stats (const n2n_edge_t *eee); +int memrnd (uint8_t *address, size_t len); /* Sockets */ char* sock_to_cstr (n2n_sock_str_t out, diff --git a/src/edge_utils.c b/src/edge_utils.c index 2c379a5..8c62d2b 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -367,9 +367,7 @@ n2n_edge_t* edge_init (const n2n_edge_conf_t *conf, int *rv) { // setup authenitcation scheme eee->conf.auth.scheme = n2n_auth_simple_id; - for(idx = 0; idx < N2N_AUTH_TOKEN_SIZE; ++idx) { - eee->conf.auth.token[idx] = n2n_rand() % 0xff; - } + memrnd(eee->conf.auth.token, N2N_AUTH_TOKEN_SIZE); eee->conf.auth.toksize = sizeof(eee->conf.auth.token); // first time calling edge_init_sockets needs -1 in the sockets for it does throw an error @@ -1059,9 +1057,7 @@ void send_register_super (n2n_edge_t *eee) { cmn.flags = 0; memcpy(cmn.community, eee->conf.community_name, N2N_COMMUNITY_SIZE); - for(idx = 0; idx < N2N_COOKIE_SIZE; ++idx) { - eee->curr_sn->last_cookie[idx] = n2n_rand() % 0xff; - } + memrnd(eee->curr_sn->last_cookie, N2N_COOKIE_SIZE); memcpy(reg.cookie, eee->curr_sn->last_cookie, N2N_COOKIE_SIZE); reg.dev_addr.net_addr = ntohl(eee->device.ip_addr); diff --git a/src/n2n.c b/src/n2n.c index 24eacd7..54616b8 100644 --- a/src/n2n.c +++ b/src/n2n.c @@ -610,6 +610,24 @@ int sock_equal (const n2n_sock_t * a, /* *********************************************** */ +// fills a specified memory area with random numbers +int memrnd (uint8_t *address, size_t len) { + + for(; len >= 8; len -= 8) { + *(uint64_t*)address = n2n_rand(); + address += 8; + } + + for(; len > 0; len--) { + *address = n2n_rand(); + address++; + } + + return 0; +} + +/* *********************************************** */ + #if defined(WIN32) int gettimeofday (struct timeval *tp, void *tzp) { diff --git a/src/sn_utils.c b/src/sn_utils.c index 2aab48e..d3920e7 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -405,15 +405,11 @@ int sn_init(n2n_sn_t *sss) { /* Random auth token */ sss->auth.scheme = n2n_auth_simple_id; - for(idx = 0; idx < N2N_AUTH_TOKEN_SIZE; ++idx) { - sss->auth.token[idx] = n2n_rand() % 0xff; - } + memrnd(sss->auth.token, N2N_AUTH_TOKEN_SIZE); sss->auth.toksize = sizeof(sss->auth.token); /* Random MAC address */ - for(i = 0; i < 6; i++) { - sss->mac_addr[i] = n2n_rand(); - } + memrnd(sss->mac_addr, N2N_MAC_SIZE); sss->mac_addr[0] &= ~0x01; /* Clear multicast bit */ sss->mac_addr[0] |= 0x02; /* Set locally-assigned bit */ @@ -890,12 +886,10 @@ static int re_register_and_purge_supernodes (n2n_sn_t *sss, struct sn_community cmn.flags = N2N_FLAGS_FROM_SUPERNODE; memcpy(cmn.community, comm->community, N2N_COMMUNITY_SIZE); - for(idx = 0; idx < N2N_COOKIE_SIZE; ++idx) { - cookie[idx] = n2n_rand() % 0xff; - } - + memrnd(cookie, N2N_COOKIE_SIZE); memcpy(reg.cookie, cookie, N2N_COOKIE_SIZE); memcpy(peer->last_cookie, cookie, N2N_COOKIE_SIZE); + reg.dev_addr.net_addr = ntohl(peer->dev_addr.net_addr); reg.dev_addr.net_bitlen = mask2bitlen(ntohl(peer->dev_addr.net_bitlen)); get_local_auth(sss, &(reg.auth)); diff --git a/src/tuntap_linux.c b/src/tuntap_linux.c index e50e040..c9faaf3 100644 --- a/src/tuntap_linux.c +++ b/src/tuntap_linux.c @@ -150,8 +150,7 @@ int tuntap_open (tuntap_dev *device, // also after the TAP interface UP status has been notified int i; - for(i = 0; i < 6; i++) - device->mac_addr[i] = n2n_rand(); + memrnd(device->mac_addr, N2N_MAC_SIZE); // clear multicast bit device->mac_addr[0] &= ~0x01;