summaryrefslogtreecommitdiff
path: root/src/socket.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/socket.c')
-rw-r--r--src/socket.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/socket.c b/src/socket.c
index e2ad9ed..fe813f5 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -41,7 +41,7 @@
#else
# define STUPIDWIN(x) (x)
#endif
-#if defined(__sun) && !defined(SO_PROTOCOL)
+#if defined(__sun) && !defined(SO_PROTOCOL) && defined(SO_PROTOTYPE)
# define SO_PROTOCOL SO_PROTOTYPE
#endif
#ifndef AI_V4MAPPED
@@ -333,18 +333,22 @@ static int try_sk_from_env(int fd, const struct addrinfo *ai, const char *intf)
return -1;
#else
optlen = sizeof(value);
+#ifdef SO_DOMAIN
ret = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &value, &optlen);
if (ret < 0 || value != ai->ai_family)
return -1;
+#endif
optlen = sizeof(value);
ret = getsockopt(fd, SOL_SOCKET, SO_TYPE, &value, &optlen);
if (ret < 0 || value != ai->ai_socktype)
return -1;
optlen = sizeof(value);
+#ifdef SO_PROTOCOL
ret = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &value, &optlen);
if (ret < 0 || value != ai->ai_protocol)
return -1;
#endif
+#endif
struct sockaddr_storage addr;
memset(&addr, 0, sizeof(addr));
optlen = sizeof(addr);
@@ -394,13 +398,15 @@ EXPORT_SYMBOL int HX_socket_from_env(const struct addrinfo *ai, const char *intf
}
top_fd = x;
}
- for (int fd = 3; fd < top_fd; ++fd)
- if (try_sk_from_env(fd, ai, intf) == fd) {
+ for (int fd = 3; fd < top_fd; ++fd) {
+ if (try_sk_from_env(fd, ai, intf) != fd)
+ continue;
#ifdef SOCK_CLOEXEC
- fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
+ if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC) != 0)
+ /* ignore */;
#endif
- return fd;
- }
+ return fd;
+ }
errno = ENOENT;
return -1;
}