|
|
@ -77,6 +77,7 @@ int edge_init(n2n_edge_t * eee) { |
|
|
|
eee->null_transop = 0; |
|
|
|
eee->udp_sock = -1; |
|
|
|
eee->udp_mgmt_sock = -1; |
|
|
|
eee->udp_multicast_sock = -1; |
|
|
|
eee->dyn_ip_mode = 0; |
|
|
|
eee->allow_routing = 0; |
|
|
|
eee->drop_multicast = 1; |
|
|
@ -177,15 +178,13 @@ void supernode2addr(n2n_sock_t * sn, const n2n_sn_name_t addrIn) { |
|
|
|
static void try_send_register(n2n_edge_t * eee, |
|
|
|
uint8_t from_supernode, |
|
|
|
const n2n_mac_t mac, |
|
|
|
const n2n_sock_t * peer) |
|
|
|
{ |
|
|
|
const n2n_sock_t * peer) { |
|
|
|
/* REVISIT: purge of pending_peers not yet done. */ |
|
|
|
struct peer_info * scan = find_peer_by_mac(eee->pending_peers, mac); |
|
|
|
macstr_t mac_buf; |
|
|
|
n2n_sock_str_t sockbuf; |
|
|
|
|
|
|
|
if(NULL == scan) |
|
|
|
{ |
|
|
|
if(scan == NULL) { |
|
|
|
scan = calloc(1, sizeof(struct peer_info)); |
|
|
|
|
|
|
|
memcpy(scan->mac_addr, mac, N2N_MAC_SIZE); |
|
|
@ -205,8 +204,12 @@ static void try_send_register(n2n_edge_t * eee, |
|
|
|
|
|
|
|
send_register(eee, &(scan->sock)); |
|
|
|
|
|
|
|
/* no send registration to the local multicast group */ |
|
|
|
traceEvent(TRACE_INFO, "Registering with multicast group %s:%u", |
|
|
|
N2N_MULTICAST_GROUP, N2N_MULTICAST_PORT); |
|
|
|
send_register(eee, &(eee->multicast_peer)); |
|
|
|
|
|
|
|
/* pending_peers now owns scan. */ |
|
|
|
} else { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -219,13 +222,10 @@ void check_peer(n2n_edge_t * eee, |
|
|
|
const n2n_sock_t * peer) { |
|
|
|
struct peer_info * scan = find_peer_by_mac(eee->known_peers, mac); |
|
|
|
|
|
|
|
if(NULL == scan) |
|
|
|
{ |
|
|
|
if(NULL == scan) { |
|
|
|
/* Not in known_peers - start the REGISTER process. */ |
|
|
|
try_send_register(eee, from_supernode, mac, peer); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
} else { |
|
|
|
/* Already in known_peers. */ |
|
|
|
update_peer_address(eee, from_supernode, mac, peer, time(NULL)); |
|
|
|
} |
|
|
@ -470,9 +470,7 @@ static void send_register_super(n2n_edge_t * eee, |
|
|
|
memcpy(cmn.community, eee->community_name, N2N_COMMUNITY_SIZE); |
|
|
|
|
|
|
|
for(idx=0; idx < N2N_COOKIE_SIZE; ++idx) |
|
|
|
{ |
|
|
|
eee->last_cookie[idx] = rand() % 0xff; |
|
|
|
} |
|
|
|
|
|
|
|
memcpy(reg.cookie, eee->last_cookie, N2N_COOKIE_SIZE); |
|
|
|
reg.auth.scheme=0; /* No auth yet */ |
|
|
@ -564,23 +562,17 @@ static void send_register_ack(n2n_edge_t * eee, |
|
|
|
* This is frequently called by the main loop. |
|
|
|
*/ |
|
|
|
void update_supernode_reg(n2n_edge_t * eee, time_t nowTime) { |
|
|
|
if(eee->sn_wait && (nowTime > (eee->last_register_req + (eee->register_lifetime/10)))) |
|
|
|
{ |
|
|
|
if(eee->sn_wait && (nowTime > (eee->last_register_req + (eee->register_lifetime/10)))) { |
|
|
|
/* fall through */ |
|
|
|
traceEvent(TRACE_DEBUG, "update_supernode_reg: doing fast retry."); |
|
|
|
} |
|
|
|
else if(nowTime < (eee->last_register_req + eee->register_lifetime)) |
|
|
|
{ |
|
|
|
} else if(nowTime < (eee->last_register_req + eee->register_lifetime)) |
|
|
|
return; /* Too early */ |
|
|
|
} |
|
|
|
|
|
|
|
if(0 == eee->sup_attempts) |
|
|
|
{ |
|
|
|
if(0 == eee->sup_attempts) { |
|
|
|
/* Give up on that supernode and try the next one. */ |
|
|
|
++(eee->sn_idx); |
|
|
|
|
|
|
|
if (eee->sn_idx >= eee->sn_num) |
|
|
|
{ |
|
|
|
if (eee->sn_idx >= eee->sn_num) { |
|
|
|
/* Got to end of list, go back to the start. Also works for list of one entry. */ |
|
|
|
eee->sn_idx=0; |
|
|
|
} |
|
|
@ -591,14 +583,10 @@ void update_supernode_reg(n2n_edge_t * eee, time_t nowTime) { |
|
|
|
eee->sup_attempts = N2N_EDGE_SUP_ATTEMPTS; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
--(eee->sup_attempts); |
|
|
|
} |
|
|
|
|
|
|
|
if(eee->re_resolve_supernode_ip || (eee->sn_num > 1)) |
|
|
|
{ |
|
|
|
supernode2addr(&(eee->supernode), eee->sn_ip_array[eee->sn_idx]); |
|
|
|
} |
|
|
|
|
|
|
|
traceEvent(TRACE_DEBUG, "Registering with supernode (%s) (attempts left %u)", |
|
|
|
supernode_ip(eee), (unsigned int)eee->sup_attempts); |
|
|
@ -1230,7 +1218,7 @@ static void startTunReadThread(n2n_edge_t *eee) { |
|
|
|
/* ************************************** */ |
|
|
|
|
|
|
|
/** Read a datagram from the main UDP socket to the internet. */ |
|
|
|
static void readFromIPSocket(n2n_edge_t * eee) { |
|
|
|
static void readFromIPSocket(n2n_edge_t * eee, int in_sock) { |
|
|
|
n2n_common_t cmn; /* common fields in the packet header */ |
|
|
|
|
|
|
|
n2n_sock_str_t sockbuf1; |
|
|
@ -1252,11 +1240,10 @@ static void readFromIPSocket(n2n_edge_t * eee) { |
|
|
|
size_t i; |
|
|
|
|
|
|
|
i = sizeof(sender_sock); |
|
|
|
recvlen=recvfrom(eee->udp_sock, udp_buf, N2N_PKT_BUF_SIZE, 0/*flags*/, |
|
|
|
recvlen = recvfrom(in_sock, udp_buf, N2N_PKT_BUF_SIZE, 0/*flags*/, |
|
|
|
(struct sockaddr *)&sender_sock, (socklen_t*)&i); |
|
|
|
|
|
|
|
if(recvlen < 0) |
|
|
|
{ |
|
|
|
if(recvlen < 0) { |
|
|
|
traceEvent(TRACE_ERROR, "recvfrom failed with %s", strerror(errno)); |
|
|
|
|
|
|
|
return; /* failed to receive data from UDP */ |
|
|
@ -1264,7 +1251,7 @@ static void readFromIPSocket(n2n_edge_t * eee) { |
|
|
|
|
|
|
|
/* REVISIT: when UDP/IPv6 is supported we will need a flag to indicate which
|
|
|
|
* IP transport version the packet arrived on. May need to UDP sockets. */ |
|
|
|
sender.family = AF_INET; /* udp_sock was opened PF_INET v4 */ |
|
|
|
sender.family = AF_INET; /* UDP socket was opened PF_INET v4 */ |
|
|
|
sender.port = ntohs(sender_sock.sin_port); |
|
|
|
memcpy(&(sender.addr.v4), &(sender_sock.sin_addr.s_addr), IPV4_SIZE); |
|
|
|
|
|
|
@ -1343,9 +1330,7 @@ static void readFromIPSocket(n2n_edge_t * eee) { |
|
|
|
decode_REGISTER_ACK(&ra, &cmn, udp_buf, &rem, &idx); |
|
|
|
|
|
|
|
if(ra.sock.family) |
|
|
|
{ |
|
|
|
orig_sender = &(ra.sock); |
|
|
|
} |
|
|
|
|
|
|
|
traceEvent(TRACE_INFO, "Rx REGISTER_ACK src=%s dst=%s from peer %s (%s)", |
|
|
|
macaddr_str(mac_buf1, ra.srcMac), |
|
|
@ -1448,6 +1433,9 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) { |
|
|
|
FD_SET(eee->udp_sock, &socket_mask); |
|
|
|
FD_SET(eee->udp_mgmt_sock, &socket_mask); |
|
|
|
max_sock = max(eee->udp_sock, eee->udp_mgmt_sock); |
|
|
|
FD_SET(eee->udp_multicast_sock, &socket_mask); |
|
|
|
max_sock = max(eee->udp_sock, eee->udp_multicast_sock); |
|
|
|
|
|
|
|
#ifndef WIN32 |
|
|
|
FD_SET(eee->device.fd, &socket_mask); |
|
|
|
max_sock = max(max_sock, eee->device.fd); |
|
|
@ -1459,22 +1447,26 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) { |
|
|
|
nowTime=time(NULL); |
|
|
|
|
|
|
|
/* Make sure ciphers are updated before the packet is treated. */ |
|
|
|
if((nowTime - lastTransop) > TRANSOP_TICK_INTERVAL) |
|
|
|
{ |
|
|
|
if((nowTime - lastTransop) > TRANSOP_TICK_INTERVAL) { |
|
|
|
lastTransop = nowTime; |
|
|
|
|
|
|
|
n2n_tick_transop(eee, nowTime); |
|
|
|
} |
|
|
|
|
|
|
|
if(rc > 0) |
|
|
|
{ |
|
|
|
if(rc > 0) { |
|
|
|
/* Any or all of the FDs could have input; check them all. */ |
|
|
|
|
|
|
|
if(FD_ISSET(eee->udp_sock, &socket_mask)) |
|
|
|
{ |
|
|
|
/* Read a cooked socket from the internet socket. Writes on the TAP
|
|
|
|
if(FD_ISSET(eee->udp_sock, &socket_mask)) { |
|
|
|
/* Read a cooked socket from the internet socket (unicast). Writes on the TAP
|
|
|
|
* socket. */ |
|
|
|
readFromIPSocket(eee); |
|
|
|
readFromIPSocket(eee, eee->udp_sock); |
|
|
|
} |
|
|
|
|
|
|
|
if(FD_ISSET(eee->udp_multicast_sock, &socket_mask)) { |
|
|
|
/* Read a cooked socket from the internet socket (multicast). Writes on the TAP
|
|
|
|
* socket. */ |
|
|
|
traceEvent(TRACE_WARNING, "**** Received packet from multicast socket ****"); |
|
|
|
readFromIPSocket(eee, eee->udp_multicast_sock); |
|
|
|
} |
|
|
|
|
|
|
|
#ifdef __ANDROID_NDK__ |
|
|
@ -1500,8 +1492,6 @@ int run_edge_loop(n2n_edge_t * eee, int *keep_running) { |
|
|
|
} |
|
|
|
|
|
|
|
/* Finished processing select data. */ |
|
|
|
|
|
|
|
|
|
|
|
update_supernode_reg(eee, nowTime); |
|
|
|
|
|
|
|
numPurged = purge_expired_registrations(&(eee->known_peers)); |
|
|
@ -1593,9 +1583,7 @@ int edge_init_keyschedule(n2n_edge_t * eee) { |
|
|
|
n2n_tick_transop(eee, now); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
traceEvent(TRACE_ERROR, "Failed to process '%s'", eee->keyschedule); |
|
|
|
} |
|
|
|
|
|
|
|
return retval; |
|
|
|
} |
|
|
@ -1610,6 +1598,9 @@ void edge_term(n2n_edge_t * eee) { |
|
|
|
if(eee->udp_mgmt_sock >= 0) |
|
|
|
closesocket(eee->udp_mgmt_sock); |
|
|
|
|
|
|
|
if(eee->udp_multicast_sock >= 0) |
|
|
|
closesocket(eee->udp_multicast_sock); |
|
|
|
|
|
|
|
clear_peer_list(&(eee->pending_peers)); |
|
|
|
clear_peer_list(&(eee->known_peers)); |
|
|
|
|
|
|
@ -1670,10 +1661,25 @@ int quick_edge_init(char *device_name, char *community_name, |
|
|
|
return(-3); |
|
|
|
|
|
|
|
eee.udp_mgmt_sock = open_socket(0 /* any port */, 0 /* bind LOOPBACK */); |
|
|
|
|
|
|
|
if(eee.udp_mgmt_sock < 0) |
|
|
|
return(-4); |
|
|
|
|
|
|
|
eee.udp_multicast_sock = open_socket(N2N_MULTICAST_PORT, 1 /* bind ANY */); |
|
|
|
if(eee.udp_multicast_sock < 0) |
|
|
|
return(-5); |
|
|
|
else { |
|
|
|
/* Bind eee.udp_multicast_sock to multicast group */ |
|
|
|
struct ip_mreq mreq; |
|
|
|
|
|
|
|
mreq.imr_multiaddr.s_addr = inet_addr(N2N_MULTICAST_GROUP); |
|
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
|
|
|
if (setsockopt(eee.udp_multicast_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { |
|
|
|
traceEvent(TRACE_ERROR, "Failed to bind to local multicast group %s:%u", |
|
|
|
N2N_MULTICAST_GROUP, N2N_MULTICAST_PORT); |
|
|
|
return(-6); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
update_supernode_reg(&eee, time(NULL)); |
|
|
|
|
|
|
|
return(run_edge_loop(&eee, keep_on_running)); |
|
|
|