Browse Source

internally renamed dhcp --> auto ip

pull/372/head
Logan007 4 years ago
parent
commit
a35675da4d
  1. 6
      include/n2n.h
  2. 8
      include/n2n_define.h
  3. 29
      src/sn.c
  4. 50
      src/sn_utils.c

6
include/n2n.h

@ -370,7 +370,7 @@ struct sn_community
he_context_t *header_iv_ctx; /* Header IV ecnryption cipher context, REMOVE as soon as seperate fields for checksum and replay protection available */ he_context_t *header_iv_ctx; /* Header IV ecnryption cipher context, REMOVE as soon as seperate fields for checksum and replay protection available */
struct peer_info *edges; /* Link list of registered edges. */ struct peer_info *edges; /* Link list of registered edges. */
int64_t number_enc_packets; /* Number of encrypted packets handled so far, required for sorting from time to time */ int64_t number_enc_packets; /* Number of encrypted packets handled so far, required for sorting from time to time */
n2n_ip_subnet_t dhcp_net; /* Address range of dhcp service. */ n2n_ip_subnet_t auto_ip_net; /* Address range of auto ip address service. */
UT_hash_handle hh; /* makes this structure hashable */ UT_hash_handle hh; /* makes this structure hashable */
}; };
@ -391,8 +391,8 @@ typedef struct n2n_sn
uint16_t mport; /* Management UDP port to bind to. */ uint16_t mport; /* Management UDP port to bind to. */
int sock; /* Main socket for UDP traffic with edges. */ int sock; /* Main socket for UDP traffic with edges. */
int mgmt_sock; /* management socket. */ int mgmt_sock; /* management socket. */
n2n_ip_subnet_t min_dhcp_net; /* Address range of dhcp service. */ n2n_ip_subnet_t min_auto_ip_net; /* Address range of auto_ip service. */
n2n_ip_subnet_t max_dhcp_net; /* Address range of dhcp service. */ n2n_ip_subnet_t max_auto_ip_net; /* Address range of auto_ip service. */
#ifndef WIN32 #ifndef WIN32
uid_t userid; uid_t userid;
gid_t groupid; gid_t groupid;

8
include/n2n_define.h

@ -101,10 +101,10 @@
#define TUNTAP_IP_MODE_STATIC 1 #define TUNTAP_IP_MODE_STATIC 1
#define TUNTAP_IP_MODE_DHCP 2 #define TUNTAP_IP_MODE_DHCP 2
/* Default network segment of the dhcp service provided by sn. */ /* Default network segment of the auto ip address service provided by sn. */
#define N2N_SN_MIN_DHCP_NET_DEFAULT "10.128.0.0" #define N2N_SN_MIN_AUTO_IP_NET_DEFAULT "10.128.0.0"
#define N2N_SN_MAX_DHCP_NET_DEFAULT "10.255.255.0" #define N2N_SN_MAX_AUTO_IP_NET_DEFAULT "10.255.255.0"
#define N2N_SN_DHCP_NET_BIT_DEFAULT 24 #define N2N_SN_AUTO_IP_NET_BIT_DEFAULT 24
/* ************************************** */ /* ************************************** */

29
src/sn.c

