예제 #1
0
/*----------------------------------------------------------------------
|   PLT_UPnP::Start()
+---------------------------------------------------------------------*/
NPT_Result
PLT_UPnP::Start()
{
    NPT_LOG_INFO("Starting UPnP...");

    NPT_AutoLock lock(m_Lock);

    if (m_Started == true) NPT_CHECK_SEVERE(NPT_ERROR_INVALID_STATE);
    
    NPT_List<NPT_IpAddress> ips;
    PLT_UPnPMessageHelper::GetIPAddresses(ips);
    
    /* Create multicast socket and bind on 1900. If other apps didn't
       play nicely by setting the REUSE_ADDR flag, this could fail */
    NPT_UdpMulticastSocket* socket = new NPT_UdpMulticastSocket();
    NPT_CHECK_SEVERE(socket->Bind(NPT_SocketAddress(NPT_IpAddress::Any, 1900), true));
    
    /* Join multicast group for every ip we found */
    NPT_CHECK_SEVERE(ips.ApplyUntil(PLT_SsdpInitMulticastIterator(socket),
                                    NPT_UntilResultNotEquals(NPT_SUCCESS)));

    /* create the ssdp listener */
    m_SsdpListenTask = new PLT_SsdpListenTask(socket);
    NPT_CHECK_SEVERE(m_TaskManager.StartTask(m_SsdpListenTask));

    /* start devices & ctrlpoints */
    // TODO: Starting devices and ctrlpoints could fail?
    m_CtrlPoints.Apply(PLT_UPnP_CtrlPointStartIterator(m_SsdpListenTask));
    m_Devices.Apply(PLT_UPnP_DeviceStartIterator(m_SsdpListenTask));

    m_Started = true;
    return NPT_SUCCESS;
}
예제 #2
0
NPT_UdpMulticastSocket* Server::getNewMulticastSocket()
{
    NPT_NetworkInterface* serverInterface = gMS->serverInterface();

    auto addresses = serverInterface->GetAddresses();
    if (!addresses.GetFirstItem())
    {
        throw new std::runtime_error("No usable addresses found for UPnP multicast");
    }

    NPT_SocketAddress localAddress((*addresses.GetFirstItem()).GetPrimaryAddress(), 0);

    NPT_UdpMulticastSocket* ssdpSocket = new NPT_UdpMulticastSocket();
    ssdpSocket->Bind(localAddress, true);
    ssdpSocket->SetTimeToLive(32);
    return ssdpSocket;
}
예제 #3
0
/*----------------------------------------------------------------------
|       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;
}