Changeset 9777


Ignore:
Timestamp:
Dec 15, 2009, 5:34:36 PM (12 years ago)
Author:
charles
Message:

(trunk) update our DHT snapshot

Location:
trunk/third-party/dht
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/third-party/dht/CHANGES

    r9718 r9777  
     115 December 2009: dht-0.13
     2
     3  * Implemented protection against incorrect addresses in the DHT.
     4  * Tweaked neighborhood maintenance to wake up less often.
     5
    1611 December 2009: dht-0.12
    27  * Fixed slightly incorrect formatting of DHT messages.
  • trunk/third-party/dht/dht.c

    r9657 r9777  
    225225    0xFF, 0xFF, 0xFF, 0xFF
    226226};
     227static const unsigned char v4prefix[16] = {
     228    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0
     229};
    227230
    228231static int dht_socket = -1;
     
    296299    for(i = 0; i < buflen; i++)
    297300        fprintf(f, "%02x", buf[i]);
     301}
     302
     303static int
     304is_martian(struct sockaddr *sa)
     305{
     306    switch(sa->sa_family) {
     307    case AF_INET: {
     308        struct sockaddr_in *sin = (struct sockaddr_in*)sa;
     309        const unsigned char *address = (const unsigned char*)&sin->sin_addr;
     310        return sin->sin_port == 0 ||
     311            (address[0] == 0) ||
     312            (address[0] == 127) ||
     313            ((address[0] & 0xE0) == 0xE0);
     314    }
     315    case AF_INET6: {
     316        struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
     317        const unsigned char *address = (const unsigned char*)&sin6->sin6_addr;
     318        return sin6->sin6_port == 0 ||
     319            (address[0] == 0xFF) ||
     320            (address[0] == 0xFE && (address[1] & 0xC0) == 0x80) ||
     321            (memcmp(address, zeroes, 15) == 0 &&
     322             (address[15] == 0 || address[15] == 1)) ||
     323            (memcmp(address, v4prefix, 12) == 0);
     324    }
     325
     326    default:
     327        return 0;
     328    }
    298329}
    299330
     
    619650
    620651    if(id_cmp(id, myid) == 0)
     652        return NULL;
     653
     654    if(is_martian(sa))
    621655        return NULL;
    622656
     
    645679    }
    646680
    647     /* New node.  First, try to get rid of a known-bad node. */
     681    /* New node. */
     682
     683    if(mybucket) {
     684        if(sa->sa_family == AF_INET)
     685            mybucket_grow_time = now.tv_sec;
     686        else
     687            mybucket6_grow_time = now.tv_sec;
     688    }
     689
     690    /* First, try to get rid of a known-bad node. */
    648691    n = b->nodes;
    649692    while(n) {
     
    655698            n->pinged_time = 0;
    656699            n->pinged = 0;
    657             if(mybucket) {
    658                 if(sa->sa_family == AF_INET)
    659                     mybucket_grow_time = now.tv_sec;
    660                 else
    661                     mybucket6_grow_time = now.tv_sec;
    662             }
    663700            return n;
    664701        }
     
    706743            debugf("Splitting.\n");
    707744            b = split_bucket(b);
    708             if(sa->sa_family == AF_INET)
    709                 mybucket_grow_time = now.tv_sec;
    710             else
    711                 mybucket6_grow_time = now.tv_sec;
    712745            return new_node(id, sa, salen, confirm);
    713746        }
     
    734767    b->nodes = n;
    735768    b->count++;
    736     if(mybucket) {
    737         if(sa->sa_family == AF_INET)
    738             mybucket_grow_time = now.tv_sec;
    739         else
    740             mybucket6_grow_time = now.tv_sec;
    741     }
    742769    return n;
    743770}
     
    16531680
    16541681    if(b == NULL)
    1655         return -1;
     1682        return 0;
    16561683
    16571684    memcpy(id, myid, 20);
     
    16681695
    16691696    if(q) {
     1697        /* Since our node-id is the same in both DHTs, it's probably
     1698           profitable to query both families. */
    16701699        int want = dht_socket >= 0 && dht_socket6 >= 0 ? (WANT4 | WANT6) : -1;
    16711700        n = random_node(q);
     
    16801709            pinged(n, q);
    16811710        }
    1682     }
    1683     return 1;
     1711        return 1;
     1712    }
     1713    return 0;
    16841714}
    16851715
     
    17971827
    17981828        if(rc < 0 || sourcelen > sizeof(struct sockaddr_storage))
     1829            goto dontread;
     1830
     1831        if(is_martian(source))
    17991832            goto dontread;
    18001833
     
    26462679    const unsigned char *p;
    26472680
     2681    /* This code will happily crash if the buffer is not NUL-terminated. */
     2682    if(buf[buflen] != '\0') {
     2683        debugf("Eek!  parse_message with unterminated buffer.\n");
     2684        return -1;
     2685    }
     2686
    26482687#define CHECK(ptr, len)                                                 \
    26492688    if(((unsigned char*)ptr) + (len) > (buf) + (buflen)) goto overflow;
Note: See TracChangeset for help on using the changeset viewer.