File Info

Rev. efeab20e689880041b1ce76d479bb22c776d3f74
Size 13,149 bytes
Time 2020-07-04 19:41:12
Author simphone
Log Message

simphone 0.8.1

Content

diff -urb dht-0.26/dht.c dht/dht.c
--- dht-0.26/dht.c
+++ dht/dht.c
@@ -30,6 +30,7 @@
 
 /* For memmem. */
 #define _GNU_SOURCE
+#define _CRT_SECURE_NO_DEPRECATE // _snprintf
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -56,10 +57,17 @@
 #ifndef WINVER
 #define WINVER _WIN32_WINNT
 #endif
+#include <time.h>
+#include <winsock2.h>
+
 #include <ws2tcpip.h>
 #include <windows.h>
 #endif
 
+#ifdef SIM_MODULE
+#include "../simcore/logger.h"
+#endif
+
 #include "dht.h"
 
 #ifndef HAVE_MEMMEM
@@ -72,7 +80,7 @@
 #define MSG_CONFIRM 0
 #endif
 
-#if !defined(_WIN32) || defined(__MINGW32__)
+#if 0 // !defined(_WIN32) || defined(__MINGW32__)
 #define dht_gettimeofday(_ts, _tz) gettimeofday((_ts), (_tz))
 #else
 extern int dht_gettimeofday(struct timeval *tv, struct timezone *tz);
@@ -170,6 +178,8 @@
     time_t step_time;           /* the time of the last search_step */
     unsigned char id[20];
     unsigned short port;        /* 0 for pure searches */
+    int inflight;
+    void *closure;
     int done;
     struct search_node nodes[SEARCH_NODES];
     int numnodes;
@@ -351,23 +361,44 @@
 {
     va_list args;
     va_start(args, format);
+#ifdef SIM_MODULE
+    log_print_(SIM_MODULE, SIM_LOG_XTRA, format, args);
+#else
     if(dht_debug)
         vfprintf(dht_debug, format, args);
+#endif
     va_end(args);
     if(dht_debug)
         fflush(dht_debug);
 }
 
 static void
-debug_printable(const unsigned char *buf, int buflen)
+debug_printable(const char *message, const unsigned char *buf, int buflen)
 {
     int i;
+#ifdef SIM_MODULE
+    if(log_protect_(SIM_MODULE, SIM_LOG_XTRA)) {
+        log_xtra_(SIM_MODULE, "%s", message);
+        for(i = 0; i < buflen; i++)
+            log_xtra_(SIM_MODULE, "%c",
+                      buf[i] >= 32 && buf[i] <= 126 ? buf[i] : '.');
+        log_xtra_(SIM_MODULE, "\n");
+        log_unprotect_();
+    }
+#else
     if(dht_debug) {
+        debugf(message);
         for(i = 0; i < buflen; i++)
             putc(buf[i] >= 32 && buf[i] <= 126 ? buf[i] : '.', dht_debug);
+        debugf("\n");
     }
+#endif
 }
 
