File: dnssddiscoverer.cpp

package info (click to toggle)
kio-extras 4%3A25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 31,928 kB
  • sloc: cpp: 28,852; ansic: 3,084; perl: 1,048; xml: 116; sh: 92; python: 28; makefile: 9
file content (104 lines) | stat: -rw-r--r-- 3,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
    SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
    SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
*/

#include "dnssddiscoverer.h"
#include "kio_smb.h"

DNSSDDiscovery::DNSSDDiscovery(KDNSSD::RemoteService::Ptr service)
    : m_service(service)
{
}

QString DNSSDDiscovery::udsName() const
{
    return m_service->serviceName();
}

KIO::UDSEntry DNSSDDiscovery::toEntry() const
{
    KIO::UDSEntry entry;
    entry.reserve(6);
    entry.fastInsert(KIO::UDSEntry::UDS_NAME, udsName());

    entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
    entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH));
    entry.fastInsert(KIO::UDSEntry::UDS_ICON_NAME, "network-server");

    // TODO: it may be better to resolve the host to an ip address. dnssd
    //   being able to find a service doesn't mean name resolution is
    //   properly set up for its domain. So, we may not be able to resolve
    //   this without help from avahi. OTOH KDNSSD doesn't have API for this
    //   and from a platform POV we should probably assume that if avahi
    //   is functional it is also set up as resolution provider.
    //   Given the plugin design on glibc's libnss however I am not sure
    //   that assumption will be true all the time. ~sitter, 2018
    QUrl u;
    u.setScheme(QStringLiteral("smb"));
    u.setHost(m_service->hostName());
    const int defaultPort = 445;
    if (m_service->port() > 0 && m_service->port() != defaultPort) {
        u.setPort(m_service->port());
    }
    u.setPath("/"); // https://bugs.kde.org/show_bug.cgi?id=388922

    entry.fastInsert(KIO::UDSEntry::UDS_URL, u.url());
    entry.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("application/x-smb-server"));
    return entry;
}

DNSSDDiscoverer::DNSSDDiscoverer()
{
    connect(&m_browser, &KDNSSD::ServiceBrowser::serviceAdded, this, [this](KDNSSD::RemoteService::Ptr service) {
        qCDebug(KIO_SMB_LOG) << "DNSSD added:" << service->serviceName() << service->type() << service->domain() << service->hostName() << service->port();
        // Manual contains check. We need to use the == of the underlying
        // objects, not the pointers. The same service may have >1
        // RemoteService* instances representing it, so the == impl of
        // RemoteService::Ptr is useless here.
        for (const auto &servicePtr : qAsConst(m_services)) {
            if (*service == *servicePtr) {
                return;
            }
        }

        connect(service.data(), &KDNSSD::RemoteService::resolved, this, [this, service] {
            ++m_resolvedCount;
            Q_EMIT newDiscovery(Discovery::Ptr(new DNSSDDiscovery(service)));
            maybeFinish();
        });

        // Schedule resolution of hostname. We'll later call resolve
        // which will block until the resolution is done. This basically
        // gives us a head start on discovery.
        service->resolveAsync();
        m_services.append(service);
    });
    connect(&m_browser, &KDNSSD::ServiceBrowser::finished, this, &DNSSDDiscoverer::stop);
}

void DNSSDDiscoverer::start()
{
    m_browser.startBrowse();
}

void DNSSDDiscoverer::stop()
{
    m_browser.disconnect();
    m_disconnected = true;
    maybeFinish();
}

bool DNSSDDiscoverer::isFinished() const
{
    return m_disconnected && m_services.count() == m_resolvedCount;
}

void DNSSDDiscoverer::maybeFinish()
{
    if (isFinished()) {
        Q_EMIT finished();
    }
}

#include "moc_dnssddiscoverer.cpp"