cleaned up mac type usage (#583)

This commit is contained in:
Logan oos Even 2021-01-13 14:50:26 +05:45 committed by GitHub
parent 7d918c4da2
commit 7eeca3d78f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 49 deletions

View File

@ -1,5 +1,5 @@
/**
* (C) 2007-20 - ntop.org and contributors
* (C) 2007-21 - ntop.org and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -193,7 +193,9 @@ uint8_t mask2bitlen (uint32_t mask);
char* macaddr_str (macstr_t buf, const n2n_mac_t mac);
int str2mac (uint8_t * outmac /* 6 bytes */, const char * s);
int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn);
uint8_t is_multi_broadcast (const uint8_t * dest_mac);
uint8_t is_multi_broadcast (const n2n_mac_t dest_mac);
uint8_t is_broadcast (const n2n_mac_t dest_mac);
uint8_t is_null_mac (const n2n_mac_t dest_mac);
char* msg_type2str (uint16_t msg_type);
void hexdump (const uint8_t * buf, size_t len);
void print_n2n_version ();
@ -248,7 +250,7 @@ int comm_init (struct sn_community *comm, char *cmn);
int sn_init (n2n_sn_t *sss);
void sn_term (n2n_sn_t *sss);
int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn);
struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n_sock_t *sock, n2n_mac_t *mac, int *skip_add);
struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n_sock_t *sock, const n2n_mac_t mac, int *skip_add);
int run_sn_loop (n2n_sn_t *sss, int *keep_running);
int assign_one_ip_subnet (n2n_sn_t *sss, struct sn_community *comm);
const char* compression_str (uint8_t cmpr);

View File

@ -1,5 +1,5 @@
/**
* (C) 2007-20 - ntop.org and contributors
* (C) 2007-21 - ntop.org and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,6 +27,13 @@ typedef uint8_t n2n_cookie_t[N2N_COOKIE_SIZE];
typedef uint8_t n2n_desc_t[N2N_DESC_SIZE];
typedef char n2n_sock_str_t[N2N_SOCKBUF_SIZE]; /* tracing string buffer */
// those are definitely not typedefs (with a view to the filename) but neither are they defines
static const n2n_mac_t broadcast_mac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static const n2n_mac_t multicast_mac = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 }; /* First 3 bytes are meaningful */
static const n2n_mac_t ipv6_multicast_mac = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; /* First 2 bytes are meaningful */
static const n2n_mac_t null_mac = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
#if defined(_MSC_VER) || defined(__MINGW32__)
#include "getopt.h"

View File

@ -1,5 +1,5 @@
/**
* (C) 2007-20 - ntop.org and contributors
* (C) 2007-21 - ntop.org and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -86,7 +86,7 @@ int encode_mac (uint8_t * base,
size_t * idx,
const n2n_mac_t m);
int decode_mac (uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passing a n2n_mac_t */
int decode_mac (n2n_mac_t out,
const uint8_t * base,
size_t * rem,
size_t * idx);

View File

