添加广播MDNS服务

This commit is contained in:
2024-08-19 09:39:32 +08:00
parent 1f7bc017ca
commit 0a5b0db9a5
192 changed files with 22181 additions and 616 deletions

View File

@@ -0,0 +1,88 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_ABSTRACTSERVER_H
#define QMDNSENGINE_ABSTRACTSERVER_H
#include <QObject>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class Message;
/**
* @brief Base class for sending and receiving DNS messages
*
* Many of the other classes in this library require the ability to send and
* receive DNS messages. By having them use this base class, they become far
* easier to test. Any class derived from this one that implements the pure
* virtual methods can be used for sending and receiving DNS messages.
*/
class QMDNSENGINE_EXPORT AbstractServer : public QObject
{
Q_OBJECT
public:
/**
* @brief Abstract constructor
*/
explicit AbstractServer(QObject *parent = 0);
/**
* @brief Send a message to its provided destination
*
* The message should be sent over the IP protocol specified in the
* message and to the target address and port specified in the message.
*/
virtual void sendMessage(const Message &message) = 0;
/**
* @brief Send a message to the multicast address on each interface
*
* The message should be sent over both IPv4 and IPv6 on all interfaces.
*/
virtual void sendMessageToAll(const Message &message) = 0;
Q_SIGNALS:
/**
* @brief Indicate that a DNS message was received
* @param message newly received message
*/
void messageReceived(const Message &message);
/**
* @brief Indicate that an error has occurred
* @param message brief description of the error
*/
void error(const QString &message);
};
}
#endif // QMDNSENGINE_ABSTRACTSERVER_H

View File

@@ -0,0 +1,101 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_BITMAP_H
#define QMDNSENGINE_BITMAP_H
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class QMDNSENGINE_EXPORT BitmapPrivate;
/**
* @brief 256-bit bitmap
*
* Bitmaps are used in QMdnsEngine::NSEC records to indicate which records are
* available. Bitmaps in mDNS records use only the first block (block 0).
*/
class QMDNSENGINE_EXPORT Bitmap
{
public:
/**
* @brief Create an empty bitmap
*/
Bitmap();
/**
* @brief Create a copy of an existing bitmap
*/
Bitmap(const Bitmap &other);
/**
* @brief Assignment operator
*/
Bitmap &operator=(const Bitmap &other);
/**
* @brief Equality operator
*/
bool operator==(const Bitmap &other);
/**
* @brief Destroy the bitmap
*/
virtual ~Bitmap();
/**
* @brief Retrieve the length of the block in bytes
*
* This method indicates how many bytes are pointed to by the data()
* method.
*/
quint8 length() const;
/**
* @brief Retrieve a pointer to the underlying data in the bitmap
*
* Use the length() method to determine how many bytes contain valid data.
*/
const quint8 *data() const;
/**
* @brief Set the data to be stored in the bitmap
*
* The length parameter indicates how many bytes of data are valid. The
* actual bytes are copied to the bitmap.
*/
void setData(quint8 length, const quint8 *data);
private:
BitmapPrivate *const d;
};
}
#endif // QMDNSENGINE_BITMAP_H

View File

