From df869bfd3b8130a207d7dd77d2cb64faa1b0378d Mon Sep 17 00:00:00 2001 From: Logan007 Date: Mon, 21 Sep 2020 13:37:06 +0545 Subject: [PATCH] merged edge's subnet parameter into address parameter --- include/n2n.h | 1 - include/n2n_define.h | 4 ++ src/edge.c | 101 +++++++++++++++++++++---------------------- 3 files changed, 54 insertions(+), 52 deletions(-) diff --git a/include/n2n.h b/include/n2n.h index 926c812..a9735f5 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -257,7 +257,6 @@ typedef struct n2n_tuntap_priv_config { dec_ip_str_t netmask; char device_mac[N2N_MACNAMSIZ]; int mtu; - uint8_t got_s; uint8_t daemon; #ifndef WIN32 uid_t userid; diff --git a/include/n2n_define.h b/include/n2n_define.h index 96dd01a..97e913d 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -89,6 +89,10 @@ #define N2N_MACNAMSIZ 18 /* AA:BB:CC:DD:EE:FF + NULL*/ #define N2N_IF_MODE_SIZE 16 /* static | dhcp */ +#define N2N_EDGE_DEFAULT_DEV_NAME "edge0" +#define N2N_EDGE_DEFAULT_NETMASK "255.255.255.0" /* default netmask for edge ip address... */ +#define N2N_EDGE_DEFAULT_CIDR_NM 24 /* ... also in cidr format */ + #define N2N_SN_LPORT_DEFAULT 7654 #define N2N_SN_PKTBUF_SIZE 2048 diff --git a/src/edge.c b/src/edge.c index 633cdf9..ae5eac8 100644 --- a/src/edge.c +++ b/src/edge.c @@ -45,55 +45,66 @@ int num_cap = sizeof(cap_values)/sizeof(cap_value_t); /** Find the address and IP mode for the tuntap device. * - * s is one of these forms: + * s is of the form: * - * := | A.B.C.D + * ["static"|"dhcp",":"] (|) [/] * - * | static: | dhcp: + * for example static:192.168.8.5/24 * - * If the mode is present (colon required) then fill ip_mode with that value - * otherwise do not change ip_mode. Fill ip_mode with everything after the - * colon if it is present; or s if colon is not present. - * - * ip_add and ip_mode are NULL terminated if modified. + * Fill the parts of the string into the fileds, ip_mode only if + * present. All strings are NULL terminated. * * return 0 on success and -1 on error */ static int scan_address(char * ip_addr, size_t addr_size, + char * netmask, size_t netmask_size, char * ip_mode, size_t mode_size, - const char * s) { + char * s) { + int retval = -1; - char * p; + char * start; + char * end; + int bitlen = N2N_EDGE_DEFAULT_CIDR_NM; - if((NULL == s) || (NULL == ip_addr)) - { - return -1; - } + if((NULL == s) || (NULL == ip_addr) || (NULL == netmask)) { + return -1; + } memset(ip_addr, 0, addr_size); + memset(netmask, 0, netmask_size); - p = strpbrk(s, ":"); + start = s; + end = strpbrk(s, ":"); - if(p) - { - /* colon is present */ - if(ip_mode) - { - size_t end=0; - - memset(ip_mode, 0, mode_size); - end = MIN(p-s, (ssize_t)(mode_size-1)); /* ensure NULL term */ - strncpy(ip_mode, s, end); - strncpy(ip_addr, p+1, addr_size-1); /* ensure NULL term */ - retval = 0; - } - } - else - { - /* colon is not present */ - strncpy(ip_addr, s, addr_size-1); - ip_addr[addr_size-1] = '\0'; + if(end) { + // colon is present + if(ip_mode) { + memset(ip_mode, 0, mode_size); + strncpy(ip_mode, start, (size_t)MIN(end - start, mode_size - 1)); } + start = end + 1; + } else { + // colon is not present + } + // start now points to first address character + retval = 0; // we have got an address + + end = strpbrk(start, "/"); + + if(!end) + // no slash present -- default end + end = s + strlen(s); + + strncpy(ip_addr, start, (size_t)MIN(end - start, addr_size - 1)); // ensure NULL term + + if(end) { + // slash is present + + // now, handle the sub-network address + sscanf(end + 1, "%u", &bitlen); + bitlen = htobe32(bitlen2mask(bitlen)); + inet_ntop(AF_INET, &bitlen, netmask, netmask_size); + } return retval; } @@ -110,11 +121,10 @@ static void help() { #if defined(N2N_CAN_NAME_IFACE) "-d " #endif /* #if defined(N2N_CAN_NAME_IFACE) */ - "-a [static:|dhcp:] " + "-a [static:|dhcp:][/nn] " "-c " "[-k ]\n" " " - "[-s ] " #ifndef WIN32 "[-u -g ]" #endif /* #ifndef WIN32 */ @@ -139,10 +149,9 @@ static void help() { printf("-d | tap device name\n"); #endif - printf("-a | Set interface address. For DHCP use '-r -a dhcp:0.0.0.0'\n"); + printf("-a [mode:]
[/nn] | Interface address and optional subnet (cidr, default /24). For DHCP use '-r -a dhcp:0.0.0.0'\n"); printf("-c | n2n community name the edge belongs to.\n"); printf("-k | Encryption key (ASCII) - also N2N_KEY=.\n"); - printf("-s | Edge interface netmask in dotted decimal notation (255.255.255.0).\n"); printf("-l | Supernode IP:port\n"); printf("-i | Registration interval, for NAT hole punching (default 20 seconds)\n"); printf("-L | TTL for registration packet when UDP NAT hole punching through supernode (default 0 for not set )\n"); @@ -273,6 +282,7 @@ static int setOption(int optkey, char *optargument, n2n_tuntap_priv_config_t *ec case 'a': /* IP address and mode of TUNTAP interface */ { scan_address(ec->ip_addr, N2N_NETMASK_STR_SIZE, + ec->netmask, N2N_NETMASK_STR_SIZE, ec->ip_mode, N2N_IF_MODE_SIZE, optargument); break; @@ -475,17 +485,6 @@ static int setOption(int optkey, char *optargument, n2n_tuntap_priv_config_t *ec break; } - case 's': /* Subnet Mask */ - { - if(0 != ec->got_s) { - traceEvent(TRACE_WARNING, "Multiple subnet masks supplied"); - } - strncpy(ec->netmask, optargument, N2N_NETMASK_STR_SIZE); - ec->netmask[N2N_NETMASK_STR_SIZE - 1] = '\0'; - ec->got_s = 1; - break; - } - case 'S': { conf->allow_p2p = 0; @@ -809,9 +808,9 @@ int main(int argc, char* argv[]) { #ifdef WIN32 ec.tuntap_dev_name[0] = '\0'; #else - snprintf(ec.tuntap_dev_name, sizeof(ec.tuntap_dev_name), "edge0"); + snprintf(ec.tuntap_dev_name, sizeof(ec.tuntap_dev_name), N2N_EDGE_DEFAULT_DEV_NAME); #endif - snprintf(ec.netmask, sizeof(ec.netmask), "255.255.255.0"); + snprintf(ec.netmask, sizeof(ec.netmask), N2N_EDGE_DEFAULT_NETMASK); if((argc >= 2) && (argv[1][0] != '-')) { rc = loadFromFile(argv[1], &conf, &ec);