瀏覽代碼

set IPV6_V6ONLY = 0 for (#409)

ithewei 2 年之前
父節點
當前提交
1ab8a8e551
共有 4 個文件被更改,包括 21 次插入0 次删除
  1. 4 0
      base/hsocket.c
  2. 8 0
      base/hsocket.h
  3. 6 0
      docs/API.md
  4. 3 0
      event/hloop.c

+ 4 - 0
base/hsocket.c

@@ -186,6 +186,10 @@ static int sockaddr_bind(sockaddr_u* localaddr, int type) {
     // so_reuseport(sockfd, 1);
 #endif
 
+    if (localaddr->sa.sa_family == AF_INET6) {
+        ip_v6only(sockfd, 0);
+    }
+
     if (bind(sockfd, &localaddr->sa, sockaddr_len(localaddr)) < 0) {
         perror("bind");
         goto error;

+ 8 - 0
base/hsocket.h

@@ -190,6 +190,14 @@ HV_INLINE int udp_broadcast(int sockfd, int on DEFAULT(1)) {
     return setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (const char*)&on, sizeof(int));
 }
 
+HV_INLINE int ip_v6only(int sockfd, int on DEFAULT(1)) {
+#ifdef IPV6_V6ONLY
+    return setsockopt(sockfd, IPPROTO_IP, IPV6_V6ONLY, (const char*)&on, sizeof(int));
+#else
+    return 0;
+#endif
+}
+
 // send timeout
 HV_INLINE int so_sndtimeo(int sockfd, int timeout) {
 #ifdef OS_WIN

+ 6 - 0
docs/API.md

@@ -228,8 +228,14 @@
 - tcp_nopush
 - tcp_keepalive
 - udp_broadcast
+- ip_v6only
 - so_sndtimeo
 - so_rcvtimeo
+- so_sndbuf
+- so_rcvbuf
+- so_reuseaddr
+- so_reuseport
+- so_linger
 
 ### hlog.h
 - default_logger

+ 3 - 0
event/hloop.c

@@ -949,6 +949,9 @@ hio_t* hio_create_socket(hloop_t* loop, const char* host, int port, hio_type_e t
         so_reuseaddr(sockfd, 1);
         // so_reuseport(sockfd, 1);
 #endif
+        if (addr.sa.sa_family == AF_INET6) {
+            ip_v6only(sockfd, 0);
+        }
         if (bind(sockfd, &addr.sa, sockaddr_len(&addr)) < 0) {
             perror("bind");
             closesocket(sockfd);