pex-msg: fix memory leak on fread fail in pex_msg_update_request_init
[project/unetd.git] / pex.c
diff --git a/pex.c b/pex.c
index b2285767f4f5dcda22d6d290a39b62ec726db447..632d016be4b7f5afd4c36315f83147edfb48aede 100644 (file)
--- a/pex.c
+++ b/pex.c
@@ -330,6 +330,7 @@ static void
 network_pex_query_hosts(struct network *net)
 {
        struct network_host *host;
+       uint64_t now;
        int rv = rand();
        int hosts = 0;
        int i;
@@ -355,6 +356,7 @@ network_pex_query_hosts(struct network *net)
        if (!hosts)
                return;
 
+       now = unet_gettime();
        rv %= net->hosts.count;
        for (i = 0; i < 2; i++) {
                avl_for_each_element(&net->hosts, host, node) {
@@ -368,11 +370,13 @@ network_pex_query_hosts(struct network *net)
                        if (host == net->net_config.local_host)
                                continue;
 
-                       if (!peer->state.connected)
+                       if (!peer->state.connected ||
+                           peer->state.last_query_sent + 15 >= now)
                                continue;
 
                        D_PEER(net, peer, "send query for %d hosts", hosts);
                        pex_msg_send(net, peer);
+                       peer->state.last_query_sent = now;
                        return;
                }
        }
@@ -421,6 +425,7 @@ void network_pex_event(struct network *net, struct network_peer *peer,
 
        switch (ev) {
        case PEX_EV_HANDSHAKE:
+               peer->state.last_query_sent = 0;
                pex_send_hello(net, peer);
                if (net->config.type == NETWORK_TYPE_DYNAMIC)
                        network_pex_send_update_request(net, peer, NULL);