@@ -0,0 +1,122 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_BROWSER_H
#define QMDNSENGINE_BROWSER_H
#include <QByteArray>
#include <QObject>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class AbstractServer;
class Cache;
class Service;
class QMDNSENGINE_EXPORT BrowserPrivate;
/**
* @brief %Browser for local services
*
* This class provides a simple way to discover services on the local network.
* A cache may be provided in the constructor to store records for future
* queries.
*
* To browse for services of any type:
*
* @code
* QMdnsEngine::Browser browser(&server, QMdnsEngine::MdnsBrowseType);
* @endcode
*
* To browse for services of a specific type:
*
* @code
* QMdnsEngine::Browser browser(&server, "_http._tcp.local.");
* @endcode
*
* When a service is found, the serviceAdded() signal is emitted:
*
* @code
* connect(&browser, &QMdnsEngine::Browser::serviceAdded, [](const QMdnsEngine::Service &service) {
* qDebug() << "Service added:" << service.name();
* });
* @endcode
*
* The serviceUpdated() and serviceRemoved() signals are emitted when services
* are updated (their properties change) or are removed, respectively.
*/
class QMDNSENGINE_EXPORT Browser : public QObject
{
Q_OBJECT
public:
/**
* @brief Create a new browser instance
* @param server server to use for receiving and sending mDNS messages
* @param type service type to browse for
* @param cache DNS cache to use or null to create one
* @param parent QObject
*/
Browser(AbstractServer *server, const QByteArray &type, Cache *cache = 0, QObject *parent = 0);
Q_SIGNALS:
/**
* @brief Indicate that a new service has been added
*
* This signal is emitted when the PTR and SRV records for a service are
* received. If TXT records are received later, the serviceUpdated()
* signal will be emitted.
*/
void serviceAdded(const Service &service);
/**
* @brief Indicate that the specified service was updated
*
* This signal is emitted when the SRV record for a service (identified by
* its name and type) or a TXT record has changed.
*/
void serviceUpdated(const Service &service);
/**
* @brief Indicate that the specified service was removed
*
* This signal is emitted when an essential record (PTR or SRV) is
* expiring from the cache. This will also occur when an updated PTR or
* SRV record is received with a TTL of 0.
*/
void serviceRemoved(const Service &service);
private:
BrowserPrivate *const d;
};
}
#endif // QMDNSENGINE_BROWSER_H

View File

@@ -0,0 +1,132 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_CACHE_H
#define QMDNSENGINE_CACHE_H
#include <QList>
#include <QObject>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class Record;
class QMDNSENGINE_EXPORT CachePrivate;
/**
* @brief %Cache for DNS records
*
* Records are added to the cache using the addRecord() method which are then
* stored in the cache until they are considered to have expired, at which
* point they are purged. The shouldQuery() signal is used to indicate when a
* record is approaching expiry and the recordExpired() signal indicates when
* a record has expired (at which point it is removed).
*
* The cache can be queried to retrieve one or more records matching a given
* type. For example, to retrieve all TXT records that match a given name:
*
* @code
* Cache cache;
*
* QList<QMdnsEngine::Record> records;
* cache.lookupRecords("My Service._http._tcp.local.", QMdnsEngine::TXT, records);
*
* for (const QMdnsEngine::Record &record : records) {
* qDebug() << "Record:" << record.name();
* }
* @endcode
*
* Alternatively, lookupRecord() can be used to find a single record.
*/
class QMDNSENGINE_EXPORT Cache : public QObject
{
Q_OBJECT
public:
/**
* @brief Create an empty cache.
*/
explicit Cache(QObject *parent = 0);
/**
* @brief Add a record to the cache
* @param record add this record to the cache
*
* The TTL for the record will be added to the current time to calculate
* when the record expires. Existing records of the same name and type
* will be replaced, resetting their expiration.
*/
void addRecord(const Record &record);
/**
* @brief Retrieve a single record from the cache
* @param name name of record to retrieve or null for any
* @param type type of record to retrieve or ANY for all types
* @param record storage for the record retrieved
* @return true if a record was retrieved
*
* Some record types allow multiple records to be stored with identical
* names and types. This method will only retrieve the first matching
* record. Use lookupRecords() to obtain all of the records.
*/
bool lookupRecord(const QByteArray &name, quint16 type, Record &record) const;
/**
* @brief Retrieve multiple records from the cache
* @param name name of records to retrieve or null for any
* @param type type of records to retrieve or ANY for all types
* @param records storage for the records retrieved
* @return true if records were retrieved
*/
bool lookupRecords(const QByteArray &name, quint16 type, QList<Record> &records) const;
Q_SIGNALS:
/**
* @brief Indicate that a record will expire soon and a new query should be issued
* @param record reference to the record that will soon expire
*
* This signal is emitted when a record reaches approximately 50%, 85%,
* 90%, and 95% of its lifetime.
*/
void shouldQuery(const Record &record);
/**
* @brief Indicate that the specified record expired
* @param record reference to the record that has expired
*/
void recordExpired(const Record &record);
private:
CachePrivate *const d;
};
}
#endif // QMDNSENGINE_CACHE_H

