|
| 1 | +diff --git a/src/Networking.h b/src/Networking.h |
| 2 | +index e26888c..d0364a2 100644 |
| 3 | +--- a/src/Networking.h |
| 4 | ++++ b/src/Networking.h |
| 5 | +@@ -55,12 +55,15 @@ inline SOCKET dup(SOCKET socket) { |
| 6 | + } |
| 7 | + #else |
| 8 | + #include <sys/socket.h> |
| 9 | ++#include <sys/un.h> |
| 10 | + #include <netinet/in.h> |
| 11 | + #include <netinet/tcp.h> |
| 12 | + #include <netdb.h> |
| 13 | + #include <unistd.h> |
| 14 | + #include <arpa/inet.h> |
| 15 | + #include <cstring> |
| 16 | ++// Unix Domain Sockets support |
| 17 | ++#define UWS_UDS |
| 18 | + #define SOCKET_ERROR -1 |
| 19 | + #define INVALID_SOCKET -1 |
| 20 | + #define WIN32_EXPORT |
| 21 | +diff --git a/src/Node.h b/src/Node.h |
| 22 | +index 5c31caf..8c7649a 100644 |
| 23 | +--- a/src/Node.h |
| 24 | ++++ b/src/Node.h |
| 25 | +@@ -128,37 +128,55 @@ public: |
| 26 | + // todo: hostname, backlog |
| 27 | + template <void A(Socket *s)> |
| 28 | + bool listen(const char *host, int port, uS::TLS::Context sslContext, int options, uS::NodeData *nodeData, void *user) { |
| 29 | +- addrinfo hints, *result; |
| 30 | ++ uv_os_sock_t listenFd = SOCKET_ERROR; |
| 31 | ++ Context *netContext = nodeData->netContext; |
| 32 | ++ |
| 33 | ++ addrinfo hints, *result = nullptr; |
| 34 | ++ addrinfo *listenAddr; |
| 35 | ++ |
| 36 | + memset(&hints, 0, sizeof(addrinfo)); |
| 37 | + |
| 38 | + hints.ai_flags = AI_PASSIVE; |
| 39 | + hints.ai_family = AF_UNSPEC; |
| 40 | + hints.ai_socktype = SOCK_STREAM; |
| 41 | + |
| 42 | +- Context *netContext = nodeData->netContext; |
| 43 | ++#ifdef UWS_UDS |
| 44 | ++ sockaddr_un unixsv; |
| 45 | ++ if (host && strncmp(host, "unix:", 5) == 0) { |
| 46 | ++ listenFd = netContext->createSocket(AF_UNIX, hints.ai_socktype, hints.ai_protocol); |
| 47 | + |
| 48 | +- if (getaddrinfo(host, std::to_string(port).c_str(), &hints, &result)) { |
| 49 | +- return true; |
| 50 | +- } |
| 51 | ++ unixsv.sun_family = AF_UNIX; |
| 52 | ++ strncpy(unixsv.sun_path, host + 5, strlen(host + 5) + 1); |
| 53 | ++ |
| 54 | ++ hints.ai_addr = (sockaddr *) &unixsv; |
| 55 | ++ hints.ai_addrlen = sizeof(unixsv); |
| 56 | ++ listenAddr = &hints; |
| 57 | ++ |
| 58 | ++ unlink(unixsv.sun_path); |
| 59 | ++ } else |
| 60 | ++#endif |
| 61 | ++ { |
| 62 | ++ if (getaddrinfo(host, std::to_string(port).c_str(), &hints, &result)) { |
| 63 | ++ return true; |
| 64 | ++ } |
| 65 | ++ |
| 66 | ++ if ((options & uS::ONLY_IPV4) == 0) { |
| 67 | ++ for (addrinfo *a = result; a && listenFd == SOCKET_ERROR; a = a->ai_next) { |
| 68 | ++ if (a->ai_family == AF_INET6) { |
| 69 | ++ listenFd = netContext->createSocket(a->ai_family, a->ai_socktype, a->ai_protocol); |
| 70 | ++ listenAddr = a; |
| 71 | ++ } |
| 72 | ++ } |
| 73 | ++ } |
| 74 | + |
| 75 | +- uv_os_sock_t listenFd = SOCKET_ERROR; |
| 76 | +- addrinfo *listenAddr; |
| 77 | +- if ((options & uS::ONLY_IPV4) == 0) { |
| 78 | + for (addrinfo *a = result; a && listenFd == SOCKET_ERROR; a = a->ai_next) { |
| 79 | +- if (a->ai_family == AF_INET6) { |
| 80 | ++ if (a->ai_family == AF_INET) { |
| 81 | + listenFd = netContext->createSocket(a->ai_family, a->ai_socktype, a->ai_protocol); |
| 82 | + listenAddr = a; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +- for (addrinfo *a = result; a && listenFd == SOCKET_ERROR; a = a->ai_next) { |
| 88 | +- if (a->ai_family == AF_INET) { |
| 89 | +- listenFd = netContext->createSocket(a->ai_family, a->ai_socktype, a->ai_protocol); |
| 90 | +- listenAddr = a; |
| 91 | +- } |
| 92 | +- } |
| 93 | +- |
| 94 | + if (listenFd == SOCKET_ERROR) { |
| 95 | + freeaddrinfo(result); |
| 96 | + return true; |
| 97 | +diff --git a/src/Socket.cpp b/src/Socket.cpp |
| 98 | +index c35bbf8..bf0ac35 100644 |
| 99 | +--- a/src/Socket.cpp |
| 100 | ++++ b/src/Socket.cpp |
| 101 | +@@ -18,10 +18,13 @@ Socket::Address Socket::getAddress() |
| 102 | + sockaddr_in *ipv4 = (sockaddr_in *) &addr; |
| 103 | + inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)); |
| 104 | + return {ntohs(ipv4->sin_port), buf, "IPv4"}; |
| 105 | +- } else { |
| 106 | ++ } else if (addr.ss_family == AF_INET6) { |
| 107 | + sockaddr_in6 *ipv6 = (sockaddr_in6 *) &addr; |
| 108 | + inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)); |
| 109 | + return {ntohs(ipv6->sin6_port), buf, "IPv6"}; |
| 110 | ++ } else { |
| 111 | ++ //sockaddr_un *unix = (sockaddr_un *) &addr; |
| 112 | ++ return {0, "", "UNIX"}; |
| 113 | + } |
| 114 | + } |
| 115 | + |
0 commit comments