Exemple #1
0
TBool MdnsPlatform::NifsMatch(const NetworkAdapter& aNif1, const NetworkAdapter& aNif2)
{
    if (aNif1.Address() == aNif2.Address() && aNif1.Subnet() == aNif2.Subnet() && strcmp(aNif1.Name(), aNif2.Name()) == 0) {
        return true;
    }
    return false;
}
Exemple #2
0
void MdnsPlatform::SubnetListChanged()
{
    iInterfacesLock.Wait();
    NetworkAdapter* current = iEnv.NetworkAdapterList().CurrentAdapter(kNifCookie);
    NetworkAdapterList& nifList = iEnv.NetworkAdapterList();
    std::vector<NetworkAdapter*>* subnetList = nifList.CreateSubnetList();
    for (TInt i=(TInt)iInterfaces.size()-1; i>=0; i--) {
        // Remove adapters with subnets no longer available and which don't
        // match the currently selected adapter (if there is one).
        if (InterfaceIndex(iInterfaces[i]->Adapter(), *subnetList) == -1
                || (current != NULL && current->Address() != iInterfaces[i]->Adapter().Address())) {
            mDNS_DeregisterInterface(iMdns, &iInterfaces[i]->Info(), false);
            delete iInterfaces[i];
            iInterfaces.erase(iInterfaces.begin()+i);
        }
    }
    if (current == NULL) { // no adapter selected; add new subnets
        for (TUint i=0; i<(TUint)subnetList->size(); i++) {
            NetworkAdapter* nif = (*subnetList)[i];
            if (InterfaceIndex(*nif) == -1) {
                AddInterface(nif);
            }
        }
    }
    if (current != NULL) {
        if (iInterfaces.size() == 0) { // current adapter is on a newly added subnet
            AddInterface(current);
            iClient.SetMulticastIf(current->Address());
        }
        current->RemoveRef(kNifCookie);
    }
    iInterfacesLock.Signal();
    nifList.DestroySubnetList(subnetList);
}
Exemple #3
0
void DviProtocolUpnp::GetResourceManagerUri(const NetworkAdapter& aAdapter, Brh& aUri)
{
    if (iDevice.ResourceManager() == NULL) {
        return;
    }
    for (TUint i=0; i<(TUint)iAdapters.size(); i++) {
        if (iAdapters[i]->Interface() == aAdapter.Address()) {
            WriterBwh writer(1024);
            writer.Write("http://");
            Bws<Endpoint::kMaxEndpointBytes> buf;
            Endpoint ep(iAdapters[i]->ServerPort(), aAdapter.Address());
            ep.AppendEndpoint(buf);
            writer.Write(buf);
            writer.Write('/');
            Uri::Escape(writer, iDevice.Udn());
            writer.Write('/');
            writer.Write(kProtocolName);
            writer.Write('/');
            writer.Write(iDevice.kResourceDir);
            writer.Write('/');
            writer.TransferTo(aUri);
            return;
        }
    }
}
Exemple #4
0
static void RandomiseUdn(Bwh& aUdn)
{
    aUdn.Grow(aUdn.Bytes() + 1 + Ascii::kMaxUintStringBytes + 1);
    aUdn.Append('-');
    Bws<Ascii::kMaxUintStringBytes> buf;
    NetworkAdapter* nif = Stack::NetworkAdapterList().CurrentAdapter(kAdapterCookie);
    TUint max = nif->Address();
    TUint seed = DviStack::ServerUpnp().Port(nif->Address());
    SetRandomSeed(seed);
    nif->RemoveRef(kAdapterCookie);
    (void)Ascii::AppendDec(buf, Random(max));
    aUdn.Append(buf);
    aUdn.PtrZ();
}
Exemple #5
0
DviProtocolUpnpAdapterSpecificData::DviProtocolUpnpAdapterSpecificData(DvStack& aDvStack, IUpnpMsearchHandler& aMsearchHandler, const NetworkAdapter& aAdapter, Bwx& aUriBase, TUint aServerPort)
    : iRefCount(1)
    , iDvStack(aDvStack)
    , iMsearchHandler(&aMsearchHandler)
    , iId(0x7fffffff)
    , iSubnet(aAdapter.Subnet())
    , iAdapter(aAdapter.Address())
    , iUriBase(aUriBase)
    , iServerPort(aServerPort)
    , iBonjourWebPage(0)
    , iDevice(NULL)
{
    iListener = &(iDvStack.Env().MulticastListenerClaim(aAdapter.Address()));
    iId = iListener->AddMsearchHandler(this);
}
Exemple #6
0
void MdnsPlatform::CurrentAdapterChanged()
{
    iInterfacesLock.Wait();
    NetworkAdapter* current = iEnv.NetworkAdapterList().CurrentAdapter(kNifCookie);
    NetworkAdapterList& nifList = iEnv.NetworkAdapterList();
    std::vector<NetworkAdapter*>* subnetList = nifList.CreateSubnetList();

    if (current != NULL) {
        // Remove existing interface(s) if not belonging to current adapter,
        // then add current adapter if it didn't exist before.
        for (TInt i=(TInt)iInterfaces.size()-1; i>=0; i--) {
            if (InterfaceIndex(iInterfaces[i]->Adapter(), *subnetList) == -1
                || (current != NULL && current->Address() != iInterfaces[i]->Adapter().Address())) {
                    mDNS_DeregisterInterface(iMdns, &iInterfaces[i]->Info(), false);
                    delete iInterfaces[i];
                    iInterfaces.erase(iInterfaces.begin()+i);
            }
        }
        if (iInterfaces.size() == 0) { // current adapter is on a newly added subnet
            AddInterface(current);
        }
        // Set multicast iface in case current adapter was previously in a list
        // subnets so no multicast iface was set.
        iClient.SetMulticastIf(current->Address());
        current->RemoveRef(kNifCookie);
    }
    else {
        // No adapter selected.
        // First, check if selected interface should be removed.
        for (TInt i=(TInt)iInterfaces.size()-1; i>=0; i--) {
            if (InterfaceIndex(iInterfaces[i]->Adapter(), *subnetList) == -1) {
                mDNS_DeregisterInterface(iMdns, &iInterfaces[i]->Info(), false);
                delete iInterfaces[i];
                iInterfaces.erase(iInterfaces.begin()+i);
            }
        }

        // Then, re-add all subnets that aren't already added.
        for (TUint i=0; i<(TUint)subnetList->size(); i++) {
            NetworkAdapter* nif = (*subnetList)[i];
            if (InterfaceIndex(*nif) == -1) {
                AddInterface(nif);
            }
        }
    }
    iInterfacesLock.Signal();
    nifList.DestroySubnetList(subnetList);
}
Exemple #7
0
TBool CpiDeviceListUpnp::IsLocationReachable(const Brx& aLocation) const
{
    /* linux's filtering of multicast messages appears to be buggy and messages
       received by all interfaces are sometimes delivered to sockets which are
       bound to / members of a group on a single (different) interface.  It'd be
       more correct to filter these out in SsdpListenerMulticast but that would
       require API changes which would be more inconvenient than just moving the
       filtering here.
       This should be reconsidered if we ever add more clients of SsdpListenerMulticast */
    TBool reachable = false;
    Uri uri;
    try {
        uri.Replace(aLocation);
    }
    catch (UriError&) {
        return false;
    }
    iLock.Wait();
    Endpoint endpt(0, uri.Host());
    NetworkAdapter* nif = iCpStack.Env().NetworkAdapterList().CurrentAdapter("CpiDeviceListUpnp::IsLocationReachable");
    if (nif != NULL) {
        if (nif->Address() == iInterface && nif->ContainsAddress(endpt.Address())) {
            reachable = true;
        }
        nif->RemoveRef("CpiDeviceListUpnp::IsLocationReachable");
    }
    iLock.Signal();
    return reachable;
}
Exemple #8
0
MdnsPlatform::Status MdnsPlatform::Init()
{
    LOG(kBonjour, "Bonjour             Init\n");
    LOG(kBonjour, "Bonjour             Init - Set FQDN\n");
    DoSetHostName();
    LOG(kBonjour, "Bonjour             Init - Register Interface\n");

    iInterfacesLock.Wait();
    Status status = mStatus_NoError;
    NetworkAdapterList& nifList = iEnv.NetworkAdapterList();
    Functor functorSubnet = MakeFunctor(*this, &MdnsPlatform::SubnetListChanged);
    iSubnetListChangeListenerId = nifList.AddSubnetListChangeListener(functorSubnet, "MdnsPlatform-subnet");
    Functor functorAdapter = MakeFunctor(*this, &MdnsPlatform::CurrentAdapterChanged);
    iCurrentAdapterChangeListenerId = nifList.AddCurrentChangeListener(functorAdapter, "MdnsPlatform-current");

    NetworkAdapter* current = iEnv.NetworkAdapterList().CurrentAdapter(kNifCookie);
    if (current == NULL) { // Listening on all adapters.
        std::vector<NetworkAdapter*>* subnetList = nifList.CreateSubnetList();
        for (TUint i=0; i<(TUint)subnetList->size() && status==mStatus_NoError; i++) {
            status = AddInterface((*subnetList)[i]);
        }
        nifList.DestroySubnetList(subnetList);
    }
    else { // Using a single adapter.
        status = AddInterface(current);
        iClient.SetMulticastIf(current->Address());
        current->RemoveRef(kNifCookie);
    }

    iInterfacesLock.Signal();
    if (status == mStatus_NoError) {
        mDNSCoreInitComplete(iMdns, status);
    }
    return status;
}
void NetworkAdapterList::TraceAdapter(const TChar* aPrefix, NetworkAdapter& aAdapter)
{ // static
    Endpoint ep(0, aAdapter.Address());
    Bws<Endpoint::kMaxAddressBytes> addr;
    ep.AppendAddress(addr);
    LOG(kTrace, "%s: %s(%s)\n", aPrefix, aAdapter.Name(), (const TChar*)addr.Ptr());
}
Exemple #10
0
void SuiteAlive::Test()
{
    Environment& env = iDvStack.Env();
    Blocker* blocker = new Blocker(env);
    CpListenerBasic* listener = new CpListenerBasic;
    NetworkAdapter* nif = env.NetworkAdapterList().CurrentAdapter(kAdapterCookie);
    SsdpListenerMulticast* listenerMulticast = new SsdpListenerMulticast(env, nif->Address());
    nif->RemoveRef(kAdapterCookie);
    TInt listenerId = listenerMulticast->AddNotifyHandler(listener);
    listenerMulticast->Start();
    DviDevice* device = new DviDeviceStandard(iDvStack, gNameDevice1);
    device->SetAttribute("Upnp.Domain", "a.b.c");
    device->SetAttribute("Upnp.Type", "type1");
    device->SetAttribute("Upnp.Version", "1");
    AddService(device, new DviService(iDvStack, "a.b.c", "service1", 1));
    AddService(device, new DviService(iDvStack, "a.b.c", "service2", 1));
    device->SetEnabled();
    blocker->Wait(1);
    /* we expect 5 messages but linux sometimes reports multicast messages from
      all subnets to listeners on any single subnet so just check that we've
      received a multiple of 5 messages */

    TEST(listener->TotalAlives() > 0);
    TEST(listener->TotalAlives() == listener->TotalByeByes());
    TEST(listener->TotalAlives() % 5 == 0);

    TUint byebyes = listener->TotalByeByes();
    Functor disabled = MakeFunctor(*this, &SuiteAlive::Disabled);
    device->SetDisabled(disabled);
    iSem.Wait();
    blocker->Wait(1); /* semaphore being signalled implies that the device has been
                         disabled.  We may require some extra time to receive the
                         multicast byebye confirming this */
    TEST(listener->TotalByeByes() > byebyes);
    TEST(listener->TotalByeByes() % 5 == 0);

    TUint alives = listener->TotalAlives();
    byebyes = listener->TotalByeByes();
    device->SetEnabled();
    blocker->Wait(1);
    TEST(listener->TotalAlives() > alives);
    TEST(listener->TotalAlives() - alives == listener->TotalByeByes() - byebyes);
    TEST(listener->TotalAlives() % 5 == 0);

    // Control point doesn't process ssdp:update notifications
    // check that updates are basically working by counting the extra alive messages instead
    alives = listener->TotalAlives();
    device->SetAttribute("Upnp.TestUpdate", "1");
    blocker->Wait(1);
    TEST(listener->TotalAlives() > alives);
    TEST(listener->TotalAlives() % 5 == 0);

    device->Destroy();
    listenerMulticast->RemoveNotifyHandler(listenerId);
    delete listenerMulticast;
    delete listener;
    delete blocker;
}
Exemple #11
0
void CpiDeviceListUpnp::HandleInterfaceChange(TBool aNewSubnet)
{
    NetworkAdapter* current = iCpStack.Env().NetworkAdapterList().CurrentAdapter("CpiDeviceListUpnp::HandleInterfaceChange");
    if (aNewSubnet && current != NULL && current->Address() == iInterface) {
        // list of subnets has changed but our interface is still available so there's nothing for us to do here
        current->RemoveRef("CpiDeviceListUpnp::HandleInterfaceChange");
        return;
    }
    iNextRefreshTimer->Cancel();
    iCpStack.Env().Mutex().Wait();
    iPendingRefreshCount = 0;
    iCpStack.Env().Mutex().Signal();
    StopListeners();

    if (current == NULL) {
        iLock.Wait();
        iInterface = 0;
        iLock.Signal();
        RemoveAll();
        return;
    }

    // we used to only remove devices for subnet changes
    // its not clear why this was correct - any interface change will result in control/event urls changing
    RemoveAll();
    
    iLock.Wait();
    iInterface = current->Address();
    iLock.Signal();
    TUint msearchTime = iCpStack.Env().InitParams().MsearchTimeSecs();
    Mutex& lock = iCpStack.Env().Mutex();
    lock.Wait();
    iPendingRefreshCount = (kMaxMsearchRetryForNewAdapterSecs + msearchTime - 1) / (2 * msearchTime);
    lock.Signal();
    current->RemoveRef("CpiDeviceListUpnp::HandleInterfaceChange");

    iSsdpLock.Wait();
    iUnicastListener = new SsdpListenerUnicast(iCpStack.Env(), *this, iInterface);
    iUnicastListener->Start();
    iMulticastListener = &(iCpStack.Env().MulticastListenerClaim(iInterface));
    iNotifyHandlerId = iMulticastListener->AddNotifyHandler(this);
    iSsdpLock.Signal();
    Refresh();
}
Exemple #12
0
void CpiSubscription::DoSubscribe()
{
    CpStack& cpStack = iDevice.GetCpStack();
    Bws<Uri::kMaxUriBytes> uri;
    uri.Append(Http::kSchemeHttp);
    NetworkAdapter* nif = cpStack.Env().NetworkAdapterList().CurrentAdapter("CpiSubscription::DoSubscribe");
    if (nif == NULL) {
        THROW(NetworkError);
    }
    TIpAddress nifAddr = nif->Address();
    nif->RemoveRef("CpiSubscription::DoSubscribe");
    Endpoint endpt(cpStack.SubscriptionManager().EventServerPort(), nifAddr);
    Endpoint::EndpointBuf buf;
    endpt.AppendEndpoint(buf);
    uri.Append(buf);
    uri.Append('/');
    Uri subscriber(uri);

    LOG(kEvent, "Subscribing - service = ");
    LOG(kEvent, iServiceType.FullName());
    LOG(kEvent, "\n    subscriber = ");
    LOG(kEvent, subscriber.AbsoluteUri());
    LOG(kEvent, "\n");

    iNextSequenceNumber = 0;
    TUint renewSecs;
    try {
        renewSecs = iDevice.Subscribe(*this, subscriber);
    }
    catch (XmlError&) {
        // we don't expect to ever get here.  Its worth capturing some debug info if we do.
        Brh deviceXml;
        if (!iDevice.GetAttribute("Upnp.DeviceXml", deviceXml)) {
            deviceXml.Set("[missing]");
        }
        const Brx& udn = iDevice.Udn();
        cpStack.Env().Mutex().Wait();
        Log::Print("XmlError attempting to subscribe to device ");
        Log::Print(udn);
        Log::Print(", with xml\n\n");
        Log::Print(deviceXml);
        Log::Print("\n\n");
        cpStack.Env().Mutex().Signal();
        THROW(XmlError);
    }

    cpStack.SubscriptionManager().Add(*this);

    LOG(kEvent, "Subscription for ");
    LOG(kEvent, iServiceType.FullName());
    LOG(kEvent, " completed\n    Sid is ");
    LOG(kEvent, iSid);
    LOG(kEvent, "\n    Renew in %u secs\n", renewSecs);

    SetRenewTimer(renewSecs);
}
Exemple #13
0
DviProtocolUpnp::DviProtocolUpnp(DviDevice& aDevice)
    : iDevice(aDevice)
    , iDvStack(aDevice.GetDvStack())
    , iLock("DMUP")
    , iUpdateCount(0)
    , iSuppressScheduledEvents(false)
{
    SetAttribute(kAttributeKeyVersionMajor, "1");
    SetAttribute(kAttributeKeyVersionMinor, "1");
    iLock.Wait();
    iServer = &(iDvStack.ServerUpnp());
    NetworkAdapterList& adapterList = iDvStack.Env().NetworkAdapterList();
    Functor functor = MakeFunctor(*this, &DviProtocolUpnp::HandleInterfaceChange);
    iCurrentAdapterChangeListenerId = adapterList.AddCurrentChangeListener(functor, "DviProtocolUpnp-current");
    iSubnetListChangeListenerId = adapterList.AddSubnetListChangeListener(functor, "DviProtocolUpnp-subnet");
    std::vector<NetworkAdapter*>* subnetList = adapterList.CreateSubnetList();
    AutoNetworkAdapterRef ref(iDvStack.Env(), "DviProtocolUpnp ctor");
    const NetworkAdapter* current = ref.Adapter();
    const TIpAddress kLoopbackAddr = MakeIpAddress(127, 0, 0, 1);
    for (TUint i=0; i<subnetList->size(); i++) {
        NetworkAdapter* subnet = (*subnetList)[i];
        if (current != NULL && subnet->Address() != current->Address() && subnet->Address() != kLoopbackAddr) {
            continue;
        }
        try {
            AddInterface(*subnet);
        }
        catch (NetworkError&) {
            // some hosts may have adapters that don't support multicast
            // we can't differentiate between no support ever and transient failure
            // (typical on Windows & Mac after hibernation) so just ignore this exception
            // and trust that we'll get advertised on another interface.
            char* adapterName = subnet->FullName();
            LOG2(kTrace, kError, "DvDevice unable to use adapter %s\n", adapterName);
            delete adapterName;
        }
    }
    NetworkAdapterList::DestroySubnetList(subnetList);
    iAliveTimer = new Timer(iDvStack.Env(), MakeFunctor(*this, &DviProtocolUpnp::SendAliveNotifications), "DviProtocolUpnp");
    iLock.Signal();
}
Exemple #14
0
SuiteMsearch::SuiteMsearch()
    : Suite("Msearches")
{
    RandomiseUdn(gNameDevice1);
    RandomiseUdn(gNameDevice2);
    RandomiseUdn(gNameDevice2Embedded1);
    iBlocker = new Blocker;
    iListener = new CpListenerMsearch;
    NetworkAdapter* nif = Stack::NetworkAdapterList().CurrentAdapter(kAdapterCookie);
    iListenerUnicast = new SsdpListenerUnicast(*iListener, nif->Address());
    nif->RemoveRef(kAdapterCookie);
    iListenerUnicast->Start();
}
Exemple #15
0
void DviServer::SubnetListChanged()
{
    /* DviProtocolUpnp relies on servers being available on all appropriate interfaces.
       We assume this happens through DviServer being created before any devices
       so registering for subnet change notification earlier.  Assuming NetworkAdapterList
       always runs its listeners in the order they registered, we'll have updated before
       any device listeners are run. */

    AutoMutex a(iLock);
    NetworkAdapterList& adapterList = iDvStack.Env().NetworkAdapterList();
    AutoNetworkAdapterRef ref(iDvStack.Env(), "DviServer::SubnetListChanged");
    NetworkAdapter* current = ref.Adapter();
    if (adapterList.SingleSubnetModeEnabled()) {
        TInt i;
        // remove servers whose interface is no longer available
        for (i = (TInt)iServers.size() - 1; i >= 0; i--) {
            DviServer::Server* server = iServers[i];
            if (current == NULL || server->Interface() != current->Address()) {
                delete server;
                iServers.erase(iServers.begin() + i);
            }
        }
        // add server if 'current' is a new subnet
        if (current != NULL && iServers.size() == 0) {
            AddServer(*current);
        }
    }
    else {
        std::vector<NetworkAdapter*>* subnetList = adapterList.CreateSubnetList();
        std::vector<NetworkAdapter*>* nifList = adapterList.CreateNetworkAdapterList();
        TInt i;
        // remove servers whose interface is no longer available
        for (i = (TInt)iServers.size() - 1; i >= 0; i--) {
            DviServer::Server* server = iServers[i];
            if (FindInterface(server->Interface(), *nifList) == -1) {
                delete server;
                iServers.erase(iServers.begin() + i);
            }
        }
        // add servers for new subnets
        for (i = 0; i < (TInt)subnetList->size(); i++) {
            NetworkAdapter* subnet = (*subnetList)[i];
            if (FindServer(subnet->Subnet()) == -1) {
                AddServer(*subnet);
            }
        }
        NetworkAdapterList::DestroyNetworkAdapterList(nifList);
        NetworkAdapterList::DestroySubnetList(subnetList);
    }
}
Exemple #16
0
void CpiDeviceListUpnp::HandleInterfaceChange()
{
    NetworkAdapter* current = iCpStack.Env().NetworkAdapterList().CurrentAdapter("CpiDeviceListUpnp::HandleInterfaceChange");
    if (current != NULL && current->Address() == iInterface) {
        // list of subnets has changed but our interface is still available so there's nothing for us to do here
        current->RemoveRef("CpiDeviceListUpnp::HandleInterfaceChange");
        return;
    }
    StopListeners();

    if (current == NULL) {
        iLock.Wait();
        iInterface = 0;
        iLock.Signal();
        RemoveAll();
        return;
    }

    // we used to only remove devices for subnet changes
    // its not clear why this was correct - any interface change will result in control/event urls changing
    RemoveAll();
    
    iLock.Wait();
    iInterface = current->Address();
    iLock.Signal();
    current->RemoveRef("CpiDeviceListUpnp::HandleInterfaceChange");

    {
        AutoMutex a(iSsdpLock);
        iUnicastListener = new SsdpListenerUnicast(iCpStack.Env(), *this, iInterface);
        iUnicastListener->Start();
        iMulticastListener = &(iCpStack.Env().MulticastListenerClaim(iInterface));
        iNotifyHandlerId = iMulticastListener->AddNotifyHandler(this);
    }
    Refresh();
}
Exemple #17
0
SuiteMsearch::SuiteMsearch(DvStack& aDvStack)
    : Suite("Msearches")
    , iDvStack(aDvStack)
{
    RandomiseUdn(iDvStack.Env(), gNameDevice1);
    RandomiseUdn(iDvStack.Env(), gNameDevice2);
    RandomiseUdn(iDvStack.Env(), gNameDevice2Embedded1);
    Print("UDNs: \n    ");
    Print(gNameDevice1);
    Print("\n    ");
    Print(gNameDevice2);
    Print("\n    ");
    Print(gNameDevice2Embedded1);
    Print("\n\n");
    Environment& env = iDvStack.Env();
    iBlocker = new Blocker(env);
    iListener = new CpListenerMsearch(env);
    NetworkAdapter* nif = env.NetworkAdapterList().CurrentAdapter(kAdapterCookie);
    iListenerUnicast = new SsdpListenerUnicast(env, *iListener, nif->Address());
    nif->RemoveRef(kAdapterCookie);
    iListenerUnicast->Start();
}
Exemple #18
0
TIpAddress STDCALL OhNetNetworkAdapterAddress(OhNetHandleNetworkAdapter aNif)
{
    NetworkAdapter* nif = reinterpret_cast<NetworkAdapter*>(aNif);
    ASSERT(nif != NULL);
    return nif->Address();
}