summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton <anton@picapica.im>2026-11-24 01:29:01 +0400
committerAnton <anton@picapica.im>2026-11-24 01:29:47 +0400
commit908460d3cead2d7df781d86120d7b0b5a568aac1 (patch)
treefb9c03c64eeff5df36a4801918c7a2d9fe82bec2
parent07a182be13a101224d07b5895e219979363415f4 (diff)
downloadpica-pica-908460d3cead2d7df781d86120d7b0b5a568aac1.tar.gz
pica-pica-908460d3cead2d7df781d86120d7b0b5a568aac1.zip
pica-client: filter private range direct c2c addrs
only connect to those that fall into subnets that connecting host is attached to, limit private range connect timeouts to 200ms.
-rw-r--r--PICA_client.c168
-rw-r--r--PICA_client.h25
-rw-r--r--PICA_media.c123
-rw-r--r--PICA_media.h12
-rw-r--r--PICA_netconf.c132
-rw-r--r--PICA_netconf.h19
-rw-r--r--doc/proto-doc-latest.txt16
7 files changed, 464 insertions, 31 deletions
diff --git a/PICA_client.c b/PICA_client.c
index 6ee2b06..d48f1cd 100644
--- a/PICA_client.c
+++ b/PICA_client.c
@@ -632,9 +632,25 @@ static unsigned int procmsg_INITRESP_c2c(unsigned char* buf, unsigned int nb, vo
return 1;
}
+/* "address:port", for the log; buf has PICA_DIRECTC2C_ADDRSTRSIZE octets */
+#define PICA_DIRECTC2C_ADDRSTRSIZE (INET6_ADDRSTRLEN + 6)
+
+static const char *directc2c_addrstr(const struct sockaddr_in *a, char *buf)
+{
+ char ip[INET6_ADDRSTRLEN];
+
+ if (!inet_ntop(AF_INET, &a->sin_addr, ip, sizeof(ip)))
+ strcpy(ip, "?");
+
+ sprintf(buf, "%s:%u", ip, (unsigned int)ntohs(a->sin_port));
+
+ return buf;
+}
+
static int directc2c_connect_next(struct PICA_directc2c *dc2c, struct PICA_c2c *c2c)
{
PICA_TRACEFUNC
+ char addrstr[PICA_DIRECTC2C_ADDRSTRSIZE];
int ret;
if (dc2c->ssl)
@@ -651,9 +667,10 @@ static int directc2c_connect_next(struct PICA_directc2c *dc2c, struct PICA_c2c *
dc2c->addrpos += 2;
if (dc2c->addrpos + PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE > *(uint16_t*)dc2c->addrlist + 2)
+ {
+ fprintf(stderr, "directc2c: no more addresses to try\n");
return PICA_ERRNOTFOUND;
-
- dc2c->attempt_started = time(0);
+ }
switch(dc2c->addrlist[dc2c->addrpos])
{
@@ -667,7 +684,15 @@ static int directc2c_connect_next(struct PICA_directc2c *dc2c, struct PICA_c2c *
dc2c->addr.sin_addr.s_addr = *((in_addr_t*)(dc2c->addrlist + dc2c->addrpos + 1));
dc2c->addr.sin_port = *((uint16_t*)(dc2c->addrlist + dc2c->addrpos + 5));
- printf("received direct IPv4 addr %s port %i\n", inet_ntoa(dc2c->addr.sin_addr), ntohs(dc2c->addr.sin_port));
+ /* the list only holds private addresses of a network this host is
+ * on, see directc2c_addrlist_ipv4() */
+ dc2c->attempt_started = PICA_media_now();
+ dc2c->attempt_deadline = dc2c->attempt_started
+ + (PICA_is_private_addr_ipv4(dc2c->addr.sin_addr.s_addr)
+ ? PICA_DIRECTC2C_LAN_CONNECT_TIMEOUT : PICA_DIRECTC2C_CONNECT_TIMEOUT);
+
+ fprintf(stderr, "directc2c: connecting to %s, timeout %u ms\n", directc2c_addrstr(&dc2c->addr, addrstr),
+ (unsigned int)(dc2c->attempt_deadline - dc2c->attempt_started));
dc2c->state = PICA_DIRECTC2C_CONNSTATE_CONNECTING;
@@ -676,7 +701,10 @@ static int directc2c_connect_next(struct PICA_directc2c *dc2c, struct PICA_c2c *
ret = process_first_async_connect_result(ret);
if (ret != PICA_OK)
+ {
+ fprintf(stderr, "directc2c: connection to %s failed\n", addrstr);
dc2c->state = PICA_DIRECTC2C_CONNSTATE_FAILED;
+ }
dc2c->addrpos += PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE;
break;
@@ -688,8 +716,56 @@ static int directc2c_connect_next(struct PICA_directc2c *dc2c, struct PICA_c2c *
return PICA_OK;
}
+/* Whether an IPv4 address of a received address list is worth connecting
+ * to. A private one is only if it is on a private network this host is on:
+ * anywhere else it is some other network's address, which leads nowhere or
+ * to the wrong host. Logs the decision.
+ */
+static int directc2c_addr_usable(const struct sockaddr_in *a, const in_addr_t *ifaddrs,
+ const in_addr_t *ifmasks, int ifcount)
+{
+ char addrstr[PICA_DIRECTC2C_ADDRSTRSIZE];
+ char ifaddr[INET6_ADDRSTRLEN], ifmask[INET6_ADDRSTRLEN];
+ int i;
+
+ directc2c_addrstr(a, addrstr);
+
+ if (!PICA_is_private_addr_ipv4(a->sin_addr.s_addr))
+ {
+ fprintf(stderr, "directc2c: 0x0B address %s - public, will be tried\n", addrstr);
+ return 1;
+ }
+
+ if (ifcount == 0)
+ {
+ fprintf(stderr, "directc2c: 0x0B address %s - private, ignored: this host is on no private network\n", addrstr);
+ return 0;
+ }
+
+ i = PICA_find_subnet_ipv4(a->sin_addr.s_addr, ifaddrs, ifmasks, ifcount);
+
+ if (i < 0)
+ {
+ fprintf(stderr, "directc2c: 0x0B address %s - private, ignored: not on a private network of this host\n",
+ addrstr);
+ return 0;
+ }
+
+ if (!inet_ntop(AF_INET, &ifaddrs[i], ifaddr, sizeof(ifaddr)))
+ strcpy(ifaddr, "?");
+
+ if (!inet_ntop(AF_INET, &ifmasks[i], ifmask, sizeof(ifmask)))
+ strcpy(ifmask, "?");
+
+ fprintf(stderr, "directc2c: 0x0B address %s - private, on the network of local address %s netmask %s, will be tried\n",
+ addrstr, ifaddr, ifmask);
+
+ return 1;
+}
+
/* Picks the IPv4 elements out of a received address list, in their order,
- * into list, behind a length field in host byte order.
+ * into list, behind a length field in host byte order - those worth
+ * connecting to, see directc2c_addr_usable().
* Elements of the other types are stepped over by their size, see the 0xA4
* message. An element of an unknown type ends the list, as there is no
* knowing where it ends; so does a truncated one.
@@ -697,39 +773,70 @@ static int directc2c_connect_next(struct PICA_directc2c *dc2c, struct PICA_c2c *
*/
static unsigned int directc2c_addrlist_ipv4(const unsigned char *items, unsigned int len, uint8_t *list)
{
+ in_addr_t ifaddrs[PICA_IFADDRS_MAX], ifmasks[PICA_IFADDRS_MAX];
+ int ifcount;
unsigned int pos = 0;
uint16_t n = 0;
+ ifcount = PICA_get_private_ifaddrs_ipv4(ifaddrs, ifmasks, PICA_IFADDRS_MAX);
+
+ fprintf(stderr, "directc2c: received 0x0B, %u octets of addresses\n", len);
+
while (pos < len && n < PICA_DIRECTC2C_MAX_ADDRS)
{
switch (items[pos])
{
case PICA_PROTO_DIRECTC2C_IPV4:
+ {
+ struct sockaddr_in a;
+
if (pos + PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE > len)
+ {
+ fprintf(stderr, "directc2c: 0x0B truncated IPv4 address, end of list\n");
goto end;
+ }
+
+ memset(&a, 0, sizeof(a));
+ a.sin_family = AF_INET;
+ memcpy(&a.sin_addr.s_addr, items + pos + 1, 4);
+ memcpy(&a.sin_port, items + pos + 5, 2);
+
+ if (directc2c_addr_usable(&a, ifaddrs, ifmasks, ifcount))
+ {
+ memcpy(list + 2 + n * PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE,
+ items + pos, PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE);
+ n++;
+ }
- memcpy(list + 2 + n * PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE,
- items + pos, PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE);
- n++;
pos += PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE;
break;
+ }
case PICA_PROTO_DIRECTC2C_IPV6:
+ fprintf(stderr, "directc2c: 0x0B IPv6 address - ignored, not supported\n");
pos += PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV6_SIZE;
break;
case PICA_PROTO_DIRECTC2C_DNS:
if (pos + 1 >= len)
+ {
+ fprintf(stderr, "directc2c: 0x0B truncated DNS name, end of list\n");
goto end;
+ }
+ fprintf(stderr, "directc2c: 0x0B DNS name - ignored, not supported\n");
pos += 2 + items[pos + 1] + 2;
break;
default:
+ fprintf(stderr, "directc2c: 0x0B unknown address type 0x%02X, end of list\n", items[pos]);
goto end;
}
}
+ if (pos < len)
+ fprintf(stderr, "directc2c: 0x0B more than %i usable addresses, the rest is ignored\n", PICA_DIRECTC2C_MAX_ADDRS);
+
end:
*(uint16_t*)list = n * PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE;
@@ -3339,8 +3446,14 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
*pdprev = dnext;
d->next = NULL;
+ char addrstr[PICA_DIRECTC2C_ADDRSTRSIZE];
+
+ directc2c_addrstr(&d->addr, addrstr);
+
if ((c2c = find_matching_c2c(c2n, d)) && (PICA_verify_peer_cert_common(&d->peer_cert, d->ssl, c2c->peer_id) == PICA_OK))
{
+ fprintf(stderr, "directc2c: incoming connection from %s accepted\n", addrstr);
+
if (c2c->direct)
PICA_close_directc2c(c2c->direct);
@@ -3362,6 +3475,8 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
}
else
{
+ fprintf(stderr, "directc2c: incoming connection from %s rejected: %s\n", addrstr,
+ c2c ? "the certificate is not the peer's" : "no connection is waiting for it");
PICA_close_directc2c(d);
}
/* pdprev stays put: d was spliced out above */
@@ -3395,14 +3510,25 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
if (c2c->directc2c_state == PICA_DIRECTC2C_STATE_CONNECTING)
{
+ char addrstr[PICA_DIRECTC2C_ADDRSTRSIZE];
+
d = c2c->direct;
ret = PICA_OK;
- /* on to the next address, see PICA_DIRECTC2C_CONNECT_TIMEOUT */
+ directc2c_addrstr(&d->addr, addrstr);
+
+ /* On to the next address, see PICA_DIRECTC2C_CONNECT_TIMEOUT -
+ * unless the socket has something for us: the event loop may
+ * not have run for a while, and a connection that has made it
+ * meanwhile is not a late one.
+ */
if ((d->state == PICA_DIRECTC2C_CONNSTATE_CONNECTING || d->state == PICA_DIRECTC2C_CONNSTATE_WAITINGTLS)
- && time(0) - d->attempt_started > PICA_DIRECTC2C_CONNECT_TIMEOUT)
+ && PICA_media_now() >= d->attempt_deadline
+ && !FD_ISSET(d->sck, wfds) && !FD_ISSET(d->sck, rfds))
{
- printf("direct connection to %s port %i timed out\n", inet_ntoa(d->addr.sin_addr), ntohs(d->addr.sin_port));
+ fprintf(stderr, "directc2c: connection to %s failed: %s timed out after %u ms\n", addrstr,
+ d->state == PICA_DIRECTC2C_CONNSTATE_CONNECTING ? "TCP connection" : "TLS handshake",
+ (unsigned int)(PICA_media_now() - d->attempt_started));
d->state = PICA_DIRECTC2C_CONNSTATE_FAILED;
}
@@ -3416,10 +3542,26 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
if (ret == PICA_OK)
{
+ fprintf(stderr, "directc2c: TCP connection to %s made in %u ms, TLS handshake\n", addrstr,
+ (unsigned int)(PICA_media_now() - d->attempt_started));
+
+ /* the TLS handshake gets the rest of the full timeout,
+ * also for a private address, see
+ * PICA_DIRECTC2C_LAN_CONNECT_TIMEOUT */
+ d->attempt_deadline = d->attempt_started + PICA_DIRECTC2C_CONNECT_TIMEOUT;
+
ret = directc2c_stage2_starttls(d, c2c);
+
+ if (ret != PICA_OK)
+ {
+ fprintf(stderr, "directc2c: connection to %s failed: TLS could not be started\n", addrstr);
+ d->state = PICA_DIRECTC2C_CONNSTATE_FAILED;
+ }
}
else
{
+ fprintf(stderr, "directc2c: connection to %s failed: TCP connection refused or unreachable\n",
+ addrstr);
d->state = PICA_DIRECTC2C_CONNSTATE_FAILED;
}
}
@@ -3436,6 +3578,7 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
if (ret != PICA_OK)
{
+ fprintf(stderr, "directc2c: connection to %s failed: TLS handshake failed\n", addrstr);
d->state = PICA_DIRECTC2C_CONNSTATE_FAILED;
}
@@ -3446,6 +3589,7 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
if (ret != PICA_OK)
{
+ fprintf(stderr, "directc2c: connection to %s failed: the certificate is not the peer's\n", addrstr);
d->state = PICA_DIRECTC2C_CONNSTATE_FAILED;
break;
}
@@ -3454,10 +3598,14 @@ static void process_directc2c(struct PICA_c2n *c2n, fd_set *rfds, fd_set *wfds)
if (ret != PICA_OK)
{
+ fprintf(stderr, "directc2c: connection to %s failed: could not send 0x0D\n", addrstr);
d->state = PICA_DIRECTC2C_CONNSTATE_FAILED;
break;
}
+ fprintf(stderr, "directc2c: connection to %s established in %u ms\n", addrstr,
+ (unsigned int)(PICA_media_now() - d->attempt_started));
+
d->state = PICA_DIRECTC2C_CONNSTATE_ACTIVE;
}
diff --git a/PICA_client.h b/PICA_client.h
index 1a8798f..2f7441d 100644
--- a/PICA_client.h
+++ b/PICA_client.h
@@ -157,12 +157,20 @@ typedef u_short in_port_t;
#define PICA_DIRECTC2C_MAX_ACCEPTED 32
/* How long an outgoing direct connection may take to connect and finish its
- * TLS handshake before the next announced address is tried. Without it, an
- * address that silently drops the packets - a private address of the peer's
- * network, seen from another one - would hold up the addresses after it for
- * as long as the system keeps retrying the connection, minutes.
+ * TLS handshake before the next announced address is tried, in milliseconds.
+ * Without it, an address that silently drops the packets would hold up the
+ * addresses after it for as long as the system keeps retrying the
+ * connection, minutes.
*/
-#define PICA_DIRECTC2C_CONNECT_TIMEOUT 5
+#define PICA_DIRECTC2C_CONNECT_TIMEOUT 5000
+
+/* How long the TCP connection to a private address may take, in
+ * milliseconds. Only one on a private network this host is on is tried at
+ * all, and there the round trip is a matter of a few milliseconds; the TLS
+ * handshake, which can take longer on a slow machine, then gets the rest of
+ * PICA_DIRECTC2C_CONNECT_TIMEOUT.
+ */
+#define PICA_DIRECTC2C_LAN_CONNECT_TIMEOUT 200
/* Most addresses of a received 0x0B message that are tried, and most that
* a listener announces.
@@ -327,10 +335,13 @@ struct PICA_directc2c
enum PICA_directc2c_connection_state state;
- /* when the connection to the current address was started, see
+ /* PICA_media_now() when the connection to the current address was
+ * started, and when it has to be connected by - the TCP connection, or
+ * in PICA_DIRECTC2C_CONNSTATE_WAITINGTLS the TLS handshake as well, see
* PICA_DIRECTC2C_CONNECT_TIMEOUT
*/
- time_t attempt_started;
+ uint64_t attempt_started;
+ uint64_t attempt_deadline;
/* When this incoming connection was accepted, used to reap it if its TLS
* handshake never completes. Only meaningful for accepted (incoming)
diff --git a/PICA_media.c b/PICA_media.c
index 658eba7..745b0e4 100644
--- a/PICA_media.c
+++ b/PICA_media.c
@@ -243,17 +243,77 @@ static struct PICA_media_channel *media_channel_new(struct PICA_c2c *c2c)
return m;
}
+/* "address:port", for the log; buf has PICA_MEDIA_ADDRSTRSIZE octets */
+#define PICA_MEDIA_ADDRSTRSIZE (INET6_ADDRSTRLEN + 6)
+
+static const char *media_addrstr(const struct sockaddr_in *a, char *buf)
+{
+ const unsigned char *ip = (const unsigned char*)&a->sin_addr.s_addr;
+
+ sprintf(buf, "%u.%u.%u.%u:%u", ip[0], ip[1], ip[2], ip[3], (unsigned int)ntohs(a->sin_port));
+
+ return buf;
+}
+
/* Whether an address from the peer's 0x70/0x71 is one worth sending probes
- * to: a unicast address of a host, not of this one, and a port.
+ * to: a unicast address of a host, and a port. A private one only if it is
+ * on one of the private networks this host is on, given by ifaddrs/ifmasks
+ * - anywhere else it belongs to some other network, as for 0x0B. Logs the
+ * decision.
*/
-static int is_probe_target(const struct sockaddr_in *a)
+static int is_probe_target(const struct sockaddr_in *a, const in_addr_t *ifaddrs,
+ const in_addr_t *ifmasks, int ifcount)
{
in_addr_t addr = a->sin_addr.s_addr;
+ char addrstr[PICA_MEDIA_ADDRSTRSIZE];
+ int i;
+
+ media_addrstr(a, addrstr);
if (a->sin_port == 0)
+ {
+ fprintf(stderr, "media: call address %s - ignored: no port number\n", addrstr);
+ return 0;
+ }
+
+ if (!PICA_is_private_addr_ipv4(addr))
+ {
+ if (PICA_is_reserved_addr_ipv4(addr))
+ {
+ fprintf(stderr, "media: call address %s - ignored: reserved address\n", addrstr);
+ return 0;
+ }
+
+ fprintf(stderr, "media: call address %s - public, will be probed for %u ms\n", addrstr,
+ PICA_MEDIA_PUNCH_TIMEOUT);
+ return 1;
+ }
+
+ if (ifcount == 0)
+ {
+ fprintf(stderr, "media: call address %s - private, ignored: this host is on no private network\n", addrstr);
return 0;
+ }
+
+ i = PICA_find_subnet_ipv4(addr, ifaddrs, ifmasks, ifcount);
+
+ if (i < 0)
+ {
+ fprintf(stderr, "media: call address %s - private, ignored: not on a private network of this host\n",
+ addrstr);
+ return 0;
+ }
- return PICA_is_private_addr_ipv4(addr) || !PICA_is_reserved_addr_ipv4(addr);
+ {
+ const unsigned char *ip = (const unsigned char*)&ifaddrs[i];
+ const unsigned char *mask = (const unsigned char*)&ifmasks[i];
+
+ fprintf(stderr, "media: call address %s - private, on the network of local address %u.%u.%u.%u "
+ "netmask %u.%u.%u.%u, will be probed for %u ms\n", addrstr,
+ ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2], mask[3], PICA_MEDIA_LAN_PROBE_TIMEOUT);
+ }
+
+ return 1;
}
/* Stops a request for the call socket's address still waiting for the node,
@@ -435,6 +495,8 @@ unsigned int PICA_media_call_write_info(struct PICA_c2c *c2c, unsigned char *buf
int PICA_media_call_read_info(struct PICA_c2c *c2c, const unsigned char *buf, unsigned int len)
{
struct PICA_media_call *mc = c2c->media_call;
+ in_addr_t ifaddrs[PICA_IFADDRS_MAX], ifmasks[PICA_IFADDRS_MAX];
+ int ifcount;
unsigned int pos;
int addresses = 0;
@@ -450,6 +512,8 @@ int PICA_media_call_read_info(struct PICA_c2c *c2c, const unsigned char *buf, un
memcpy(mc->peer_token, buf, PICA_PROTO_CALL_TOKENSIZE);
pos = PICA_PROTO_CALL_TOKENSIZE;
+ ifcount = PICA_get_private_ifaddrs_ipv4(ifaddrs, ifmasks, PICA_IFADDRS_MAX);
+
while (pos < len && addresses < PICA_PROTO_CALL_MAXADDRS)
{
struct sockaddr_in *a;
@@ -467,7 +531,7 @@ int PICA_media_call_read_info(struct PICA_c2c *c2c, const unsigned char *buf, un
memcpy(&a->sin_addr.s_addr, buf + pos + 1, 4);
memcpy(&a->sin_port, buf + pos + 5, 2);
- if (is_probe_target(a))
+ if (is_probe_target(a, ifaddrs, ifmasks, ifcount))
mc->peer_addrs_count++;
pos += PICA_PROTO_DIRECTC2C_ADDRLIST_ITEM_IPV4_SIZE;
@@ -646,20 +710,38 @@ static void media_send_probe_to(struct PICA_media_channel *m, const struct socka
static void media_send_probes(struct PICA_media_channel *m, uint64_t now)
{
struct PICA_media_call *mc = m->c2c->media_call;
+ int lan_due, public_due;
int i;
- if (m->dtls_seen || now - m->last_probe < PICA_MEDIA_PROBE_INTERVAL)
+ if (m->dtls_seen)
return;
- m->last_probe = now;
+ public_due = now - m->last_probe >= PICA_MEDIA_PROBE_INTERVAL;
- if (m->state == PICA_MEDIA_STATE_PUNCHING)
+ if (m->state != PICA_MEDIA_STATE_PUNCHING)
{
- for (i = 0; i < mc->peer_addrs_count; i++)
- media_send_probe_to(m, &mc->peer_addrs[i]);
+ if (public_due)
+ {
+ m->last_probe = now;
+ media_send_probe_to(m, NULL);
+ }
+
+ return;
}
- else
- media_send_probe_to(m, NULL);
+
+ /* the private addresses only at first, see PICA_MEDIA_LAN_PROBE_TIMEOUT */
+ lan_due = now - m->punch_started <= PICA_MEDIA_LAN_PROBE_TIMEOUT
+ && now - m->last_lan_probe >= PICA_MEDIA_LAN_PROBE_INTERVAL;
+
+ if (lan_due)
+ m->last_lan_probe = now;
+
+ if (public_due)
+ m->last_probe = now;
+
+ for (i = 0; i < mc->peer_addrs_count; i++)
+ if (PICA_is_private_addr_ipv4(mc->peer_addrs[i].sin_addr.s_addr) ? lan_due : public_due)
+ media_send_probe_to(m, &mc->peer_addrs[i]);
}
/* Attempt 2, by hole punching between the call sockets. */
@@ -683,11 +765,14 @@ static int media_punch_start(struct PICA_c2c *c2c)
m->owns_sck = 0;
m->is_dtls_client = mc->is_caller;
m->state = PICA_MEDIA_STATE_PUNCHING;
- m->deadline = PICA_media_now() + PICA_MEDIA_PUNCH_TIMEOUT;
+ m->punch_started = PICA_media_now();
+ m->deadline = m->punch_started + PICA_MEDIA_PUNCH_TIMEOUT;
c2c->media = m;
- media_send_probes(m, PICA_media_now());
+ fprintf(stderr, "media: hole punching, probing %i addresses of the peer\n", mc->peer_addrs_count);
+
+ media_send_probes(m, m->punch_started);
return PICA_OK;
}
@@ -831,6 +916,13 @@ static void media_activate(struct PICA_media_channel *m)
m->state = PICA_MEDIA_STATE_ACTIVE;
+ {
+ char addrstr[PICA_MEDIA_ADDRSTRSIZE];
+
+ fprintf(stderr, "media: connection with %s established by attempt %i\n",
+ media_addrstr(&m->peer_addr, addrstr), m->attempt);
+ }
+
PICA_media_transport_changed(m->c2c, PICA_CALL_TRANSPORT_MEDIAC2C, SSL_get_cipher_name(m->ssl));
}
@@ -932,8 +1024,13 @@ static int is_our_probe(struct PICA_media_channel *m, const unsigned char *buf,
*/
static void media_punch_select(struct PICA_media_channel *m, const struct sockaddr_in *from)
{
+ char addrstr[PICA_MEDIA_ADDRSTRSIZE];
+
PICA_TRACEFUNC
+ fprintf(stderr, "media: hole punching, the peer's probe arrived from %s after %u ms, selected\n",
+ media_addrstr(from, addrstr), (unsigned int)(PICA_media_now() - m->punch_started));
+
m->peer_addr = *from;
if (connect(m->sck, (const struct sockaddr*)&m->peer_addr, sizeof(m->peer_addr)) == SOCKET_ERROR)
diff --git a/PICA_media.h b/PICA_media.h
index b63fb65..f17b6ce 100644
--- a/PICA_media.h
+++ b/PICA_media.h
@@ -72,6 +72,15 @@
#define PICA_MEDIA_PUNCH_TIMEOUT 10000
/* and the handshake has this long to complete once it is */
#define PICA_MEDIA_PUNCH_HANDSHAKE_TIMEOUT 10000
+/* A private address of the peer is only probed if it is on a private network
+ * this host is on, and there for this long from the start of attempt 2, as
+ * often as given below: a probe that gets through at all does so within a few
+ * milliseconds on a local network. One arriving at the peer's socket before
+ * the peer starts sending probes waits there for it, so a peer starting late
+ * still selects the address - and answers from it.
+ */
+#define PICA_MEDIA_LAN_PROBE_TIMEOUT 200
+#define PICA_MEDIA_LAN_PROBE_INTERVAL 50
/* How long 0x70 and 0x71 wait for the node to report the address of the
* media socket before they are sent without it, in milliseconds.
@@ -196,6 +205,9 @@ struct PICA_media_channel
/* attempt 2 */
uint64_t last_probe;
+ /* when the probes started, and the last one to a private address */
+ uint64_t punch_started;
+ uint64_t last_lan_probe;
/* the first DTLS datagram has arrived from the selected address, the
* peer no longer needs probes */
int dtls_seen;
diff --git a/PICA_netconf.c b/PICA_netconf.c
index 1407834..2ada9c3 100644
--- a/PICA_netconf.c
+++ b/PICA_netconf.c
@@ -194,6 +194,138 @@ int PICA_get_ifaddrs_ipv4(in_addr_t *addrs, int maxcount)
}
#endif
+/* Stores a private interface address with its mask, skipping duplicates
+ * and masks that would make every address look local.
+ */
+static void private_ifaddr_add(in_addr_t *addrs, in_addr_t *masks, int maxcount, int *count,
+ in_addr_t a, in_addr_t mask)
+{
+ int i;
+
+ if (!PICA_is_private_addr_ipv4(a) || mask == 0)
+ return;
+
+ for (i = 0; i < *count; i++)
+ if (addrs[i] == a)
+ return;
+
+ if (*count < maxcount)
+ {
+ addrs[*count] = a;
+ masks[*count] = mask;
+ (*count)++;
+ }
+}
+
+#ifndef WIN32
+int PICA_get_private_ifaddrs_ipv4(in_addr_t *addrs, in_addr_t *masks, int maxcount)
+{
+ struct ifaddrs *ifaddr, *ifa;
+ int count = 0;
+
+ if (!addrs || !masks || maxcount <= 0)
+ return 0;
+
+ if (getifaddrs(&ifaddr) == -1)
+ return 0;
+
+ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
+ {
+ if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET || ifa->ifa_netmask == NULL)
+ continue;
+
+ if (ifa->ifa_flags & IFF_LOOPBACK)
+ continue;
+
+ if (!((ifa->ifa_flags & IFF_RUNNING) && (ifa->ifa_flags & IFF_UP)))
+ continue;
+
+ private_ifaddr_add(addrs, masks, maxcount, &count,
+ ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr,
+ ((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr);
+ }
+
+ freeifaddrs(ifaddr);
+
+ return count;
+}
+#else
+#ifndef MIB_IPADDR_DISCONNECTED
+#define MIB_IPADDR_DISCONNECTED 0x0008
+#endif
+#ifndef MIB_IPADDR_DELETED
+#define MIB_IPADDR_DELETED 0x0040
+#endif
+
+/* GetAdaptersAddresses() only reports the prefix length from Vista on, and
+ * only to code built for Vista; GetIpAddrTable() has the mask on every
+ * version.
+ */
+int PICA_get_private_ifaddrs_ipv4(in_addr_t *addrs, in_addr_t *masks, int maxcount)
+{
+ PMIB_IPADDRTABLE table = NULL;
+ ULONG size = 0;
+ DWORD ret;
+ DWORD i;
+ int count = 0;
+ int tries;
+
+ if (!addrs || !masks || maxcount <= 0)
+ return 0;
+
+ /* as in PICA_get_ifaddrs_ipv4(), the size needed is only known by
+ * trying */
+ for (tries = 0; tries < 3; tries++)
+ {
+ ret = GetIpAddrTable(table, &size, FALSE);
+
+ if (ret == NO_ERROR)
+ break;
+
+ free(table);
+ table = NULL;
+
+ if (ret != ERROR_INSUFFICIENT_BUFFER)
+ return 0;
+
+ table = (PMIB_IPADDRTABLE)malloc(size);
+
+ if (!table)
+ return 0;
+ }
+
+ if (ret != NO_ERROR || !table)
+ {
+ free(table);
+ return 0;
+ }
+
+ for (i = 0; i < table->dwNumEntries; i++)
+ {
+ if (table->table[i].wType & (MIB_IPADDR_DISCONNECTED | MIB_IPADDR_DELETED))
+ continue;
+
+ private_ifaddr_add(addrs, masks, maxcount, &count,
+ (in_addr_t)table->table[i].dwAddr, (in_addr_t)table->table[i].dwMask);
+ }
+
+ free(table);
+
+ return count;
+}
+#endif
+
+int PICA_find_subnet_ipv4(in_addr_t addr, const in_addr_t *addrs, const in_addr_t *masks, int count)
+{
+ int i;
+
+ for (i = 0; i < count; i++)
+ if ((addr & masks[i]) == (addrs[i] & masks[i]))
+ return i;
+
+ return -1;
+}
+
in_addr_t PICA_guess_listening_addr_ipv4()
{
in_addr_t addrs[PICA_IFADDRS_MAX];
diff --git a/PICA_netconf.h b/PICA_netconf.h
index c6f4aa2..02a9afd 100644
--- a/PICA_netconf.h
+++ b/PICA_netconf.h
@@ -129,6 +129,25 @@ int PICA_is_private_addr_ipv4(in_addr_t);
*/
int PICA_get_ifaddrs_ipv4(in_addr_t *addrs, int maxcount);
+/**
+ * The private addresses (see PICA_is_private_addr_ipv4()) of the network
+ * interfaces that are up, with their netmasks, i.e. the private networks
+ * this host is on.
+ * \param addrs, masks filled with the addresses and their netmasks, network
+ * byte order
+ * \param maxcount size of addrs and masks
+ * \return number of addresses stored, 0 if there are none or the system
+ * cannot be asked
+ */
+int PICA_get_private_ifaddrs_ipv4(in_addr_t *addrs, in_addr_t *masks, int maxcount);
+
+/**
+ * Looks for the interface address of PICA_get_private_ifaddrs_ipv4() whose
+ * network addr is on.
+ * \return its index, or -1 if addr is on none of them
+ */
+int PICA_find_subnet_ipv4(in_addr_t addr, const in_addr_t *addrs, const in_addr_t *masks, int count);
+
in_addr_t PICA_guess_listening_addr_ipv4();
/**
diff --git a/doc/proto-doc-latest.txt b/doc/proto-doc-latest.txt
index 1929940..ac71d38 100644
--- a/doc/proto-doc-latest.txt
+++ b/doc/proto-doc-latest.txt
@@ -369,6 +369,13 @@ The connecting client tries the addresses one at a time, in the order of the
list. It skips the elements of a type it does not connect to (e.g. 0xA1 without
IPv6 connectivity), stepping over them by their size as given in the 0xA4
description; such elements are not an error.
+A private address (the subnets above, and 100.64.0.0/10) is only tried if it
+is on the network of a private address of one of the connecting client's own
+network interfaces, as given by that interface's netmask; any other private
+address belongs to some other network and is skipped without a connection
+attempt. Since such an address is on the local network, the TCP connection to
+it is given up after 200 milliseconds, while the TLS handshake that follows
+has as long as for any other address.
Having received the announcement, the other client can create a connection to
that address and close the connection through the node.
The announced port numbers are used both for TCP directc2c connections and for
@@ -625,7 +632,14 @@ list. Each side uses its media socket for the call - the one its address list
was made for - and the caller acts as the DTLS client.
1. Each side sends a media connection probe to every address of the peer's
-list, repeating every 200 milliseconds.
+list, repeating every 200 milliseconds. A private address in the list is
+handled as in the 0x0B message: it is skipped unless it is on the network of
+a private address of one of the side's own network interfaces. One that is
+not skipped is local, and is sent probes only during the first 200
+milliseconds, every 50 milliseconds. A probe that arrives before the
+receiving side has started sending its own waits on that side's media
+socket, so a side starting later than the other still selects the address in
+step 2 and answers from it.
2. When a side receives a probe carrying its own probe token, it selects the
address the probe came from. That address need not be in the peer's list: a
NAT can map the peer's socket to another port for this destination than for