| 218 | } |
| 219 | |
| 220 | static sock_t bindInternal(int family, int socktype, int protocol, |
| 221 | const struct sockaddr* addr, socklen_t addrlen, |
| 222 | std::string& error) |
| 223 | { |
| 224 | int errNum; |
| 225 | sock_t fd = socket(family, socktype, protocol); |
| 226 | errNum = SOCKET_ERRNO; |
| 227 | if (fd == (sock_t)-1) { |
| 228 | error = errorMsg(errNum); |
| 229 | return -1; |
| 230 | } |
| 231 | util::make_fd_cloexec(fd); |
| 232 | int sockopt = 1; |
| 233 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (a2_sockopt_t)&sockopt, |
| 234 | sizeof(sockopt)) < 0) { |
| 235 | errNum = SOCKET_ERRNO; |
| 236 | error = errorMsg(errNum); |
| 237 | CLOSE(fd); |
| 238 | return -1; |
| 239 | } |
| 240 | #ifdef IPV6_V6ONLY |
| 241 | if (family == AF_INET6) { |
| 242 | int sockopt = 1; |
| 243 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (a2_sockopt_t)&sockopt, |
| 244 | sizeof(sockopt)) < 0) { |
| 245 | errNum = SOCKET_ERRNO; |
| 246 | error = errorMsg(errNum); |
| 247 | CLOSE(fd); |
| 248 | return -1; |
| 249 | } |
| 250 | } |
| 251 | #endif // IPV6_V6ONLY |
| 252 | |
| 253 | applySocketBufferSize(fd); |
| 254 | |
| 255 | if (::bind(fd, addr, addrlen) == -1) { |
| 256 | errNum = SOCKET_ERRNO; |
| 257 | error = errorMsg(errNum); |
| 258 | CLOSE(fd); |
| 259 | return -1; |
| 260 | } |
| 261 | return fd; |
| 262 | } |
| 263 | |
| 264 | static sock_t bindTo(const char* host, uint16_t port, int family, int sockType, |
| 265 | int getaddrinfoFlags, std::string& error) |
no test coverage detected