/*---------------------------------------------------------------------- | NPT_SocketAddress::operator== +---------------------------------------------------------------------*/ bool NPT_SocketAddress::operator==(const NPT_SocketAddress& other) const { return (other.GetIpAddress().AsLong() == m_IpAddress.AsLong() && other.GetPort() == m_Port); }
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 + "]"); */ } } } }); }
/*---------------------------------------------------------------------- | 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)); }