/*---------------------------------------------------------------------- | GetEndPointUdpSocket +---------------------------------------------------------------------*/ static NPT_Result GetEndPointUdpSocket(EndPoint* endpoint, NPT_UdpSocket*& udp_socket) { // default return values udp_socket = NULL; switch (endpoint->type) { case ENDPOINT_TYPE_UDP_SERVER: { udp_socket = new NPT_UdpSocket(); // info if (Options.verbose) { NPT_Debug("listening on port %d", endpoint->info.udp_server.port); } // listen on port, any addr return udp_socket->Bind(NPT_SocketAddress(NPT_IpAddress::Any, endpoint->info.udp_server.port)); } break; case ENDPOINT_TYPE_MULTICAST_SERVER: { NPT_UdpMulticastSocket* udp_multicast_socket = new NPT_UdpMulticastSocket(); udp_socket = udp_multicast_socket; // info if (Options.verbose) { NPT_Debug("listening on port %d\n", endpoint->info.multicast_server.port); } // listen on port, any addr NPT_CHECK(udp_socket->Bind(NPT_SocketAddress(NPT_IpAddress::Any, endpoint->info.multicast_server.port))); // info if (Options.verbose) { NPT_Debug("joining multicast group %s\n", endpoint->info.multicast_server.groupname); } // resolve name NPT_IpAddress address; NPT_CHECK(address.ResolveName(endpoint->info.multicast_server.groupname)); // join the group NPT_CHECK(udp_multicast_socket->JoinGroup(address)); return NPT_SUCCESS; } break; default: return NPT_FAILURE; } return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int /*argc*/, char** /*argv*/) { // setup debugging #if defined(WIN32) && defined(_DEBUG) int flags = _crtDbgFlag | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF; _CrtSetDbgFlag(flags); //AllocConsole(); //freopen("CONOUT$", "w", stdout); #endif NPT_IpAddress addr; NPT_Result result; result = addr.ResolveName("www.perdu.com"); CHECK(NPT_SUCCEEDED(result)); Resolver resolver1("www.perdu.com", addr); result = addr.ResolveName("zebulon.bok.net"); CHECK(NPT_SUCCEEDED(result)); Resolver resolver2("zebulon.bok.net", addr); resolver1.Start(); resolver2.Start(); NPT_System::Sleep(10.0); NeedToStop = true; resolver1.Wait(); resolver2.Wait(); #if defined(WIN32) && defined(_DEBUG) _CrtDumpMemoryLeaks(); #endif return 0; }
virtual void Run() { while (!NeedToStop) { NPT_IpAddress addr; m_Result = addr.ResolveName(m_Name); if (NPT_FAILED(m_Result)) { NPT_Console::OutputF("ERROR: ResolveName failed (%d)\n", m_Result); return; } if (!(addr == m_Addr)) { m_Result = NPT_FAILURE; NPT_Console::OutputF("ERROR: wrong IP address (%s instead of %s for %s)\n", addr.ToString().GetChars(), m_Addr.ToString().GetChars(), m_Name.GetChars()); return; } } }
/*---------------------------------------------------------------------- | PLT_SsdpListenTask::DoInit +---------------------------------------------------------------------*/ void PLT_SsdpListenTask::DoInit() { if (m_Multicast) { if (m_JoinHard) { NPT_List<NPT_IpAddress> ips; PLT_UPnPMessageHelper::GetIPAddresses(ips); /* Join multicast group for every ip we found */ ips.Apply(PLT_SsdpInitMulticastIterator((NPT_UdpMulticastSocket*)m_Socket)); } else { NPT_IpAddress addr; addr.ResolveName("239.255.255.250"); ((NPT_UdpMulticastSocket*)m_Socket)->JoinGroup(addr, NPT_IpAddress::Any); } } }
/*---------------------------------------------------------------------- | GetEndPointStreams +---------------------------------------------------------------------*/ static NPT_Result GetEndPointStreams(EndPoint* endpoint, NPT_InputStreamReference* input_stream, NPT_OutputStreamReference* output_stream) { // default return values if (input_stream) *input_stream = NULL; if (output_stream) *output_stream = NULL; switch (endpoint->type) { case ENDPOINT_TYPE_UDP_CLIENT: { NPT_UdpSocket sender; // info if (Options.verbose) { NPT_Debug("sending to %s on port %d\n", endpoint->info.udp_client.hostname, endpoint->info.udp_client.port); } // resolve name NPT_IpAddress address; NPT_CHECK(address.ResolveName(endpoint->info.udp_client.hostname)); // connect socket NPT_CHECK(sender.Connect(NPT_SocketAddress(address, endpoint->info.udp_client.port))); // get the streams if (input_stream) { NPT_CHECK(sender.GetInputStream(*input_stream)); } if (output_stream) { NPT_CHECK(sender.GetOutputStream(*output_stream)); } return NPT_SUCCESS; } break; case ENDPOINT_TYPE_TCP_CLIENT: { NPT_TcpClientSocket client; // info if (Options.verbose) { NPT_Debug("connecting to %s on port %d\n", endpoint->info.tcp_client.hostname, endpoint->info.tcp_client.port); } // resolve the name NPT_IpAddress address; NPT_CHECK(address.ResolveName(endpoint->info.tcp_client.hostname)); // connect NPT_CHECK(client.Connect(NPT_SocketAddress(address, endpoint->info.tcp_client.port))); // info if (Options.verbose) { NPT_Debug("connected\n"); } // get the streams if (input_stream) { NPT_CHECK(client.GetInputStream(*input_stream)); } if (output_stream) { NPT_CHECK(client.GetOutputStream(*output_stream)); } return NPT_SUCCESS; } break; case ENDPOINT_TYPE_MULTICAST_CLIENT: { NPT_UdpMulticastSocket sender; // info if (Options.verbose) { NPT_Debug("sending to %s on port %d\n", endpoint->info.multicast_client.groupname, endpoint->info.multicast_client.port); } // set time to live NPT_CHECK(sender.SetTimeToLive(endpoint->info.multicast_client.ttl)); // resolve name NPT_IpAddress address; NPT_CHECK(address.ResolveName(endpoint->info.multicast_client.groupname)); // connect socket NPT_CHECK(sender.Connect(NPT_SocketAddress(address, endpoint->info.multicast_client.port))); // get the streams if (input_stream) { NPT_CHECK(sender.GetInputStream(*input_stream)); } if (output_stream) { NPT_CHECK(sender.GetOutputStream(*output_stream)); } return NPT_SUCCESS; } break; case ENDPOINT_TYPE_UDP_SERVER: { NPT_UdpSocket listener; // info if (Options.verbose) { NPT_Debug("listening on port %d", endpoint->info.udp_server.port); } // listen on port, any addr NPT_CHECK(listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, endpoint->info.udp_server.port))); // get the streams if (input_stream) { NPT_CHECK(listener.GetInputStream(*input_stream)); } if (output_stream) { NPT_CHECK(listener.GetOutputStream(*output_stream)); } return NPT_SUCCESS; } break; case ENDPOINT_TYPE_TCP_SERVER: { NPT_TcpServerSocket server; NPT_Socket* client; // info if (Options.verbose) { NPT_Debug("waiting for client on port %d\n", endpoint->info.tcp_server.port); } // bind to the address NPT_CHECK(server.Bind(NPT_SocketAddress(NPT_IpAddress::Any, endpoint->info.tcp_server.port))); // wait for connection NPT_CHECK(server.WaitForNewClient(client)); // info if (Options.verbose) { NPT_Debug("client connected\n"); } // get the streams if (input_stream) { NPT_CHECK(client->GetInputStream(*input_stream)); } if (output_stream) { NPT_CHECK(client->GetOutputStream(*output_stream)); } delete client; return NPT_SUCCESS; } break; case ENDPOINT_TYPE_MULTICAST_SERVER: { NPT_UdpMulticastSocket listener; // info if (Options.verbose) { NPT_Debug("listening on port %d\n", endpoint->info.multicast_server.port); } // listen on port, any addr NPT_CHECK(listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, endpoint->info.multicast_server.port))); // info if (Options.verbose) { NPT_Debug("joining multicast group %s\n", endpoint->info.multicast_server.groupname); } // resolve name NPT_IpAddress address; NPT_CHECK(address.ResolveName(endpoint->info.multicast_server.groupname)); // join the group NPT_CHECK(listener.JoinGroup(address)); // get the streams if (input_stream) { NPT_CHECK(listener.GetInputStream(*input_stream)); } if (output_stream) { NPT_CHECK(listener.GetOutputStream(*output_stream)); } return NPT_SUCCESS; } break; case ENDPOINT_TYPE_FILE: { // create a file object NPT_File file(endpoint->info.file.name); if (endpoint->direction == ENDPOINT_DIRECTION_IN) { NPT_CHECK(file.Open(NPT_FILE_OPEN_MODE_READ | NPT_FILE_OPEN_MODE_UNBUFFERED)); } else { NPT_CHECK(file.Open(NPT_FILE_OPEN_MODE_WRITE | NPT_FILE_OPEN_MODE_CREATE| NPT_FILE_OPEN_MODE_UNBUFFERED)); } // get the streams if (input_stream) { NPT_CHECK(file.GetInputStream(*input_stream)); } if (output_stream) { NPT_CHECK(file.GetOutputStream(*output_stream)); } return NPT_SUCCESS; } break; case ENDPOINT_TYPE_SERIAL_PORT: { // create a serial port object NPT_SerialPort serial_port(endpoint->info.serial_port.name); NPT_CHECK(serial_port.Open(endpoint->info.serial_port.speed)); // get the streams if (input_stream) { NPT_CHECK(serial_port.GetInputStream(*input_stream)); } if (output_stream) { NPT_CHECK(serial_port.GetOutputStream(*output_stream)); } return NPT_SUCCESS; } break; } return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int /*argc*/, char** /*argv*/) { // setup debugging #if defined(WIN32) && defined(_DEBUG) int flags = _crtDbgFlag | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF; _CrtSetDbgFlag(flags); //AllocConsole(); //freopen("CONOUT$", "w", stdout); #endif NPT_Result result; TcpServerThread* server_thread = NULL; NPT_TcpClientSocket* tcp_client = NULL; NPT_TcpServerSocket* tcp_server = NULL; CancellerThread* canceller = NULL; NPT_SocketAddress address(NPT_IpAddress(127,0,0,1), 10000); #if 0 result = RemoteIpAddress.ResolveName("www.google.com"); CHECK(result == NPT_SUCCESS); NPT_Console::Output("--- test for immediate connection\n"); NPT_Console::Output("[01] starting write server thread\n"); server_thread = new TcpServerThread(); server_thread->Start(); NPT_Console::Output("[01] waiting for server to be ready...\n"); server_thread->m_Ready.WaitUntilEquals(1); NPT_Console::Output("[01] server thread ready\n"); NPT_Console::Output("[01] waiting a while...\n"); NPT_System::Sleep(3.0); tcp_client = new NPT_TcpClientSocket(); NPT_Console::Output("[01] connection to 127.0.0.1:10000\n"); result = tcp_client->Connect(address); NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result)); CHECK(result == NPT_SUCCESS); delete tcp_client; NPT_Console::Output("[01] terminating server\n"); server_thread->m_Interrupted = true; server_thread->Wait(); delete server_thread; NPT_Console::Output("\n--- test for refused local connection\n"); address.SetPort(89); tcp_client = new NPT_TcpClientSocket(); NPT_Console::Output("[01] connecting to 127.0.0.1:89\n"); result = tcp_client->Connect(address); NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result)); CHECK(result == NPT_ERROR_CONNECTION_REFUSED); delete tcp_client; /*NPT_Console::Output("\n--- test for refused remote connection\n"); address.SetIpAddress(RemoteIpAddress); address.SetPort(81); tcp_client = new NPT_TcpClientSocket(); NPT_Console::Output("[01] connecting to www.google.com:81\n"); result = tcp_client->Connect(address); NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result)); CHECK(result == NPT_ERROR_CONNECTION_REFUSED); delete tcp_client;*/ NPT_Console::Output("\n--- test for connection timeout\n"); address.SetIpAddress(NPT_IpAddress(1,1,1,1)); NPT_Console::Output("[01] connecting to 1.1.1.1:89\n"); tcp_client = new NPT_TcpClientSocket(); result = tcp_client->Connect(address, 3000); NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result)); CHECK(result == NPT_ERROR_TIMEOUT); delete tcp_client; NPT_Console::Output("\n--- test for remote connection\n"); address.SetIpAddress(RemoteIpAddress); address.SetPort(80); NPT_Console::Output("[01] connecting to www.google.com:80\n"); tcp_client = new NPT_TcpClientSocket(); result = tcp_client->Connect(address); NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result)); CHECK(result == NPT_SUCCESS); delete tcp_client; #endif for (int i=0; i<2; i++) { NPT_Console::OutputF("\n--- test for cancelled connection, shutdown=%d\n", i); address.SetIpAddress(NPT_IpAddress(1,1,1,1)); address.SetPort(89); NPT_Console::Output("[01] connecting to 1.1.1.1:89\n"); tcp_client = new NPT_TcpClientSocket(NPT_SOCKET_FLAG_CANCELLABLE); canceller = new CancellerThread(tcp_client, 3.0f, i==1); result = tcp_client->Connect(address); NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result)); CHECK(result == NPT_ERROR_CANCELLED); canceller->Wait(); delete canceller; delete tcp_client; } for (int i=0; i<2; i++) { NPT_Console::OutputF("\n--- testing read cancellation, shutdown=%d\n", i); address.SetIpAddress(RemoteIpAddress); address.SetPort(80); NPT_Console::Output("[01] connecting to www.google.com:80\n"); tcp_client = new NPT_TcpClientSocket(NPT_SOCKET_FLAG_CANCELLABLE); result = tcp_client->Connect(address); NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result)); CHECK(result == NPT_SUCCESS); canceller = new CancellerThread(tcp_client, 3.0f, i==1); NPT_InputStreamReference input; tcp_client->GetInputStream(input); unsigned char buffer[4096]; NPT_SetMemory(buffer, 0, sizeof(buffer)); result = input->Read(buffer, 4096); NPT_Console::OutputF("{00} read returned %d (%s)\n", result, NPT_ResultText(result)); CHECK(result == NPT_ERROR_CANCELLED); delete tcp_client; canceller->Wait(); delete canceller; } for (int i=0; i<2; i++) { NPT_Console::OutputF("\n--- testing write cancellation, shutdown=%d\n", i); server_thread = new TcpServerThread(); server_thread->Start(); NPT_Console::Output("[01] waiting for server to be ready...\n"); server_thread->m_Ready.WaitUntilEquals(1); NPT_Console::Output("[01] server thread ready\n"); NPT_Console::Output("[01] waiting a while...\n"); NPT_System::Sleep(3.0); address.SetIpAddress(NPT_IpAddress(127,0,0,1)); address.SetPort(10000); NPT_Console::Output("[01] connecting to localhost:10000\n"); tcp_client = new NPT_TcpClientSocket(NPT_SOCKET_FLAG_CANCELLABLE); result = tcp_client->Connect(address); NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result)); CHECK(result == NPT_SUCCESS); canceller = new CancellerThread(tcp_client, 3.0f, i==1); NPT_OutputStreamReference output; tcp_client->GetOutputStream(output); NPT_Size total_written = 0; unsigned char buffer[4096]; NPT_SetMemory(buffer, 0, sizeof(buffer)); do { NPT_Size bytes_written = 0; result = output->Write(buffer, 4096, &bytes_written); if (NPT_SUCCEEDED(result)) { total_written += bytes_written; } } while (NPT_SUCCEEDED(result)); output = NULL; NPT_Console::OutputF("{01} write returned %d (%s)\n", result, NPT_ResultText(result)); NPT_Console::OutputF("{01} wrote %d bytes total\n", total_written); CHECK(result == NPT_ERROR_CANCELLED); delete tcp_client; canceller->Wait(); delete canceller; server_thread->m_Interrupted = true; server_thread->Wait(); delete server_thread; } for (int i=0; i<2; i++) { NPT_Console::OutputF("\n--- testing accept cancellation, shutdown=%d\n", i); NPT_Console::Output("{03} waiting for connection on port 10000\n"); address.SetIpAddress(NPT_IpAddress(127,0,0,1)); address.SetPort(10000); tcp_server = new NPT_TcpServerSocket(NPT_SOCKET_FLAG_CANCELLABLE); result = tcp_server->Bind(address, true); CHECK(result == NPT_SUCCESS); canceller = new CancellerThread(tcp_server, 3.0f, i==1); NPT_Socket* new_client = NULL; result = tcp_server->WaitForNewClient(new_client); NPT_Console::OutputF("{03} WaitForNewClient returned %d (%s)\n", result, NPT_ResultText(result)); CHECK(result == NPT_ERROR_CANCELLED); canceller->Wait(); delete canceller; delete tcp_server; } NPT_Console::Output("------------\n"); NPT_Console::Output("bye bye\n"); #if defined(WIN32) && defined(_DEBUG) _CrtDumpMemoryLeaks(); #endif return 0; }