View File

@@ -0,0 +1,123 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_DNS_H
#define QMDNSENGINE_DNS_H
#include <QByteArray>
#include <QMap>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class Message;
class Record;
enum {
/// IPv4 address record
A = 1,
/// IPv6 address record
AAAA = 28,
/// Wildcard for cache lookups
ANY = 255,
/// List of records
NSEC = 47,
/// Pointer to hostname
PTR = 12,
/// %Service information
SRV = 33,
/// Arbitrary metadata
TXT = 16
};
/**
* @brief Parse a name from a raw DNS packet
* @param packet raw DNS packet data
* @param offset offset into the packet where the name begins
* @param name reference to QByteArray to store the name in
* @return true if no errors occurred
*
* The offset will be incremented by the number of bytes read. Name
* compression requires access to the contents of the packet.
*/
QMDNSENGINE_EXPORT bool parseName(const QByteArray &packet, quint16 &offset, QByteArray &name);
/**
* @brief Write a name to a raw DNS packet
* @param packet raw DNS packet to write to
* @param offset offset to update with the number of bytes written
* @param name name to write to the packet
* @param nameMap map of names already written to their offsets
*
* The offset will be incremented by the number of bytes read. The name map
* will be updated with offsets of any names written so that it can be passed
* to future invocations of this function.
*/
QMDNSENGINE_EXPORT void writeName(QByteArray &packet, quint16 &offset, const QByteArray &name, QMap<QByteArray, quint16> &nameMap);
/**
* @brief Parse a record from a raw DNS packet
* @param packet raw DNS packet data
* @param offset offset into the packet where the record begins
* @param record reference to Record to populate
* @return true if no errors occurred
*/
QMDNSENGINE_EXPORT bool parseRecord(const QByteArray &packet, quint16 &offset, Record &record);
/**
* @brief Write a record to a raw DNS packet
* @param packet raw DNS packet to write to
* @param offset offset to update with the number of bytes written
* @param record record to write to the packet
* @param nameMap map of names already written to their offsets
*/
QMDNSENGINE_EXPORT void writeRecord(QByteArray &packet, quint16 &offset, Record &record, QMap<QByteArray, quint16> &nameMap);
/**
* @brief Populate a Message with data from a raw DNS packet
* @param packet raw DNS packet data
* @param message reference to Message to populate
* @return true if no errors occurred
*/
QMDNSENGINE_EXPORT bool fromPacket(const QByteArray &packet, Message &message);
/**
* @brief Create a raw DNS packet from a Message
* @param message Message to create the packet from
* @param packet storage for raw DNS packet
*/
QMDNSENGINE_EXPORT void toPacket(const Message &message, QByteArray &packet);
/**
* @brief Retrieve the string representation of a DNS type
* @param type integer type
* @return human-readable name for the type
*/
QMDNSENGINE_EXPORT QString typeName(quint16 type);
}
#endif // QMDNSENGINE_DNS_H

View File

