ithewei преди 3 години
родител
ревизия
6e56fdee37
променени са 4 файла, в които са добавени 51 реда и са изтрити 37 реда
  1. 2 24
      base/hsocket.c
  2. 46 1
      base/hsocket.h
  3. 2 9
      event/hloop.c
  4. 1 3
      examples/nmap/nmap.cpp

+ 2 - 24
base/hsocket.c

@@ -177,30 +177,8 @@ static int sockaddr_bind(sockaddr_u* localaddr, int type) {
         return socket_errno_negative();
     }
 
-
-#ifdef SO_REUSEADDR
-    {
-        // NOTE: SO_REUSEADDR allow to reuse sockaddr of TIME_WAIT status
-        int reuseaddr = 1;
-        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr, sizeof(int)) < 0) {
-            perror("setsockopt");
-            goto error;
-        }
-    }
-#endif
-
-/*
-#ifdef SO_REUSEPORT
-    {
-        // NOTE: SO_REUSEPORT allow multiple sockets to bind same port
-        int reuseport = 1;
-        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuseport, sizeof(int)) < 0) {
-            perror("setsockopt");
-            goto error;
-        }
-    }
-#endif
-*/
+    so_reuseaddr(sockfd, 1);
+    // so_reuseport(sockfd, 1);
 
     if (bind(sockfd, &localaddr->sa, sockaddr_len(localaddr)) < 0) {
         perror("bind");

+ 46 - 1
base/hsocket.h

@@ -166,7 +166,7 @@ HV_INLINE int tcp_nopush(int sockfd, int on DEFAULT(1)) {
 #elif defined(TCP_CORK)
     return setsockopt(sockfd, IPPROTO_TCP, TCP_CORK, (const char*)&on, sizeof(int));
 #else
-    return -10;
+    return 0;
 #endif
 }
 
@@ -211,6 +211,51 @@ HV_INLINE int so_rcvtimeo(int sockfd, int timeout) {
 #endif
 }
 
+// send buffer size
+HV_INLINE int so_sndbuf(int sockfd, int len) {
+    return setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (const char*)&len, sizeof(int));
+}
+
+// recv buffer size
+HV_INLINE int so_rcvbuf(int sockfd, int len) {
+    return setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (const char*)&len, sizeof(int));
+}
+
+HV_INLINE int so_reuseaddr(int sockfd, int on DEFAULT(1)) {
+#ifdef SO_REUSEADDR
+    // NOTE: SO_REUSEADDR allow to reuse sockaddr of TIME_WAIT status
+    return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(int));
+#else
+    return 0;
+#endif
+}
+
+HV_INLINE int so_reuseport(int sockfd, int on DEFAULT(1)) {
+#ifdef SO_REUSEPORT
+    // NOTE: SO_REUSEPORT allow multiple sockets to bind same port
+    return setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&on, sizeof(int));
+#else
+    return 0;
+#endif
+}
+
+HV_INLINE int so_linger(int sockfd, int timeout DEFAULT(1)) {
+#ifdef SO_LINGER
+    struct linger linger;
+    if (timeout >= 0) {
+        linger.l_onoff = 1;
+        linger.l_linger = timeout;
+    } else {
+        linger.l_onoff = 0;
+        linger.l_linger = 0;
+    }
+    // NOTE: SO_LINGER change the default behavior of close, send RST, avoid TIME_WAIT
+    return setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger, sizeof(linger));
+#else
+    return 0;
+#endif
+}
+
 END_EXTERN_C
 
 #endif // HV_SOCKET_H_

+ 2 - 9
event/hloop.c

@@ -941,15 +941,8 @@ hio_t* hio_create_socket(hloop_t* loop, const char* host, int port, hio_type_e t
     }
     hio_t* io = NULL;
     if (side == HIO_SERVER_SIDE) {
-#ifdef SO_REUSEADDR
-        // NOTE: SO_REUSEADDR allow to reuse sockaddr of TIME_WAIT status
-        int reuseaddr = 1;
-        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr, sizeof(int)) < 0) {
-            perror("setsockopt");
-            closesocket(sockfd);
-            return NULL;
-        }
-#endif
+        so_reuseaddr(sockfd, 1);
+        // so_reuseport(sockfd, 1);
         if (bind(sockfd, &addr.sa, sockaddr_len(&addr)) < 0) {
             perror("bind");
             closesocket(sockfd);

+ 1 - 3
examples/nmap/nmap.cpp

@@ -96,9 +96,7 @@ int nmap_discover(Nmap* nmap) {
                 return -socket_errno();
             }
             nonblocking(sockfd);
-            int len = 425984; // 416K
-            socklen_t optlen = sizeof(len);
-            setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (const char*)&len, optlen);
+            so_sndbuf(sockfd, 425984); // 416K
 
             io = hio_get(loop, sockfd);
             if (io == NULL) return -1;