+#ifdef SIM_MODULE
+#define fprintf(f, ...) log_any_((const char*)f, SIM_LOG_INFO, __VA_ARGS__)
+#endif
+
 static void
 print_hex(FILE *f, const unsigned char *buf, int buflen)
 {
@@ -1120,7 +1151,7 @@
             numsearches--;
             if (!sr->done) {
                 if(callback)
-                    (*callback)(closure,
+                    (*callback)(closure ? closure : sr->closure,
                                 sr->af == AF_INET ?
                                 DHT_EVENT_SEARCH_DONE : DHT_EVENT_SEARCH_DONE6,
                                 sr->id, NULL, 0);
@@ -1249,7 +1280,7 @@
     j = 0;
     for(i = 0; i < sr->numnodes; i++) {
         j += search_send_get_peers(sr, &sr->nodes[i]);
-        if(j >= DHT_INFLIGHT_QUERIES)
+        if(j >= sr->inflight)
             break;
     }
     sr->step_time = now.tv_sec;
@@ -1258,7 +1289,7 @@
  done:
     sr->done = 1;
     if(callback)
-        (*callback)(closure,
+        (*callback)(closure ? closure : sr->closure,
                     sr->af == AF_INET ?
                     DHT_EVENT_SEARCH_DONE : DHT_EVENT_SEARCH_DONE6,
                     sr->id, NULL, 0);
@@ -1314,12 +1345,14 @@
 /* Start a search.  If port is non-zero, perform an announce when the
    search is complete. */
 int
-dht_search(const unsigned char *id, int port, int af,
-           dht_callback_t *callback, void *closure)
+dht_search_param(const unsigned char *id, int port, int af,
+                 dht_callback_t *callback, void *closure,
+                 int alpha, int beta)
 {
     struct search *sr;
     struct storage *st;
     struct bucket *b = find_bucket(id, af);
+    int sr_duplicate;
 
     if(b == NULL) {
         errno = EAFNOSUPPORT;
@@ -1363,7 +1396,7 @@
         sr = sr->next;
     }
 
-    int sr_duplicate = sr && !sr->done;
+    sr_duplicate = sr && !sr->done;
 
     if(sr) {
         /* We're reusing data from an old search.  Reusing the same tid
@@ -1399,6 +1432,8 @@
     }
 
     sr->port = port;
+    sr->inflight = alpha;
+    sr->closure = closure;
 
     insert_search_bucket(b, sr);
 
@@ -1413,6 +1448,7 @@
         insert_search_bucket(find_bucket(myid, af), sr);
 
     search_step(sr, callback, closure);
+    sr->inflight = beta;
     search_time = now.tv_sec;
     if(sr_duplicate) {
         return 0;
@@ -1421,6 +1457,39 @@
     }
 }
 
+int
+dht_search(const unsigned char *id, int port, int af,
+           dht_callback_t *callback, void *closure)
+{
+    return dht_search_param(id, port, af, callback, closure,
+                            DHT_INFLIGHT_QUERIES, DHT_INFLIGHT_QUERIES);
+}
+
+int
+dht_end_search(const unsigned char *id, int af,
+               dht_callback_t *callback, void *closure)
+{
+    struct search *sr = searches;
+
+    while(sr) {
+        if(sr->af == af && id_cmp(sr->id, id) == 0)
+            break;
+        sr = sr->next;
+    }
+
+    if(sr) {
+        sr->done = 1;
+        if(callback)
+            (*callback)(closure ? closure : sr->closure,
+                        sr->af == AF_INET ?
+                        DHT_EVENT_SEARCH_DONE : DHT_EVENT_SEARCH_DONE6,
+                        sr->id, NULL, 0);
+        sr->step_time = now.tv_sec;
+        return 1;
+    }
+    return 0;
+}
+
 /* A struct storage stores all the stored peer addresses for a given info
    hash. */
 
@@ -1759,7 +1828,9 @@
     }
 
     fprintf(f, "\n\n");
+#ifndef SIM_MODULE
     fflush(f);
+#endif
 }
 
 int
@@ -1767,6 +1838,11 @@
 {
     int rc;
 
+    if(s < 0 && s6 < 0) {
+        errno = EINVAL;
+        return -1;
+    }
+
     if(dht_socket >= 0 || dht_socket6 >= 0 || buckets || buckets6) {
         errno = EBUSY;
         return -1;
@@ -1838,6 +1914,15 @@
     return -1;
 }
 
+void
+dht_set_socket(int s, int s6)
+{
+    if(s >= 0 && dht_socket >= 0)
+        dht_socket = s;
+    if(s6 >= 0 && dht_socket6 >= 0)
+        dht_socket6 = s6;
+}
+
 int
 dht_uninit()
 {
@@ -1893,7 +1978,7 @@
 token_bucket(void)
 {
     if(token_bucket_tokens == 0) {
-        token_bucket_tokens = MIN(MAX_TOKEN_BUCKET_TOKENS,
+        token_bucket_tokens = (int)MIN(MAX_TOKEN_BUCKET_TOKENS,
                                   100 * (now.tv_sec - token_bucket_time));
         token_bucket_time = now.tv_sec;
     }
@@ -2056,9 +2141,7 @@
         message = parse_message(buf, buflen, &m);
 
         if(message < 0 || message == ERROR || id_cmp(m.id, zeroes) == 0) {
-            debugf("Unparseable message: ");
-            debug_printable(buf, buflen);
-            debugf("\n");
+            debug_printable("Unparseable message: ", buf, buflen);
             goto dontread;
         }
 
@@ -2078,9 +2161,8 @@
         switch(message) {
         case REPLY:
             if(m.tid_len != 4) {
-                debugf("Broken node truncates transaction ids: ");
-                debug_printable(buf, buflen);
-                debugf("\n");
+                debug_printable("Broken node truncates transaction ids: ",
+                                buf, buflen);
                 /* This is really annoying, as it means that we will
                    time-out all our searches that go through this node.
                    Kill it. */
@@ -2158,11 +2240,13 @@
                                m.values_len / 6, m.values6_len / 18);
                         if(callback) {
                             if(m.values_len > 0)
-                                (*callback)(closure, DHT_EVENT_VALUES, sr->id,
+                                (*callback)(closure ? closure : sr->closure,
+                                            DHT_EVENT_VALUES, sr->id,
                                             (void*)m.values, m.values_len);
 
                             if(m.values6_len > 0)
-                                (*callback)(closure, DHT_EVENT_VALUES6, sr->id,
+                                (*callback)(closure ? closure : sr->closure,
+                                            DHT_EVENT_VALUES6, sr->id,
                                             (void*)m.values6, m.values6_len);
                         }
                     }
@@ -2189,9 +2273,7 @@
                     search_send_get_peers(sr, NULL);
                 }
             } else {
-                debugf("Unexpected reply: ");
-                debug_printable(buf, buflen);
-                debugf("\n");
+                debug_printable("Unexpected reply: ", buf, buflen);
             }
             break;
         case PING:
@@ -2333,7 +2415,7 @@
            maintenance. */
 
         if(soon)
-            confirm_nodes_time = now.tv_sec + 5 + random() % 10;
+            confirm_nodes_time = now.tv_sec + (5 + random() % 10);
         else
             confirm_nodes_time = now.tv_sec + 60 + random() % 120;
     }
@@ -2354,8 +2436,8 @@
 }
 
 int
-dht_get_nodes(struct sockaddr_in *sin, int *num,
-              struct sockaddr_in6 *sin6, int *num6)
+dht_get_nodes_id(struct sockaddr_in *sin, unsigned char *id, int *num,
+                 struct sockaddr_in6 *sin6, unsigned char *id6, int *num6)
 {
     int i, j;
     struct bucket *b;
@@ -2373,6 +2455,7 @@
     while(n && i < *num) {
         if(node_good(n)) {
             sin[i] = *(struct sockaddr_in*)&n->ss;
+            if(id) memcpy( &id[i * 20], n->id, 20 );
             i++;
         }
         n = n->next;
@@ -2385,6 +2468,7 @@
             while(n && i < *num) {
                 if(node_good(n)) {
                     sin[i] = *(struct sockaddr_in*)&n->ss;
+                    if(id) memcpy( &id[i * 20], n->id, 20 );
                     i++;
                 }
                 n = n->next;
@@ -2405,6 +2489,7 @@
     while(n && j < *num6) {
         if(node_good(n)) {
             sin6[j] = *(struct sockaddr_in6*)&n->ss;
+            if(id6) memcpy( &id6[j * 20], n->id, 20 );
             j++;
         }
         n = n->next;
@@ -2417,6 +2502,7 @@
             while(n && j < *num6) {
                 if(node_good(n)) {
                     sin6[j] = *(struct sockaddr_in6*)&n->ss;
+                    if(id6) memcpy( &id6[j * 20], n->id, 20 );
                     j++;
                 }
                 n = n->next;
@@ -2433,6 +2519,13 @@
 }
 
 int
+dht_get_nodes(struct sockaddr_in *sin, int *num,
+              struct sockaddr_in6 *sin6, int *num6)
+{
+    return dht_get_nodes_id(sin, NULL, num, sin6, NULL, num6);
+}
+
+int
 dht_insert_node(const unsigned char *id, struct sockaddr *sa, int salen)
 {
     struct node *n;
@@ -2913,7 +3006,7 @@
         if(q && *q == ':' && l > 0 && l < PARSE_TID_LEN) {
             CHECK(q + 1, l);
             memcpy(m->tid, q + 1, l);
-            m->tid_len = l;
+            m->tid_len = (unsigned short)l;
         }
     }
 
@@ -2935,7 +3028,7 @@
         char *q;
         l = strtol((char*)p + 7, &q, 10);
         if(q && *q == 'e' && l > 0 && l < 0x10000)
-            m->port = l;
+            m->port = (unsigned short)l;
     }
 
     p = dht_memmem(buf, buflen, "12:implied_porti", 16);
@@ -2944,7 +3037,7 @@
         char *q;
         l = strtol((char*)p + 16, &q, 10);
         if(q && *q == 'e' && l > 0 && l < 0x10000)
-            m->implied_port = l;
+            m->implied_port = (unsigned short)l;
     }
 
     p = dht_memmem(buf, buflen, "6:target20:", 11);
@@ -2961,7 +3054,7 @@
         if(q && *q == ':' && l > 0 && l < PARSE_TOKEN_LEN) {
             CHECK(q + 1, l);
             memcpy(m->token, q + 1, l);
-            m->token_len = l;
+            m->token_len = (unsigned short)l;
         }
     }
 
@@ -2973,7 +3066,7 @@
         if(q && *q == ':' && l > 0 && l <= PARSE_NODES_LEN) {
             CHECK(q + 1, l);
             memcpy(m->nodes, q + 1, l);
-            m->nodes_len = l;
+            m->nodes_len = (unsigned short)l;
         }
     }
 
@@ -2985,7 +3078,7 @@
         if(q && *q == ':' && l > 0 && l <= PARSE_NODES6_LEN) {
             CHECK(q + 1, l);
             memcpy(m->nodes6, q + 1, l);
-            m->nodes6_len = l;
+            m->nodes6_len = (unsigned short)l;
         }
     }
 
diff -urb dht-0.26/dht.h dht/dht.h
--- dht-0.26/dht.h
+++ dht/dht.h
@@ -45,12 +45,20 @@
                  dht_callback_t *callback, void *closure);
 int dht_search(const unsigned char *id, int port, int af,
                dht_callback_t *callback, void *closure);
+int dht_search_param(const unsigned char *id, int port, int af,
+                     dht_callback_t *callback, void *closure,
+                     int alpha, int beta);
+int dht_end_search(const unsigned char *id, int af,
+                   dht_callback_t *callback, void *closure);
 int dht_nodes(int af,
               int *good_return, int *dubious_return, int *cached_return,
               int *incoming_return);
 void dht_dump_tables(FILE *f);
 int dht_get_nodes(struct sockaddr_in *sin, int *num,
                   struct sockaddr_in6 *sin6, int *num6);
+int dht_get_nodes_id(struct sockaddr_in *sin, unsigned char *id, int *num,
+                     struct sockaddr_in6 *sin6, unsigned char *id6, int *num6);
+void dht_set_socket(int s, int s6);
 int dht_uninit(void);
 
 /* This must be provided by the user. */
Show on old repository browser