@@ -0,0 +1,95 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_HOSTNAME_H
#define QMDNSENGINE_HOSTNAME_H
#include <QObject>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class AbstractServer;
class QMDNSENGINE_EXPORT HostnamePrivate;
/**
* @brief %Hostname reserved for exclusive use
*
* In order to provide services on the local network, a unique hostname must
* be used. This class asserts a hostname (by first confirming that it is not
* in use) and then responds to A and AAAA queries for the hostname.
*
* @code
* QMdnsEngine::Hostname hostname(&server);
*
* connect(&hostname, &QMdnsEngine::Hostname::hostnameChanged, [](const QByteArray &hostname) {
* qDebug() << "New hostname:" << hostname;
* });
* @endcode
*/
class QMDNSENGINE_EXPORT Hostname : public QObject
{
Q_OBJECT
public:
/**
* @brief Create a new hostname
*/
Hostname(AbstractServer *server, QObject *parent = 0);
/**
* @brief Determine if a hostname has been registered
*
* A hostname is not considered registered until a probe for the desired
* name has been completed and no matching records were received.
*/
bool isRegistered() const;
/**
* @brief Retrieve the current hostname
*
* This value is only valid when isRegistered() returns true.
*/
QByteArray hostname() const;
Q_SIGNALS:
/**
* @brief Indicate that the current hostname has changed
* @param hostname new hostname
*/
void hostnameChanged(const QByteArray &hostname);
private:
HostnamePrivate *const d;
};
}
#endif // QMDNSENGINE_HOSTNAME_H

View File

@@ -0,0 +1,58 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_MDNS_H
#define QMDNSENGINE_MDNS_H
#include <QByteArray>
#include <QHostAddress>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
/**
* @brief Standard port for mDNS
*/
QMDNSENGINE_EXPORT extern const quint16 MdnsPort;
/**
* @brief Standard IPv4 address for mDNS
*/
QMDNSENGINE_EXPORT extern const QHostAddress MdnsIpv4Address;
/**
* @brief Standard IPv6 address for mDNS
*/
QMDNSENGINE_EXPORT extern const QHostAddress MdnsIpv6Address;
/**
* @brief Service type for browsing service types
*/
QMDNSENGINE_EXPORT extern const QByteArray MdnsBrowseType;
}
#endif // QMDNSENGINE_MDNS_H

View File

@@ -0,0 +1,191 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_MESSAGE_H
#define QMDNSENGINE_MESSAGE_H
#include <QHostAddress>
#include <QList>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class Query;
class Record;
class QMDNSENGINE_EXPORT MessagePrivate;
/**
* @brief DNS message
*
* A DNS message consists of a header and zero or more queries and records.
* Instances of this class are created and initialized by
* [AbstractServer](@ref QMdnsEngine::AbstractServer) when messages are
* received from the network.
*
* If a message is being constructed in reply to one received from the
* network, the reply() method can be used to simplify initialization:
*
* @code
* connect(&server, &QMdnsEngine::Server::messageReceived, [](const QMdnsEngine::Message &message) {
* QMdnsEngine::Message reply;
* reply.reply(message);
* server.sendMessage(reply);
* });
* @endcode
*/
class QMDNSENGINE_EXPORT Message
{
public:
/**
* @brief Create an empty message
*/
Message();
/**
* @brief Create a copy of an existing message
*/
Message(const Message &other);
/**
* @brief Assignment operator
*/
Message &operator=(const Message &other);
/**
* @brief Destroy the message
*/
virtual ~Message();
/**
* @brief Retrieve the address for the message
*
* When receiving messages, this is the address that the message was
* received from.
*/
QHostAddress address() const;
/**
* @brief Set the address for the message
*
* When sending messages, this is the address that the message will be
* sent to. QMdnsEngine::MdnsIpv4Address and QMdnsEngine::MdnsIpv6Address
* can be used for mDNS messages.
*/
void setAddress(const QHostAddress &address);
/**
* @brief Retrieve the port for the message
*
* When receiving messages, this is the port that the message was received
* from. For traditional queries, this will be an ephemeral port. For mDNS
* queries, this will always equal QMdnsEngine::MdnsPort.
*/
quint16 port() const;
/**
* @brief Set the port for the message
*
* When sending messages, this is the port that the message will be sent
* to. This should be set to QMdnsEngine::MdnsPort unless the message is a
* reply to a traditional DNS query.
*/
void setPort(quint16 port);
/**
* @brief Retrieve the transaction ID for the message
*
* This is always set to 1 for mDNS messages. Traditional DNS queries may
* set this to an arbitrary integer.
*/
quint16 transactionId() const;
/**
* @brief Set the transaction ID for the message
*
* The default transaction ID is 0. This value should not be changed
* unless responding to a traditional DNS query.
*/
void setTransactionId(quint16 transactionId);
/**
* @brief Determine if the message is a response
*/
bool isResponse() const;
/**
* @brief Set whether the message is a response
*/
void setResponse(bool isResponse);
/**
* @brief Determine if the message is truncated
*/
bool isTruncated() const;
/**
* @brief Set whether the message is truncated
*/
void setTruncated(bool isTruncated);
/**
* @brief Retrieve a list of queries in the message
*/
QList<Query> queries() const;
/**
* @brief Add a query to the message
*/
void addQuery(const Query &query);
/**
* @brief Retrieve a list of records in the message
*/
QList<Record> records() const;
/**
* @brief Add a record to the message
*/
void addRecord(const Record &record);
/**
* @brief Reply to another message
*
* The message will be correctly initialized to respond to the other
* message. This includes setting the target address, port, and
* transaction ID.
*/
void reply(const Message &other);
private:
MessagePrivate *const d;
};
}
#endif // QMDNSENGINE_MESSAGE_H

