Quake II RTX doxygen  1.0 dev
win.h File Reference

Go to the source code of this file.

Functions

static const char * os_error_string (int err)
 
static ssize_t os_udp_recv (qsocket_t sock, void *data, size_t len, netadr_t *from)
 
static ssize_t os_udp_send (qsocket_t sock, const void *data, size_t len, const netadr_t *to)
 
static neterr_t os_get_error (void)
 
static ssize_t os_recv (qsocket_t sock, void *data, size_t len, int flags)
 
static ssize_t os_send (qsocket_t sock, const void *data, size_t len, int flags)
 
static neterr_t os_listen (qsocket_t sock, int backlog)
 
static neterr_t os_accept (qsocket_t sock, qsocket_t *newsock, netadr_t *from)
 
static neterr_t os_connect (qsocket_t sock, const netadr_t *to)
 
static neterr_t os_make_nonblock (qsocket_t sock, int val)
 
static neterr_t os_setsockopt (qsocket_t sock, int level, int name, int val)
 
static neterr_t os_getsockopt (qsocket_t sock, int level, int name, int *val)
 
static neterr_t os_bind (qsocket_t sock, const struct sockaddr *addr, size_t addrlen)
 
static neterr_t os_getsockname (qsocket_t sock, netadr_t *name)
 
static void os_closesocket (qsocket_t sock)
 
static qsocket_t os_socket (int domain, int type, int protocol)
 
static ioentry_t * os_add_io (qsocket_t fd)
 
static ioentry_t * os_get_io (qsocket_t fd)
 
static qsocket_t os_get_fd (ioentry_t *e)
 
static int os_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
 
static void os_net_init (void)
 
static void os_net_shutdown (void)
 

Variables

struct {
   int   err
 
