MCPcopy Create free account
hub / github.com/Tencent/UnLua / inet_trybind

Function inet_trybind

Plugins/UnLuaExtensions/LuaSocket/Source/src/inet.cpp:454–492  ·  view source on GitHub ↗

-------------------------------------------------------------------------*\ * Tries to bind socket to (address, port) \*-------------------------------------------------------------------------*/

Source from the content-addressed store, hash-verified

452* Tries to bind socket to (address, port)
453\*-------------------------------------------------------------------------*/
454const char *inet_trybind(p_socket ps, int *family, const char *address,
455 const char *serv, struct addrinfo *bindhints) {
456 struct addrinfo *iterator = NULL, *resolved = NULL;
457 const char *err = NULL;
458 int current_family = *family;
459 /* translate luasocket special values to C */
460 if (strcmp(address, "*") == 0) address = NULL;
461 if (!serv) serv = "0";
462 /* try resolving */
463 err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved));
464 if (err) {
465 if (resolved) freeaddrinfo(resolved);
466 return err;
467 }
468 /* iterate over resolved addresses until one is good */
469 for (iterator = resolved; iterator; iterator = iterator->ai_next) {
470 if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) {
471 socket_destroy(ps);
472 err = inet_trycreate(ps, iterator->ai_family,
473 iterator->ai_socktype, iterator->ai_protocol);
474 if (err) continue;
475 current_family = iterator->ai_family;
476 }
477 /* try binding to local address */
478 err = socket_strerror(socket_bind(ps, (SA *) iterator->ai_addr,
479 (socklen_t) iterator->ai_addrlen));
480 /* keep trying unless bind succeeded */
481 if (err == NULL) {
482 *family = current_family;
483 /* set to non-blocking after bind */
484 socket_setnonblocking(ps);
485 break;
486 }
487 }
488 /* cleanup and return error */
489 freeaddrinfo(resolved);
490 /* here, if err is set, we failed */
491 return err;
492}
493
494/*-------------------------------------------------------------------------*\
495* Some systems do not provide these so that we provide our own.

Callers 3

meth_setsocknameFunction · 0.85
meth_bindFunction · 0.85
global_connectFunction · 0.85

Calls 6

inet_trycreateFunction · 0.85
socket_gaistrerrorFunction · 0.70
socket_destroyFunction · 0.70
socket_strerrorFunction · 0.70
socket_bindFunction · 0.70
socket_setnonblockingFunction · 0.70

Tested by

no test coverage detected