+bool send_sptps_data(void *handle, uint8_t type, const char *data, size_t len) {
+ node_t *to = handle;
+
+ if(type >= SPTPS_HANDSHAKE) {
+ char buf[len * 4 / 3 + 5];
+ b64encode(data, buf, len);
+ if(!to->status.validkey)
+ return send_request(to->nexthop->connection, "%d %s %s %s -1 -1 -1 -1", ANS_KEY, myself->name, to->name, buf);
+ else
+ return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, to->name, REQ_SPTPS, buf);
+ }
+
+ /* Send the packet */
+
+ struct sockaddr *sa;
+ socklen_t sl;
+ int sock;
+
+ sa = &(to->address.sa);
+ sl = SALEN(to->address.sa);
+ sock = to->sock;
+
+ if(sendto(listen_socket[sock].udp, data, len, 0, sa, sl) < 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 char *data, uint16_t len) {
+ node_t *from = handle;
+
+ if(type == SPTPS_HANDSHAKE) {
+ from->status.validkey = true;
+ 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;
+ inpkt.len = len;
+ memcpy(inpkt.data, data, len);
+
+ if(type == PKT_PROBE) {
+ mtu_probe_h(from, &inpkt, len);
+ return true;
+
+ }
+ if(type != 0) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Unexpected SPTPS record type %d len %d from %s (%s)", type, len, from->name, from->hostname);
+ return false;
+ }
+
+ receive_packet(from, &inpkt);
+ return true;
+}
+