/*---------------------------------------------------------------------- | UdpServerLoop +---------------------------------------------------------------------*/ static void UdpServerLoop(int port) { NPT_UdpSocket listener; // info if (Options.verbose) { NPT_Debug("listening on port %d\n", port); } NPT_Result result = listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, port)); if (NPT_FAILED(result)) { NPT_Debug("ERROR: Bind() failed (%d)\n", result); } // packet loop NPT_DataBuffer packet(32768); NPT_SocketAddress address; do { result = listener.Receive(packet, &address); if (NPT_SUCCEEDED(result)) { if (Options.verbose) { NPT_String ip = address.GetIpAddress().ToString(); NPT_Debug("Received %d bytes from %s:%d\n", packet.GetDataSize(), ip.GetChars(), address.GetPort()); } listener.Send(packet, &address); } } while (NPT_SUCCEEDED(result)); }
void Server::sendMessage(NPT_UdpSocket* socket, std::string nt, std::string message) { std::string msg = buildMsg(nt, message); NPT_IpAddress upnpAddress = getUPNPAddress(); NPT_DataBuffer buffer; NPT_SocketAddress address; address.SetIpAddress(upnpAddress); address.SetPort(UPNP_PORT); buffer.SetData((NPT_Byte*)msg.c_str(), msg.length()); socket->Send(buffer, &address); }
void Server::sendReply(std::string host, int port, std::string msg) { NPT_UdpSocket socket; NPT_DataBuffer buffer; NPT_IpAddress ip; NPT_SocketAddress address; ip.Parse(host.c_str()); address.SetIpAddress(ip); address.SetPort(port); buffer.SetData((NPT_Byte*)msg.c_str(), msg.length()); socket.Send(buffer, &address); }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { // check command line if (argc < 2) { PrintUsageAndExit(); } // init endpoints EndPoint in_endpoint; in_endpoint.direction = ENDPOINT_DIRECTION_IN; EndPoint out_endpoint; out_endpoint.direction = ENDPOINT_DIRECTION_OUT; EndPoint* current_endpoint = &in_endpoint; // init other parameters unsigned int packet_size = PUMP_DEFAULT_PACKET_SIZE; // init options Options.verbose = false; Options.show_progress = false; // parse command line argv++; char* arg; while ((arg = *argv++)) { if (current_endpoint == NULL) { NPT_Debug("ERROR: unexpected argument (%s)\n", arg); exit(1); } if (!strcmp(arg, "--packet-size")) { packet_size = strtoul(*argv++, NULL, 10); continue; } else if (!strcmp(arg, "--verbose")) { Options.verbose = true; continue; } else if (!strcmp(arg, "--show-progress")) { Options.show_progress = true; continue; } else if (!strcmp(arg, "udp")) { if (argv[0] && argv[1]) { if (!strcmp(argv[0], "server")) { if (current_endpoint->direction == ENDPOINT_DIRECTION_OUT){ NPT_Debug("ERROR: cannot use 'udp server' as output\n"); exit(1); } current_endpoint->type = ENDPOINT_TYPE_UDP_SERVER; current_endpoint->info.udp_server.port = strtoul(argv[1], NULL, 10); argv += 2; } else if (!strcmp(argv[0], "client")) { if (current_endpoint->direction == ENDPOINT_DIRECTION_IN) { NPT_Debug("ERROR: cannot use 'udp client' as input\n"); exit(1); } if (argv[2]) { current_endpoint->type = ENDPOINT_TYPE_UDP_CLIENT; current_endpoint->info.udp_client.hostname = argv[1]; current_endpoint->info.udp_client.port = strtoul(argv[2], NULL, 10); argv += 3; } else { NPT_Debug("ERROR: missing argument for 'udp client'\n"); exit(1); } } } else { NPT_Debug("ERROR: missing argument for 'udp' endpoint\n"); exit(1); } } else if (!strcmp(arg, "multicast")) { if (argv[0] && argv[1]) { if (!strcmp(argv[0], "server")) { if (current_endpoint->direction == ENDPOINT_DIRECTION_OUT){ NPT_Debug("ERROR: cannot use 'multicast server' as output\n"); exit(1); } if (argv[2]) { current_endpoint->type = ENDPOINT_TYPE_MULTICAST_SERVER; current_endpoint->info.multicast_server.groupname = argv[1]; current_endpoint->info.multicast_server.port = strtoul(argv[2], NULL, 10); argv += 3; } else { NPT_Debug("ERROR: missing argument for 'multicast server'\n"); exit(1); } } else if (!strcmp(argv[0], "client")) { if (current_endpoint->direction == ENDPOINT_DIRECTION_IN) { NPT_Debug("ERROR: cannot use 'udp client' as input\n"); exit(1); } if (argv[2] && argv[3]) { current_endpoint->type = ENDPOINT_TYPE_MULTICAST_CLIENT; current_endpoint->info.multicast_client.groupname = argv[1]; current_endpoint->info.multicast_client.port = strtoul(argv[2], NULL, 10); current_endpoint->info.multicast_client.ttl = strtoul(argv[3], NULL, 10); argv += 4; } else { NPT_Debug("ERROR: missing argument for 'multicast client'\n"); exit(1); } } } else { NPT_Debug("ERROR: missing argument for 'multicast' endpoint\n"); exit(1); } } else if (!strcmp(arg, "tcp")) { if (argv[0] && argv[1]) { if (!strcmp(argv[0], "server")) { current_endpoint->type = ENDPOINT_TYPE_TCP_SERVER; current_endpoint->info.tcp_server.port = strtoul(argv[1], NULL, 10); argv += 2; } else if (!strcmp(argv[0], "client")) { if (argv[2]) { current_endpoint->type = ENDPOINT_TYPE_TCP_CLIENT; current_endpoint->info.tcp_client.hostname = argv[1]; current_endpoint->info.tcp_client.port = strtoul(argv[2], NULL, 10); argv += 3; } else { NPT_Debug("ERROR: missing argument for 'tcp client'\n"); exit(1); } } } else { NPT_Debug("ERROR: missing argument for 'tcp' endpoint\n"); exit(1); } } else if (!strcmp(arg, "file")) { if (argv[0]) { current_endpoint->type = ENDPOINT_TYPE_FILE; current_endpoint->info.file.name = *argv++; } else { NPT_Debug("ERROR: missing argument for 'file' endpoint\n"); exit(1); } } else if (!strcmp(arg, "serial")) { if (argv[0]) { current_endpoint->type = ENDPOINT_TYPE_SERIAL_PORT; current_endpoint->info.serial_port.name = *argv++; } else { NPT_Debug("ERROR: missing argument for 'serial' endpoint\n"); exit(1); } if (argv[0]) { long speed = 0; if (NPT_FAILED(NPT_ParseInteger(*argv++, speed))) { NPT_Debug("ERROR: invalid speed for 'serial' endpoint\n"); exit(1); } current_endpoint->info.serial_port.speed = (unsigned int)speed; } else { NPT_Debug("ERROR: missing argument for 'serial' endpoint\n"); exit(1); } } else { NPT_Debug("ERROR: invalid argument (%s)\n", arg); exit(1); } if (current_endpoint == &in_endpoint) { current_endpoint = &out_endpoint; } else { current_endpoint = NULL; } } if (current_endpoint) { NPT_Debug("ERROR: missing endpoint specification\n"); exit(1); } // data pump NPT_Result result; // allocate buffer unsigned char* buffer; buffer = (unsigned char*)malloc(packet_size); if (buffer == NULL) { NPT_Debug("ERROR: out of memory\n"); exit(1); } // get output stream NPT_OutputStreamReference out; result = GetEndPointStreams(&out_endpoint, NULL, &out); if (NPT_FAILED(result)) { NPT_Debug("ERROR: failed to get stream for output (%d)", result); exit(1); } unsigned long offset = 0; unsigned long total = 0; if (in_endpoint.type == ENDPOINT_TYPE_UDP_SERVER || in_endpoint.type == ENDPOINT_TYPE_MULTICAST_SERVER) { NPT_UdpSocket* udp_socket; result = GetEndPointUdpSocket(&in_endpoint, udp_socket); // packet loop NPT_DataBuffer packet(32768); NPT_SocketAddress address; do { result = udp_socket->Receive(packet, &address); if (NPT_SUCCEEDED(result)) { if (Options.verbose) { NPT_String ip = address.GetIpAddress().ToString(); NPT_Debug("Received %d bytes from %s\n", packet.GetDataSize(), ip.GetChars()); } result = out->Write(packet.GetData(), packet.GetDataSize(), NULL); offset += packet.GetDataSize(); total += packet.GetDataSize(); } } while (NPT_SUCCEEDED(result)); } else { // get the input stream NPT_InputStreamReference in; result = GetEndPointStreams(&in_endpoint, &in, NULL); if (NPT_FAILED(result)) { NPT_Debug("ERROR: failed to get stream for input (%d)\n", result); exit(1); } // stream loop do { NPT_Size bytes_read; NPT_Size bytes_written; // send result = in->Read(buffer, packet_size, &bytes_read); if (Options.show_progress) { NPT_Debug("[%d]\r", total); } if (NPT_SUCCEEDED(result) && bytes_read) { result = out->Write(buffer, bytes_read, &bytes_written); if (Options.show_progress) { NPT_Debug("[%d]\r", total); } offset += bytes_written; total += bytes_written; } else { printf("[%d] *******************\n", result); exit(1); } } while (NPT_SUCCEEDED(result)); } delete buffer; return 0; }
void Server::listen() { _aliveThread = new std::thread([] () { int delay = 10000; while (true) { sleep(delay); printf("SENDING ALIVE\n"); sendAlive(); delay = (delay == 10000 ? 20000 : 180000); } }); _listenerThread = new std::thread([] () { bool bindErrorReported = false; while (true) { NPT_UdpMulticastSocket socket; NPT_IpAddress upnpAddress = getUPNPAddress(); // TODO: Setup local port and network interface socket.SetTimeToLive(4); socket.JoinGroup(upnpAddress); while (true) { char* buf = new char[1024]; NPT_DataBuffer receivePacket; NPT_SocketAddress address; receivePacket.SetBuffer((NPT_Byte*)buf, 1024); socket.Receive(receivePacket, &address); std::string s((char*)receivePacket.GetData(), receivePacket.GetDataSize()); if (s.compare(0, 8, "M-SEARCH") == 0) { NPT_String remoteAddr = address.GetIpAddress().ToString(); int remotePort = address.GetPort(); // if (gMS->isAddressAllowed(address)) { if (s.find_first_of("urn:schemas-upnp-org:service:ContentDirectory:1") > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, "urn:schemas-upnp-org:service:ContentDirectory:1"); } if (s.find_first_of("upnp:rootdevice") > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, "upnp:rootdevice"); } if (s.find_first_of("urn:schemas-upnp-org:device:MediaServer:1") > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, "urn:schemas-upnp-org:device:MediaServer:1"); } if (s.find_first_of("ssdp:all") > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, "urn:schemas-upnp-org:device:MediaServer:1"); } if (s.find_first_of(gMS->udnString()) > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, gMS->udnString()); } } } else if (s.compare(0, 6, "NOTIFY") == 0) { /* String remoteAddr = address.getHostAddress(); int remotePort = receivePacket.getPort(); logger.trace("Receiving a NOTIFY from [" + remoteAddr + ":" + remotePort + "]"); */ } } } }); }
/*---------------------------------------------------------------------- | NPT_SocketAddress::operator== +---------------------------------------------------------------------*/ bool NPT_SocketAddress::operator==(const NPT_SocketAddress& other) const { return (other.GetIpAddress().AsLong() == m_IpAddress.AsLong() && other.GetPort() == m_Port); }