View File

@@ -0,0 +1,88 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_PROBER_H
#define QMDNSENGINE_PROBER_H
#include <QObject>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class AbstractServer;
class Record;
class QMDNSENGINE_EXPORT ProberPrivate;
/**
* @brief %Prober to confirm that a record is unique
*
* Before responding to queries for a record, its uniqueness on the network
* must be confirmed. This class takes care of probing for existing records
* that match and adjusts the record's name until a unique one is found.
*
* For example, to probe for a SRV record:
*
* @code
* QMdnsEngine::Record record;
* record.setName("My Service._http._tcp.local.");
* record.setType(QMdnsEngine::SRV);
* record.setPort(1234);
* record.setTarget(hostname.hostname());
*
* QMdnsEngine::Prober prober(&server, record);
* connect(&prober, &QMdnsEngine::Prober::nameConfirmed, [](const QByteArray &name) {
* qDebug() << "Name confirmed:" << name;
* });
* @endcode
*/
class QMDNSENGINE_EXPORT Prober : public QObject
{
Q_OBJECT
public:
/**
* @brief Create a new prober
*/
Prober(AbstractServer *server, const Record &record, QObject *parent = 0);
Q_SIGNALS:
/**
* @brief Indicate that the name has been confirmed unique
* @param name that was confirmed to be unique
*/
void nameConfirmed(const QByteArray &name);
private:
ProberPrivate *const d;
};
}
#endif // QMDNSENGINE_PROBER_H

View File

@@ -0,0 +1,90 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_PROVIDER_H
#define QMDNSENGINE_PROVIDER_H
#include <QObject>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class AbstractServer;
class Hostname;
class Service;
class QMDNSENGINE_EXPORT ProviderPrivate;
/**
* @brief %Provider for a single mDNS service
*
* This class provide a [Service](@ref QMdnsEngine::Service) on the local
* network by responding to the appropriate DNS queries. A hostname is
* required for creating the SRV record.
*
* The provider needs to be given a reference to the service through the
* update() method so that it can construct DNS records:
*
* @code
* QMdnsEngine::Service service;
* service.setType("_http._tcp.local.");
* service.setName("My Service");
* service.setPort(1234);
*
* QMdnsEngine::Provider provider;
* provider.update(service);
* @endcode
*
* This method can also be used to update the provider's records.
*/
class QMDNSENGINE_EXPORT Provider : public QObject
{
Q_OBJECT
public:
/**
* @brief Create a new service provider
*/
Provider(AbstractServer *server, Hostname *hostname, QObject *parent = 0);
/**
* @brief Update the service with the provided information
* @param service updated service description
*
* This class will not respond to any DNS queries until the hostname is
* confirmed and this method is called.
*/
void update(const Service &service);
private:
ProviderPrivate *const d;
};
}
#endif // QMDNSENGINE_PROVIDER_H

View File

