| 1 | module net |
| 2 | |
| 3 | const is_windows = true |
| 4 | |
| 5 | // Link to Winsock library |
| 6 | #flag -lws2_32 |
| 7 | #include <winsock2.h> |
| 8 | #include <ws2tcpip.h> |
| 9 | |
| 10 | // Constants that windows needs |
| 11 | pub const fionbio = C.FIONBIO |
| 12 | pub const msg_nosignal = 0 |
| 13 | pub const msg_dontwait = 0 |
| 14 | |
| 15 | pub const error_ewouldblock = int(WsaError.wsaewouldblock) |
| 16 | pub const error_einprogress = int(WsaError.wsaeinprogress) |
| 17 | pub const error_eagain = int(WsaError.wsaewouldblock) // on windows, is also wsaewouldblock |
| 18 | pub const error_eintr = int(WsaError.wsaeintr) |
| 19 | |
| 20 | const wsa_v22 = 0x202 |
| 21 | |
| 22 | // WsaError is all of the socket errors that WSA provides from WSAGetLastError |
| 23 | pub enum WsaError { |
| 24 | // MessageId: WSAEINTR, A blocking operation was interrupted by a call to WSACancelBlockingCall. |
| 25 | wsaeintr = 10004 |
| 26 | // MessageId: WSAEBADF, The file handle supplied is not valid. |
| 27 | wsaebadf = 10009 |
| 28 | // MessageId: WSAEACCES, An attempt was made to access a socket in a way forbidden by its access permissions. |
| 29 | wsaeacces = 10013 |
| 30 | // MessageId: WSAEFAULT, The system detected an invalid pointer address in attempting to use a pointer argument in a call. |
| 31 | wsaefault = 10014 |
| 32 | // MessageId: WSAEINVAL, An invalid argument was supplied. |
| 33 | wsaeinval = 10022 |
| 34 | // MessageId: WSAEMFILE, Too many open sockets. |
| 35 | wsaemfile = 10024 |
| 36 | // MessageId: WSAEWOULDBLOCK, A non-blocking socket operation could not be completed immediately. |
| 37 | wsaewouldblock = 10035 |
| 38 | // MessageId: WSAEINPROGRESS, A blocking operation is currently executing. |
| 39 | wsaeinprogress = 10036 |
| 40 | // MessageId: WSAEALREADY, An operation was attempted on a non-blocking socket that already had an operation in progress. |
| 41 | wsaealready = 10037 |
| 42 | // MessageId: WSAENOTSOCK, An operation was attempted on something that is not a socket. |
| 43 | wsaenotsock = 10038 |
| 44 | // MessageId: WSAEDESTADDRREQ, A required address was omitted from an operation on a socket. |
| 45 | wsaedestaddrreq = 10039 |
| 46 | // MessageId: WSAEMSGSIZE, A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself. |
| 47 | wsaemsgsize = 10040 |
| 48 | // MessageId: WSAEPROTOTYPE, A protocol was specified in the socket function call that does not support the semantics of the socket type requested. |
| 49 | wsaeprototype = 10041 |
| 50 | // MessageId: WSAENOPROTOOPT, An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call. |
| 51 | wsaenoprotoopt = 10042 |
| 52 | // MessageId: WSAEPROTONOSUPPORT, The requested protocol has not been configured into the system, or no implementation for it exists. |
| 53 | wsaeprotonosupport = 10043 |
| 54 | // MessageId: WSAESOCKTNOSUPPORT, The support for the specified socket type does not exist in this address family. |
| 55 | wsaesocktnosupport = 10044 |
| 56 | // MessageId: WSAEOPNOTSUPP, The attempted operation is not supported for the type of object referenced. |
| 57 | wsaeopnotsupp = 10045 |
| 58 | // MessageId: WSAEPFNOSUPPORT, The protocol family has not been configured into the system or no implementation for it exists. |
| 59 | wsaepfnosupport = 10046 |
| 60 | // MessageId: WSAEAFNOSUPPORT, An address incompatible with the requested protocol was used. |
| 61 | wsaeafnosupport = 10047 |
| 62 | // MessageId: WSAEADDRINUSE, Only one usage of each socket address (protocol/network address/port) is normally permitted. |
| 63 | wsaeaddrinuse = 10048 |
| 64 | // MessageId: WSAEADDRNOTAVAIL, The requested address is not valid in its context. |
| 65 | wsaeaddrnotavail = 10049 |
| 66 | // MessageId: WSAENETDOWN, A socket operation encountered a dead network. |
| 67 | wsaenetdown = 10050 |
| 68 | // MessageId: WSAENETUNREACH, A socket operation was attempted to an unreachable network. |
| 69 | wsaenetunreach = 10051 |
| 70 | // MessageId: WSAENETRESET, The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress. |
| 71 | wsaenetreset = 10052 |
| 72 | // MessageId: WSAECONNABORTED, An established connection was aborted by the software in your host machine. |
| 73 | wsaeconnaborted = 10053 |
| 74 | // MessageId: WSAECONNRESET, An existing connection was forcibly closed by the remote host. |
| 75 | wsaeconnreset = 10054 |
| 76 | // MessageId: WSAENOBUFS, An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full. |
| 77 | wsaenobufs = 10055 |
| 78 | // MessageId: WSAEISCONN, A connect request was made on an already connected socket. |
| 79 | wsaeisconn = 10056 |
| 80 | // MessageId: WSAENOTCONN, A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. |
| 81 | wsaenotconn = 10057 |
| 82 | // MessageId: WSAESHUTDOWN, A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call. |
| 83 | wsaeshutdown = 10058 |
| 84 | // MessageId: WSAETOOMANYREFS, Too many references to some kernel object. |
| 85 | wsaetoomanyrefs = 10059 |
| 86 | // MessageId: WSAETIMEDOUT, A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. |
| 87 | wsaetimedout = 10060 |
| 88 | // MessageId: WSAECONNREFUSED, No connection could be made because the target machine actively refused it. |
| 89 | wsaeconnrefused = 10061 |
| 90 | // MessageId: WSAELOOP, Cannot translate name. |
| 91 | wsaeloop = 10062 |
| 92 | // MessageId: WSAENAMETOOLONG, Name component or name was too long. |
| 93 | wsaenametoolong = 10063 |
| 94 | // MessageId: WSAEHOSTDOWN, A socket operation failed because the destination host was down. |
| 95 | wsaehostdown = 10064 |
| 96 | // MessageId: WSAEHOSTUNREACH, A socket operation was attempted to an unreachable host. |
| 97 | wsaehostunreach = 10065 |
| 98 | // MessageId: WSAENOTEMPTY, Cannot remove a directory that is not empty. |
| 99 | wsaenotempty = 10066 |
| 100 | // MessageId: WSAEPROCLIM, A Windows Sockets implementation may have a limit on the number of applications that may use it simultaneously. |
| 101 | wsaeproclim = 10067 |
| 102 | // MessageId: WSAEUSERS, Ran out of quota. |
| 103 | wsaeusers = 10068 |
| 104 | // MessageId: WSAEDQUOT, Ran out of disk quota. |
| 105 | wsaedquot = 10069 |
| 106 | // MessageId: WSAESTALE, File handle reference is no longer available. |
| 107 | wsaestale = 10070 |
| 108 | // MessageId: WSAEREMOTE, Item is not available locally. |
| 109 | wsaeremote = 10071 |
| 110 | // MessageId: WSASYSNOTREADY, WSAStartup cannot function at this time because the underlying system it uses to provide network services is currently unavailable. |
| 111 | wsasysnotready = 10091 |
| 112 | // MessageId: WSAVERNOTSUPPORTED, The Windows Sockets version requested is not supported. |
| 113 | wsavernotsupported = 10092 |
| 114 | // MessageId: WSANOTINITIALISED, Either the application has not called WSAStartup, or WSAStartup failed. |
| 115 | wsanotinitialised = 10093 |
| 116 | // MessageId: WSAEDISCON, Returned by WSARecv or WSARecvFrom to indicate the remote party has initiated a graceful shutdown sequence. |
| 117 | wsaediscon = 10101 |
| 118 | // MessageId: WSAENOMORE, No more results can be returned by WSALookupServiceNext. |
| 119 | wsaenomore = 10102 |
| 120 | // MessageId: WSAECANCELLED, A call to WSALookupServiceEnd was made while this call was still processing. The call has been canceled. |
| 121 | wsaecancelled = 10103 |
| 122 | // MessageId: WSAEINVALIDPROCTABLE, The procedure call table is invalid. |
| 123 | wsaeinvalidproctable = 10104 |
| 124 | // MessageId: WSAEINVALIDPROVIDER, The requested service provider is invalid. |
| 125 | wsaeinvalidprovider = 10105 |
| 126 | // MessageId: WSAEPROVIDERFAILEDINIT, The requested service provider could not be loaded or initialized. |
| 127 | wsaeproviderfailedinit = 10106 |
| 128 | // MessageId: WSASYSCALLFAILURE, A system call has failed. |
| 129 | wsasyscallfailure = 10107 |
| 130 | // MessageId: WSASERVICE_NOT_FOUND, No such service is known. The service cannot be found in the specified name space. |
| 131 | wsaservice_not_found = 10108 |
| 132 | // MessageId: WSATYPE_NOT_FOUND, The specified class was not found. |
| 133 | wsatype_not_found = 10109 |
| 134 | // MessageId: WSA_E_NO_MORE, No more results can be returned by WSALookupServiceNext. |
| 135 | wsa_e_no_more = 10110 |
| 136 | // MessageId: WSA_E_CANCELLED, A call to WSALookupServiceEnd was made while this call was still processing. The call has been canceled. |
| 137 | wsa_e_cancelled = 10111 |
| 138 | // MessageId: WSAEREFUSED, A database query failed because it was actively refused. |
| 139 | wsaerefused = 10112 |
| 140 | // MessageId: WSAHOST_NOT_FOUND, No such host is known. |
| 141 | wsahost_not_found = 11001 |
| 142 | // MessageId: WSATRY_AGAIN, This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. |
| 143 | wsatry_again = 11002 |
| 144 | // MessageId: WSANO_RECOVERY, A non-recoverable error occurred during a database lookup. |
| 145 | wsano_recovery = 11003 |
| 146 | // MessageId: WSANO_DATA, The requested name is valid, but no data of the requested type was found. |
| 147 | wsano_data = 11004 |
| 148 | // MessageId: WSA_QOS_RECEIVERS, At least one reserve has arrived. |
| 149 | wsa_qos_receivers = 11005 |
| 150 | // MessageId: WSA_QOS_SENDERS, At least one path has arrived. |
| 151 | wsa_qos_senders = 11006 |
| 152 | // MessageId: WSA_QOS_NO_SENDERS, There are no senders. |
| 153 | wsa_qos_no_senders = 11007 |
| 154 | // MessageId: WSA_QOS_NO_RECEIVERS, There are no receivers. |
| 155 | wsa_qos_no_receivers = 11008 |
| 156 | // MessageId: WSA_QOS_REQUEST_CONFIRMED, Reserve has been confirmed. |
| 157 | wsa_qos_request_confirmed = 11009 |
| 158 | // MessageId: WSA_QOS_ADMISSION_FAILURE, Error due to lack of resources. |
| 159 | wsa_qos_admission_failure = 11010 |
| 160 | // MessageId: WSA_QOS_POLICY_FAILURE, Rejected for administrative reasons - bad credentials. |
| 161 | wsa_qos_policy_failure = 11011 |
| 162 | // MessageId: WSA_QOS_BAD_STYLE, Unknown or conflicting style. |
| 163 | wsa_qos_bad_style = 11012 |
| 164 | // MessageId: WSA_QOS_BAD_OBJECT, Problem with some part of the filterspec or providerspecific buffer in general. |
| 165 | wsa_qos_bad_object = 11013 |
| 166 | // MessageId: WSA_QOS_TRAFFIC_CTRL_ERROR, Problem with some part of the flowspec. |
| 167 | wsa_qos_traffic_ctrl_error = 11014 |
| 168 | // MessageId: WSA_QOS_GENERIC_ERROR, General QOS error. |
| 169 | wsa_qos_generic_error = 11015 |
| 170 | // MessageId: WSA_QOS_ESERVICETYPE, An invalid or unrecognized service type was found in the flowspec. |
| 171 | wsa_qos_eservicetype = 11016 |
| 172 | // MessageId: WSA_QOS_EFLOWSPEC, An invalid or inconsistent flowspec was found in the QOS structure. |
| 173 | wsa_qos_eflowspec = 11017 |
| 174 | // MessageId: WSA_QOS_EPROVSPECBUF, Invalid QOS provider-specific buffer. |
| 175 | wsa_qos_eprovspecbuf = 11018 |
| 176 | // MessageId: WSA_QOS_EFILTERSTYLE, An invalid QOS filter style was used. |
| 177 | wsa_qos_efilterstyle = 11019 |
| 178 | // MessageId: WSA_QOS_EFILTERTYPE, An invalid QOS filter type was used. |
| 179 | wsa_qos_efiltertype = 11020 |
| 180 | // MessageId: WSA_QOS_EFILTERCOUNT, An incorrect number of QOS FILTERSPECs were specified in the FLOWDESCRIPTOR. |
| 181 | wsa_qos_efiltercount = 11021 |
| 182 | // MessageId: WSA_QOS_EOBJLENGTH, An object with an invalid ObjectLength field was specified in the QOS provider-specific buffer. |
| 183 | wsa_qos_eobjlength = 11022 |
| 184 | // MessageId: WSA_QOS_EFLOWCOUNT, An incorrect number of flow descriptors was specified in the QOS structure. |
| 185 | wsa_qos_eflowcount = 11023 |
| 186 | // MessageId: WSA_QOS_EUNKOWNPSOBJ, An unrecognized object was found in the QOS provider-specific buffer. |
| 187 | wsa_qos_eunkownpsobj = 11024 |
| 188 | // MessageId: WSA_QOS_EPOLICYOBJ, An invalid policy object was found in the QOS provider-specific buffer. |
| 189 | wsa_qos_epolicyobj = 11025 |
| 190 | // MessageId: WSA_QOS_EFLOWDESC, An invalid QOS flow descriptor was found in the flow descriptor list. |
| 191 | wsa_qos_eflowdesc = 11026 |
| 192 | // MessageId: WSA_QOS_EPSFLOWSPEC, An invalid or inconsistent flowspec was found in the QOS provider specific buffer. |
| 193 | wsa_qos_epsflowspec = 11027 |
| 194 | // MessageId: WSA_QOS_EPSFILTERSPEC, An invalid FILTERSPEC was found in the QOS provider-specific buffer. |
| 195 | wsa_qos_epsfilterspec = 11028 |
| 196 | // MessageId: WSA_QOS_ESDMODEOBJ, An invalid shape discard mode object was found in the QOS provider specific buffer. |
| 197 | wsa_qos_esdmodeobj = 11029 |
| 198 | // MessageId: WSA_QOS_ESHAPERATEOBJ, An invalid shaping rate object was found in the QOS provider-specific buffer. |
| 199 | wsa_qos_eshaperateobj = 11030 |
| 200 | // MessageId: WSA_QOS_RESERVED_PETYPE, A reserved policy element was found in the QOS provider-specific buffer. |
| 201 | wsa_qos_reserved_petype = 11031 |
| 202 | // MessageId: WSA_SECURE_HOST_NOT_FOUND, No such host is known securely. |
| 203 | wsa_secure_host_not_found = 11032 |
| 204 | // MessageId: WSA_IPSEC_NAME_POLICY_ERROR, Name based IPSEC policy could not be added. |
| 205 | wsa_ipsec_name_policy_error = 11033 |
| 206 | } |
| 207 | |
| 208 | // wsa_error casts an int to its WsaError value |
| 209 | pub fn wsa_error(code int) WsaError { |
| 210 | return unsafe { WsaError(code) } |
| 211 | } |
| 212 | |
| 213 | // Error code returns the last socket error |
| 214 | pub fn error_code() int { |
| 215 | return C.WSAGetLastError() |
| 216 | } |
| 217 | |
| 218 | pub struct C.WSAData { |
| 219 | mut: |
| 220 | wVersion u16 |
| 221 | wHighVersion u16 |
| 222 | szDescription [257]u8 |
| 223 | szSystemStatus [129]u8 |
| 224 | iMaxSockets u16 |
| 225 | iMaxUdpDg u16 |
| 226 | lpVendorInfo &u8 |
| 227 | } |
| 228 | |
| 229 | fn init() { |
| 230 | mut wsadata := C.WSAData{ |
| 231 | lpVendorInfo: 0 |
| 232 | } |
| 233 | res := C.WSAStartup(wsa_v22, &wsadata) |
| 234 | if res != 0 { |
| 235 | panic('socket: WSAStartup failed') |
| 236 | } |
| 237 | } |
| 238 | |