Browse Source

separated 'detect_local_ip_address()' function (#781)

pull/783/head
Logan oos Even 3 years ago
committed by GitHub
parent
commit
b3bf36f867
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      include/n2n.h
  2. 32
      src/edge_utils.c
  3. 48
      src/n2n.c

2
include/n2n.h

@ -223,6 +223,8 @@ char * ip_subnet_to_str (dec_ip_bit_str_t buf, const n2n_ip_subnet_t *ipaddr);
SOCKET open_socket (int local_port, in_addr_t address, int type); SOCKET open_socket (int local_port, in_addr_t address, int type);
int sock_equal (const n2n_sock_t * a, int sock_equal (const n2n_sock_t * a,
const n2n_sock_t * b); const n2n_sock_t * b);
int detect_local_ip_address (n2n_sock_t* out_sock,
const n2n_edge_t* eee);
/* Header encryption */ /* Header encryption */
uint64_t time_stamp (void); uint64_t time_stamp (void);

32
src/edge_utils.c

@ -204,9 +204,8 @@ int supernode_connect(n2n_edge_t *eee) {
int sockopt; int sockopt;
struct sockaddr_in sn_sock; struct sockaddr_in sn_sock;
struct sockaddr_in local_sock; n2n_sock_t local_sock;
int sock_len = sizeof(local_sock); n2n_sock_str_t sockbuf;
SOCKET probe_sock;
if((eee->conf.connect_tcp) && (eee->sock >= 0)) { if((eee->conf.connect_tcp) && (eee->sock >= 0)) {
closesocket(eee->sock); closesocket(eee->sock);
@ -266,25 +265,16 @@ int supernode_connect(n2n_edge_t *eee) {
traceEvent(TRACE_INFO, "PMTU discovery %s", (eee->conf.disable_pmtu_discovery) ? "disabled" : "enabled"); traceEvent(TRACE_INFO, "PMTU discovery %s", (eee->conf.disable_pmtu_discovery) ? "disabled" : "enabled");
#endif #endif
// detetct local port even/especially if chosen by OS... memset(&local_sock, 0, sizeof(n2n_sock_t));
if((getsockname(eee->sock, (struct sockaddr *)&local_sock, &sock_len) == 0) if(detect_local_ip_address(&local_sock, eee) == 0) {
&& (local_sock.sin_family == AF_INET) // always overwrite local port even/especially if chosen by OS...
&& (sock_len == sizeof(local_sock))) { eee->conf.preferred_sock.port = local_sock.port;
// ... and write to local preferred socket -- no matter if used or not // only if auto-detection mode, ...
eee->conf.preferred_sock.port = ntohs(local_sock.sin_port);
// probe for & overwrite local address only if auto-detection mode
if(eee->conf.preferred_sock_auto) { if(eee->conf.preferred_sock_auto) {
probe_sock = socket(PF_INET, SOCK_DGRAM, 0); // ... overwrite IP address, too (whole socket struct here)
// connecting the UDP socket makes getsockname read the local address it uses to connect (to the sn in this case) memcpy(&eee->conf.preferred_sock, &local_sock, sizeof(n2n_sock_t));
// we cannot do it with the real (eee->sock) socket because socket does not accept any conenction from elsewhere then, traceEvent(TRACE_INFO, "determined local socket [%s]",
// e.g. from another edge instead of the supernode; hence, we use a temporary socket sock_to_cstr(sockbuf, &local_sock));
connect(probe_sock, (struct sockaddr *)&sn_sock, sizeof(sn_sock));
if((getsockname(probe_sock, (struct sockaddr *)&local_sock, &sock_len) == 0)
&& (local_sock.sin_family == AF_INET)
&& (sock_len == sizeof(local_sock))) {
memcpy(&(eee->conf.preferred_sock.addr.v4), &(local_sock.sin_addr.s_addr), IPV4_SIZE);
}
close(probe_sock);
} }
} }

48
src/n2n.c

@ -61,6 +61,54 @@ SOCKET open_socket (int local_port, in_addr_t address, int type /* 0 = UDP, TCP
} }
int detect_local_ip_address (n2n_sock_t* out_sock, const n2n_edge_t* eee) {
struct sockaddr_in local_sock;
struct sockaddr_in sn_sock;
int sock_len = sizeof(local_sock);
int port = 0;
SOCKET probe_sock;
out_sock->family = AF_INVALID;
// always detetct local port even/especially if chosen by OS...
if((getsockname(eee->sock, (struct sockaddr *)&local_sock, &sock_len) == 0)
&& (local_sock.sin_family == AF_INET)
&& (sock_len == sizeof(local_sock)))
// remember the port number
out_sock->port = ntohs(local_sock.sin_port);
else
return -1;
// probe for local IP address
probe_sock = socket(PF_INET, SOCK_DGRAM, 0);
// connecting the UDP socket makes getsockname read the local address it uses to connect (to the sn in this case);
// we cannot do it with the real (eee->sock) socket because socket does not accept any conenction from elsewhere then,
// e.g. from another edge instead of the supernode; as re-connecting to AF_UNSPEC might not work to release the socket
// on non-UNIXoids, we use a temporary socket
if(probe_sock >= 0) {
fill_sockaddr((struct sockaddr*)&sn_sock, sizeof(sn_sock), &eee->curr_sn->sock);
if(connect(probe_sock, (struct sockaddr *)&sn_sock, sizeof(sn_sock)) == 0) {
if((getsockname(probe_sock, (struct sockaddr *)&local_sock, &sock_len) == 0)
&& (local_sock.sin_family == AF_INET)
&& (sock_len == sizeof(local_sock))) {
memcpy(&(out_sock->addr.v4), &(local_sock.sin_addr.s_addr), IPV4_SIZE);
} else
return -4;
} else
return -3;
close(probe_sock);
} else
return -2;
out_sock->family = AF_INET;
return 0;
}
static int traceLevel = 2 /* NORMAL */; static int traceLevel = 2 /* NORMAL */;
static int useSyslog = 0, syslog_opened = 0; static int useSyslog = 0, syslog_opened = 0;
static FILE *traceFile = NULL; static FILE *traceFile = NULL;

Loading…
Cancel
Save