@@ -0,0 +1,40 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_EXPORT_H
#define QMDNSENGINE_EXPORT_H
#include <QtCore/qglobal.h>
#if defined(BUILD_SHARED_LIBS)
# if defined(QMDNSENGINE_LIBRARY)
# define QMDNSENGINE_EXPORT Q_DECL_EXPORT
# else
# define QMDNSENGINE_EXPORT Q_DECL_IMPORT
# endif
#else
# define QMDNSENGINE_EXPORT
#endif
#endif // QMDNSENGINE_EXPORT_H

View File

@@ -0,0 +1,116 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_QUERY_H
#define QMDNSENGINE_QUERY_H
#include <QByteArray>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class QMDNSENGINE_EXPORT QueryPrivate;
/**
* @brief DNS query
*
* This class represents a query for a DNS record. For example, to query for
* the IPv4 address of a local host:
*
* @code
* QMdnsEngine::Query query;
* query.setName("myserver.local.");
* query.setType(QMdnsEngine::A);
*
* message.addQuery(query);
* @endcode
*/
class QMDNSENGINE_EXPORT Query
{
public:
/**
* @brief Create an empty query
*/
Query();
/**
* @brief Create a copy of an existing query
*/
Query(const Query &other);
/**
* @brief Assignment operator
*/
Query &operator=(const Query &other);
/**
* @brief Destroy the query
*/
virtual ~Query();
/**
* @brief Retrieve the name being queried
*/
QByteArray name() const;
/**
* @brief Set the name to query
*/
void setName(const QByteArray &name);
/**
* @brief Retrieve the type of record being queried
*/
quint16 type() const;
/**
* @brief Set the type of record to query
*
* Constants, such as QMdnsEngine::SRV are provided for convenience.
*/
void setType(quint16 type);
/**
* @brief Determine if a unicast response is desired
*/
bool unicastResponse() const;
/**
* @brief Set whether a unicast response is desired
*/
void setUnicastResponse(bool unicastResponse);
private:
QueryPrivate *const d;
};
QMDNSENGINE_EXPORT QDebug operator<<(QDebug dbg, const Query &query);
}
#endif // QMDNSENGINE_QUERY_H

View File

