Fixes for datatype size on amd64.
[crack-attack.git] / enet / win32.c
blob230007160cb3ebdc570a4bfbeff6d4e9f52c9cfd
1 /**
2 @file win32.c
3 @brief ENet Win32 system specific functions
4 */
5 #ifdef WIN32
7 #include <time.h>
8 #define ENET_BUILDING_LIB 1
9 #include "enet/enet.h"
11 static enet_uint32 timeBase = 0;
13 int
14 enet_initialize (void)
16 WORD versionRequested = MAKEWORD (1, 1);
17 WSADATA wsaData;
19 if (WSAStartup (versionRequested, & wsaData))
20 return -1;
22 if (LOBYTE (wsaData.wVersion) != 1||
23 HIBYTE (wsaData.wVersion) != 1)
25 WSACleanup ();
27 return -1;
30 timeBeginPeriod (1);
32 return 0;
35 void
36 enet_deinitialize (void)
38 timeEndPeriod (1);
40 WSACleanup ();
43 enet_uint32
44 enet_time_get (void)
46 return (enet_uint32) timeGetTime () - timeBase;
49 void
50 enet_time_set (enet_uint32 newTimeBase)
52 timeBase = (enet_uint32) timeGetTime () - newTimeBase;
55 int
56 enet_address_set_host (ENetAddress * address, const char * name)
58 struct hostent * hostEntry;
60 hostEntry = gethostbyname (name);
61 if (hostEntry == NULL ||
62 hostEntry -> h_addrtype != AF_INET)
64 unsigned long host = inet_addr (name);
65 if (host == INADDR_NONE)
66 return -1;
67 address -> host = host;
68 return 0;
71 address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
73 return 0;
76 int
77 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
79 struct in_addr in;
80 struct hostent * hostEntry;
82 in.s_addr = address -> host;
84 hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
85 if (hostEntry == NULL)
87 char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
88 if (addr == NULL)
89 return -1;
90 strncpy (name, addr, nameLength);
91 return 0;
94 strncpy (name, hostEntry -> h_name, nameLength);
96 return 0;
99 ENetSocket
100 enet_socket_create (ENetSocketType type, const ENetAddress * address)
102 ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
103 u_long nonBlocking = 1;
104 int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE,
105 allowBroadcasting = 1;
106 struct sockaddr_in sin;
108 if (newSocket == ENET_SOCKET_NULL)
109 return ENET_SOCKET_NULL;
111 if (type == ENET_SOCKET_TYPE_DATAGRAM)
113 ioctlsocket (newSocket, FIONBIO, & nonBlocking);
115 setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) & receiveBufferSize, sizeof (int));
116 setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) & allowBroadcasting, sizeof (int));
119 memset (& sin, 0, sizeof (struct sockaddr_in));
121 sin.sin_family = AF_INET;
123 if (address != NULL)
125 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
126 sin.sin_addr.s_addr = address -> host;
128 else
130 sin.sin_port = 0;
131 sin.sin_addr.s_addr = INADDR_ANY;
134 if (bind (newSocket,
135 (struct sockaddr *) & sin,
136 sizeof (struct sockaddr_in)) == SOCKET_ERROR ||
137 (type == ENET_SOCKET_TYPE_STREAM &&
138 address != NULL &&
139 listen (newSocket, SOMAXCONN) == SOCKET_ERROR))
141 closesocket (newSocket);
143 return ENET_SOCKET_NULL;
146 return newSocket;
150 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
152 struct sockaddr_in sin;
154 memset (& sin, 0, sizeof (struct sockaddr_in));
156 sin.sin_family = AF_INET;
157 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
158 sin.sin_addr.s_addr = address -> host;
160 return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
163 ENetSocket
164 enet_socket_accept (ENetSocket socket, ENetAddress * address)
166 SOCKET result;
167 struct sockaddr_in sin;
168 int sinLength = sizeof (struct sockaddr_in);
170 result = accept (socket,
171 address != NULL ? (struct sockaddr *) & sin : NULL,
172 address != NULL ? & sinLength : NULL);
174 if (result == INVALID_SOCKET)
175 return ENET_SOCKET_NULL;
177 if (address != NULL)
179 address -> host = (enet_uint32) sin.sin_addr.s_addr;
180 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
183 return result;
186 void
187 enet_socket_destroy (ENetSocket socket)
189 closesocket (socket);
193 enet_socket_send (ENetSocket socket,
194 const ENetAddress * address,
195 const ENetBuffer * buffers,
196 size_t bufferCount)
198 struct sockaddr_in sin;
199 DWORD sentLength;
201 if (address != NULL)
203 sin.sin_family = AF_INET;
204 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
205 sin.sin_addr.s_addr = address -> host;
208 if (WSASendTo (socket,
209 (LPWSABUF) buffers,
210 (DWORD) bufferCount,
211 & sentLength,
213 address != NULL ? (struct sockaddr *) & sin : 0,
214 address != NULL ? sizeof (struct sockaddr_in) : 0,
215 NULL,
216 NULL) == SOCKET_ERROR)
218 if (WSAGetLastError () == WSAEWOULDBLOCK)
219 return 0;
221 return -1;
224 return (int) sentLength;
228 enet_socket_receive (ENetSocket socket,
229 ENetAddress * address,
230 ENetBuffer * buffers,
231 size_t bufferCount)
233 INT sinLength = sizeof (struct sockaddr_in);
234 DWORD flags = 0,
235 recvLength;
236 struct sockaddr_in sin;
238 if (WSARecvFrom (socket,
239 (LPWSABUF) buffers,
240 (DWORD) bufferCount,
241 & recvLength,
242 & flags,
243 address != NULL ? (struct sockaddr *) & sin : NULL,
244 address != NULL ? & sinLength : NULL,
245 NULL,
246 NULL) == SOCKET_ERROR)
248 switch (WSAGetLastError ())
250 case WSAEWOULDBLOCK:
251 case WSAECONNRESET:
252 return 0;
255 return -1;
258 if (flags & MSG_PARTIAL)
259 return -1;
261 if (address != NULL)
263 address -> host = (enet_uint32) sin.sin_addr.s_addr;
264 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
267 return (int) recvLength;
271 enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
273 fd_set readSet, writeSet;
274 struct timeval timeVal;
275 int selectCount;
277 timeVal.tv_sec = timeout / 1000;
278 timeVal.tv_usec = (timeout % 1000) * 1000;
280 FD_ZERO (& readSet);
281 FD_ZERO (& writeSet);
283 if (* condition & ENET_SOCKET_WAIT_SEND)
284 FD_SET (socket, & writeSet);
286 if (* condition & ENET_SOCKET_WAIT_RECEIVE)
287 FD_SET (socket, & readSet);
289 selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
291 if (selectCount < 0)
292 return -1;
294 * condition = ENET_SOCKET_WAIT_NONE;
296 if (selectCount == 0)
297 return 0;
299 if (FD_ISSET (socket, & writeSet))
300 * condition |= ENET_SOCKET_WAIT_SEND;
302 if (FD_ISSET (socket, & readSet))
303 * condition |= ENET_SOCKET_WAIT_RECEIVE;
305 return 0;
308 #endif