Internal wrapper around 'socket' to provide Linux-style support for * syscall-saving methods where available. * * In addition to regular socket behavior, you can use a bitwise or to set the * flags EVUTIL_SOCK_NONBLOCK and EVUTIL_SOCK_CLOEXEC in the 'type' argument, * to make the socket nonblocking or close-on-exec with as few syscalls as * possible. */
| 95 | * possible. |
| 96 | */ |
| 97 | evutil_socket_t evutil_socket_(int domain, int type, int protocol) { |
| 98 | evutil_socket_t r; |
| 99 | #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) |
| 100 | r = socket(domain, type, protocol); |
| 101 | if (r >= 0) |
| 102 | return r; |
| 103 | else if ((type & (SOCK_NONBLOCK | SOCK_CLOEXEC)) == 0) |
| 104 | return -1; |
| 105 | #endif |
| 106 | #define SOCKET_TYPE_MASK (~(EVUTIL_SOCK_NONBLOCK | EVUTIL_SOCK_CLOEXEC)) |
| 107 | r = socket(domain, type & SOCKET_TYPE_MASK, protocol); |
| 108 | if (r < 0) return -1; |
| 109 | if (type & EVUTIL_SOCK_NONBLOCK) { |
| 110 | if (evutil_fast_socket_nonblocking(r) < 0) { |
| 111 | evutil_closesocket(r); |
| 112 | return -1; |
| 113 | } |
| 114 | } |
| 115 | if (type & EVUTIL_SOCK_CLOEXEC) { |
| 116 | if (evutil_fast_socket_closeonexec(r) < 0) { |
| 117 | evutil_closesocket(r); |
| 118 | return -1; |
| 119 | } |
| 120 | } |
| 121 | return r; |
| 122 | } |
| 123 | |
| 124 | /* Create a non-blocking socket and bind it */ |
| 125 | /* todo: rename this function */ |
no test coverage detected