X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_packet.c;h=9baaf4c8be4854bbe4bd3f1f7d1738fd46a2383e;hb=e5b9bd324cc24355956e9e59e5ec2df72cf9d469;hp=c8f48d32b1f9fd4682166c8a4574f3f8be7bdae8;hpb=43cf631bc10097448db041639ad07f84f647017e;p=tinc diff --git a/src/net_packet.c b/src/net_packet.c index c8f48d32..9baaf4c8 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -23,6 +23,7 @@ #include "system.h" #ifdef HAVE_ZLIB +#define ZLIB_CONST #include #endif @@ -101,6 +102,7 @@ static void udp_probe_timeout_handler(void *data) { logger(DEBUG_TRAFFIC, LOG_INFO, "Too much time has elapsed since last UDP ping response from %s (%s), stopping UDP communication", n->name, n->hostname); n->status.udp_confirmed = false; + n->udp_ping_rtt = -1; n->maxrecentlen = 0; n->mtuprobes = 0; n->minmtu = 0; @@ -150,7 +152,15 @@ static void udp_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { len = ntohs(len16); } - logger(DEBUG_TRAFFIC, LOG_INFO, "Got type %d UDP probe reply %d from %s (%s)", DATA(packet)[0], len, n->name, n->hostname); + if(n->udp_ping_sent.tv_sec != 0) { // a probe in flight + gettimeofday(&now, NULL); + struct timeval rtt; + timersub(&now, &n->udp_ping_sent, &rtt); + n->udp_ping_rtt = rtt.tv_sec * 1000000 + rtt.tv_usec; + logger(DEBUG_TRAFFIC, LOG_INFO, "Got type %d UDP probe reply %d from %s (%s) rtt=%d.%03d", DATA(packet)[0], len, n->name, n->hostname, n->udp_ping_rtt / 1000, n->udp_ping_rtt % 1000); + } else { + logger(DEBUG_TRAFFIC, LOG_INFO, "Got type %d UDP probe reply %d from %s (%s)", DATA(packet)[0], len, n->name, n->hostname); + } /* It's a valid reply: now we know bidirectional communication is possible using the address and socket that the reply @@ -158,13 +168,16 @@ static void udp_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { if(!n->status.udp_confirmed) { n->status.udp_confirmed = true; fprintf(stderr, "Updating address cache...\n"); - if (!n->address_cache) + + if(!n->address_cache) { n->address_cache = open_address_cache(n); + } + reset_address_cache(n->address_cache, &n->address); } - // Reset the UDP ping timer. - n->udp_ping_sent = now; + // Reset the UDP ping timer. (no probe in flight) + n->udp_ping_sent.tv_sec = 0; if(udp_discovery) { timeout_del(&n->udp_ping_timeout); @@ -246,9 +259,22 @@ static length_t uncompress_packet(uint8_t *dest, const uint8_t *source, length_t #ifdef HAVE_ZLIB else { unsigned long destlen = MAXSIZE; + static z_stream stream; - if(uncompress(dest, &destlen, source, len) == Z_OK) { - return destlen; + if(stream.next_in) { + inflateReset(&stream); + } else { + inflateInit(&stream); + } + + stream.next_in = source; + stream.avail_in = len; + stream.next_out = dest; + stream.avail_out = destlen; + stream.total_out = 0; + + if(inflate(&stream, Z_FINISH) == Z_STREAM_END) { + return stream.total_out; } else { return -1; } @@ -436,7 +462,10 @@ static bool receive_udppacket(node_t *n, vpn_packet_t *inpkt) { inpkt = outpkt; - origlen -= MTU / 64 + 20; + if (origlen > MTU / 64 + 20) + origlen -= MTU / 64 + 20; + else + origlen = 0; } if(inpkt->len > n->maxrecentlen) { @@ -1102,8 +1131,9 @@ static void try_udp(node_t *n) { int interval = n->status.udp_confirmed ? udp_discovery_keepalive_interval : udp_discovery_interval; if(ping_tx_elapsed.tv_sec >= interval) { + gettimeofday(&now, NULL); + n->udp_ping_sent = now; // a probe in flight send_udp_probe_packet(n, MIN_PROBE_SIZE); - n->udp_ping_sent = now; if(localdiscovery && !n->status.udp_confirmed && n->prevedge) { n->status.send_locally = true; @@ -1711,14 +1741,14 @@ void handle_incoming_vpn_data(void *data, int flags) { iov[i] = (struct iovec) { .iov_base = DATA(&pkt[i]), - .iov_len = MAXSIZE, + .iov_len = MAXSIZE, }; msg[i].msg_hdr = (struct msghdr) { .msg_name = &addr[i].sa, - .msg_namelen = sizeof(addr)[i], - .msg_iov = &iov[i], - .msg_iovlen = 1, + .msg_namelen = sizeof(addr)[i], + .msg_iov = &iov[i], + .msg_iovlen = 1, }; }