+bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
+ /* If there already is a lot of data in the outbuf buffer, discard this packet.
+ We use a very simple Random Early Drop algorithm. */
+
+ if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX) {
+ return true;
+ }
+
+ if(!send_request(c, "%d %d", PACKET, packet->len)) {
+ return false;
+ }
+
+ return send_meta(c, (char *)DATA(packet), packet->len);
+}
+
+bool tcppacket_h(connection_t *c, const char *request) {
+ short int len;
+
+ if(sscanf(request, "%*d %hd", &len) != 1) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
+ c->hostname);
+ return false;
+ }
+
+ /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
+
+ c->tcplen = len;
+
+ return true;
+}
+
+bool send_sptps_tcppacket(connection_t *c, const char *packet, int len) {
+ /* If there already is a lot of data in the outbuf buffer, discard this packet.
+ We use a very simple Random Early Drop algorithm. */
+
+ if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX) {
+ return true;
+ }
+
+ if(!send_request(c, "%d %d", SPTPS_PACKET, len)) {
+ return false;
+ }
+
+ send_meta_raw(c, packet, len);
+ return true;
+}
+
+bool sptps_tcppacket_h(connection_t *c, const char *request) {
+ short int len;
+
+ if(sscanf(request, "%*d %hd", &len) != 1) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "SPTPS_PACKET", c->name,
+ c->hostname);
+ return false;
+ }
+
+ /* Set sptpslen to len, this will tell receive_meta() that a SPTPS packet is coming. */
+
+ c->sptpslen = len;
+
+ return true;
+}
+
+/* Transmitting UDP information */
+
+bool send_udp_info(node_t *from, node_t *to) {
+ /* If there's a static relay in the path, there's no point in sending the message
+ farther than the static relay. */
+ to = (to->via == myself) ? to->nexthop : to->via;
+
+ if(to == NULL) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Something went wrong when selecting relay - possible fake UDP_INFO");
+ return false;
+ }
+
+ /* Skip cases where sending UDP info messages doesn't make sense.
+ This is done here in order to avoid repeating the same logic in multiple callsites. */
+
+ if(to == myself) {
+ return true;
+ }
+
+ if(!to->status.reachable) {
+ return true;
+ }
+
+ if(from == myself) {
+ if(to->connection) {
+ return true;
+ }
+
+ struct timeval elapsed;
+
+ timersub(&now, &to->udp_info_sent, &elapsed);
+
+ if(elapsed.tv_sec < udp_info_interval) {
+ return true;
+ }
+ }
+
+ if((myself->options | from->options | to->options) & OPTION_TCPONLY) {
+ return true;
+ }
+
+ if((to->nexthop->options >> 24) < 5) {
+ return true;
+ }
+
+ char *from_address, *from_port;
+ /* If we're the originator, the address we use is irrelevant
+ because the first intermediate node will ignore it.
+ We use our local address as it somewhat makes sense
+ and it's simpler than introducing an encoding for "null" addresses anyway. */
+ sockaddr2str((from != myself) ? &from->address : &to->nexthop->connection->edge->local_address, &from_address, &from_port);
+
+ bool x = send_request(to->nexthop->connection, "%d %s %s %s %s", UDP_INFO, from->name, to->name, from_address, from_port);
+
+ free(from_address);
+ free(from_port);
+
+ if(from == myself) {
+ to->udp_info_sent = now;
+ }
+
+ return x;
+}
+
+bool udp_info_h(connection_t *c, const char *request) {
+ char from_name[MAX_STRING_SIZE];
+ char to_name[MAX_STRING_SIZE];
+ char from_address[MAX_STRING_SIZE];
+ char from_port[MAX_STRING_SIZE];
+
+ if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, from_address, from_port) != 4) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "UDP_INFO", c->name, c->hostname);
+ return false;
+ }
+
+ if(!check_id(from_name) || !check_id(to_name)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "UDP_INFO", c->name, c->hostname, "invalid name");
+ return false;
+ }