diff options
Diffstat (limited to 'Swiften/LinkLocal')
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.cpp | 4 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.h | 4 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h | 6 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp | 5 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h | 4 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h | 24 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/DNSSDQuerier.h | 4 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h | 6 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp | 6 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h | 6 | ||||
| -rw-r--r-- | Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDRegisterQuery.h | 6 | ||||
| -rw-r--r-- | Swiften/LinkLocal/LinkLocalService.h | 4 | ||||
| -rw-r--r-- | Swiften/LinkLocal/LinkLocalServiceBrowser.cpp | 4 | ||||
| -rw-r--r-- | Swiften/LinkLocal/LinkLocalServiceBrowser.h | 4 | ||||
| -rw-r--r-- | Swiften/LinkLocal/LinkLocalServiceInfo.cpp | 10 | ||||
| -rw-r--r-- | Swiften/LinkLocal/LinkLocalServiceInfo.h | 8 | ||||
| -rw-r--r-- | Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp | 6 |
17 files changed, 66 insertions, 45 deletions
diff --git a/Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.cpp b/Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.cpp index 1b79946..66b4ae8 100644 --- a/Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.cpp +++ b/Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.cpp @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.h> #include <iostream> #include <Swiften/LinkLocal/DNSSD/Avahi/AvahiBrowseQuery.h> @@ -19,19 +19,19 @@ AvahiQuerier::AvahiQuerier(EventLoop* eventLoop) : eventLoop(eventLoop), client( } AvahiQuerier::~AvahiQuerier() { } std::shared_ptr<DNSSDBrowseQuery> AvahiQuerier::createBrowseQuery() { return std::make_shared<AvahiBrowseQuery>(shared_from_this(), eventLoop); } -std::shared_ptr<DNSSDRegisterQuery> AvahiQuerier::createRegisterQuery(const std::string& name, int port, const ByteArray& info) { +std::shared_ptr<DNSSDRegisterQuery> AvahiQuerier::createRegisterQuery(const std::string& name, unsigned short port, const ByteArray& info) { return std::make_shared<AvahiRegisterQuery>(name, port, info, shared_from_this(), eventLoop); } std::shared_ptr<DNSSDResolveServiceQuery> AvahiQuerier::createResolveServiceQuery(const DNSSDServiceID& service) { return std::make_shared<AvahiResolveServiceQuery>(service, shared_from_this(), eventLoop); } std::shared_ptr<DNSSDResolveHostnameQuery> AvahiQuerier::createResolveHostnameQuery(const std::string& hostname, int interfaceIndex) { return std::make_shared<AvahiResolveHostnameQuery>(hostname, interfaceIndex, shared_from_this(), eventLoop); diff --git a/Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.h b/Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.h index 5dce19d..73dd11d 100644 --- a/Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.h +++ b/Swiften/LinkLocal/DNSSD/Avahi/AvahiQuerier.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <memory> #include <avahi-client/client.h> @@ -24,19 +24,19 @@ namespace Swift { class AvahiQuerier : public DNSSDQuerier, public std::enable_shared_from_this<AvahiQuerier> { public: AvahiQuerier(EventLoop* eventLoop); ~AvahiQuerier(); std::shared_ptr<DNSSDBrowseQuery> createBrowseQuery(); std::shared_ptr<DNSSDRegisterQuery> createRegisterQuery( - const std::string& name, int port, const ByteArray& info); + const std::string& name, unsigned short port, const ByteArray& info); std::shared_ptr<DNSSDResolveServiceQuery> createResolveServiceQuery( const DNSSDServiceID&); std::shared_ptr<DNSSDResolveHostnameQuery> createResolveHostnameQuery( const std::string& hostname, int interfaceIndex); void start(); void stop(); AvahiThreadedPoll* getThreadedPoll() const { diff --git a/Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h b/Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h index 68281d0..b780043 100644 --- a/Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h +++ b/Swiften/LinkLocal/DNSSD/Avahi/AvahiRegisterQuery.h @@ -1,30 +1,30 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <avahi-client/publish.h> #include <Swiften/Base/ByteArray.h> #include <Swiften/EventLoop/EventLoop.h> #include <Swiften/LinkLocal/DNSSD/Avahi/AvahiQuery.h> #include <Swiften/LinkLocal/DNSSD/DNSSDRegisterQuery.h> namespace Swift { class AvahiQuerier; class AvahiRegisterQuery : public DNSSDRegisterQuery, public AvahiQuery { public: - AvahiRegisterQuery(const std::string& name, int port, const ByteArray& txtRecord, std::shared_ptr<AvahiQuerier> querier, EventLoop* eventLoop) : AvahiQuery(querier, eventLoop), name(name), port(port), txtRecord(txtRecord), group(0) { + AvahiRegisterQuery(const std::string& name, unsigned short port, const ByteArray& txtRecord, std::shared_ptr<AvahiQuerier> querier, EventLoop* eventLoop) : AvahiQuery(querier, eventLoop), name(name), port(port), txtRecord(txtRecord), group(0) { } void registerService(); void unregisterService(); void updateServiceInfo(const ByteArray& txtRecord); private: void doRegisterService(); @@ -44,14 +44,14 @@ namespace Swift { eventLoop->postEvent(boost::bind(boost::ref(onRegisterFinished), boost::optional<DNSSDServiceID>()), shared_from_this()); } else { } } */ private: std::string name; - int port; + unsigned short port; ByteArray txtRecord; AvahiEntryGroup* group; }; } diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp index 0906ffc..551421e 100644 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp +++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.cpp @@ -1,22 +1,23 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h> #include <fcntl.h> #include <sys/socket.h> #include <unistd.h> #include <Swiften/Base/Algorithm.h> +#include <Swiften/Base/Log.h> #include <Swiften/LinkLocal/DNSSD/Bonjour/BonjourBrowseQuery.h> #include <Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h> #include <Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveHostnameQuery.h> #include <Swiften/LinkLocal/DNSSD/Bonjour/BonjourResolveServiceQuery.h> namespace Swift { BonjourQuerier::BonjourQuerier(EventLoop* eventLoop) : eventLoop(eventLoop), stopRequested(false), thread(nullptr) { int fds[2]; @@ -30,19 +31,19 @@ BonjourQuerier::BonjourQuerier(EventLoop* eventLoop) : eventLoop(eventLoop), sto BonjourQuerier::~BonjourQuerier() { assert(!thread); } std::shared_ptr<DNSSDBrowseQuery> BonjourQuerier::createBrowseQuery() { return std::make_shared<BonjourBrowseQuery>(shared_from_this(), eventLoop); } -std::shared_ptr<DNSSDRegisterQuery> BonjourQuerier::createRegisterQuery(const std::string& name, int port, const ByteArray& info) { +std::shared_ptr<DNSSDRegisterQuery> BonjourQuerier::createRegisterQuery(const std::string& name, unsigned short port, const ByteArray& info) { return std::make_shared<BonjourRegisterQuery>(name, port, info, shared_from_this(), eventLoop); } std::shared_ptr<DNSSDResolveServiceQuery> BonjourQuerier::createResolveServiceQuery(const DNSSDServiceID& service) { return std::make_shared<BonjourResolveServiceQuery>(service, shared_from_this(), eventLoop); } std::shared_ptr<DNSSDResolveHostnameQuery> BonjourQuerier::createResolveHostnameQuery(const std::string& hostname, int interfaceIndex) { return std::make_shared<BonjourResolveHostnameQuery>(hostname, interfaceIndex, shared_from_this(), eventLoop); diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h index 77326bc..6af1c1f 100644 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h +++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuerier.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <list> #include <memory> #include <mutex> @@ -19,19 +19,19 @@ namespace Swift { class BonjourQuerier : public DNSSDQuerier, public std::enable_shared_from_this<BonjourQuerier> { public: BonjourQuerier(EventLoop* eventLoop); ~BonjourQuerier(); std::shared_ptr<DNSSDBrowseQuery> createBrowseQuery(); std::shared_ptr<DNSSDRegisterQuery> createRegisterQuery( - const std::string& name, int port, const ByteArray& info); + const std::string& name, unsigned short port, const ByteArray& info); std::shared_ptr<DNSSDResolveServiceQuery> createResolveServiceQuery( const DNSSDServiceID&); std::shared_ptr<DNSSDResolveHostnameQuery> createResolveHostnameQuery( const std::string& hostname, int interfaceIndex); void start(); void stop(); private: diff --git a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h index 8b2e955..9eb8cd9 100644 --- a/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h +++ b/Swiften/LinkLocal/DNSSD/Bonjour/BonjourRegisterQuery.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <mutex> #include <boost/numeric/conversion/cast.hpp> @@ -14,24 +14,33 @@ #include <Swiften/EventLoop/EventLoop.h> #include <Swiften/LinkLocal/DNSSD/Bonjour/BonjourQuery.h> #include <Swiften/LinkLocal/DNSSD/DNSSDRegisterQuery.h> namespace Swift { class BonjourQuerier; class BonjourRegisterQuery : public DNSSDRegisterQuery, public BonjourQuery { public: - BonjourRegisterQuery(const std::string& name, int port, const ByteArray& txtRecord, std::shared_ptr<BonjourQuerier> querier, EventLoop* eventLoop) : BonjourQuery(querier, eventLoop) { + BonjourRegisterQuery(const std::string& name, unsigned short port, const ByteArray& txtRecord, std::shared_ptr<BonjourQuerier> querier, EventLoop* eventLoop) : BonjourQuery(querier, eventLoop) { + unsigned short recordSize = 0; + try { + recordSize = boost::numeric_cast<unsigned short>(txtRecord.size()); + } + catch (const boost::numeric::bad_numeric_cast&) { + SWIFT_LOG(warning) << "Bonjour TXT record is too long (" << txtRecord.size() << " bytes), not registring service" << std::endl; + return; + } DNSServiceErrorType result = DNSServiceRegister( - &sdRef, 0, 0, name.c_str(), "_presence._tcp", nullptr, nullptr, boost::numeric_cast<unsigned short>(port), - boost::numeric_cast<unsigned short>(txtRecord.size()), vecptr(txtRecord), + &sdRef, 0, 0, name.c_str(), "_presence._tcp", nullptr, nullptr, port, + recordSize, vecptr(txtRecord), &BonjourRegisterQuery::handleServiceRegisteredStatic, this); if (result != kDNSServiceErr_NoError) { + SWIFT_LOG(warning) << "Failed to register Bonjour service" << std::endl; sdRef = nullptr; } } void registerService() { if (sdRef) { run(); } else { @@ -39,19 +48,24 @@ namespace Swift { } } void unregisterService() { finish(); } void updateServiceInfo(const ByteArray& txtRecord) { std::lock_guard<std::mutex> lock(sdRefMutex); - DNSServiceUpdateRecord(sdRef, nullptr, 0, boost::numeric_cast<unsigned short>(txtRecord.size()), vecptr(txtRecord), 0); + try { + DNSServiceUpdateRecord(sdRef, nullptr, 0, boost::numeric_cast<unsigned short>(txtRecord.size()), vecptr(txtRecord), 0); + } + catch (const boost::numeric::bad_numeric_cast&) { + SWIFT_LOG(warning) << "Bonjour TXT record is too long (" << txtRecord.size() << " bytes), not updating service record" << std::endl; + } } private: static void handleServiceRegisteredStatic(DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain, void *context) { static_cast<BonjourRegisterQuery*>(context)->handleServiceRegistered(errorCode, name, regtype, domain); } void handleServiceRegistered(DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain) { if (errorCode != kDNSServiceErr_NoError) { diff --git a/Swiften/LinkLocal/DNSSD/DNSSDQuerier.h b/Swiften/LinkLocal/DNSSD/DNSSDQuerier.h index 8f3c3ec..3924c05 100644 --- a/Swiften/LinkLocal/DNSSD/DNSSDQuerier.h +++ b/Swiften/LinkLocal/DNSSD/DNSSDQuerier.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <memory> #include <Swiften/Base/ByteArray.h> @@ -20,16 +20,16 @@ namespace Swift { class DNSSDQuerier { public: virtual ~DNSSDQuerier(); virtual void start() = 0; virtual void stop() = 0; virtual std::shared_ptr<DNSSDBrowseQuery> createBrowseQuery() = 0; virtual std::shared_ptr<DNSSDRegisterQuery> createRegisterQuery( - const std::string& name, int port, const ByteArray& info) = 0; + const std::string& name, unsigned short port, const ByteArray& info) = 0; virtual std::shared_ptr<DNSSDResolveServiceQuery> createResolveServiceQuery( const DNSSDServiceID&) = 0; virtual std::shared_ptr<DNSSDResolveHostnameQuery> createResolveHostnameQuery( const std::string& hostname, int interfaceIndex) = 0; }; } diff --git a/Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h b/Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h index b55447a..6416d69 100644 --- a/Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h +++ b/Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h @@ -1,32 +1,32 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <boost/optional.hpp> #include <boost/signals2.hpp> #include <Swiften/Base/ByteArray.h> #include <Swiften/LinkLocal/DNSSD/DNSSDServiceID.h> namespace Swift { class DNSSDResolveServiceQuery { public: struct Result { - Result(const std::string& fullName, const std::string& host, int port, const ByteArray& info) : + Result(const std::string& fullName, const std::string& host, unsigned short port, const ByteArray& info) : fullName(fullName), host(host), port(port), info(info) {} std::string fullName; std::string host; - int port; + unsigned short port; ByteArray info; }; virtual ~DNSSDResolveServiceQuery(); virtual void start() = 0; virtual void stop() = 0; boost::signals2::signal<void (const boost::optional<Result>&)> onServiceResolved; diff --git a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp index c17f8b2..3381a26 100644 --- a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp +++ b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.cpp @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h> #include <iostream> #include <boost/bind.hpp> @@ -26,19 +26,19 @@ FakeDNSSDQuerier::~FakeDNSSDQuerier() { if (!runningQueries.empty()) { std::cerr << "FakeDNSSDQuerier: Running queries not empty at destruction time" << std::endl; } } std::shared_ptr<DNSSDBrowseQuery> FakeDNSSDQuerier::createBrowseQuery() { return std::make_shared<FakeDNSSDBrowseQuery>(shared_from_this()); } -std::shared_ptr<DNSSDRegisterQuery> FakeDNSSDQuerier::createRegisterQuery(const std::string& name, int port, const ByteArray& info) { +std::shared_ptr<DNSSDRegisterQuery> FakeDNSSDQuerier::createRegisterQuery(const std::string& name, unsigned short port, const ByteArray& info) { return std::make_shared<FakeDNSSDRegisterQuery>(name, port, info, shared_from_this()); } std::shared_ptr<DNSSDResolveServiceQuery> FakeDNSSDQuerier::createResolveServiceQuery(const DNSSDServiceID& service) { return std::make_shared<FakeDNSSDResolveServiceQuery>(service, shared_from_this()); } std::shared_ptr<DNSSDResolveHostnameQuery> FakeDNSSDQuerier::createResolveHostnameQuery(const std::string& hostname, int interfaceIndex) { return std::make_shared<FakeDNSSDResolveHostnameQuery>(hostname, interfaceIndex, shared_from_this()); @@ -99,19 +99,19 @@ void FakeDNSSDQuerier::setServiceInfo(const DNSSDServiceID& id, const DNSSDResol r.first->second = info; } for (const auto& query : getQueries<FakeDNSSDResolveServiceQuery>()) { if (query->service == id) { eventLoop->postEvent(boost::bind(boost::ref(query->onServiceResolved), info), shared_from_this()); } } } -bool FakeDNSSDQuerier::isServiceRegistered(const std::string& name, int port, const ByteArray& info) { +bool FakeDNSSDQuerier::isServiceRegistered(const std::string& name, unsigned short port, const ByteArray& info) { for (const auto& query : getQueries<FakeDNSSDRegisterQuery>()) { if (query->name == name && query->port == port && query->info == info) { return true; } } return false; } void FakeDNSSDQuerier::setBrowseError() { diff --git a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h index 5d4fefd..07cb75c 100644 --- a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h +++ b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2017 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <list> #include <memory> #include <set> @@ -33,31 +33,31 @@ namespace Swift { void start() {} void stop() {} void clearAllQueriesEverRun() { allQueriesEverRun.clear(); } std::shared_ptr<DNSSDBrowseQuery> createBrowseQuery(); std::shared_ptr<DNSSDRegisterQuery> createRegisterQuery( - const std::string& name, int port, const ByteArray& info); + const std::string& name, unsigned short port, const ByteArray& info); std::shared_ptr<DNSSDResolveServiceQuery> createResolveServiceQuery( const DNSSDServiceID&); std::shared_ptr<DNSSDResolveHostnameQuery> createResolveHostnameQuery( const std::string& hostname, int interfaceIndex); void addRunningQuery(std::shared_ptr<FakeDNSSDQuery>); void removeRunningQuery(std::shared_ptr<FakeDNSSDQuery>); void addService(const DNSSDServiceID& id); void removeService(const DNSSDServiceID& id); void setServiceInfo(const DNSSDServiceID& id, const DNSSDResolveServiceQuery::Result& info); - bool isServiceRegistered(const std::string& name, int port, const ByteArray& info); + bool isServiceRegistered(const std::string& name, unsigned short port, const ByteArray& info); void setAddress(const std::string& hostname, boost::optional<HostAddress> address); void setBrowseError(); void setRegisterError(); public: template<typename T> std::vector< std::shared_ptr<T> > getAllQueriesEverRun() const { std::vector< std::shared_ptr<T> > result; diff --git a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDRegisterQuery.h b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDRegisterQuery.h index 7478841..ee6bb92 100644 --- a/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDRegisterQuery.h +++ b/Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDRegisterQuery.h @@ -1,39 +1,39 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <string> #include <Swiften/Base/ByteArray.h> #include <Swiften/LinkLocal/DNSSD/DNSSDRegisterQuery.h> #include <Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuery.h> namespace Swift { class FakeDNSSDQuerier; class FakeDNSSDRegisterQuery : public DNSSDRegisterQuery, public FakeDNSSDQuery { public: - FakeDNSSDRegisterQuery(const std::string& name, int port, const ByteArray& info, std::shared_ptr<FakeDNSSDQuerier> querier) : FakeDNSSDQuery(querier), name(name), port(port), info(info) { + FakeDNSSDRegisterQuery(const std::string& name, unsigned short port, const ByteArray& info, std::shared_ptr<FakeDNSSDQuerier> querier) : FakeDNSSDQuery(querier), name(name), port(port), info(info) { } void registerService() { run(); } void updateServiceInfo(const ByteArray& i) { info = i; } void unregisterService() { finish(); } std::string name; - int port; + unsigned short port; ByteArray info; }; } diff --git a/Swiften/LinkLocal/LinkLocalService.h b/Swiften/LinkLocal/LinkLocalService.h index 9b0e2ab..c51f890 100644 --- a/Swiften/LinkLocal/LinkLocalService.h +++ b/Swiften/LinkLocal/LinkLocalService.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <string> #include <Swiften/Base/API.h> @@ -25,19 +25,19 @@ namespace Swift { const DNSSDServiceID& getID() const { return id; } const std::string& getName() const { return id.getName(); } - int getPort() const { + unsigned short getPort() const { return info.port; } const std::string& getHostname() const { return info.host; } LinkLocalServiceInfo getInfo() const { return LinkLocalServiceInfo::createFromTXTRecord(info.info); diff --git a/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp b/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp index b79f184..b3328cd 100644 --- a/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp +++ b/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/LinkLocal/LinkLocalServiceBrowser.h> #include <iostream> #include <boost/bind.hpp> @@ -59,19 +59,19 @@ bool LinkLocalServiceBrowser::isRunning() const { bool LinkLocalServiceBrowser::hasError() const { return haveError; } bool LinkLocalServiceBrowser::isRegistered() const { return !!registerQuery; } -void LinkLocalServiceBrowser::registerService(const std::string& name, int port, const LinkLocalServiceInfo& info) { +void LinkLocalServiceBrowser::registerService(const std::string& name, unsigned short port, const LinkLocalServiceInfo& info) { assert(!registerQuery); registerQuery = querier->createRegisterQuery(name, port, info.toTXTRecord()); registerQuery->onRegisterFinished.connect( boost::bind(&LinkLocalServiceBrowser::handleRegisterFinished, this, _1)); registerQuery->registerService(); } void LinkLocalServiceBrowser::updateService(const LinkLocalServiceInfo& info) { assert(registerQuery); diff --git a/Swiften/LinkLocal/LinkLocalServiceBrowser.h b/Swiften/LinkLocal/LinkLocalServiceBrowser.h index c59a4d0..bfcfc07 100644 --- a/Swiften/LinkLocal/LinkLocalServiceBrowser.h +++ b/Swiften/LinkLocal/LinkLocalServiceBrowser.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <map> #include <memory> #include <string> @@ -29,19 +29,19 @@ namespace Swift { ~LinkLocalServiceBrowser(); void start(); void stop(); bool isRunning() const; bool hasError() const; void registerService( const std::string& name, - int port, + unsigned short port, const LinkLocalServiceInfo& info = LinkLocalServiceInfo()); void updateService( const LinkLocalServiceInfo& info = LinkLocalServiceInfo()); void unregisterService(); bool isRegistered() const; std::vector<LinkLocalService> getServices() const; // FIXME: Ugly that we need this diff --git a/Swiften/LinkLocal/LinkLocalServiceInfo.cpp b/Swiften/LinkLocal/LinkLocalServiceInfo.cpp index 771251a..102b7f3 100644 --- a/Swiften/LinkLocal/LinkLocalServiceInfo.cpp +++ b/Swiften/LinkLocal/LinkLocalServiceInfo.cpp @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2013 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/LinkLocal/LinkLocalServiceInfo.h> #include <boost/lexical_cast.hpp> #include <boost/numeric/conversion/cast.hpp> @@ -76,19 +76,25 @@ LinkLocalServiceInfo LinkLocalServiceInfo::createFromTXTRecord(const ByteArray& info.setJID(JID(entry.second)); } else if (entry.first == "msg") { info.setMessage(entry.second); } else if (entry.first == "nick") { info.setNick(entry.second); } else if (entry.first == "port.p2pj") { - info.setPort(boost::lexical_cast<int>(entry.second)); + try { + info.setPort(boost::numeric_cast<unsigned short>(boost::lexical_cast<int>(entry.second))); + } + catch (const boost::bad_lexical_cast&) { + } + catch (const boost::numeric::bad_numeric_cast&) { + } } else if (entry.first == "status") { if (entry.second == "away") { info.setStatus(Away); } else if (entry.second == "dnd") { info.setStatus(DND); } } diff --git a/Swiften/LinkLocal/LinkLocalServiceInfo.h b/Swiften/LinkLocal/LinkLocalServiceInfo.h index 9f15c6e..eb65706 100644 --- a/Swiften/LinkLocal/LinkLocalServiceInfo.h +++ b/Swiften/LinkLocal/LinkLocalServiceInfo.h @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include <string> #include <boost/optional.hpp> @@ -37,31 +37,31 @@ namespace Swift { const std::string& getMessage() const { return message; } void setMessage(const std::string& m) { message = m; } const std::string& getNick() const { return nick; } void setNick(const std::string& n) { nick = n; } Status getStatus() const { return status; } void setStatus(Status s) { status = s; } - boost::optional<int> getPort() const { return port; } - void setPort(int p) { port = p; } + boost::optional<unsigned short> getPort() const { return port; } + void setPort(unsigned short p) { port = p; } ByteArray toTXTRecord() const; static LinkLocalServiceInfo createFromTXTRecord(const ByteArray& record); private: static ByteArray getEncoded(const std::string&); static std::pair<std::string,std::string> readEntry(const ByteArray&, size_t*); private: std::string firstName; std::string lastName; std::string email; JID jid; std::string message; std::string nick; Status status; - boost::optional<int> port; + boost::optional<unsigned short> port; }; } diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp index 85ae537..ab1ee0c 100644 --- a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp +++ b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp @@ -1,11 +1,11 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <boost/bind.hpp> #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> @@ -47,19 +47,19 @@ class LinkLocalConnectorTest : public CppUnit::TestFixture { querier->setAddress("rabbithole.local", HostAddress::fromString("192.168.1.1").get()); testling->connect(); eventLoop->processEvents(); CPPUNIT_ASSERT(connectFinished); CPPUNIT_ASSERT(!connectError); CPPUNIT_ASSERT(connection->connectedTo); CPPUNIT_ASSERT_EQUAL(std::string(connection->connectedTo->getAddress().toString()), std::string("192.168.1.1")); - CPPUNIT_ASSERT_EQUAL(connection->connectedTo->getPort(), 1234); + CPPUNIT_ASSERT_EQUAL(connection->connectedTo->getPort(), static_cast<unsigned short>(1234)); } void testConnect_UnableToResolve() { std::shared_ptr<LinkLocalConnector> testling(createConnector("rabbithole.local", 1234)); querier->setAddress("rabbithole.local", boost::optional<HostAddress>()); testling->connect(); eventLoop->processEvents(); @@ -108,19 +108,19 @@ class LinkLocalConnectorTest : public CppUnit::TestFixture { CPPUNIT_ASSERT(FakeConnection::Connecting == connection->state); testling->cancel(); eventLoop->processEvents(); CPPUNIT_ASSERT(FakeConnection::Disconnected == connection->state); } private: - std::shared_ptr<LinkLocalConnector> createConnector(const std::string& hostname, int port) { + std::shared_ptr<LinkLocalConnector> createConnector(const std::string& hostname, unsigned short port) { LinkLocalService service( DNSSDServiceID("myname", "local."), DNSSDResolveServiceQuery::Result( "myname._presence._tcp.local", hostname, port, LinkLocalServiceInfo().toTXTRecord())); std::shared_ptr<LinkLocalConnector> result( new LinkLocalConnector(service, querier, connection)); result->onConnectFinished.connect( boost::bind(&LinkLocalConnectorTest::handleConnected, this, _1)); |
Swift