20 #include <winpr/config.h>
22 #include <winpr/assert.h>
23 #include <winpr/crt.h>
24 #include <winpr/synch.h>
26 #include <winpr/winsock.h>
28 #ifdef WINPR_HAVE_UNISTD_H
31 #ifdef WINPR_HAVE_SYS_FILIO_H
32 #include <sys/filio.h>
34 #ifdef WINPR_HAVE_SYS_SOCKIO_H
35 #include <sys/sockio.h>
43 #define WSAIOCTL_IFADDRS
234 #if (_WIN32_WINNT < 0x0600)
236 PCSTR winpr_inet_ntop(INT Family, PVOID pAddr, PSTR pStringBuf,
size_t StringBufSize)
238 if (Family == AF_INET)
240 struct sockaddr_in in = { 0 };
242 in.sin_family = AF_INET;
243 memcpy(&in.sin_addr, pAddr,
sizeof(
struct in_addr));
244 getnameinfo((
struct sockaddr*)&in,
sizeof(
struct sockaddr_in), pStringBuf, StringBufSize,
245 NULL, 0, NI_NUMERICHOST);
248 else if (Family == AF_INET6)
250 struct sockaddr_in6 in = { 0 };
252 in.sin6_family = AF_INET6;
253 memcpy(&in.sin6_addr, pAddr,
sizeof(
struct in_addr6));
254 getnameinfo((
struct sockaddr*)&in,
sizeof(
struct sockaddr_in6), pStringBuf, StringBufSize,
255 NULL, 0, NI_NUMERICHOST);
262 INT winpr_inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf)
264 SOCKADDR_STORAGE addr;
265 int addr_len =
sizeof(addr);
267 if ((Family != AF_INET) && (Family != AF_INET6))
270 if (WSAStringToAddressA((
char*)pszAddrString, Family, NULL, (
struct sockaddr*)&addr,
274 if (Family == AF_INET)
276 memcpy(pAddrBuf, &((
struct sockaddr_in*)&addr)->sin_addr,
sizeof(
struct in_addr));
278 else if (Family == AF_INET6)
280 memcpy(pAddrBuf, &((
struct sockaddr_in6*)&addr)->sin6_addr,
sizeof(
struct in6_addr));
292 #include <sys/ioctl.h>
293 #include <sys/socket.h>
294 #include <netinet/in.h>
295 #include <netinet/tcp.h>
299 #define MSG_NOSIGNAL 0
302 int WSAStartup(WORD wVersionRequired, LPWSADATA lpWSAData)
304 WINPR_ASSERT(lpWSAData);
306 ZeroMemory(lpWSAData,
sizeof(
WSADATA));
307 lpWSAData->wVersion = wVersionRequired;
308 lpWSAData->wHighVersion = MAKEWORD(2, 2);
317 void WSASetLastError(
int iError)
364 case WSAEDESTADDRREQ:
365 errno = EDESTADDRREQ;
380 case WSAEPROTONOSUPPORT:
381 errno = EPROTONOSUPPORT;
384 case WSAESOCKTNOSUPPORT:
385 errno = ESOCKTNOSUPPORT;
392 case WSAEPFNOSUPPORT:
393 errno = EPFNOSUPPORT;
396 case WSAEAFNOSUPPORT:
397 errno = EAFNOSUPPORT;
404 case WSAEADDRNOTAVAIL:
405 errno = EADDRNOTAVAIL;
420 case WSAECONNABORTED:
421 errno = ECONNABORTED;
444 case WSAETOOMANYREFS:
445 errno = ETOOMANYREFS;
452 case WSAECONNREFUSED:
453 errno = ECONNREFUSED;
460 case WSAENAMETOOLONG:
461 errno = ENAMETOOLONG;
468 case WSAEHOSTUNREACH:
469 errno = EHOSTUNREACH;
502 int WSAGetLastError(
void)
536 iError = WSAEWOULDBLOCK;
540 iError = WSAEINPROGRESS;
544 iError = WSAEALREADY;
548 iError = WSAENOTSOCK;
552 iError = WSAEDESTADDRREQ;
556 iError = WSAEMSGSIZE;
560 iError = WSAEPROTOTYPE;
564 iError = WSAENOPROTOOPT;
567 case EPROTONOSUPPORT:
568 iError = WSAEPROTONOSUPPORT;
571 case ESOCKTNOSUPPORT:
572 iError = WSAESOCKTNOSUPPORT;
576 iError = WSAEOPNOTSUPP;
580 iError = WSAEPFNOSUPPORT;
584 iError = WSAEAFNOSUPPORT;
588 iError = WSAEADDRINUSE;
592 iError = WSAEADDRNOTAVAIL;
596 iError = WSAENETDOWN;
600 iError = WSAENETUNREACH;
604 iError = WSAENETRESET;
608 iError = WSAECONNABORTED;
612 iError = WSAECONNRESET;
624 iError = WSAENOTCONN;
628 iError = WSAESHUTDOWN;
632 iError = WSAETOOMANYREFS;
636 iError = WSAETIMEDOUT;
640 iError = WSAECONNREFUSED;
648 iError = WSAENAMETOOLONG;
652 iError = WSAEHOSTDOWN;
656 iError = WSAEHOSTUNREACH;
660 iError = WSAENOTEMPTY;
665 iError = WSAEPROCLIM;
685 #if (EAGAIN != EWOULDBLOCK)
688 iError = WSAEWOULDBLOCK;
694 iError = WSAECONNRESET;
723 HANDLE WSACreateEvent(
void)
725 return CreateEvent(NULL, TRUE, FALSE, NULL);
728 BOOL WSASetEvent(HANDLE hEvent)
730 return SetEvent(hEvent);
733 BOOL WSAResetEvent(HANDLE hEvent)
740 BOOL WSACloseEvent(HANDLE hEvent)
742 BOOL status = CloseHandle(hEvent);
750 int WSAEventSelect(SOCKET s, WSAEVENT hEventObject, LONG lNetworkEvents)
755 if (_ioctlsocket(s, FIONBIO, &arg) != 0)
761 if (lNetworkEvents & FD_READ)
762 mode |= WINPR_FD_READ;
764 if (lNetworkEvents & FD_WRITE)
765 mode |= WINPR_FD_WRITE;
767 if (SetEventFileDescriptor(hEventObject, (
int)s, mode) < 0)
773 DWORD WSAWaitForMultipleEvents(DWORD cEvents,
const HANDLE* lphEvents, BOOL fWaitAll,
774 DWORD dwTimeout, BOOL fAlertable)
776 return WaitForMultipleObjectsEx(cEvents, lphEvents, fWaitAll, dwTimeout, fAlertable);
779 SOCKET WSASocketA(
int af,
int type,
int protocol,
LPWSAPROTOCOL_INFOA lpProtocolInfo, GROUP g,
783 s = _socket(af, type, protocol);
787 SOCKET WSASocketW(
int af,
int type,
int protocol,
LPWSAPROTOCOL_INFOW lpProtocolInfo, GROUP g,
793 int WSAIoctl(SOCKET s, DWORD dwIoControlCode, LPVOID lpvInBuffer, DWORD cbInBuffer,
794 LPVOID lpvOutBuffer, DWORD cbOutBuffer, LPDWORD lpcbBytesReturned,
795 LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
801 size_t ifreq_len = 0;
802 struct ifreq* ifreq = NULL;
803 struct ifconf ifconf = { 0 };
804 char address[128] = { 0 };
805 char broadcast[128] = { 0 };
806 char netmask[128] = { 0 };
807 char buffer[4096] = { 0 };
808 size_t numInterfaces = 0;
809 size_t maxNumInterfaces = 0;
812 struct sockaddr_in* pAddress = NULL;
813 struct sockaddr_in* pBroadcast = NULL;
814 struct sockaddr_in* pNetmask = NULL;
816 if ((dwIoControlCode != SIO_GET_INTERFACE_LIST) ||
817 (!lpvOutBuffer || !cbOutBuffer || !lpcbBytesReturned))
819 WSASetLastError(WSAEINVAL);
826 #ifdef WSAIOCTL_IFADDRS
828 struct ifaddrs* ifap = NULL;
830 if (getifaddrs(&ifap) != 0)
832 WSASetLastError(WSAENETDOWN);
839 for (
struct ifaddrs* ifa = ifap; ifa; ifa = ifa->ifa_next)
841 pInterface = &pInterfaces[index];
842 pAddress = (
struct sockaddr_in*)&pInterface->iiAddress;
843 pBroadcast = (
struct sockaddr_in*)&pInterface->iiBroadcastAddress;
844 pNetmask = (
struct sockaddr_in*)&pInterface->iiNetmask;
847 if (ifa->ifa_flags & IFF_UP)
850 if (ifa->ifa_flags & IFF_BROADCAST)
851 nFlags |= _IFF_BROADCAST;
853 if (ifa->ifa_flags & IFF_LOOPBACK)
854 nFlags |= _IFF_LOOPBACK;
856 if (ifa->ifa_flags & IFF_POINTOPOINT)
857 nFlags |= _IFF_POINTTOPOINT;
859 if (ifa->ifa_flags & IFF_MULTICAST)
860 nFlags |= _IFF_MULTICAST;
862 pInterface->iiFlags = nFlags;
866 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6))
869 getnameinfo(ifa->ifa_addr,
sizeof(
struct sockaddr), address,
sizeof(address), 0, 0,
871 inet_pton(ifa->ifa_addr->sa_family, address, (
void*)&pAddress->sin_addr);
875 ZeroMemory(pAddress,
sizeof(
struct sockaddr_in));
878 if (ifa->ifa_dstaddr)
880 if ((ifa->ifa_dstaddr->sa_family != AF_INET) &&
881 (ifa->ifa_dstaddr->sa_family != AF_INET6))
884 getnameinfo(ifa->ifa_dstaddr,
sizeof(
struct sockaddr), broadcast,
sizeof(broadcast),
885 0, 0, NI_NUMERICHOST);
886 inet_pton(ifa->ifa_dstaddr->sa_family, broadcast, (
void*)&pBroadcast->sin_addr);
890 ZeroMemory(pBroadcast,
sizeof(
struct sockaddr_in));
893 if (ifa->ifa_netmask)
895 if ((ifa->ifa_netmask->sa_family != AF_INET) &&
896 (ifa->ifa_netmask->sa_family != AF_INET6))
899 getnameinfo(ifa->ifa_netmask,
sizeof(
struct sockaddr), netmask,
sizeof(netmask), 0,
901 inet_pton(ifa->ifa_netmask->sa_family, netmask, (
void*)&pNetmask->sin_addr);
905 ZeroMemory(pNetmask,
sizeof(
struct sockaddr_in));
912 *lpcbBytesReturned = (DWORD)(numInterfaces *
sizeof(
INTERFACE_INFO));
917 ifconf.ifc_len =
sizeof(buffer);
918 ifconf.ifc_buf = buffer;
920 if (ioctl(fd, SIOCGIFCONF, &ifconf) != 0)
922 WSASetLastError(WSAENETDOWN);
929 ifreq = ifconf.ifc_req;
931 while ((ifconf.ifc_len >= 0) && (offset < (
size_t)ifconf.ifc_len) &&
932 (numInterfaces < maxNumInterfaces))
934 pInterface = &pInterfaces[index];
935 pAddress = (
struct sockaddr_in*)&pInterface->iiAddress;
936 pBroadcast = (
struct sockaddr_in*)&pInterface->iiBroadcastAddress;
937 pNetmask = (
struct sockaddr_in*)&pInterface->iiNetmask;
939 if (ioctl(fd, SIOCGIFFLAGS, ifreq) != 0)
944 if (ifreq->ifr_flags & IFF_UP)
947 if (ifreq->ifr_flags & IFF_BROADCAST)
948 nFlags |= _IFF_BROADCAST;
950 if (ifreq->ifr_flags & IFF_LOOPBACK)
951 nFlags |= _IFF_LOOPBACK;
953 if (ifreq->ifr_flags & IFF_POINTOPOINT)
954 nFlags |= _IFF_POINTTOPOINT;
956 if (ifreq->ifr_flags & IFF_MULTICAST)
957 nFlags |= _IFF_MULTICAST;
959 pInterface->iiFlags = nFlags;
961 if (ioctl(fd, SIOCGIFADDR, ifreq) != 0)
964 if ((ifreq->ifr_addr.sa_family != AF_INET) && (ifreq->ifr_addr.sa_family != AF_INET6))
967 getnameinfo(&ifreq->ifr_addr,
sizeof(ifreq->ifr_addr), address,
sizeof(address), 0, 0,
969 inet_pton(ifreq->ifr_addr.sa_family, address, (
void*)&pAddress->sin_addr);
971 if (ioctl(fd, SIOCGIFBRDADDR, ifreq) != 0)
974 if ((ifreq->ifr_addr.sa_family != AF_INET) && (ifreq->ifr_addr.sa_family != AF_INET6))
977 getnameinfo(&ifreq->ifr_addr,
sizeof(ifreq->ifr_addr), broadcast,
sizeof(broadcast), 0, 0,
979 inet_pton(ifreq->ifr_addr.sa_family, broadcast, (
void*)&pBroadcast->sin_addr);
981 if (ioctl(fd, SIOCGIFNETMASK, ifreq) != 0)
984 if ((ifreq->ifr_addr.sa_family != AF_INET) && (ifreq->ifr_addr.sa_family != AF_INET6))
987 getnameinfo(&ifreq->ifr_addr,
sizeof(ifreq->ifr_addr), netmask,
sizeof(netmask), 0, 0,
989 inet_pton(ifreq->ifr_addr.sa_family, netmask, (
void*)&pNetmask->sin_addr);
992 #if !defined(__linux__) && !defined(__sun__) && !defined(__CYGWIN__) && !defined(EMSCRIPTEN)
993 ifreq_len = IFNAMSIZ + ifreq->ifr_addr.sa_len;
995 ifreq_len =
sizeof(*ifreq);
997 ifreq = (
struct ifreq*)&((BYTE*)ifreq)[ifreq_len];
1002 *lpcbBytesReturned = (DWORD)(numInterfaces *
sizeof(
INTERFACE_INFO));
1006 SOCKET _accept(SOCKET s,
struct sockaddr* addr,
int* addrlen)
1010 socklen_t s_addrlen = (socklen_t)*addrlen;
1011 status = accept(fd, addr, &s_addrlen);
1012 *addrlen = (int)s_addrlen;
1016 int _bind(SOCKET s,
const struct sockaddr* addr,
int namelen)
1020 status = bind(fd, addr, (socklen_t)namelen);
1023 return SOCKET_ERROR;
1028 int closesocket(SOCKET s)
1036 int _connect(SOCKET s,
const struct sockaddr* name,
int namelen)
1040 status = connect(fd, name, (socklen_t)namelen);
1043 return SOCKET_ERROR;
1049 int _ioctlsocket(SOCKET s,
long cmd, u_long* argp)
1058 return SOCKET_ERROR;
1060 flags = fcntl(fd, F_GETFL);
1063 return SOCKET_ERROR;
1066 (void)fcntl(fd, F_SETFL, flags | O_NONBLOCK);
1068 (
void)fcntl(fd, F_SETFL, flags & ~(O_NONBLOCK));
1074 int _getpeername(SOCKET s,
struct sockaddr* name,
int* namelen)
1078 socklen_t s_namelen = (socklen_t)*namelen;
1079 status = getpeername(fd, name, &s_namelen);
1080 *namelen = (int)s_namelen;
1084 int _getsockname(SOCKET s,
struct sockaddr* name,
int* namelen)
1088 socklen_t s_namelen = (socklen_t)*namelen;
1089 status = getsockname(fd, name, &s_namelen);
1090 *namelen = (int)s_namelen;
1094 int _getsockopt(SOCKET s,
int level,
int optname,
char* optval,
int* optlen)
1098 socklen_t s_optlen = (socklen_t)*optlen;
1099 status = getsockopt(fd, level, optname, (
void*)optval, &s_optlen);
1100 *optlen = (int)s_optlen;
1104 u_long _htonl(u_long hostlong)
1106 WINPR_ASSERT(hostlong <= UINT32_MAX);
1107 return htonl((UINT32)hostlong);
1110 u_short _htons(u_short hostshort)
1112 return htons(hostshort);
1115 unsigned long _inet_addr(
const char* cp)
1117 return (
long)inet_addr(cp);
1120 char* _inet_ntoa(
struct in_addr in)
1122 return inet_ntoa(in);
1125 int _listen(SOCKET s,
int backlog)
1129 status = listen(fd, backlog);
1133 u_long _ntohl(u_long netlong)
1135 WINPR_ASSERT((netlong & 0xFFFFFFFF00000000ULL) == 0);
1136 return ntohl((UINT32)netlong);
1139 u_short _ntohs(u_short netshort)
1141 return ntohs(netshort);
1144 int _recv(SOCKET s,
char* buf,
int len,
int flags)
1148 status = (int)recv(fd, (
void*)buf, (size_t)len, flags);
1152 int _recvfrom(SOCKET s,
char* buf,
int len,
int flags,
struct sockaddr* from,
int* fromlen)
1156 socklen_t s_fromlen = (socklen_t)*fromlen;
1157 status = (int)recvfrom(fd, (
void*)buf, (size_t)len, flags, from, &s_fromlen);
1158 *fromlen = (int)s_fromlen;
1162 int _select(
int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds,
1163 const struct timeval* timeout)
1168 const struct timeval* cpv;
1174 status = select(nfds, readfds, writefds, exceptfds, cnv.pv);
1175 }
while ((status < 0) && (errno == EINTR));
1180 int _send(SOCKET s,
const char* buf,
int len,
int flags)
1184 flags |= MSG_NOSIGNAL;
1185 status = (int)send(fd, (
const void*)buf, (size_t)len, flags);
1189 int _sendto(SOCKET s,
const char* buf,
int len,
int flags,
const struct sockaddr* to,
int tolen)
1193 status = (int)sendto(fd, (
const void*)buf, (size_t)len, flags, to, (socklen_t)tolen);
1197 int _setsockopt(SOCKET s,
int level,
int optname,
const char* optval,
int optlen)
1201 status = setsockopt(fd, level, optname, (
const void*)optval, (socklen_t)optlen);
1205 int _shutdown(SOCKET s,
int how)
1229 return SOCKET_ERROR;
1231 status = shutdown(fd, s_how);
1235 SOCKET _socket(
int af,
int type,
int protocol)
1239 fd = socket(af, type, protocol);
1242 return INVALID_SOCKET;
1248 struct hostent* _gethostbyaddr(
const char* addr,
int len,
int type)
1250 struct hostent* host = NULL;
1251 host = gethostbyaddr((
const void*)addr, (socklen_t)len, type);
1255 struct hostent* _gethostbyname(
const char* name)
1257 struct hostent* host = NULL;
1258 host = gethostbyname(name);
1262 int _gethostname(
char* name,
int namelen)
1265 status = gethostname(name, (
size_t)namelen);
1269 struct servent* _getservbyport(
int port,
const char* proto)
1271 struct servent* serv = NULL;
1272 serv = getservbyport(port, proto);
1276 struct servent* _getservbyname(
const char* name,
const char* proto)
1278 struct servent* serv = NULL;
1279 serv = getservbyname(name, proto);
1283 struct protoent* _getprotobynumber(
int number)
1285 struct protoent* proto = NULL;
1286 proto = getprotobynumber(number);
1290 struct protoent* _getprotobyname(
const char* name)
1292 struct protoent* proto = NULL;
1293 proto = getprotobyname(name);