@@ -0,0 +1,255 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_RECORD_H
#define QMDNSENGINE_RECORD_H
#include <QByteArray>
#include <QHostAddress>
#include <QMap>
#include <qmdnsengine/bitmap.h>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class QMDNSENGINE_EXPORT RecordPrivate;
/**
* @brief DNS record
*
* This class maintains information for an individual record. Not all record
* types use every field.
*
* For example, to create a TXT record:
*
* @code
* QMdnsEngine::Record record;
* record.setName("My Service._http._tcp.local.");
* record.setType(QMdnsEngine::TXT);
* record.addAttribute("a", "value1");
* record.addAttribute("b", "value2");
*
* message.addRecord(record);
* @endcode
*/
class QMDNSENGINE_EXPORT Record
{
public:
/**
* @brief Create an uninitialized record
*/
Record();
/**
* @brief Create a copy of an existing record
*/
Record(const Record &other);
/**
* @brief Assignment operator
*/
Record &operator=(const Record &other);
/**
* @brief Equality operator
*/
bool operator==(const Record &other) const;
/**
* @brief Inequality operator
*/
bool operator!=(const Record &other) const;
/**
* @brief Destroy the record
*/
virtual ~Record();
/**
* @brief Retrieve the name of the record
*/
QByteArray name() const;
/**
* @brief Set the name of the record
*/
void setName(const QByteArray &name);
/**
* @brief Retrieve the type of the record
*/
quint16 type() const;
/**
* @brief Set the type of the record
*
* For convenience, constants for types used by mDNS, such as
* QMdnsEngine::A or QMdnsEngine::PTR, may be used here.
*/
void setType(quint16 type);
/**
* @brief Determine whether to replace or append to existing records
*
* If true, this record replaces all previous records of the same name and
* type rather than appending to them.
*/
bool flushCache() const;
/**
* @brief Set whether to replace or append to existing records
*/
void setFlushCache(bool flushCache);
/**
* @brief Retrieve the TTL (time to live) for the record
*/
quint32 ttl() const;
/**
* @brief Set the TTL (time to live) for the record
*/
void setTtl(quint32 ttl);
/**
* @brief Retrieve the address for the record
*
* This field is used by QMdnsEngine::A and QMdnsEngine::AAAA records.
*/
QHostAddress address() const;
/**
* @brief Set the address for the record
*/
void setAddress(const QHostAddress &address);
/**
* @brief Retrieve the target for the record
*
* This field is used by QMdnsEngine::PTR and QMdnsEngine::SRV records.
*/
QByteArray target() const;
/**
* @brief Set the target for the record
*/
void setTarget(const QByteArray &target);
/**
* @brief Retrieve the next domain name
*
* This field is used by QMdnsEngine::NSEC records.
*/
QByteArray nextDomainName() const;
/**
* @brief Set the next domain name
*/
void setNextDomainName(const QByteArray &nextDomainName);
/**
* @brief Retrieve the priority for the record
*
* This field is used by QMdnsEngine::SRV records.
*/
quint16 priority() const;
/**
* @brief Set the priority for the record
*
* Unless more than one QMdnsEngine::SRV record is being sent, this field
* should be set to 0.
*/
void setPriority(quint16 priority);
/**
* @brief Retrieve the weight of the record
*
* This field is used by QMdnsEngine::SRV records.
*/
quint16 weight() const;
/**
* @brief Set the weight of the record
*
* Unless more than one QMdnsEngine::SRV record is being sent, this field
* should be set to 0.
*/
void setWeight(quint16 weight);
/**
* @brief Retrieve the port for the record
*
* This field is used by QMdnsEngine::SRV records.
*/
quint16 port() const;
/**
* @brief Set the port for the record
*/
void setPort(quint16 port);
/**
* @brief Retrieve attributes for the record
*
* This field is used by QMdnsEngine::TXT records.
*/
QMap<QByteArray, QByteArray> attributes() const;
/**
* @brief Set attributes for the record
*/
void setAttributes(const QMap<QByteArray, QByteArray> &attributes);
/**
* @brief Add an attribute to the record
*/
void addAttribute(const QByteArray &key, const QByteArray &value);
/**
* @brief Retrieve the bitmap for the record
*
* This field is used by QMdnsEngine::NSEC records.
*/
Bitmap bitmap() const;
/**
* @brief Set the bitmap for the record
*/
void setBitmap(const Bitmap &bitmap);
private:
RecordPrivate *const d;
};
QMDNSENGINE_EXPORT QDebug operator<<(QDebug dbg, const Record &record);
}
#endif // QMDNSENGINE_RECORD_H

View File

@@ -0,0 +1,88 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_RESOLVER_H
#define QMDNSENGINE_RESOLVER_H
#include <QHostAddress>
#include <QObject>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class AbstractServer;
class Cache;
class QMDNSENGINE_EXPORT ResolverPrivate;
/**
* @brief %Resolver for services
*
* When [Browser](@ref QMdnsEngine::Browser) indicates that a new service has
* been found, it becomes necessary to resolve the service in order to connect
* to it. This class serves that role. A [Cache](@ref QMdnsEngine::Cache) can
* optionally be provided to speed up the resolving process.
*
* For example, assuming that `record` is a SRV record:
*
* @code
* QMdnsEngine::Resolver resolver(&server, record.target());
* connect(&resolver, &QMdnsEngine::Resolver::resolved, [](const QHostAddress &address) {
* qDebug() << "Address:" << address;
* });
* @endcode
*/
class QMDNSENGINE_EXPORT Resolver : public QObject
{
Q_OBJECT
public:
/**
* @brief Create a new resolver
*/
Resolver(AbstractServer *server, const QByteArray &name, Cache *cache = 0, QObject *parent = 0);
Q_SIGNALS:
/**
* @brief Indicate that the host resolved to an address
* @param address service address
*
* This signal will be emitted once for each resolved address. For
* example, if a host provides both A and AAAA records, this signal will
* be emitted twice.
*/
void resolved(const QHostAddress &address);
private:
ResolverPrivate *const d;
};
}
#endif // QMDNSENGINE_RESOLVER_H

