TEST_F (SocketAddressTest, Set) { SocketAddress sa; ASSERT_TRUE (sa.SetToLocalhost (AF_INET, 1138)); ASSERT_STREQ ("127.0.0.1", sa.GetIPAddress ().c_str ()); ASSERT_EQ (1138, sa.GetPort ()); ASSERT_TRUE (sa.SetToAnyAddress (AF_INET, 0)); ASSERT_STREQ ("0.0.0.0", sa.GetIPAddress ().c_str ()); ASSERT_EQ (0, sa.GetPort ()); ASSERT_TRUE (sa.SetToLocalhost (AF_INET6, 1139)); ASSERT_STREQ ("::1", sa.GetIPAddress ().c_str ()); ASSERT_EQ (1139, sa.GetPort ()); }
Error UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket) { std::unique_ptr<UDPSocket> final_send_socket; std::unique_ptr<UDPSocket> final_recv_socket; Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION)); if (log) log->Printf("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data()); Error error; std::string host_str; std::string port_str; int32_t port = INT32_MIN; if (!DecodeHostAndPort(name, host_str, port_str, port, &error)) return error; // Setup the receiving end of the UDP connection on this localhost // on port zero. After we bind to port zero we can read the port. final_recv_socket.reset(new UDPSocket(child_processes_inherit, error)); if (error.Success()) { // Socket was created, now lets bind to the requested port SocketAddress addr; addr.SetToAnyAddress(AF_INET, 0); if (::bind(final_recv_socket->GetNativeSocket(), addr, addr.GetLength()) == -1) { // Bind failed... SetLastError(error); } } assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid())); if (error.Fail()) return error; // At this point we have setup the receive port, now we need to // setup the UDP send socket struct addrinfo hints; struct addrinfo *service_info_list = nullptr; ::memset(&hints, 0, sizeof(hints)); hints.ai_family = kDomain; hints.ai_socktype = kType; int err = ::getaddrinfo(host_str.c_str(), port_str.c_str(), &hints, &service_info_list); if (err != 0) { error.SetErrorStringWithFormat( #if defined(_MSC_VER) && defined(UNICODE) "getaddrinfo(%s, %s, &hints, &info) returned error %i (%S)", #else "getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)", #endif host_str.c_str(), port_str.c_str(), err, gai_strerror(err)); return error; } for (struct addrinfo *service_info_ptr = service_info_list; service_info_ptr != nullptr; service_info_ptr = service_info_ptr->ai_next) { auto send_fd = CreateSocket( service_info_ptr->ai_family, service_info_ptr->ai_socktype, service_info_ptr->ai_protocol, child_processes_inherit, error); if (error.Success()) { final_send_socket.reset(new UDPSocket(send_fd)); final_send_socket->m_send_sockaddr = service_info_ptr; break; } else continue; } ::freeaddrinfo(service_info_list); if (!final_send_socket) return error; send_socket = final_send_socket.release(); recv_socket = final_recv_socket.release(); error.Clear(); return error; }
Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket) { std::unique_ptr<Socket> final_send_socket; std::unique_ptr<Socket> final_recv_socket; NativeSocket final_send_fd = kInvalidSocketValue; NativeSocket final_recv_fd = kInvalidSocketValue; Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); if (log) log->Printf ("Socket::UdpConnect (host/port = %s)", host_and_port.data()); Error error; std::string host_str; std::string port_str; int32_t port = INT32_MIN; if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error)) return error; // Setup the receiving end of the UDP connection on this localhost // on port zero. After we bind to port zero we can read the port. final_recv_fd = ::CreateSocket (AF_INET, SOCK_DGRAM, 0, child_processes_inherit); if (final_recv_fd == kInvalidSocketValue) { // Socket creation failed... SetLastError (error); } else { final_recv_socket.reset(new Socket(final_recv_fd, ProtocolUdp, true)); // Socket was created, now lets bind to the requested port SocketAddress addr; addr.SetToAnyAddress (AF_INET, 0); if (::bind (final_recv_fd, addr, addr.GetLength()) == -1) { // Bind failed... SetLastError (error); } } assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid())); if (error.Fail()) return error; // At this point we have setup the receive port, now we need to // setup the UDP send socket struct addrinfo hints; struct addrinfo *service_info_list = NULL; ::memset (&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; int err = ::getaddrinfo (host_str.c_str(), port_str.c_str(), &hints, &service_info_list); if (err != 0) { error.SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)", host_str.c_str(), port_str.c_str(), err, gai_strerror(err)); return error; } for (struct addrinfo *service_info_ptr = service_info_list; service_info_ptr != NULL; service_info_ptr = service_info_ptr->ai_next) { final_send_fd = ::CreateSocket (service_info_ptr->ai_family, service_info_ptr->ai_socktype, service_info_ptr->ai_protocol, child_processes_inherit); if (final_send_fd != kInvalidSocketValue) { final_send_socket.reset(new Socket(final_send_fd, ProtocolUdp, true)); final_send_socket->m_udp_send_sockaddr = service_info_ptr; break; } else continue; } :: freeaddrinfo (service_info_list); if (final_send_fd == kInvalidSocketValue) { SetLastError (error); return error; } send_socket = final_send_socket.release(); recv_socket = final_recv_socket.release(); error.Clear(); return error; }