@ -128,11 +128,11 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) {
} }
} }
if(has_net) { if(has_net) {
s->dhcp_net.net_addr = ntohl(net); s->auto_ip_net.net_addr = ntohl(net);
s->dhcp_net.net_bitlen = bitlen; s->auto_ip_net.net_bitlen = bitlen;
traceEvent(TRACE_INFO, "Assigned sub-network %s/%u to community '%s'.", traceEvent(TRACE_INFO, "Assigned sub-network %s/%u to community '%s'.",
inet_ntoa(*(struct in_addr *) &net), inet_ntoa(*(struct in_addr *) &net),
s->dhcp_net.net_bitlen, s->auto_ip_net.net_bitlen,
s->community); s->community);
} else { } else {
assign_one_ip_subnet(sss, s); assign_one_ip_subnet(sss, s);
@ -183,7 +183,7 @@ static void help() {
printf("[-u <uid> -g <gid>] "); printf("[-u <uid> -g <gid>] ");
#endif /* ifndef WIN32 */ #endif /* ifndef WIN32 */
printf("[-t <mgmt port>] "); printf("[-t <mgmt port>] ");
printf("[-d <net-net/bit>] "); printf("[-a <net-net/bit>] ");
printf("[-v] "); printf("[-v] ");
printf("\n\n"); printf("\n\n");
@ -197,7 +197,8 @@ static void help() {
printf("-g <GID> | Group ID (numeric) to use when privileges are dropped.\n"); printf("-g <GID> | Group ID (numeric) to use when privileges are dropped.\n");
#endif /* ifndef WIN32 */ #endif /* ifndef WIN32 */
printf("-t <port> | Management UDP Port (for multiple supernodes on a machine).\n"); printf("-t <port> | Management UDP Port (for multiple supernodes on a machine).\n");
printf("-d <net-net/bit> | Subnet range for community ip address service for edges. eg. -d 10.128.255.0-10.255.255.0/24\n"); printf("-a <net-net/bit> | Subnet range for auto ip address service, e.g.\n");
printf(" | -a 192.168.0.0-192.168.255.0/24, defaults to 10.128.255.0-10.255.255.0/24\n");
printf("-v | Increase verbosity. Can be used multiple times.\n"); printf("-v | Increase verbosity. Can be used multiple times.\n");
printf("-h | This help message.\n"); printf("-h | This help message.\n");
printf("\n"); printf("\n");
@ -219,7 +220,7 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
sss->mport = atoi(_optarg); sss->mport = atoi(_optarg);
break; break;
case 'd': { case 'a': {
dec_ip_str_t ip_min_str = {'\0'}; dec_ip_str_t ip_min_str = {'\0'};
dec_ip_str_t ip_max_str = {'\0'}; dec_ip_str_t ip_max_str = {'\0'};
in_addr_t net_min, net_max; in_addr_t net_min, net_max;
@ -240,23 +241,23 @@ static int setOption(int optkey, char *_optarg, n2n_sn_t *sss) {
|| ((ntohl(net_min) & ~mask) != 0) || ((ntohl(net_max) & ~mask) != 0) ) { || ((ntohl(net_min) & ~mask) != 0) || ((ntohl(net_max) & ~mask) != 0) ) {
traceEvent(TRACE_WARNING, "Bad network range '%s...%s/%u' in '%s', defaulting to '%s...%s/%d'", traceEvent(TRACE_WARNING, "Bad network range '%s...%s/%u' in '%s', defaulting to '%s...%s/%d'",
ip_min_str, ip_max_str, bitlen, _optarg, ip_min_str, ip_max_str, bitlen, _optarg,
N2N_SN_MIN_DHCP_NET_DEFAULT, N2N_SN_MAX_DHCP_NET_DEFAULT, N2N_SN_DHCP_NET_BIT_DEFAULT); N2N_SN_MIN_AUTO_IP_NET_DEFAULT, N2N_SN_MAX_AUTO_IP_NET_DEFAULT, N2N_SN_AUTO_IP_NET_BIT_DEFAULT);
break; break;
} }
if ((bitlen > 30) || (bitlen == 0)) { if ((bitlen > 30) || (bitlen == 0)) {
traceEvent(TRACE_WARNING, "Bad prefix '%hhu' in '%s', defaulting to '%s...%s/%d'", traceEvent(TRACE_WARNING, "Bad prefix '%hhu' in '%s', defaulting to '%s...%s/%d'",
bitlen, _optarg, bitlen, _optarg,
N2N_SN_MIN_DHCP_NET_DEFAULT, N2N_SN_MAX_DHCP_NET_DEFAULT, N2N_SN_DHCP_NET_BIT_DEFAULT); N2N_SN_MIN_AUTO_IP_NET_DEFAULT, N2N_SN_MAX_AUTO_IP_NET_DEFAULT, N2N_SN_AUTO_IP_NET_BIT_DEFAULT);
break; break;
} }
traceEvent(TRACE_NORMAL, "The network range for community ip address service is '%s...%s/%hhu'.", ip_min_str, ip_max_str, bitlen); traceEvent(TRACE_NORMAL, "The network range for community ip address service is '%s...%s/%hhu'.", ip_min_str, ip_max_str, bitlen);
sss->min_dhcp_net.net_addr = ntohl(net_min); sss->min_auto_ip_net.net_addr = ntohl(net_min);
sss->min_dhcp_net.net_bitlen = bitlen; sss->min_auto_ip_net.net_bitlen = bitlen;
sss->max_dhcp_net.net_addr = ntohl(net_max); sss->max_auto_ip_net.net_addr = ntohl(net_max);
sss->max_dhcp_net.net_bitlen = bitlen; sss->max_auto_ip_net.net_bitlen = bitlen;
break; break;
} }
@ -303,7 +304,7 @@ static const struct option long_options[] = {
{"foreground", no_argument, NULL, 'f'}, {"foreground", no_argument, NULL, 'f'},
{"local-port", required_argument, NULL, 'l'}, {"local-port", required_argument, NULL, 'l'},
{"mgmt-port", required_argument, NULL, 't'}, {"mgmt-port", required_argument, NULL, 't'},
{"dhcp", required_argument, NULL, 'd'}, {"autoip", required_argument, NULL, 'a'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'}, {"verbose", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
@ -315,7 +316,7 @@ static const struct option long_options[] = {
static int loadFromCLI(int argc, char * const argv[], n2n_sn_t *sss) { static int loadFromCLI(int argc, char * const argv[], n2n_sn_t *sss) {
u_char c; u_char c;
while((c = getopt_long(argc, argv, "fl:u:g:t:d:c:vh", while((c = getopt_long(argc, argv, "fl:u:g:t:a:c:vh",
long_options, NULL)) != '?') { long_options, NULL)) != '?') {
if(c == 255) break; if(c == 255) break;
setOption(c, optarg, sss); setOption(c, optarg, sss);

50
src/sn_utils.c

@ -218,12 +218,12 @@ int sn_init(n2n_sn_t *sss) {
sss->mport = N2N_SN_MGMT_PORT; sss->mport = N2N_SN_MGMT_PORT;
sss->sock = -1; sss->sock = -1;
sss->mgmt_sock = -1; sss->mgmt_sock = -1;
sss->min_dhcp_net.net_addr = inet_addr(N2N_SN_MIN_DHCP_NET_DEFAULT); sss->min_auto_ip_net.net_addr = inet_addr(N2N_SN_MIN_AUTO_IP_NET_DEFAULT);
sss->min_dhcp_net.net_addr = ntohl(sss->min_dhcp_net.net_addr); sss->min_auto_ip_net.net_addr = ntohl(sss->min_auto_ip_net.net_addr);
sss->min_dhcp_net.net_bitlen = N2N_SN_DHCP_NET_BIT_DEFAULT; sss->min_auto_ip_net.net_bitlen = N2N_SN_AUTO_IP_NET_BIT_DEFAULT;
sss->max_dhcp_net.net_addr = inet_addr(N2N_SN_MAX_DHCP_NET_DEFAULT); sss->max_auto_ip_net.net_addr = inet_addr(N2N_SN_MAX_AUTO_IP_NET_DEFAULT);
sss->max_dhcp_net.net_addr = ntohl(sss->max_dhcp_net.net_addr); sss->max_auto_ip_net.net_addr = ntohl(sss->max_auto_ip_net.net_addr);
sss->max_dhcp_net.net_bitlen = N2N_SN_DHCP_NET_BIT_DEFAULT; sss->max_auto_ip_net.net_bitlen = N2N_SN_AUTO_IP_NET_BIT_DEFAULT;
return 0; /* OK */ return 0; /* OK */
} }
@ -337,15 +337,15 @@ static signed int peer_tap_ip_sort(struct peer_info *a, struct peer_info *b) {
} }
/** The IP address assigned to the edge by the DHCP function of sn. */ /** The IP address assigned to the edge by the auto ip address function of sn. */
static int assign_one_ip_addr(struct sn_community *comm, static int assign_one_ip_addr(struct sn_community *comm,
n2n_ip_subnet_t *ipaddr) { n2n_ip_subnet_t *ipaddr) {
struct peer_info *peer, *tmpPeer; struct peer_info *peer, *tmpPeer;
uint32_t net_id, mask, max_host, host_id = 1; uint32_t net_id, mask, max_host, host_id = 1;
dec_ip_bit_str_t ip_bit_str = {'\0'}; dec_ip_bit_str_t ip_bit_str = {'\0'};
mask = bitlen2mask(comm->dhcp_net.net_bitlen); mask = bitlen2mask(comm->auto_ip_net.net_bitlen);
net_id = comm->dhcp_net.net_addr & mask; net_id = comm->auto_ip_net.net_addr & mask;
max_host = ~mask; max_host = ~mask;
HASH_SORT(comm->edges, peer_tap_ip_sort); HASH_SORT(comm->edges, peer_tap_ip_sort);
@ -366,7 +366,7 @@ static int assign_one_ip_addr(struct sn_community *comm,
} }
} }
ipaddr->net_addr = net_id | host_id; ipaddr->net_addr = net_id | host_id;
ipaddr->net_bitlen = comm->dhcp_net.net_bitlen; ipaddr->net_bitlen = comm->auto_ip_net.net_bitlen;
traceEvent(TRACE_INFO, "Assign IP %s to tap adapter of edge.", ip_subnet_to_str(ip_bit_str, ipaddr)); traceEvent(TRACE_INFO, "Assign IP %s to tap adapter of edge.", ip_subnet_to_str(ip_bit_str, ipaddr));
return 0; return 0;
@ -384,8 +384,8 @@ int subnet_available(n2n_sn_t *sss,
HASH_ITER(hh, sss->communities, cmn, tmpCmn) { HASH_ITER(hh, sss->communities, cmn, tmpCmn) {
if (cmn == comm) continue; if (cmn == comm) continue;
if( (net_id <= (cmn->dhcp_net.net_addr + ~bitlen2mask(cmn->dhcp_net.net_bitlen))) if( (net_id <= (cmn->auto_ip_net.net_addr + ~bitlen2mask(cmn->auto_ip_net.net_bitlen)))
&&(net_id + ~mask >= cmn->dhcp_net.net_addr) ) { &&(net_id + ~mask >= cmn->auto_ip_net.net_addr) ) {
success = 0; success = 0;
break; break;
} }
@ -395,7 +395,7 @@ int subnet_available(n2n_sn_t *sss,
} }
/** The IP address assigned to the edge by the DHCP function of sn. */ /** The IP address range (subnet) assigned to the community by the auto ip address function of sn. */
int assign_one_ip_subnet(n2n_sn_t *sss, int assign_one_ip_subnet(n2n_sn_t *sss,
struct sn_community *comm) { struct sn_community *comm) {
@ -404,42 +404,42 @@ int assign_one_ip_subnet(n2n_sn_t *sss,
uint8_t success; uint8_t success;
in_addr_t net; in_addr_t net;
mask = bitlen2mask(sss->min_dhcp_net.net_bitlen); mask = bitlen2mask(sss->min_auto_ip_net.net_bitlen);
// number of possible sub-networks // number of possible sub-networks
no_subnets = (sss->max_dhcp_net.net_addr - sss->min_dhcp_net.net_addr); no_subnets = (sss->max_auto_ip_net.net_addr - sss->min_auto_ip_net.net_addr);
no_subnets >>= (32 - sss->min_dhcp_net.net_bitlen); no_subnets >>= (32 - sss->min_auto_ip_net.net_bitlen);
no_subnets += 1; no_subnets += 1;
// proposal for sub-network to choose // proposal for sub-network to choose
net_id = pearson_hash_32(comm->community, N2N_COMMUNITY_SIZE) % no_subnets; net_id = pearson_hash_32(comm->community, N2N_COMMUNITY_SIZE) % no_subnets;
net_id = sss->min_dhcp_net.net_addr + (net_id << (32 - sss->min_dhcp_net.net_bitlen)); net_id = sss->min_auto_ip_net.net_addr + (net_id << (32 - sss->min_auto_ip_net.net_bitlen));
// check for availability starting from net_id, then downwards, ... // check for availability starting from net_id, then downwards, ...
net_increment = (~mask+1); net_increment = (~mask+1);
for(net_id_i=net_id; net_id_i >= sss->min_dhcp_net.net_addr; net_id_i -= net_increment) { for(net_id_i=net_id; net_id_i >= sss->min_auto_ip_net.net_addr; net_id_i -= net_increment) {
success = subnet_available(sss, comm, net_id_i, mask); success = subnet_available(sss, comm, net_id_i, mask);
if(success) break; if(success) break;
} }
// ... then upwards // ... then upwards
if(!success) { if(!success) {
for(net_id_i=net_id + net_increment; net_id_i <= sss->max_dhcp_net.net_addr; net_id_i += net_increment) { for(net_id_i=net_id + net_increment; net_id_i <= sss->max_auto_ip_net.net_addr; net_id_i += net_increment) {
success = subnet_available(sss, comm, net_id_i, mask); success = subnet_available(sss, comm, net_id_i, mask);
if(success) break; if(success) break;
} }
} }
if(success) { if(success) {
comm->dhcp_net.net_addr = net_id_i; comm->auto_ip_net.net_addr = net_id_i;
comm->dhcp_net.net_bitlen = sss->min_dhcp_net.net_bitlen; comm->auto_ip_net.net_bitlen = sss->min_auto_ip_net.net_bitlen;
net = htonl(comm->dhcp_net.net_addr); net = htonl(comm->auto_ip_net.net_addr);
traceEvent(TRACE_INFO, "Assigned sub-network %s/%u to community '%s'.", traceEvent(TRACE_INFO, "Assigned sub-network %s/%u to community '%s'.",
inet_ntoa(*(struct in_addr *) &net), inet_ntoa(*(struct in_addr *) &net),
comm->dhcp_net.net_bitlen, comm->auto_ip_net.net_bitlen,
comm->community); comm->community);
return 0; return 0;
} else { } else {
comm->dhcp_net.net_addr = 0; comm->auto_ip_net.net_addr = 0;
comm->dhcp_net.net_bitlen = 0; comm->auto_ip_net.net_bitlen = 0;
traceEvent(TRACE_WARNING, "No assignable sub-network left for community '%s'.", traceEvent(TRACE_WARNING, "No assignable sub-network left for community '%s'.",
comm->community); comm->community);
return -1; return -1;

Loading…
Cancel
Save