Skip to content

Commit 7927bd7

Browse files
authored
added optional choice of bind address (-p) to supernode (ntop#1016)
1 parent 24c9ab9 commit 7927bd7

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

include/n2n_typedefs.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ typedef struct n2n_edge_conf {
659659
char *encrypt_key;
660660
int register_interval; /**< Interval for supernode registration, also used for UDP NAT hole punching. */
661661
int register_ttl; /**< TTL for registration packet when UDP NAT hole punching through supernode. */
662-
in_addr_t bind_address; /**< The address to bind to if provided (-b) */
662+
in_addr_t bind_address; /**< The address to bind to if provided */
663663
n2n_sock_t preferred_sock; /**< propagated local sock for better p2p in LAN (-e) */
664664
uint8_t preferred_sock_auto; /**< indicates desired auto detect for preferred sock */
665665
int local_port;
@@ -813,6 +813,7 @@ typedef struct n2n_sn {
813813
sn_stats_t stats;
814814
int daemon; /* If non-zero then daemonise. */
815815
n2n_mac_t mac_addr;
816+
in_addr_t bind_address; /* The address to bind to if provided */
816817
uint16_t lport; /* Local UDP port to bind to. */
817818
uint16_t mport; /* Management UDP port to bind to. */
818819
int sock; /* Main socket for UDP traffic with edges. */

src/sn_utils.c

+1
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ int sn_init_defaults (n2n_sn_t *sss) {
745745
strncpy(sss->version, PACKAGE_VERSION, sizeof(n2n_version_t));
746746
sss->version[sizeof(n2n_version_t) - 1] = '\0';
747747
sss->daemon = 1; /* By defult run as a daemon. */
748+
sss->bind_address = INADDR_ANY; /* any address */
748749
sss->lport = N2N_SN_LPORT_DEFAULT;
749750
sss->mport = N2N_SN_MGMT_PORT;
750751
sss->sock = -1;

src/supernode.c

+37-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static void help (int level) {
6060
printf(" general usage: supernode <config file> (see supernode.conf)\n"
6161
"\n"
6262
" or supernode "
63-
"[-p <local port>] "
63+
"[-p [<local bind ip address>:]<local port>] "
6464
"\n "
6565
"[-F <federation name>] "
6666
"\n options for under- "
@@ -113,7 +113,8 @@ static void help (int level) {
113113
);
114114
printf (" OPTIONS FOR THE UNDERLYING NETWORK CONNECTION\n");
115115
printf (" ---------------------------------------------\n\n");
116-
printf(" -p <local port> | fixed local UDP port, defaults to %u\n", N2N_SN_LPORT_DEFAULT);
116+
printf(" -p [<ip>:]<port> | fixed local UDP port (defaults to %u) and optionally\n"
117+
" | bind to sepcified local IP address only ('any' by default)\n", N2N_SN_LPORT_DEFAULT);
117118
printf(" -F <fed name> | name of the supernode's federation, defaults to\n"
118119
" | '%s'\n", (char *)FEDERATION_NAME);
119120
printf(" -l <host:port> | ip address or name, and port of known supernode\n");
@@ -167,14 +168,39 @@ static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) {
167168
//traceEvent(TRACE_NORMAL, "Option %c = %s", optkey, _optarg ? _optarg : "");
168169

169170
switch(optkey) {
170-
case 'p': /* local-port */
171-
sss->lport = atoi(_optarg);
172-
173-
if(sss->lport == 0)
174-
traceEvent(TRACE_WARNING, "bad local port format, defaulting to %u", N2N_SN_LPORT_DEFAULT);
175-
// default is made sure in sn_init()
176-
171+
case 'p': { /* local-port */
172+
char* colon = strpbrk(_optarg, ":");
173+
if(colon) { /*ip address:port */
174+
*colon = 0;
175+
sss->bind_address = ntohl(inet_addr(_optarg));
176+
sss->lport = atoi(++colon);
177+
178+
if(sss->bind_address == INADDR_NONE) {
179+
traceEvent(TRACE_WARNING, "bad address to bind to, binding to any IP address");
180+
sss->bind_address = INADDR_ANY;
181+
}
182+
if(sss->lport == 0) {
183+
traceEvent(TRACE_WARNING, "bad local port format, defaulting to %u", N2N_SN_LPORT_DEFAULT);
184+
sss->lport = N2N_SN_LPORT_DEFAULT;
185+
}
186+
} else { /* ip address or port only */
187+
char* dot = strpbrk(_optarg, ".");
188+
if(dot) { /* ip address only */
189+
sss->bind_address = ntohl(inet_addr(_optarg));
190+
if(sss->bind_address == INADDR_NONE) {
191+
traceEvent(TRACE_WARNING, "bad address to bind to, binding to any IP address");
192+
sss->bind_address = INADDR_ANY;
193+
}
194+
} else { /* port only */
195+
sss->lport = atoi(_optarg);
196+
if(sss->lport == 0) {
197+
traceEvent(TRACE_WARNING, "bad local port format, defaulting to %u", N2N_SN_LPORT_DEFAULT);
198+
sss->lport = N2N_SN_LPORT_DEFAULT;
199+
}
200+
}
201+
}
177202
break;
203+
}
178204

179205
case 't': /* mgmt-port */
180206
sss->mport = atoi(_optarg);
@@ -603,7 +629,7 @@ int main (int argc, char * const argv[]) {
603629

604630
traceEvent(TRACE_DEBUG, "traceLevel is %d", getTraceLevel());
605631

606-
sss_node.sock = open_socket(sss_node.lport, INADDR_ANY, 0 /* UDP */);
632+
sss_node.sock = open_socket(sss_node.lport, sss_node.bind_address, 0 /* UDP */);
607633
if(-1 == sss_node.sock) {
608634
traceEvent(TRACE_ERROR, "failed to open main socket. %s", strerror(errno));
609635
exit(-2);
@@ -612,7 +638,7 @@ int main (int argc, char * const argv[]) {
612638
}
613639

614640
#ifdef N2N_HAVE_TCP
615-
sss_node.tcp_sock = open_socket(sss_node.lport, INADDR_ANY, 1 /* TCP */);
641+
sss_node.tcp_sock = open_socket(sss_node.lport, sss_node.bind_address, 1 /* TCP */);
616642
if(-1 == sss_node.tcp_sock) {
617643
traceEvent(TRACE_ERROR, "failed to open auxiliary TCP socket, %s", strerror(errno));
618644
exit(-2);

supernode.1

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ Lines starting with a "#" are ignored.
2626
An equal sign ('=') should be used between key and value. Example: -p=7777
2727
.SH OPTIONS FOR THE UNDERLYING NETWORK CONNECTION
2828
.TP
29-
\fB\-p \fR<\fIlocal_port\fR>, \fB\-\-local-port\fR=<\fIlocal_port\fR>
30-
listen on this fixed local UDP port, defaults to 7654
29+
\fB\-p \fR[<\fIlocal_ip_address\fR>:]<\fIlocal_port\fR>, \fB\-\-local-port\fR=...
30+
binds supernode to this fixed UDP port on 'any' local IP address, defaults to 7654.
31+
Optionally, the edge can bind to the provided local ip address only.
3132
.TP
3233
\fB\-F \fR<\fIfed_name\fR>
3334
name of the supernode's federation, defaults to '*Federation' (see also N2N_FEDERATION in ENVIRONMENT)

0 commit comments

Comments
 (0)