1 //! Adapt the Winsock2 API to resemble a POSIX-style libc API. 2 3 #![allow(unused_imports)] 4 #![allow(non_camel_case_types)] 5 6 use windows_sys::Win32::Networking::WinSock; 7 8 pub(crate) use libc::{ 9 c_char, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint, c_ulong, c_ulonglong, 10 c_ushort, c_void, ssize_t, 11 }; 12 pub(crate) type socklen_t = i32; 13 14 // windows-sys declares these constants as unsigned. For better compatibility 15 // with Unix-family APIs, redeclare them as signed. Filed upstream: 16 // <https://github.com/microsoft/windows-rs/issues/1718> 17 pub(crate) const AF_INET: i32 = WinSock::AF_INET as _; 18 pub(crate) const AF_INET6: i32 = WinSock::AF_INET6 as _; 19 pub(crate) const AF_UNSPEC: i32 = WinSock::AF_UNSPEC as _; 20 pub(crate) const SO_TYPE: i32 = WinSock::SO_TYPE as _; 21 pub(crate) const SO_REUSEADDR: i32 = WinSock::SO_REUSEADDR as _; 22 pub(crate) const SO_BROADCAST: i32 = WinSock::SO_BROADCAST as _; 23 pub(crate) const SO_LINGER: i32 = WinSock::SO_LINGER as _; 24 pub(crate) const SOL_SOCKET: i32 = WinSock::SOL_SOCKET as _; 25 pub(crate) const SO_RCVTIMEO: i32 = WinSock::SO_RCVTIMEO as _; 26 pub(crate) const SO_SNDTIMEO: i32 = WinSock::SO_SNDTIMEO as _; 27 pub(crate) const IP_TTL: i32 = WinSock::IP_TTL as _; 28 pub(crate) const TCP_NODELAY: i32 = WinSock::TCP_NODELAY as _; 29 pub(crate) const IP_ADD_MEMBERSHIP: i32 = WinSock::IP_ADD_MEMBERSHIP as _; 30 pub(crate) const IP_DROP_MEMBERSHIP: i32 = WinSock::IP_DROP_MEMBERSHIP as _; 31 pub(crate) const IP_MULTICAST_TTL: i32 = WinSock::IP_MULTICAST_TTL as _; 32 pub(crate) const IP_MULTICAST_LOOP: i32 = WinSock::IP_MULTICAST_LOOP as _; 33 pub(crate) const IPV6_ADD_MEMBERSHIP: i32 = WinSock::IPV6_ADD_MEMBERSHIP as _; 34 pub(crate) const IPV6_DROP_MEMBERSHIP: i32 = WinSock::IPV6_DROP_MEMBERSHIP as _; 35 pub(crate) const IPV6_MULTICAST_LOOP: i32 = WinSock::IPV6_MULTICAST_LOOP as _; 36 pub(crate) const IPV6_V6ONLY: i32 = WinSock::IPV6_V6ONLY as _; 37 pub(crate) const POLLERR: i16 = WinSock::POLLERR as _; 38 pub(crate) const POLLIN: i16 = WinSock::POLLIN as _; 39 pub(crate) const POLLNVAL: i16 = WinSock::POLLNVAL as _; 40 pub(crate) const POLLHUP: i16 = WinSock::POLLHUP as _; 41 pub(crate) const POLLPRI: i16 = WinSock::POLLPRI as _; 42 pub(crate) const POLLOUT: i16 = WinSock::POLLOUT as _; 43 pub(crate) const POLLRDNORM: i16 = WinSock::POLLRDNORM as _; 44 pub(crate) const POLLWRNORM: i16 = WinSock::POLLWRNORM as _; 45 pub(crate) const POLLRDBAND: i16 = WinSock::POLLRDBAND as _; 46 pub(crate) const POLLWRBAND: i16 = WinSock::POLLWRBAND as _; 47 48 // As above, cast the types for better compatibility, and also rename these to 49 // their Unix names. 50 pub(crate) const SHUT_RDWR: i32 = WinSock::SD_BOTH as _; 51 pub(crate) const SHUT_RD: i32 = WinSock::SD_RECEIVE as _; 52 pub(crate) const SHUT_WR: i32 = WinSock::SD_SEND as _; 53 54 // Include the contents of `WinSock`, renaming as needed to match POSIX. 55 // 56 // Use `WSA_E_CANCELLED` for `ECANCELED` instead of `WSAECANCELLED`, because 57 // `WSAECANCELLED` will be removed in the future. 58 // <https://docs.microsoft.com/en-us/windows/win32/api/ws2spi/nc-ws2spi-lpnsplookupserviceend#remarks> 59 pub(crate) use WinSock::{ 60 closesocket as close, ioctlsocket as ioctl, WSAPoll as poll, ADDRESS_FAMILY as sa_family_t, 61 ADDRINFOA as addrinfo, IN6_ADDR as in6_addr, IN_ADDR as in_addr, IPV6_MREQ as ipv6_mreq, 62 IP_MREQ as ip_mreq, LINGER as linger, SOCKADDR as sockaddr, SOCKADDR_IN as sockaddr_in, 63 SOCKADDR_IN6 as sockaddr_in6, SOCKADDR_STORAGE as sockaddr_storage, WSAEACCES as EACCES, 64 WSAEADDRINUSE as EADDRINUSE, WSAEADDRNOTAVAIL as EADDRNOTAVAIL, 65 WSAEAFNOSUPPORT as EAFNOSUPPORT, WSAEALREADY as EALREADY, WSAEBADF as EBADF, 66 WSAECONNABORTED as ECONNABORTED, WSAECONNREFUSED as ECONNREFUSED, WSAECONNRESET as ECONNRESET, 67 WSAEDESTADDRREQ as EDESTADDRREQ, WSAEDISCON as EDISCON, WSAEDQUOT as EDQUOT, 68 WSAEFAULT as EFAULT, WSAEHOSTDOWN as EHOSTDOWN, WSAEHOSTUNREACH as EHOSTUNREACH, 69 WSAEINPROGRESS as EINPROGRESS, WSAEINTR as EINTR, WSAEINVAL as EINVAL, 70 WSAEINVALIDPROCTABLE as EINVALIDPROCTABLE, WSAEINVALIDPROVIDER as EINVALIDPROVIDER, 71 WSAEISCONN as EISCONN, WSAELOOP as ELOOP, WSAEMFILE as EMFILE, WSAEMSGSIZE as EMSGSIZE, 72 WSAENAMETOOLONG as ENAMETOOLONG, WSAENETDOWN as ENETDOWN, WSAENETRESET as ENETRESET, 73 WSAENETUNREACH as ENETUNREACH, WSAENOBUFS as ENOBUFS, WSAENOMORE as ENOMORE, 74 WSAENOPROTOOPT as ENOPROTOOPT, WSAENOTCONN as ENOTCONN, WSAENOTEMPTY as ENOTEMPTY, 75 WSAENOTSOCK as ENOTSOCK, WSAEOPNOTSUPP as EOPNOTSUPP, WSAEPFNOSUPPORT as EPFNOSUPPORT, 76 WSAEPROCLIM as EPROCLIM, WSAEPROTONOSUPPORT as EPROTONOSUPPORT, WSAEPROTOTYPE as EPROTOTYPE, 77 WSAEPROVIDERFAILEDINIT as EPROVIDERFAILEDINIT, WSAEREFUSED as EREFUSED, WSAEREMOTE as EREMOTE, 78 WSAESHUTDOWN as ESHUTDOWN, WSAESOCKTNOSUPPORT as ESOCKTNOSUPPORT, WSAESTALE as ESTALE, 79 WSAETIMEDOUT as ETIMEDOUT, WSAETOOMANYREFS as ETOOMANYREFS, WSAEUSERS as EUSERS, 80 WSAEWOULDBLOCK as EWOULDBLOCK, WSAEWOULDBLOCK as EAGAIN, WSAPOLLFD as pollfd, 81 WSA_E_CANCELLED as ECANCELED, *, 82 }; 83