Create a non-blocking socket and bind it */ todo: rename this function */
| 124 | /* Create a non-blocking socket and bind it */ |
| 125 | /* todo: rename this function */ |
| 126 | static evutil_socket_t bind_socket_ai(struct evutil_addrinfo* ai, int reuse) { |
| 127 | evutil_socket_t fd; |
| 128 | |
| 129 | int on = 1, r; |
| 130 | |
| 131 | /* Create listen socket */ |
| 132 | fd = evutil_socket_(ai ? ai->ai_family : AF_INET, |
| 133 | SOCK_STREAM | EVUTIL_SOCK_NONBLOCK | EVUTIL_SOCK_CLOEXEC, 0); |
| 134 | if (fd == -1) { |
| 135 | // event_sock_warn(-1, "socket"); |
| 136 | return (-1); |
| 137 | } |
| 138 | |
| 139 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&on, sizeof(on)) < 0) { |
| 140 | LogPrint(BCLog::ERROR, "bind_socket_ai setsockopt(%d, SOL_SOCKET, SO_KEEPALIVE) failed\n", fd); |
| 141 | evutil_closesocket(fd); |
| 142 | return (-1); |
| 143 | } |
| 144 | |
| 145 | if (evutil_make_listen_socket_reuseable(fd) < 0) { |
| 146 | LogPrint(BCLog::ERROR, "bind_socket_ai evutil_make_listen_socket_reuseable failed\n"); |
| 147 | evutil_closesocket(fd); |
| 148 | return (-1); |
| 149 | } |
| 150 | |
| 151 | #if defined(IPV6_V6ONLY) |
| 152 | int ai_family = ai ? ai->ai_family : AF_INET; |
| 153 | // AF_INET6 |
| 154 | if (ai_family == PF_INET6) { |
| 155 | on = 1; |
| 156 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&on, (ev_socklen_t)sizeof(on)) < 0) { |
| 157 | LogPrint(BCLog::ERROR, "bind_socket_ai evutil_make_listen_socket_ipv6only failed\n"); |
| 158 | return (-1); |
| 159 | } |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | if (ai != NULL) { |
| 164 | r = bind(fd, ai->ai_addr, (ev_socklen_t)ai->ai_addrlen); |
| 165 | if (r == -1) { |
| 166 | int serrno = EVUTIL_SOCKET_ERROR(); |
| 167 | LogPrint(BCLog::ERROR, "bind_socket_ai bind failed, err: %s\n", |
| 168 | evutil_socket_error_to_string(serrno)); |
| 169 | evutil_closesocket(fd); |
| 170 | return (-1); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return (fd); |
| 175 | } |
| 176 | |
| 177 | static evutil_socket_t bind_socket(const char* address, ev_uint16_t port, int reuse) { |
| 178 | evutil_socket_t fd; |
no test coverage detected