diff options
Diffstat (limited to 'src/lib/mhd_sockets.c')
-rw-r--r-- | src/lib/mhd_sockets.c | 239 |
1 files changed, 120 insertions, 119 deletions
diff --git a/src/lib/mhd_sockets.c b/src/lib/mhd_sockets.c index 356d288f..248b9a68 100644 --- a/src/lib/mhd_sockets.c +++ b/src/lib/mhd_sockets.c | |||
@@ -37,10 +37,10 @@ | |||
37 | * @param err the WinSock error code. | 37 | * @param err the WinSock error code. |
38 | * @return pointer to string description of specified WinSock error. | 38 | * @return pointer to string description of specified WinSock error. |
39 | */ | 39 | */ |
40 | const char* MHD_W32_strerror_winsock_(int err) | 40 | const char*MHD_W32_strerror_winsock_ (int err) |
41 | { | 41 | { |
42 | switch (err) | 42 | switch (err) |
43 | { | 43 | { |
44 | case 0: | 44 | case 0: |
45 | return "No error"; | 45 | return "No error"; |
46 | case WSA_INVALID_HANDLE: | 46 | case WSA_INVALID_HANDLE: |
@@ -233,7 +233,7 @@ const char* MHD_W32_strerror_winsock_(int err) | |||
233 | return "Invalid QoS shaping rate object"; | 233 | return "Invalid QoS shaping rate object"; |
234 | case WSA_QOS_RESERVED_PETYPE: | 234 | case WSA_QOS_RESERVED_PETYPE: |
235 | return "Reserved policy QoS element type"; | 235 | return "Reserved policy QoS element type"; |
236 | } | 236 | } |
237 | return "Unknown winsock error"; | 237 | return "Unknown winsock error"; |
238 | } | 238 | } |
239 | 239 | ||
@@ -246,113 +246,114 @@ const char* MHD_W32_strerror_winsock_(int err) | |||
246 | * @return non-zero if succeeded, zero otherwise | 246 | * @return non-zero if succeeded, zero otherwise |
247 | */ | 247 | */ |
248 | int | 248 | int |
249 | MHD_W32_socket_pair_(SOCKET sockets_pair[2], int non_blk) | 249 | MHD_W32_socket_pair_ (SOCKET sockets_pair[2], int non_blk) |
250 | { | 250 | { |
251 | int i; | 251 | int i; |
252 | 252 | ||
253 | if (! sockets_pair) | 253 | if (! sockets_pair) |
254 | { | 254 | { |
255 | WSASetLastError (WSAEFAULT); | 255 | WSASetLastError (WSAEFAULT); |
256 | return 0; | 256 | return 0; |
257 | } | 257 | } |
258 | 258 | ||
259 | #define PAIRMAXTRYIES 800 | 259 | #define PAIRMAXTRYIES 800 |
260 | for (i = 0; i < PAIRMAXTRYIES; i++) | 260 | for (i = 0; i < PAIRMAXTRYIES; i++) |
261 | { | ||
262 | struct sockaddr_in listen_addr; | ||
263 | SOCKET listen_s; | ||
264 | static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */ | ||
265 | int addr_len = c_addinlen; | ||
266 | unsigned long on_val = 1; | ||
267 | unsigned long off_val = 0; | ||
268 | |||
269 | listen_s = socket (AF_INET, | ||
270 | SOCK_STREAM, | ||
271 | IPPROTO_TCP); | ||
272 | if (INVALID_SOCKET == listen_s) | ||
273 | break; /* can't create even single socket */ | ||
274 | |||
275 | listen_addr.sin_family = AF_INET; | ||
276 | listen_addr.sin_port = 0; /* same as htons(0) */ | ||
277 | listen_addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | ||
278 | if ( ((0 == bind (listen_s, | ||
279 | (struct sockaddr*) &listen_addr, | ||
280 | c_addinlen)) && | ||
281 | (0 == listen (listen_s, | ||
282 | 1) ) && | ||
283 | (0 == getsockname (listen_s, | ||
284 | (struct sockaddr*) &listen_addr, | ||
285 | &addr_len))) ) | ||
261 | { | 286 | { |
262 | struct sockaddr_in listen_addr; | 287 | SOCKET client_s = socket (AF_INET, |
263 | SOCKET listen_s; | 288 | SOCK_STREAM, |
264 | static const int c_addinlen = sizeof(struct sockaddr_in); /* help compiler to optimize */ | 289 | IPPROTO_TCP); |
265 | int addr_len = c_addinlen; | 290 | struct sockaddr_in accepted_from_addr; |
266 | unsigned long on_val = 1; | 291 | struct sockaddr_in client_addr; |
267 | unsigned long off_val = 0; | 292 | SOCKET server_s; |
268 | 293 | ||
269 | listen_s = socket (AF_INET, | 294 | if (INVALID_SOCKET == client_s) |
270 | SOCK_STREAM, | 295 | { |
271 | IPPROTO_TCP); | 296 | /* try again */ |
272 | if (INVALID_SOCKET == listen_s) | 297 | closesocket (listen_s); |
273 | break; /* can't create even single socket */ | 298 | continue; |
274 | 299 | } | |
275 | listen_addr.sin_family = AF_INET; | 300 | |
276 | listen_addr.sin_port = 0; /* same as htons(0) */ | 301 | if ( (0 != ioctlsocket (client_s, |
277 | listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | 302 | FIONBIO, |
278 | if ( (0 == bind (listen_s, | 303 | &on_val)) || |
279 | (struct sockaddr*) &listen_addr, | 304 | ( (0 != connect (client_s, |
280 | c_addinlen) && | 305 | (struct sockaddr*) &listen_addr, |
281 | (0 == listen (listen_s, | 306 | c_addinlen)) && |
282 | 1) ) && | 307 | (WSAGetLastError () != WSAEWOULDBLOCK)) ) |
283 | (0 == getsockname (listen_s, | 308 | { |
284 | (struct sockaddr*) &listen_addr, | 309 | /* try again */ |
285 | &addr_len))) ) | 310 | closesocket (listen_s); |
286 | { | 311 | closesocket (client_s); |
287 | SOCKET client_s = socket(AF_INET, | 312 | continue; |
288 | SOCK_STREAM, | 313 | } |
289 | IPPROTO_TCP); | 314 | |
290 | struct sockaddr_in accepted_from_addr; | 315 | addr_len = c_addinlen; |
291 | struct sockaddr_in client_addr; | 316 | server_s = accept (listen_s, |
292 | SOCKET server_s; | 317 | (struct sockaddr*) &accepted_from_addr, |
293 | 318 | &addr_len); | |
294 | if (INVALID_SOCKET == client_s) | 319 | if (INVALID_SOCKET == server_s) |
295 | { | 320 | { |
296 | /* try again */ | 321 | /* try again */ |
297 | closesocket (listen_s); | 322 | closesocket (listen_s); |
298 | continue; | 323 | closesocket (client_s); |
299 | } | 324 | continue; |
300 | 325 | } | |
301 | if ( (0 != ioctlsocket (client_s, | 326 | |
302 | FIONBIO, | 327 | addr_len = c_addinlen; |
303 | &on_val)) || | 328 | if ( (0 == getsockname (client_s, |
304 | ( (0 != connect (client_s, | 329 | (struct sockaddr*) &client_addr, |
305 | (struct sockaddr*) &listen_addr, | 330 | &addr_len)) && |
306 | c_addinlen)) && | 331 | (accepted_from_addr.sin_family == client_addr.sin_family) && |
307 | (WSAGetLastError() != WSAEWOULDBLOCK)) ) | 332 | (accepted_from_addr.sin_port == client_addr.sin_port) && |
308 | { | 333 | (accepted_from_addr.sin_addr.s_addr == |
309 | /* try again */ | 334 | client_addr.sin_addr.s_addr) && |
310 | closesocket (listen_s); | 335 | ( (0 != non_blk) ? |
311 | closesocket (client_s); | 336 | (0 == ioctlsocket (server_s, |
312 | continue; | 337 | FIONBIO, |
313 | } | 338 | &on_val)) : |
314 | 339 | (0 == ioctlsocket (client_s, | |
315 | addr_len = c_addinlen; | 340 | FIONBIO, |
316 | server_s = accept (listen_s, | 341 | &off_val)) ) ) |
317 | (struct sockaddr*) &accepted_from_addr, | 342 | { |
318 | &addr_len); | 343 | closesocket (listen_s); |
319 | if (INVALID_SOCKET == server_s) | 344 | sockets_pair[0] = server_s; |
320 | { | 345 | sockets_pair[1] = client_s; |
321 | /* try again */ | 346 | return ! 0; |
322 | closesocket (listen_s); | 347 | } |
323 | closesocket (client_s); | 348 | closesocket (server_s); |
324 | continue; | 349 | closesocket (client_s); |
325 | } | ||
326 | |||
327 | addr_len = c_addinlen; | ||
328 | if ( (0 == getsockname (client_s, | ||
329 | (struct sockaddr*) &client_addr, | ||
330 | &addr_len)) && | ||
331 | (accepted_from_addr.sin_family == client_addr.sin_family) && | ||
332 | (accepted_from_addr.sin_port == client_addr.sin_port) && | ||
333 | (accepted_from_addr.sin_addr.s_addr == client_addr.sin_addr.s_addr) && | ||
334 | ( (0 != non_blk) ? | ||
335 | (0 == ioctlsocket(server_s, | ||
336 | FIONBIO, | ||
337 | &on_val)) : | ||
338 | (0 == ioctlsocket(client_s, | ||
339 | FIONBIO, | ||
340 | &off_val)) ) ) | ||
341 | { | ||
342 | closesocket (listen_s); | ||
343 | sockets_pair[0] = server_s; | ||
344 | sockets_pair[1] = client_s; | ||
345 | return !0; | ||
346 | } | ||
347 | closesocket (server_s); | ||
348 | closesocket (client_s); | ||
349 | } | ||
350 | closesocket(listen_s); | ||
351 | } | 350 | } |
351 | closesocket (listen_s); | ||
352 | } | ||
352 | 353 | ||
353 | sockets_pair[0] = INVALID_SOCKET; | 354 | sockets_pair[0] = INVALID_SOCKET; |
354 | sockets_pair[1] = INVALID_SOCKET; | 355 | sockets_pair[1] = INVALID_SOCKET; |
355 | WSASetLastError(WSAECONNREFUSED); | 356 | WSASetLastError (WSAECONNREFUSED); |
356 | 357 | ||
357 | return 0; | 358 | return 0; |
358 | } | 359 | } |
@@ -383,9 +384,9 @@ MHD_add_to_fd_set_ (MHD_socket fd, | |||
383 | set, | 384 | set, |
384 | fd_setsize)) | 385 | fd_setsize)) |
385 | return 0; | 386 | return 0; |
386 | MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_(fd, | 387 | MHD_SCKT_ADD_FD_TO_FDSET_SETSIZE_ (fd, |
387 | set, | 388 | set, |
388 | fd_setsize); | 389 | fd_setsize); |
389 | if ( (NULL != max_fd) && | 390 | if ( (NULL != max_fd) && |
390 | ( (fd > *max_fd) || | 391 | ( (fd > *max_fd) || |
391 | (MHD_INVALID_SOCKET == *max_fd) ) ) | 392 | (MHD_INVALID_SOCKET == *max_fd) ) ) |
@@ -424,7 +425,7 @@ MHD_socket_nonblocking_ (MHD_socket sock) | |||
424 | &flags)) | 425 | &flags)) |
425 | return 0; | 426 | return 0; |
426 | #endif /* MHD_WINSOCK_SOCKETS */ | 427 | #endif /* MHD_WINSOCK_SOCKETS */ |
427 | return !0; | 428 | return ! 0; |
428 | } | 429 | } |
429 | 430 | ||
430 | 431 | ||
@@ -452,12 +453,12 @@ MHD_socket_noninheritable_ (MHD_socket sock) | |||
452 | flags | FD_CLOEXEC)) ) | 453 | flags | FD_CLOEXEC)) ) |
453 | return 0; | 454 | return 0; |
454 | #elif defined(MHD_WINSOCK_SOCKETS) | 455 | #elif defined(MHD_WINSOCK_SOCKETS) |
455 | if (! SetHandleInformation ((HANDLE)sock, | 456 | if (! SetHandleInformation ((HANDLE) sock, |
456 | HANDLE_FLAG_INHERIT, | 457 | HANDLE_FLAG_INHERIT, |
457 | 0)) | 458 | 0)) |
458 | return 0; | 459 | return 0; |
459 | #endif /* MHD_WINSOCK_SOCKETS */ | 460 | #endif /* MHD_WINSOCK_SOCKETS */ |
460 | return !0; | 461 | return ! 0; |
461 | } | 462 | } |
462 | 463 | ||
463 | 464 | ||
@@ -477,7 +478,7 @@ MHD_socket_create_listen_ (int pf) | |||
477 | fd = socket (pf, | 478 | fd = socket (pf, |
478 | SOCK_STREAM | SOCK_CLOEXEC, | 479 | SOCK_STREAM | SOCK_CLOEXEC, |
479 | 0); | 480 | 0); |
480 | cloexec_set = !0; | 481 | cloexec_set = ! 0; |
481 | #elif defined(MHD_WINSOCK_SOCKETS) && defined (WSA_FLAG_NO_HANDLE_INHERIT) | 482 | #elif defined(MHD_WINSOCK_SOCKETS) && defined (WSA_FLAG_NO_HANDLE_INHERIT) |
482 | fd = WSASocketW (pf, | 483 | fd = WSASocketW (pf, |
483 | SOCK_STREAM, | 484 | SOCK_STREAM, |
@@ -485,27 +486,27 @@ MHD_socket_create_listen_ (int pf) | |||
485 | NULL, | 486 | NULL, |
486 | 0, | 487 | 0, |
487 | WSA_FLAG_NO_HANDLE_INHERIT); | 488 | WSA_FLAG_NO_HANDLE_INHERIT); |
488 | cloexec_set = !0; | 489 | cloexec_set = ! 0; |
489 | #else /* !SOCK_CLOEXEC */ | 490 | #else /* !SOCK_CLOEXEC */ |
490 | fd = MHD_INVALID_SOCKET; | 491 | fd = MHD_INVALID_SOCKET; |
491 | #endif /* !SOCK_CLOEXEC */ | 492 | #endif /* !SOCK_CLOEXEC */ |
492 | if (MHD_INVALID_SOCKET == fd) | 493 | if (MHD_INVALID_SOCKET == fd) |
493 | { | 494 | { |
494 | fd = socket (pf, | 495 | fd = socket (pf, |
495 | SOCK_STREAM, | 496 | SOCK_STREAM, |
496 | 0); | 497 | 0); |
497 | cloexec_set = 0; | 498 | cloexec_set = 0; |
498 | } | 499 | } |
499 | if (MHD_INVALID_SOCKET == fd) | 500 | if (MHD_INVALID_SOCKET == fd) |
500 | return MHD_INVALID_SOCKET; | 501 | return MHD_INVALID_SOCKET; |
501 | #ifdef MHD_socket_nosignal_ | 502 | #ifdef MHD_socket_nosignal_ |
502 | if(! MHD_socket_nosignal_(fd)) | 503 | if (! MHD_socket_nosignal_ (fd)) |
503 | { | 504 | { |
504 | const int err = MHD_socket_get_error_ (); | 505 | const int err = MHD_socket_get_error_ (); |
505 | (void) MHD_socket_close_ (fd); | 506 | (void) MHD_socket_close_ (fd); |
506 | MHD_socket_fset_error_ (err); | 507 | MHD_socket_fset_error_ (err); |
507 | return MHD_INVALID_SOCKET; | 508 | return MHD_INVALID_SOCKET; |
508 | } | 509 | } |
509 | #endif /* MHD_socket_nosignal_ */ | 510 | #endif /* MHD_socket_nosignal_ */ |
510 | if (! cloexec_set) | 511 | if (! cloexec_set) |
511 | (void) MHD_socket_noninheritable_ (fd); | 512 | (void) MHD_socket_noninheritable_ (fd); |