added memrnd() (#685)

This commit is contained in:
Logan oos Even 2021-04-19 00:47:37 +05:45 committed by GitHub
parent cfa92d28d8
commit 667b7df083
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 18 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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) {

View File

@ -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));

View File

@ -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;