+ return -1;
+}
+
+/* VPN packet I/O */
+
+static void receive_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *packet) {
+ logger(DEBUG_TRAFFIC, LOG_DEBUG, "Received packet of %d bytes from %s (%s)",
+ packet->len, n->name, n->hostname);
+
+ n->in_packets++;
+ n->in_bytes += packet->len;
+
+ route(mesh, n, packet);
+}
+
+static bool try_mac(meshlink_handle_t *mesh, node_t *n, const vpn_packet_t *inpkt) {
+ return sptps_verify_datagram(&n->sptps, inpkt->data, inpkt->len);
+}
+
+static void receive_udppacket(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *inpkt) {
+ if(!n->sptps.state) {
+ if(!n->status.waitingforkey) {
+ logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got packet from %s (%s) but we haven't exchanged keys yet", n->name, n->hostname);
+ send_req_key(mesh, n);
+ } else {
+ logger(DEBUG_TRAFFIC, LOG_DEBUG, "Got packet from %s (%s) but he hasn't got our key yet", n->name, n->hostname);
+ }
+ return;
+ }
+ sptps_receive_data(&n->sptps, inpkt->data, inpkt->len);
+}
+
+void receive_tcppacket(meshlink_handle_t *mesh, connection_t *c, const char *buffer, int len) {
+ vpn_packet_t outpkt;
+
+ if(len > sizeof outpkt.data)
+ return;
+
+ outpkt.len = len;
+ outpkt.tcp = true;
+ memcpy(outpkt.data, buffer, len);
+
+ receive_packet(mesh, c->node, &outpkt);
+}
+
+static void send_sptps_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *origpkt) {
+ if(!n->status.validkey) {
+ logger(DEBUG_TRAFFIC, LOG_INFO, "No valid key known yet for %s (%s)", n->name, n->hostname);
+ if(!n->status.waitingforkey)
+ send_req_key(mesh, n);
+ else if(n->last_req_key + 10 < mesh->loop.now.tv_sec) {
+ logger(DEBUG_ALWAYS, LOG_DEBUG, "No key from %s after 10 seconds, restarting SPTPS", n->name);
+ sptps_stop(&n->sptps);
+ n->status.waitingforkey = false;
+ send_req_key(mesh, n);
+ }
+ return;
+ }
+
+ uint8_t type = 0;
+
+ // If it's a probe, send it immediately without trying to compress it.
+ if(origpkt->probe) {
+ sptps_send_record(&n->sptps, PKT_PROBE, origpkt->data, origpkt->len);
+ return;
+ }
+
+ vpn_packet_t outpkt;
+
+ if(n->outcompression) {
+ int len = compress_packet(outpkt.data, origpkt->data, origpkt->len, n->outcompression);
+ if(len < 0) {
+ logger(DEBUG_TRAFFIC, LOG_ERR, "Error while compressing packet to %s (%s)", n->name, n->hostname);
+ } else if(len < origpkt->len) {
+ outpkt.len = len;
+ origpkt = &outpkt;
+ type |= PKT_COMPRESSED;
+ }
+ }
+
+ sptps_send_record(&n->sptps, type, origpkt->data, origpkt->len);
+ return;
+}
+
+static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const sockaddr_t **sa, int *sock) {
+ /* Latest guess */
+ *sa = &n->address;
+ *sock = n->sock;
+
+ /* If the UDP address is confirmed, use it. */
+ if(n->status.udp_confirmed)
+ return;
+
+ /* Send every third packet to n->address; that could be set
+ to the node's reflexive UDP address discovered during key
+ exchange. */
+
+ static int x = 0;
+ if(++x >= 3) {
+ x = 0;
+ return;
+ }
+
+ /* Otherwise, address are found in edges to this node.
+ So we pick a random edge and a random socket. */
+
+ int i = 0;
+ int j = rand() % n->edge_tree->count;
+ edge_t *candidate = NULL;
+
+ for splay_each(edge_t, e, n->edge_tree) {
+ if(i++ == j) {
+ candidate = e->reverse;
+ break;
+ }
+ }
+
+ if(candidate) {
+ *sa = &candidate->address;
+ *sock = rand() % mesh->listen_sockets;
+ }
+
+ /* Make sure we have a suitable socket for the chosen address */
+ if(mesh->listen_socket[*sock].sa.sa.sa_family != (*sa)->sa.sa_family) {
+ for(int i = 0; i < mesh->listen_sockets; i++) {
+ if(mesh->listen_socket[i].sa.sa.sa_family == (*sa)->sa.sa_family) {
+ *sock = i;
+ break;
+ }
+ }
+ }
+}
+
+static void choose_broadcast_address(meshlink_handle_t *mesh, const node_t *n, const sockaddr_t **sa, int *sock) {
+ static sockaddr_t broadcast_ipv4 = {
+ .in = {
+ .sin_family = AF_INET,
+ .sin_addr.s_addr = -1,
+ }
+ };
+
+ static sockaddr_t broadcast_ipv6 = {
+ .in6 = {
+ .sin6_family = AF_INET6,
+ .sin6_addr.s6_addr[0x0] = 0xff,
+ .sin6_addr.s6_addr[0x1] = 0x02,
+ .sin6_addr.s6_addr[0xf] = 0x01,
+ }
+ };
+
+ *sock = rand() % mesh->listen_sockets;
+
+ if(mesh->listen_socket[*sock].sa.sa.sa_family == AF_INET6) {
+ if(mesh->localdiscovery_address.sa.sa_family == AF_INET6) {
+ mesh->localdiscovery_address.in6.sin6_port = n->prevedge->address.in.sin_port;
+ *sa = &mesh->localdiscovery_address;
+ } else {
+ broadcast_ipv6.in6.sin6_port = n->prevedge->address.in.sin_port;
+ broadcast_ipv6.in6.sin6_scope_id = mesh->listen_socket[*sock].sa.in6.sin6_scope_id;
+ *sa = &broadcast_ipv6;
+ }
+ } else {
+ if(mesh->localdiscovery_address.sa.sa_family == AF_INET) {
+ mesh->localdiscovery_address.in.sin_port = n->prevedge->address.in.sin_port;
+ *sa = &mesh->localdiscovery_address;
+ } else {
+ broadcast_ipv4.in.sin_port = n->prevedge->address.in.sin_port;
+ *sa = &broadcast_ipv4;
+ }
+ }
+}
+
+static void send_udppacket(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *origpkt) {
+ if(!n->status.reachable) {
+ logger(DEBUG_TRAFFIC, LOG_INFO, "Trying to send UDP packet to unreachable node %s (%s)", n->name, n->hostname);
+ return;
+ }
+
+ return send_sptps_packet(mesh, n, origpkt);
+}
+
+bool send_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
+ node_t *to = handle;
+ meshlink_handle_t *mesh = to->mesh;
+
+ /* Send it via TCP if it is a handshake packet, TCPOnly is in use, or this packet is larger than the MTU. */
+
+ if(type >= SPTPS_HANDSHAKE || ((mesh->self->options | to->options) & OPTION_TCPONLY) || (type != PKT_PROBE && len > to->minmtu)) {
+ char buf[len * 4 / 3 + 5];
+ b64encode(data, buf, len);
+ /* If no valid key is known yet, send the packets using ANS_KEY requests,
+ to ensure we get to learn the reflexive UDP address. */
+ if(!to->status.validkey) {
+ to->incompression = mesh->self->incompression;
+ return send_request(mesh, to->nexthop->connection, "%d %s %s %s -1 -1 -1 %d", ANS_KEY, mesh->self->name, to->name, buf, to->incompression);
+ } else {
+ return send_request(mesh, to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_SPTPS, buf);
+ }
+ }
+
+ /* Otherwise, send the packet via UDP */
+
+ const sockaddr_t *sa;
+ int sock;
+
+ if(to->status.broadcast)
+ choose_broadcast_address(mesh, to, &sa, &sock);
+ else
+ choose_udp_address(mesh, to, &sa, &sock);
+
+ if(sendto(mesh->listen_socket[sock].udp.fd, data, len, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) {
+ if(sockmsgsize(sockerrno)) {
+ if(to->maxmtu >= len)
+ to->maxmtu = len - 1;
+ if(to->mtu >= len)
+ to->mtu = len - 1;
+ } else {
+ logger(DEBUG_TRAFFIC, LOG_WARNING, "Error sending UDP SPTPS packet to %s (%s): %s", to->name, to->hostname, sockstrerror(sockerrno));
+ return false;
+ }
+ }
+
+ return true;
+}
+
+bool receive_sptps_record(void *handle, uint8_t type, const void *data, uint16_t len) {
+ node_t *from = handle;
+ meshlink_handle_t *mesh = from->mesh;
+
+ if(type == SPTPS_HANDSHAKE) {
+ if(!from->status.validkey) {
+ from->status.validkey = true;
+ from->status.waitingforkey = false;
+ logger(DEBUG_META, LOG_INFO, "SPTPS key exchange with %s (%s) succesful", from->name, from->hostname);
+ }
+ return true;
+ }
+
+ if(len > MTU) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Packet from %s (%s) larger than maximum supported size (%d > %d)", from->name, from->hostname, len, MTU);
+ return false;
+ }
+
+ vpn_packet_t inpkt;
+
+ if(type == PKT_PROBE) {
+ inpkt.len = len;
+ inpkt.probe = true;
+ memcpy(inpkt.data, data, len);
+ mtu_probe_h(mesh, from, &inpkt, len);
+ return true;
+ } else {
+ inpkt.probe = false;
+ }
+
+ if(type & ~(PKT_COMPRESSED)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Unexpected SPTPS record type %d len %d from %s (%s)", type, len, from->name, from->hostname);
+ return false;
+ }
+
+ if(type & PKT_COMPRESSED) {
+ uint16_t ulen = uncompress_packet(inpkt.data, (const uint8_t *)data, len, from->incompression);
+ if(ulen < 0) {
+ return false;
+ } else {
+ inpkt.len = ulen;
+ }
+ if(inpkt.len > MAXSIZE)
+ abort();
+ } else {
+ memcpy(inpkt.data, data, len);
+ inpkt.len = len;
+ }
+
+ receive_packet(mesh, from, &inpkt);
+ return true;