@ -645,8 +645,6 @@ int is_empty_ip_address (const n2n_sock_t * sock) {
/* ************************************** */
static const n2n_mac_t broadcast_mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
static const n2n_mac_t null_mac = {0, 0, 0, 0, 0, 0};
/** Check if a known peer socket has changed and possibly register again.
*/
@ -765,7 +763,7 @@ static void check_join_multicast_group (n2n_edge_t *eee) {
/** Send a QUERY_PEER packet to the current supernode. */
static void send_query_peer (n2n_edge_t * eee,
const n2n_mac_t dstMac) {
const n2n_mac_t dst_mac) {
uint8_t pktbuf[N2N_PKT_BUF_SIZE];
size_t idx;
@ -783,12 +781,12 @@ static void send_query_peer (n2n_edge_t * eee,
encode_mac(query.srcMac, &idx, eee->device.mac_addr);
idx = 0;
encode_mac(query.targetMac, &idx, dstMac);
encode_mac(query.targetMac, &idx, dst_mac);
idx = 0;
encode_QUERY_PEER(pktbuf, &idx, &cmn, &query);
if(memcmp(dstMac, null_mac, sizeof(n2n_mac_t)) != 0) {
if(is_null_mac(dst_mac)) {
traceEvent(TRACE_DEBUG, "send QUERY_PEER to supernode");
@ -1320,7 +1318,7 @@ static char *get_ip_from_arp (dec_ip_str_t buf, const n2n_mac_t req_mac) {
strncpy(buf, "0.0.0.0", N2N_NETMASK_STR_SIZE - 1);
if(0 == memcmp(null_mac, req_mac, sizeof(n2n_mac_t))) {
if(is_null_mac(req_mac)) {
traceEvent(TRACE_DEBUG, "MAC address is null.");
return buf;
}
@ -2022,7 +2020,7 @@ void readFromIPSocket (n2n_edge_t * eee, int in_sock) {
if(is_valid_peer_sock(&reg.sock))
orig_sender = &(reg.sock);
via_multicast = !memcmp(reg.dstMac, null_mac, N2N_MAC_SIZE);
via_multicast = is_null_mac(reg.dstMac);
if(via_multicast && !memcmp(reg.srcMac, eee->device.mac_addr, N2N_MAC_SIZE)) {
traceEvent(TRACE_DEBUG, "Skipping REGISTER from self");
@ -2130,7 +2128,7 @@ void readFromIPSocket (n2n_edge_t * eee, int in_sock) {
for(i = 0; i < ra.num_sn; i++) {
skip_add = SN_ADD;
sn = add_sn_to_list_by_mac_or_sock(&(eee->conf.supernodes), &(payload->sock), &(payload->mac), &skip_add);
sn = add_sn_to_list_by_mac_or_sock(&(eee->conf.supernodes), &(payload->sock), payload->mac, &skip_add);
if(skip_add == SN_ADD_ADDED) {
sn->ip_addr = calloc(1,N2N_EDGE_SN_HOST_SIZE);
@ -2198,7 +2196,7 @@ void readFromIPSocket (n2n_edge_t * eee, int in_sock) {
decode_REGISTER_SUPER_NAK(&nak, &cmn, udp_buf, &rem, &idx);
traceEvent(TRACE_INFO, "Rx REGISTER_SUPER_NAK");
if((memcmp(&(nak.srcMac), &(eee->device.mac_addr), sizeof(n2n_mac_t))) == 0) {
if((memcmp(nak.srcMac, eee->device.mac_addr, sizeof(n2n_mac_t))) == 0) {
traceEvent(TRACE_ERROR, "%s is already used. Stopping the program.", macaddr_str(mac_buf1, nak.srcMac));
exit(1);
} else {
@ -2237,9 +2235,9 @@ void readFromIPSocket (n2n_edge_t * eee, int in_sock) {
break;
}
if(memcmp(pi.mac, null_mac, sizeof(n2n_mac_t)) == 0) {
if(is_null_mac(pi.mac)) {
skip_add = SN_ADD_SKIP;
scan = add_sn_to_list_by_mac_or_sock(&(eee->conf.supernodes), &sender, &pi.srcMac, &skip_add);
scan = add_sn_to_list_by_mac_or_sock(&(eee->conf.supernodes), &sender, pi.srcMac, &skip_add);
if(scan != NULL) {
scan->last_seen = now;
/* The data type depends on the actual selection strategy that has been chosen. */
@ -2994,7 +2992,7 @@ int edge_conf_add_supernode (n2n_edge_conf_t *conf, const char *ip_and_port) {
}
skip_add = SN_ADD;
sn = add_sn_to_list_by_mac_or_sock(&(conf->supernodes), sock, (n2n_mac_t *)null_mac, &skip_add);
sn = add_sn_to_list_by_mac_or_sock(&(conf->supernodes), sock, null_mac, &skip_add);
if(sn != NULL) {
sn->ip_addr = calloc(1,N2N_EDGE_SN_HOST_SIZE);
@ -3002,7 +3000,7 @@ int edge_conf_add_supernode (n2n_edge_conf_t *conf, const char *ip_and_port) {
if(sn->ip_addr != NULL) {
strncpy(sn->ip_addr, ip_and_port, N2N_EDGE_SN_HOST_SIZE - 1);
memcpy(&(sn->sock), sock, sizeof(n2n_sock_t));
memcpy(&(sn->mac_addr), null_mac, sizeof(n2n_mac_t));
memcpy(sn->mac_addr, null_mac, sizeof(n2n_mac_t));
sn->purgeable = SN_UNPURGEABLE;
sn->last_valid_time_stamp = initial_time_stamp();
}

View File

@ -25,10 +25,6 @@
#include <assert.h>
static const uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static const uint8_t multicast_addr[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 }; /* First 3 bytes are meaningful */
static const uint8_t ipv6_multicast_addr[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; /* First 2 bytes are meaningful */
static const n2n_mac_t null_mac = {0, 0, 0, 0, 0, 0};
/* ************************************** */
@ -321,11 +317,11 @@ int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn) {
/* ************************************** */
struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n_sock_t *sock, n2n_mac_t *mac, int *skip_add) {
struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n_sock_t *sock, const n2n_mac_t mac, int *skip_add) {
struct peer_info *scan, *tmp, *peer = NULL;
if(memcmp(mac, null_mac, sizeof(n2n_mac_t)) != 0) { /* not zero MAC */
if(!is_null_mac(mac)) { /* not zero MAC */
HASH_FIND_PEER(*sn_list, mac, peer);
}
@ -333,7 +329,7 @@ struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n
HASH_ITER(hh, *sn_list, scan, tmp) {
if(memcmp(&(scan->sock), sock, sizeof(n2n_sock_t)) == 0) {
HASH_DEL(*sn_list, scan);
memcpy(&(scan->mac_addr), mac, sizeof(n2n_mac_t));
memcpy(scan->mac_addr, mac, sizeof(n2n_mac_t));
HASH_ADD_PEER(*sn_list, scan);
peer = scan;
break;
@ -345,7 +341,7 @@ struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n
if(peer) {
sn_selection_criterion_default(&(peer->selection_criterion));
memcpy(&(peer->sock), sock, sizeof(n2n_sock_t));
memcpy(&(peer->mac_addr), mac, sizeof(n2n_mac_t));
memcpy(peer->mac_addr, mac, sizeof(n2n_mac_t));
HASH_ADD_PEER(*sn_list, peer);
*skip_add = SN_ADD_ADDED;
}
@ -357,16 +353,33 @@ struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n
/* ************************************************ */
uint8_t is_multi_broadcast (const uint8_t * dest_mac) {
int is_broadcast = (memcmp(broadcast_addr, dest_mac, 6) == 0);
int is_multicast = (memcmp(multicast_addr, dest_mac, 3) == 0);
int is_ipv6_multicast = (memcmp(ipv6_multicast_addr, dest_mac, 2) == 0);
/* http://www.faqs.org/rfcs/rfc908.html */
uint8_t is_multi_broadcast (const n2n_mac_t dest_mac) {
int is_broadcast = (memcmp(broadcast_mac, dest_mac, N2N_MAC_SIZE) == 0);
//REVISIT: multicast has bit #24 reset, test!
int is_multicast = (memcmp(multicast_mac, dest_mac, 3) == 0);
int is_ipv6_multicast = (memcmp(ipv6_multicast_mac, dest_mac, 2) == 0);
return is_broadcast || is_multicast || is_ipv6_multicast;
}
/* http://www.faqs.org/rfcs/rfc908.html */
uint8_t is_broadcast (const n2n_mac_t dest_mac) {
int is_broadcast = (memcmp(broadcast_mac, dest_mac, N2N_MAC_SIZE) == 0);
return is_broadcast;
}
uint8_t is_null_mac (const n2n_mac_t dest_mac) {
int is_null_mac = (memcmp(null_mac, dest_mac, N2N_MAC_SIZE) == 0);
return is_null_mac;
}
/* *********************************************** */

View File

@ -24,7 +24,6 @@
#define HASH_FIND_COMMUNITY(head, name, out) HASH_FIND_STR(head, name, out)
static n2n_sn_t sss_node;
static const n2n_mac_t null_mac = {0, 0, 0, 0, 0, 0};
/** Load the list of allowed communities. Existing/previous ones will be removed
*
@ -285,14 +284,14 @@ static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) {
if(sss->federation != NULL) {
skip_add = SN_ADD;
anchor_sn = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), socket, (n2n_mac_t*) null_mac, &skip_add);
anchor_sn = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), socket, null_mac, &skip_add);
if(anchor_sn != NULL) {
anchor_sn->ip_addr = calloc(1, N2N_EDGE_SN_HOST_SIZE);
if(anchor_sn->ip_addr) {
strncpy(anchor_sn->ip_addr, _optarg, N2N_EDGE_SN_HOST_SIZE - 1);
memcpy(&(anchor_sn->sock), socket, sizeof(n2n_sock_t));
memcpy(&(anchor_sn->mac_addr), null_mac, sizeof(n2n_mac_t));
memcpy(anchor_sn->mac_addr, null_mac, sizeof(n2n_mac_t));
anchor_sn->purgeable = SN_UNPURGEABLE;
anchor_sn->last_valid_time_stamp = initial_time_stamp();

View File

@ -75,7 +75,6 @@ static int process_udp (n2n_sn_t *sss,
size_t udp_size,
time_t now);
static const n2n_mac_t null_mac = {0, 0, 0, 0, 0, 0};
/* ************************************** */
@ -393,7 +392,7 @@ static int update_edge (n2n_sn_t *sss,
if(iter->dev_addr.net_addr == reg->dev_addr.net_addr) {
scan = iter;
HASH_DEL(comm->edges, scan);
memcpy(&(scan->mac_addr), reg->edgeMac, sizeof(n2n_mac_t));
memcpy(scan->mac_addr, reg->edgeMac, sizeof(n2n_mac_t));
HASH_ADD_PEER(comm->edges, scan);
break;
}
@ -908,7 +907,6 @@ static int process_udp (n2n_sn_t * sss,
char buf[32];
struct sn_community *comm, *tmp;
uint64_t stamp;
const n2n_mac_t null_mac = {0, 0, 0, 0, 0, 0}; /* 00:00:00:00:00:00 */
traceEvent(TRACE_DEBUG, "Processing incoming UDP packet [len: %lu][sender: %s:%u]",
udp_size, intoa(ntohl(sender_sock->sin_addr.s_addr), buf, sizeof(buf)),
@ -1258,9 +1256,9 @@ static int process_udp (n2n_sn_t * sss,
memcpy(&(ack.cookie), &(reg.cookie), sizeof(n2n_cookie_t));
if(comm->is_federation == IS_FEDERATION) {
memcpy(&(ack.edgeMac), &(sss->mac_addr), sizeof(n2n_mac_t));
memcpy(ack.edgeMac, sss->mac_addr, sizeof(n2n_mac_t));
} else {
memcpy(&(ack.edgeMac), &(reg.edgeMac), sizeof(n2n_mac_t));
memcpy(ack.edgeMac, reg.edgeMac, sizeof(n2n_mac_t));
}
if((reg.dev_addr.net_addr == 0) || (reg.dev_addr.net_addr == 0xFFFFFFFF) || (reg.dev_addr.net_bitlen == 0) ||
@ -1279,7 +1277,7 @@ static int process_udp (n2n_sn_t * sss,
/* Add sender's data to federation (or update it) */
if(comm->is_federation == IS_FEDERATION) {
skip_add = SN_ADD;
p = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), &(ack.sock), &(reg.edgeMac), &skip_add);
p = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), &(ack.sock), reg.edgeMac, &skip_add);
}
// REVISIT: consider adding last_seen
@ -1302,7 +1300,7 @@ static int process_udp (n2n_sn_t * sss,
* their SN_ACTIVE time before they get re-registred to. */
if(((++num)*REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE) > REG_SUPER_ACK_PAYLOAD_SPACE) break; /* no more space available in REGISTER_SUPER_ACK payload */
memcpy(&(payload->sock), &(peer->sock), sizeof(n2n_sock_t));
memcpy(&(payload->mac), &(peer->mac_addr), sizeof(n2n_mac_t));
memcpy(payload->mac, peer->mac_addr, sizeof(n2n_mac_t));
// shift to next payload entry
payload++;
}
@ -1312,7 +1310,7 @@ static int process_udp (n2n_sn_t * sss,
macaddr_str(mac_buf, reg.edgeMac),
sock_to_cstr(sockbuf, &(ack.sock)));
if(memcmp(reg.edgeMac, &null_mac, N2N_MAC_SIZE) != 0) {
if(!is_null_mac(reg.edgeMac)) {
if(cmn.flags & N2N_FLAGS_SOCKET) {
ret_value = update_edge(sss, &reg, comm, &(ack.sock), SN_ADD_SKIP, now);
} else {
@ -1323,7 +1321,7 @@ static int process_udp (n2n_sn_t * sss,
if(ret_value == update_edge_auth_fail) {
cmn2.pc = n2n_register_super_nak;
memcpy(&(nak.cookie), &(reg.cookie), sizeof(n2n_cookie_t));
memcpy(&(nak.srcMac), &(reg.edgeMac), sizeof(n2n_mac_t));
memcpy(nak.srcMac, reg.edgeMac, sizeof(n2n_mac_t));
encode_REGISTER_SUPER_NAK(ackbuf, &encx, &cmn2, &nak);
@ -1479,7 +1477,7 @@ static int process_udp (n2n_sn_t * sss,
if(comm->is_federation == IS_FEDERATION) {
skip_add = SN_ADD_SKIP;
scan = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), &sender, &(ack.edgeMac), &skip_add);
scan = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), &sender, ack.edgeMac, &skip_add);
if(scan != NULL) {
scan->last_seen = now;
} else {
@ -1492,7 +1490,7 @@ static int process_udp (n2n_sn_t * sss,
for(i = 0; i < ack.num_sn; i++) {
skip_add = SN_ADD;
tmp = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), &(payload->sock), &(payload->mac), &skip_add);
tmp = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), &(payload->sock), payload->mac, &skip_add);
if(skip_add == SN_ADD_ADDED) {
tmp->last_seen = now - LAST_SEEN_SN_NEW;
@ -1597,7 +1595,7 @@ static int process_udp (n2n_sn_t * sss,
}
}
if(memcmp(query.targetMac, null_mac, sizeof(n2n_mac_t)) == 0) {
if(is_null_mac(query.targetMac)) {
traceEvent(TRACE_DEBUG, "Rx PING from %s.",
macaddr_str(mac_buf, query.srcMac));

View File

@ -1,5 +1,5 @@
/**
* (C) 2007-20 - ntop.org and contributors
* (C) 2007-21 - ntop.org and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -179,7 +179,7 @@ int encode_mac (uint8_t * base,
return encode_buf(base, idx, m, N2N_MAC_SIZE);
}
int decode_mac (uint8_t * out, /* of size N2N_MAC_SIZE. This clearer than passing a n2n_mac_t */
int decode_mac (n2n_mac_t out, /* n2n_mac_t is typedefed array type which is always passed by reference */
const uint8_t * base,
size_t * rem,
size_t * idx) {