Skip to content

Commit e7bfec9

Browse files
author
Yorhel
committed
Add support for obtaining active_ip from the local socket
As proposed in reply 4 at http://dev.yorhel.nl/ncdc/bug/76 This option is activated with a '/hset active_ip local'.
1 parent 3945226 commit e7bfec9

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

src/doc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ static const doc_set_t doc_sets[] = {
281281
" separated with a comma. When unset, '0.0.0.0,::' is assumed. Only the IP"
282282
" version used to connect to the hub is used. That is, if you connect to an"
283283
" IPv6 hub, then the configured IPv6 address is used and the IPv4 address is"
284-
" ignored."
284+
" ignored.\n\n"
285+
"When set to the special value `local', ncdc will automatically get your IP"
286+
" address from the local network interface that is used to connect to the"
287+
" hub. This option should only be used if there is no NAT between you and"
288+
" the hub, because this will give the wrong IP if you are behind a NAT."
285289
},
286290
{ "active_port", 1, "<integer>",
287291
"The listen port for incoming connections in active mode. Set to `0' to"

src/hub.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,10 @@ char *hub_ip(hub_t *hub) {
510510
if(!net_is_connected(hub->net))
511511
return NULL;
512512

513-
// Somewhat roundabout way of obtaining the IP for the correct version
514513
char *ip = var_get(hub->id, VAR_active_ip);
515-
if(!net_is_ipv6(hub->net)) {
514+
if(ip && strcmp(ip, "local") == 0) {
515+
ip = (char *)net_localaddr(hub->net);
516+
} else if(!net_is_ipv6(hub->net)) {
516517
struct in_addr ip4 = var_parse_ip4(ip);
517518
ip = ip4_isany(ip4) ? NULL : (char *)ip4_unpack(ip4);
518519
} else {

src/net.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ struct net_t {
6969
dnscon_t *dnscon; // state DNS,CON. Setting ->net to NULL 'cancels' DNS resolving.
7070
int sock; // state CON,ASY,SYN,DIS
7171
int socksrc; // state CON,ASY,DIS. Glib event source on 'sock'.
72-
char addr[64]; // state ASY,SYN,DIS
72+
char addr[56]; // state ASY,SYN,DIS, ip:port
73+
char laddr[40]; // state ASY,SYN,DIS, ip only
7374

7475
gnutls_session_t tls; // state ASY,SYN,DIS (only if tls is enabled)
7576
void (*cb_handshake)(net_t *, const char *); // state ASY, called after complete handshake.
@@ -1011,6 +1012,18 @@ void net_connected(net_t *n, int sock, const char *addr, gboolean v6) {
10111012
n->wbuf = g_string_sized_new(1024);
10121013
n->rbuf = g_string_sized_new(1024);
10131014

1015+
if(v6) {
1016+
struct sockaddr_in6 a;
1017+
socklen_t l = sizeof(a);
1018+
getsockname(sock, (void *)&a, &l);
1019+
strcpy(n->laddr, ip6_unpack(a.sin6_addr));
1020+
} else {
1021+
struct sockaddr_in a;
1022+
socklen_t l = sizeof(a);
1023+
getsockname(sock, (void *)&a, &l);
1024+
strcpy(n->laddr, ip4_unpack(a.sin_addr));
1025+
}
1026+
10141027
ratecalc_reset(&n->rate_in);
10151028
ratecalc_reset(&n->rate_out);
10161029
ratecalc_register(&n->rate_in, RCC_DOWN);
@@ -1211,6 +1224,7 @@ void net_set_keepalive(net_t *n, const char *msg) {
12111224

12121225

12131226
const char *net_remoteaddr(net_t *n) { return n->addr; }
1227+
const char *net_localaddr(net_t *n) { return n->laddr; }
12141228
ratecalc_t *net_rate_in(net_t *n) { return &n->rate_in; }
12151229
ratecalc_t *net_rate_out(net_t *n) { return &n->rate_out; }
12161230
void *net_handle(net_t *n) { return n->handle; }

src/vars.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ static char *p_ip(const char *val, GError **err) {
143143
return g_strdup_printf("%s,%s", ip4_unpack(i4), ip6_unpack(i6));
144144
}
145145

146+
static char *p_active_ip(const char *val, GError **err) {
147+
if(strcmp(val, "local") == 0)
148+
return g_strdup(val);
149+
return p_ip(val, err);
150+
}
151+
146152
static char *p_regex(const char *val, GError **err) {
147153
GRegex *r = g_regex_new(val, 0, 0, err);
148154
if(!r)
@@ -939,7 +945,7 @@ struct var_t {
939945
// name g h format parse suggest getraw setraw default/init
940946
#define VARS\
941947
V(active, 1,1, f_bool, p_bool, su_bool, NULL, s_active_conf, "false")\
942-
V(active_ip, 1,1, f_id, p_ip, su_old, NULL, s_active_conf, NULL)\
948+
V(active_ip, 1,1, f_id, p_active_ip, su_old, NULL, s_active_conf, NULL)\
943949
V(active_port, 1,1, f_int, p_active_port, NULL, NULL, s_active_conf, NULL)\
944950
V(active_udp_port, 1,1, f_int, p_active_port, NULL, g_active_udp, s_active_conf, NULL)\
945951
V(adc_blom, 1,1, f_bool, p_bool, su_bool, NULL, NULL, "false")\

0 commit comments

Comments
 (0)