   const char *   msg
 
wsa_error_table []
 

Function Documentation

◆ os_accept()

static neterr_t os_accept ( qsocket_t  sock,
qsocket_t *  newsock,
netadr_t *  from 
)
static

Definition at line 212 of file win.h.

213 {
214  struct sockaddr_storage addr;
215  int addrlen;
216  SOCKET s;
217 
218  memset(&addr, 0, sizeof(addr));
219  addrlen = sizeof(addr);
220  s = accept(sock, (struct sockaddr *)&addr, &addrlen);
221 
222  NET_SockadrToNetadr(&addr, from);
223 
224  if (s == INVALID_SOCKET) {
225  *newsock = -1;
226  return os_get_error();
227  }
228 
229  *newsock = s;
230  return NET_OK;
231 }

◆ os_add_io()

static ioentry_t* os_add_io ( qsocket_t  fd)
static

Definition at line 332 of file win.h.

333 {
334  ioentry_t *e;
335  int i;
336 
337  for (i = 0, e = io_entries; i < io_numfds; i++, e++) {
338  if (!e->inuse)
339  break;
340  }
341 
342  if (i == io_numfds) {
343  if (++io_numfds > FD_SETSIZE)
344  Com_Error(ERR_FATAL, "%s: no more space for fd: %Id", __func__, fd);
345  }
346 
347  e->fd = fd;
348  return e;
349 }

◆ os_bind()

static neterr_t os_bind ( qsocket_t  sock,
const struct sockaddr *  addr,
size_t  addrlen 
)
static

Definition at line 289 of file win.h.

290 {
291  if (bind(sock, addr, addrlen) == SOCKET_ERROR) {
292  net_error = WSAGetLastError();
293  return NET_ERROR;
294  }
295 
296  return NET_OK;
297 }

◆ os_closesocket()

static void os_closesocket ( qsocket_t  sock)
static

Definition at line 315 of file win.h.

316 {
317  closesocket(sock);
318 }

◆ os_connect()

static neterr_t os_connect ( qsocket_t  sock,
const netadr_t *  to 
)
static

Definition at line 233 of file win.h.

234 {
235  struct sockaddr_storage addr;
236  int addrlen;
237 
238  addrlen = NET_NetadrToSockadr(to, &addr);
239 
240  if (connect(sock, (struct sockaddr *)&addr, addrlen) == SOCKET_ERROR) {
241  net_error = WSAGetLastError();
242  if (net_error == WSAEWOULDBLOCK)
243  return NET_OK;
244 
245  return NET_ERROR;
246  }
247 
248  return NET_OK;
249 }

◆ os_error_string()

static const char* os_error_string ( int  err)
static

Definition at line 93 of file win.h.

94 {
95  int i;
96 
97  for (i = 0; wsa_error_table[i].err != -1; i++) {
98  if (wsa_error_table[i].err == err)
99  break;
100  }
101 
102  return wsa_error_table[i].msg;
103 }

Referenced by os_net_init().

◆ os_get_error()

static neterr_t os_get_error ( void  )
static

Definition at line 173 of file win.h.

174 {
175  net_error = WSAGetLastError();
176  if (net_error == WSAEWOULDBLOCK)
177  return NET_AGAIN;
178 
179  return NET_ERROR;
180 }

Referenced by os_accept(), os_recv(), and os_send().

◆ os_get_fd()

static qsocket_t os_get_fd ( ioentry_t *  e)
static

Definition at line 367 of file win.h.

368 {
369  return e->fd;
370 }

◆ os_get_io()

static ioentry_t* os_get_io ( qsocket_t  fd)
static

Definition at line 351 of file win.h.

352 {
353  ioentry_t *e;
354  int i;
355 
356  for (i = 0, e = io_entries; i < io_numfds; i++, e++) {
357  if (!e->inuse)
358  continue;
359  if (e->fd == fd)
360  return e;
361  }
362 
363  Com_Error(ERR_FATAL, "%s: fd not found: %Id", __func__, fd);
364  return NULL;
365 }

◆ os_getsockname()

static neterr_t os_getsockname ( qsocket_t  sock,
netadr_t *  name 
)
static

Definition at line 299 of file win.h.

300 {
301  struct sockaddr_storage addr;
302  int addrlen;
303 
304  memset(&addr, 0, sizeof(addr));
305  addrlen = sizeof(addr);
306  if (getsockname(sock, (struct sockaddr *)&addr, &addrlen) == SOCKET_ERROR) {
307  net_error = WSAGetLastError();
308  return NET_ERROR;
309  }
310 
311  NET_SockadrToNetadr(&addr, name);
312  return NET_OK;
313 }

◆ os_getsockopt()

static neterr_t os_getsockopt ( qsocket_t  sock,
int  level,
int  name,
int val 
)
static

Definition at line 275 of file win.h.

276 {
277  u_long _val;
278  int optlen = sizeof(_val);
279 
280  if (getsockopt(sock, level, name, (char *)&_val, &optlen) == SOCKET_ERROR) {
281  net_error = WSAGetLastError();
282  return NET_ERROR;
283  }
284 
285  *val = _val;
286  return NET_OK;
287 }

◆ os_listen()

static neterr_t os_listen ( qsocket_t  sock,
int  backlog 
)
static

Definition at line 202 of file win.h.

203 {
204  if (listen(sock, backlog) == SOCKET_ERROR) {
205  net_error = WSAGetLastError();
206  return NET_ERROR;
207  }
208 
209  return NET_OK;
210 }

◆ os_make_nonblock()

static neterr_t os_make_nonblock ( qsocket_t  sock,
int  val 
)
static

Definition at line 251 of file win.h.

252 {
253  u_long _val = val;
254 
255  if (ioctlsocket(sock, FIONBIO, &_val) == SOCKET_ERROR) {
256  net_error = WSAGetLastError();
257  return NET_ERROR;
258  }
259 
260  return NET_OK;
261 }

◆ os_net_init()

static void os_net_init ( void  )
static

Definition at line 383 of file win.h.

384 {
385  WSADATA ws;
386  int ret;
387 
388  ret = WSAStartup(MAKEWORD(1, 1), &ws);
389  if (ret) {
390  Com_Error(ERR_FATAL, "Winsock initialization failed: %s (%d)",
391  os_error_string(ret), ret);
392  }
393 
394  Com_DPrintf("Winsock initialized\n");
395 }

◆ os_net_shutdown()

static void os_net_shutdown ( void  )
static

Definition at line 397 of file win.h.

398 {
399  WSACleanup();
400 }

◆ os_recv()

static ssize_t os_recv ( qsocket_t  sock,
void data,
size_t  len,
int  flags 
)
static

Definition at line 182 of file win.h.

183 {
184  int ret = recv(sock, data, len, flags);
185 
186  if (ret == SOCKET_ERROR)
187  return os_get_error();
188 
189  return ret;
190 }

◆ os_select()

static int os_select ( int  nfds,
fd_set *  rfds,
fd_set *  wfds,
fd_set *  efds,
struct timeval *  tv 
)
static

Definition at line 372 of file win.h.

374 {
375  int ret = select(nfds, rfds, wfds, efds, tv);
376 
377  if (ret == SOCKET_ERROR)
378  net_error = WSAGetLastError();
379 
380  return ret;
381 }

◆ os_send()

static ssize_t os_send ( qsocket_t  sock,
const void data,
size_t  len,
int  flags 
)
static

Definition at line 192 of file win.h.

193 {
194  int ret = send(sock, data, len, flags);
195 
196  if (ret == SOCKET_ERROR)
197  return os_get_error();
198 
199  return ret;
200 }

◆ os_setsockopt()

static neterr_t os_setsockopt ( qsocket_t  sock,
int  level,
int  name,
int  val 
)
static

Definition at line 263 of file win.h.

264 {
265  u_long _val = val;
266 
267  if (setsockopt(sock, level, name, (char *)&_val, sizeof(_val)) == SOCKET_ERROR) {
268  net_error = WSAGetLastError();
269  return NET_ERROR;
270  }
271 
272  return NET_OK;
273 }

◆ os_socket()

static qsocket_t os_socket ( int  domain,
int  type,
int  protocol 
)
static

Definition at line 320 of file win.h.

321 {
322  SOCKET s = socket(domain, type, protocol);
323 
324  if (s == INVALID_SOCKET) {
325  net_error = WSAGetLastError();
326  return -1;
327  }
328 
329  return s;
330 }

◆ os_udp_recv()

static ssize_t os_udp_recv ( qsocket_t  sock,
void data,
size_t  len,
netadr_t *  from 
)
static

Definition at line 105 of file win.h.

107 {
108  struct sockaddr_storage addr;
109  int addrlen;
110  int ret;
111  int tries;
112 
113  for (tries = 0; tries < MAX_ERROR_RETRIES; tries++) {
114  memset(&addr, 0, sizeof(addr));
115  addrlen = sizeof(addr);
116  ret = recvfrom(sock, data, len, 0,
117  (struct sockaddr *)&addr, &addrlen);
118 
119  NET_SockadrToNetadr(&addr, from);
120 
121  if (ret != SOCKET_ERROR)
122  return ret;
123 
124  net_error = WSAGetLastError();
125 
126  // wouldblock is silent
127  if (net_error == WSAEWOULDBLOCK)
128  return NET_AGAIN;
129 
130 #if USE_ICMP
131  if (net_error == WSAECONNRESET || net_error == WSAENETRESET) {
132  // winsock has already provided us with
133  // a valid address from ICMP error packet
134  NET_ErrorEvent(sock, from, net_error, 0);
135  continue;
136  }
137 #endif
138 
139  break;
140  }
141 
142  return NET_ERROR;
143 }

◆ os_udp_send()

static ssize_t os_udp_send ( qsocket_t  sock,
const void data,
size_t  len,
const netadr_t *  to 
)
static

Definition at line 145 of file win.h.

147 {
148  struct sockaddr_storage addr;
149  int addrlen;
150  int ret;
151 
152  addrlen = NET_NetadrToSockadr(to, &addr);
153 
154  ret = sendto(sock, data, len, 0,
155  (struct sockaddr *)&addr, addrlen);
156 
157  if (ret != SOCKET_ERROR)
158  return ret;
159 
160  net_error = WSAGetLastError();
161 
162  // wouldblock is silent
163  if (net_error == WSAEWOULDBLOCK || net_error == WSAEINTR)
164  return NET_AGAIN;
165 
166  // some PPP links do not allow broadcasts
167  if (net_error == WSAEADDRNOTAVAIL && to->type == NA_BROADCAST)
168  return NET_AGAIN;
169 
170  return NET_ERROR;
171 }

Variable Documentation

◆ err

◆ msg

◆ wsa_error_table

const { ... } wsa_error_table[]

Referenced by os_error_string().

MAX_ERROR_RETRIES
#define MAX_ERROR_RETRIES
Definition: net.c:65
net_error
static int net_error
Definition: net.c:110
tv
float * tv(float x, float y, float z)
Definition: g_utils.c:248
os_error_string
static const char * os_error_string(int err)
Definition: win.h:93
Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition: g_main.c:258
io_numfds
static int io_numfds
Definition: net.c:123
NET_SockadrToNetadr
static void NET_SockadrToNetadr(const struct sockaddr_storage *s, netadr_t *a)
Definition: net.c:176
NET_NetadrToSockadr
static size_t NET_NetadrToSockadr(const netadr_t *a, struct sockaddr_storage *s)
Definition: net.c:145
io_entries
static ioentry_t io_entries[FD_SETSIZE]
Definition: net.c:122
level
level_locals_t level
Definition: g_main.c:22
err
int err
Definition: win.h:24
os_get_error
static neterr_t os_get_error(void)
Definition: win.h:173
wsa_error_table
static const struct @10 wsa_error_table[]