mirror of
https://github.com/ntop/n2n.git
synced 2024-09-20 00:51:10 +02:00
relocated 'detect_local_ip_address()' (#792)
This commit is contained in:
parent
9c22d28562
commit
05f40c64db
47
src/n2n.c
47
src/n2n.c
|
@ -61,53 +61,6 @@ 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;
|
|
||||||
socklen_t sock_len = sizeof(local_sock);
|
|
||||||
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((int)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;
|
|
||||||
closesocket(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…
Reference in New Issue
Block a user