]> Sergey Matveev's repositories - dht-bootstrap.git/commitdiff
select -> poll
authorSergey Matveev <stargrave@stargrave.org>
Wed, 9 Nov 2022 09:51:24 +0000 (12:51 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 9 Nov 2022 10:27:17 +0000 (13:27 +0300)
CHANGES
dht-bootstrap.c

diff --git a/CHANGES b/CHANGES
index 3e586500254ecdf74a8527d33b06490f2323fdb8..b903e72ed8ebd14fa5b61f8828d6ad9da546207a 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,7 @@
 2022-11-09:
 
   * Ability to explicitly specify IP addresses to bind to
 2022-11-09:
 
   * Ability to explicitly specify IP addresses to bind to
+  * select() replaced with poll()
 
 20 November 2011: dht-bootstrap-0.2
 
 
 20 November 2011: dht-bootstrap-0.2
 
index de5bb395c5416b1412712d76e3688f519a1b2092..33ef79808d12d75080a1562bac7a344493f83caa 100644 (file)
@@ -20,22 +20,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */
 
 THE SOFTWARE.
 */
 
-#include <arpa/inet.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <netdb.h>
 #include <netinet/in.h>
+#include <poll.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
-#include <sys/time.h>
-#include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
 
 #include <time.h>
 #include <unistd.h>
 
-#define MAX(x, y) ((x) >= (y) ? (x) : (y))
 #define MIN(x, y) ((x) <= (y) ? (x) : (y))
 
 static int
 #define MIN(x, y) ((x) <= (y) ? (x) : (y))
 
 static int
@@ -508,23 +505,28 @@ main(int argc, char **argv)
     token_bucket_time = time(NULL);
     token_bucket_tokens = MAX_TOKEN_BUCKET_TOKENS;
     int send4 = 0;
     token_bucket_time = time(NULL);
     token_bucket_tokens = MAX_TOKEN_BUCKET_TOKENS;
     int send4 = 0;
+    struct pollfd fds[2];
+    if (dht_socket >= 0) {
+        fds[0].fd = dht_socket;
+        fds[0].events = POLLIN;
+    } else {
+        fds[0].fd = -1;
+    }
+    if (dht_socket6 >= 0) {
+        fds[1].fd = dht_socket6;
+        fds[1].events = POLLIN;
+    } else {
+        fds[1].fd = -1;
+    }
 
     while (1) {
 
     while (1) {
-        struct timeval tv;
-        fd_set readfds;
-
+        int tv_sec = 0;
         if ((dht_socket >= 0 && list_elements(&v4_confirmed) <= 16) ||
             (dht_socket6 >= 0 && list_elements(&v6_confirmed) <= 16))
         if ((dht_socket >= 0 && list_elements(&v4_confirmed) <= 16) ||
             (dht_socket6 >= 0 && list_elements(&v6_confirmed) <= 16))
-            tv.tv_sec = 0;
+            tv_sec = 0;
         else
         else
-            tv.tv_sec = random() % 30;
-        tv.tv_usec = random() % 1000000;
-
-        FD_ZERO(&readfds);
-        if (dht_socket >= 0)
-            FD_SET(dht_socket, &readfds);
-        if (dht_socket6 >= 0)
-            FD_SET(dht_socket6, &readfds);
+            tv_sec = random() % 30;
+        int tv_msec = random() % 1000;
 
         if (dht_debug)
             debugf(
 
         if (dht_debug)
             debugf(
@@ -534,13 +536,11 @@ main(int argc, char **argv)
                 list_elements(&v4_new),
                 list_elements(&v6_new));
 
                 list_elements(&v4_new),
                 list_elements(&v6_new));
 
-        int rc = select(MAX(dht_socket, dht_socket6) + 1, &readfds, NULL, NULL, &tv);
+        int rc = poll(fds, 2, tv_sec * 1000 + tv_msec);
 
         if (rc < 0) {
 
         if (rc < 0) {
-            if (errno != EINTR) {
-                perror("select");
-                sleep(1);
-            }
+            perror("poll");
+            sleep(1);
         }
 
         if (rc > 0) {
         }
 
         if (rc > 0) {
@@ -557,10 +557,20 @@ main(int argc, char **argv)
             struct sockaddr_storage source_storage;
             struct sockaddr *source = (struct sockaddr *)&source_storage;
             socklen_t sourcelen = sizeof(source_storage);
             struct sockaddr_storage source_storage;
             struct sockaddr *source = (struct sockaddr *)&source_storage;
             socklen_t sourcelen = sizeof(source_storage);
-            if (dht_socket >= 0 && FD_ISSET(dht_socket, &readfds)) {
-                rc = recvfrom(dht_socket, buf, 1536, 0, source, &sourcelen);
-            } else if (dht_socket6 >= 0 && FD_ISSET(dht_socket6, &readfds)) {
-                rc = recvfrom(dht_socket6, buf, 1536, 0, source, &sourcelen);
+            if (fds[0].revents != 0) {
+                if ((fds[0].revents & (POLLERR | POLLNVAL)) > 0) {
+                    fprintf(stderr, "error in fds[0]");
+                    rc = -1;
+                } else {
+                    rc = recvfrom(dht_socket, buf, 1536, 0, source, &sourcelen);
+                }
+            } else if (fds[1].revents != 0) {
+                if ((fds[1].revents & (POLLERR | POLLNVAL)) > 0) {
+                    fprintf(stderr, "error in fds[1]");
+                    rc = -1;
+                } else {
+                    rc = recvfrom(dht_socket6, buf, 1536, 0, source, &sourcelen);
+                };
             }
 
             if (rc < 0 || sourcelen > sizeof(struct sockaddr_storage))
             }
 
             if (rc < 0 || sourcelen > sizeof(struct sockaddr_storage))