pn_socket_t pn_listen(pn_io_t *io, const char *host, const char *port) { struct addrinfo *addr; int code = getaddrinfo(host, amqp_service(port), NULL, &addr); if (code) { pn_error_format(io->error, PN_ERR, "getaddrinfo(%s, %s): %s\n", host, port, gai_strerror(code)); return INVALID_SOCKET; } pn_socket_t sock = pni_create_socket(addr->ai_family); if (sock == INVALID_SOCKET) { pni_win32_error(io->error, "pni_create_socket", WSAGetLastError()); return INVALID_SOCKET; } ensure_unique(io, sock); bool optval = 1; if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (const char *) &optval, sizeof(optval)) == -1) { pni_win32_error(io->error, "setsockopt", WSAGetLastError()); closesocket(sock); return INVALID_SOCKET; } if (bind(sock, addr->ai_addr, addr->ai_addrlen) == -1) { pni_win32_error(io->error, "bind", WSAGetLastError()); freeaddrinfo(addr); closesocket(sock); return INVALID_SOCKET; } freeaddrinfo(addr); if (listen(sock, 50) == -1) { pni_win32_error(io->error, "listen", WSAGetLastError()); closesocket(sock); return INVALID_SOCKET; } if (io->iocp->selector) { iocpdesc_t *iocpd = pni_iocpdesc_create(io->iocp, sock, false); if (!iocpd) { pn_i_error_from_errno(io->error, "register"); closesocket(sock); return INVALID_SOCKET; } pni_iocpdesc_start(iocpd); } return sock; }
descriptor connect(const proton::url& u) { // convert "0.0.0.0" to "127.0.0.1" on Windows for outgoing sockets std::string host = (u.host() == "0.0.0.0") ? "127.0.0.1" : u.host(); descriptor fd = INVALID_SOCKET; try{ auto_addrinfo addr; gai_check(::getaddrinfo(host.empty() ? 0 : host.c_str(), amqp_service(u.port().empty() ? 0 : u.port().c_str()), 0, &addr.ptr), "connect address invalid: "); fd = check(::socket(addr->ai_family, SOCK_STREAM, 0), "connect socket: "); check(::connect(fd, addr->ai_addr, addr->ai_addrlen), "connect: "); return fd; } catch (...) { if (fd != INVALID_SOCKET) ::closesocket(fd); throw; } }
pn_socket_t pn_connect(pn_io_t *io, const char *hostarg, const char *port) { // convert "0.0.0.0" to "127.0.0.1" on Windows for outgoing sockets const char *host = strcmp("0.0.0.0", hostarg) ? hostarg : "127.0.0.1"; struct addrinfo *addr; int code = getaddrinfo(host, amqp_service(port), NULL, &addr); if (code) { pn_error_format(io->error, PN_ERR, "getaddrinfo(%s, %s): %s", host, port, gai_strerror(code)); return INVALID_SOCKET; } pn_socket_t sock = pni_create_socket(addr->ai_family); if (sock == INVALID_SOCKET) { pni_win32_error(io->error, "proton pni_create_socket", WSAGetLastError()); freeaddrinfo(addr); return INVALID_SOCKET; } ensure_unique(io, sock); pn_configure_sock(io, sock); if (io->iocp->selector) { return pni_iocp_begin_connect(io->iocp, sock, addr, io->error); } else { if (connect(sock, addr->ai_addr, addr->ai_addrlen) != 0) { if (WSAGetLastError() != WSAEWOULDBLOCK) { pni_win32_error(io->error, "connect", WSAGetLastError()); freeaddrinfo(addr); closesocket(sock); return INVALID_SOCKET; } } freeaddrinfo(addr); return sock; } }