View File

@@ -0,0 +1,78 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_SERVER_H
#define QMDNSENGINE_SERVER_H
#include <qmdnsengine/abstractserver.h>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class Message;
class QMDNSENGINE_EXPORT ServerPrivate;
/**
* @brief mDNS server
*
* This class provides an implementation of
* [AbstractServer](@ref QMdnsEngine::AbstractServer) that uses all available
* local network adapters to send and receive mDNS messages.
*
* The class takes care of watching for the addition and removal of network
* interfaces, automatically joining multicast groups when new interfaces are
* available.
*/
class QMDNSENGINE_EXPORT Server : public AbstractServer
{
Q_OBJECT
public:
/**
* @brief Create a new server
*/
explicit Server(QObject *parent = 0);
/**
* @brief Implementation of AbstractServer::sendMessage()
*/
virtual void sendMessage(const Message &message);
/**
* @brief Implementation of AbstractServer::sendMessageToAll()
*/
virtual void sendMessageToAll(const Message &message);
private:
ServerPrivate *const d;
};
}
#endif // QMDNSENGINE_SERVER_H

View File

@@ -0,0 +1,156 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef QMDNSENGINE_SERVICE_H
#define QMDNSENGINE_SERVICE_H
#include <QByteArray>
#include <QHostAddress>
#include <QList>
#include <QMap>
#include "qmdnsengine_export.h"
namespace QMdnsEngine
{
class QMDNSENGINE_EXPORT ServicePrivate;
/**
* @brief %Service available on the local network
*
* This class contains the descriptive information necessary to represent an
* individual service made available to the local network. Instances are
* provided by [Browser](@ref QMdnsEngine::Browser) as services are
* discovered. Instances must be created and passed to
* [Provider::update()](@ref QMdnsEngine::Provider::update) to provide a
* service.
*/
class QMDNSENGINE_EXPORT Service
{
public:
/**
* @brief Create an uninitialized service
*/
Service();
/**
* @brief Create a copy of an existing service
*/
Service(const Service &other);
/**
* @brief Assignment operator
*/
Service &operator=(const Service &other);
/**
* @brief Equality operator
*/
bool operator==(const Service &other) const;
/**
* @brief Inequality operator
*/
bool operator!=(const Service &other) const;
/**
* @brief Destroy the service
*/
virtual ~Service();
/**
* @brief Retrieve the service type
*/
QByteArray type() const;
/**
* @brief Set the service type
*
* For example, an HTTP service might use "_http._tcp".
*/
void setType(const QByteArray &type);
/**
* @brief Retrieve the service name
*/
QByteArray name() const;
/**
* @brief Set the service name
*
* This is combined with the service type and domain to form the FQDN for
* the service.
*/
void setName(const QByteArray &name);
/**
* @brief Retrieve the hostname of the device providing the service
*/
QByteArray hostname() const;
/**
* @brief Set the hostname of the device providing the service
*/
void setHostname(const QByteArray &hostname);
/**
* @brief Retrieve the service port
*/
quint16 port() const;
/**
* @brief Set the service port
*/
void setPort(quint16 port);
/**
* @brief Retrieve the attributes for the service
*
* Boolean attributes will have null values (invoking QByteArray::isNull()
* on the value will return true).
*/
QMap<QByteArray, QByteArray> attributes() const;
/**
* @brief Set the attributes for the service
*/
void setAttributes(const QMap<QByteArray, QByteArray> &attributes);
/**
* @brief Add an attribute to the service
*/
void addAttribute(const QByteArray &key, const QByteArray &value);
private:
ServicePrivate *const d;
};
QMDNSENGINE_EXPORT QDebug operator<<(QDebug debug, const Service &service);
}
#endif // QMDNSENGINE_SERVICE_H