diff options
author | Alan Conway <aconway@apache.org> | 2010-05-11 14:39:58 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2010-05-11 14:39:58 +0000 |
commit | 16da0e0c511c0c1cf4ea592640c522754065200a (patch) | |
tree | 59fb80994db731fabe19897c95a6912e68716360 /cpp/src | |
parent | be8e1bf6b6a0d760bddbbe6642d477a95f36ab42 (diff) | |
download | qpid-python-16da0e0c511c0c1cf4ea592640c522754065200a.tar.gz |
Support for multiple protocols in qpid::Url.
- simplified qpid::Address to hold (protocol,host,port) triples.
- protocol plugins call Url:addProtocol to add tags to Url parser.
- use Address::protocol when establishing connections.
- ssl_test: tests using URL to connect.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@943130 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src')
24 files changed, 152 insertions, 155 deletions
diff --git a/cpp/src/qmf/engine/BrokerProxyImpl.cpp b/cpp/src/qmf/engine/BrokerProxyImpl.cpp index b99cb414dc..69acc2dd37 100644 --- a/cpp/src/qmf/engine/BrokerProxyImpl.cpp +++ b/cpp/src/qmf/engine/BrokerProxyImpl.cpp @@ -78,7 +78,7 @@ BrokerEvent BrokerEventImpl::copy() BrokerProxyImpl::BrokerProxyImpl(BrokerProxy& pub, Console& _console) : publicObject(pub), console(_console) { stringstream qn; - qpid::TcpAddress addr; + qpid::Address addr; SystemInfo::getLocalHostname(addr); qn << "qmfc-" << SystemInfo::getProcessName() << "-" << addr << "-" << SystemInfo::getProcessId(); diff --git a/cpp/src/qpid/Address.cpp b/cpp/src/qpid/Address.cpp index c3f6829fd3..e2b2dfbcdf 100644 --- a/cpp/src/qpid/Address.cpp +++ b/cpp/src/qpid/Address.cpp @@ -17,6 +17,7 @@ */ #include "qpid/Address.h" +#include "qpid/client/ConnectionSettings.h" #include <ostream> @@ -24,35 +25,14 @@ using namespace std; namespace qpid { -TcpAddress::TcpAddress(const std::string& h, uint16_t p): host(h), port(p) {} +const string Address::TCP("tcp"); -struct AddressOstreamVisitor : public boost::static_visitor<ostream&> { - ostream& out; - AddressOstreamVisitor(ostream& o) : out(o) {} - template <class T> ostream& operator()(const T& data) { return out << data; } -}; - -ostream& operator<<(ostream& os, const Address& addr) { - AddressOstreamVisitor visitor(os); - return boost::apply_visitor(visitor, addr.value); -} - -bool operator==(const TcpAddress& x, const TcpAddress& y) { - return y.host==x.host && y.port == x.port; -} - -ostream& operator<<(ostream& os, const TcpAddress& a) { - return os << "tcp:" << a.host << ":" << a.port; -} - -ExampleAddress::ExampleAddress(const char c) : data(c) {} - -bool operator==(const ExampleAddress& x, const ExampleAddress& y) { - return x.data == y.data; +ostream& operator<<(ostream& os, const Address& a) { + return os << a.protocol << ":" << a.host << ":" << a.port; } -ostream& operator<<(ostream& os, const ExampleAddress& ex) { - return os << "example:" << ex.data; +bool operator==(const Address& x, const Address& y) { + return y.protocol==x.protocol && y.host==x.host && y.port == x.port; } } // namespace qpid diff --git a/cpp/src/qpid/Url.cpp b/cpp/src/qpid/Url.cpp index 3b4eb60ddd..845a98c87b 100644 --- a/cpp/src/qpid/Url.cpp +++ b/cpp/src/qpid/Url.cpp @@ -21,6 +21,7 @@ #include "qpid/Msg.h" #include "qpid/sys/SystemInfo.h" #include "qpid/sys/StrError.h" +#include "qpid/client/Connector.h" #include <boost/lexical_cast.hpp> @@ -36,7 +37,7 @@ namespace qpid { Url::Invalid::Invalid(const string& s) : Exception(s) {} Url Url::getHostNameUrl(uint16_t port) { - TcpAddress address(std::string(), port); + Address address("tcp", std::string(), port); if (!sys::SystemInfo::getLocalHostname(address)) throw Url::Invalid(QPID_MSG("Cannot get host name: " << qpid::sys::strError(errno))); return Url(address); @@ -68,50 +69,55 @@ ostream& operator<<(ostream& os, const Url& url) { return os; } +static const std::string TCP = "tcp"; + +/** Simple recursive-descent parser for this grammar: + url = ["amqp:"] protocol_addr *("," protocol_addr) + protocol_addr = [ protocol_tag ":" ] host [":" port] + protocol_tag = "tcp" / "rdma" / "ssl" -/** Simple recursive-descent parser for url grammar in AMQP 0-10 spec: - - amqp_url = "amqp:" prot_addr_list - prot_addr_list = [prot_addr ","]* prot_addr - prot_addr = tcp_prot_addr | tls_prot_addr - - tcp_prot_addr = tcp_id tcp_addr - tcp_id = "tcp:" | "" - tcp_addr = [host [":" port] ] - host = <as per http://www.ietf.org/rfc/rfc3986.txt> - port = number]]> */ class UrlParser { public: UrlParser(Url& u, const char* s) : url(u), text(s), end(s+strlen(s)), i(s) {} - bool parse() { return literal("amqp:") && list(&UrlParser::protAddr, &UrlParser::comma) && i == end; } + bool parse() { + literal("amqp:"); // Optional + return list(&UrlParser::protocolAddr, &UrlParser::comma) && i == end; + } private: typedef bool (UrlParser::*Rule)(); bool comma() { return literal(","); } - // NOTE: tcpAddr must be last since it is allowed to omit it's tcp: tag. - bool protAddr() { return exampleAddr() || tcpAddr(); } - - bool tcpAddr() { - TcpAddress addr; - literal("tcp:"); // Don't check result, allowed to be absent. - return addIf(host(addr.host) && (literal(":") ? port(addr.port) : true), addr); + bool protocolAddr() { + Address addr(Address::TCP, "", Address::AMQP_PORT); // Set up defaults + protocolTag(addr.protocol); // Optional + bool ok = (host(addr.host) && + (literal(":") ? port(addr.port) : true)); + if (ok) url.push_back(addr); + return ok; } - - // Placeholder address type till we have multiple address types. Address is a single char. - bool exampleAddr () { - if (literal("example:") && i < end) { - ExampleAddress ex(*i++); - url.push_back(ex); - return true; + + bool protocolTag(string& result) { + const char* j = std::find(i,end,':'); + if (j != end) { + string tag(i,j); + if (std::find(Url::protocols.begin(), Url::protocols.end(), tag) != + Url::protocols.end()) + { + i = j+1; + result = tag; + return true; + } } return false; } - // FIXME aconway 2008-11-20: this does not implement http://www.ietf.org/rfc/rfc3986.txt. - // Works for DNS names and ipv4 literals but won't handle ipv6. + // TODO aconway 2008-11-20: this does not fully implement + // http://www.ietf.org/rfc/rfc3986.txt. Works for DNS names and + // ipv4 literals but won't handle ipv6. + // bool host(string& h) { const char* start=i; while (unreserved() || pctEncoded()) @@ -129,8 +135,6 @@ class UrlParser { bool port(uint16_t& p) { return decimalInt(p); } - template <class AddrType> bool addIf(bool ok, const AddrType& addr) { if (ok) url.push_back(addr); return ok; } - template <class IntType> bool decimalInt(IntType& n) { const char* start = i; while (decDigit()) @@ -209,4 +213,8 @@ std::istream& operator>>(std::istream& is, Url& url) { return is; } +std::vector<std::string> Url::protocols; + +void Url::addProtocol(const std::string& tag) { protocols.push_back(tag); } + } // namespace qpid diff --git a/cpp/src/qpid/broker/Broker.cpp b/cpp/src/qpid/broker/Broker.cpp index df621fe4fb..09157c1e62 100644 --- a/cpp/src/qpid/broker/Broker.cpp +++ b/cpp/src/qpid/broker/Broker.cpp @@ -437,6 +437,7 @@ uint16_t Broker::getPort(const std::string& name) const { void Broker::registerProtocolFactory(const std::string& name, ProtocolFactory::shared_ptr protocolFactory) { protocolFactories[name] = protocolFactory; + Url::addProtocol(name); } void Broker::accept() { @@ -461,8 +462,8 @@ void Broker::connect( sys::ConnectionCodec::Factory* f) { url.throwIfEmpty(); - const TcpAddress* addr=url[0].get<TcpAddress>(); - connect(addr->host, addr->port, TCP_TRANSPORT, failed, f); + const Address& addr=url[0]; + connect(addr.host, addr.port, addr.protocol, failed, f); } uint32_t Broker::queueMoveMessages( diff --git a/cpp/src/qpid/broker/Link.cpp b/cpp/src/qpid/broker/Link.cpp index 6db6fe7637..5a1dfd9656 100644 --- a/cpp/src/qpid/broker/Link.cpp +++ b/cpp/src/qpid/broker/Link.cpp @@ -307,11 +307,12 @@ void Link::maintenanceVisit () connection->requestIOProcessing (boost::bind(&Link::ioThreadProcessing, this)); } -void Link::reconnect(const qpid::TcpAddress& a) +void Link::reconnect(const qpid::Address& a) { Mutex::ScopedLock mutex(lock); host = a.host; port = a.port; + transport = a.protocol; startConnectionLH(); if (mgmtObject != 0) { stringstream errorString; @@ -322,11 +323,10 @@ void Link::reconnect(const qpid::TcpAddress& a) bool Link::tryFailover() { - //TODO: urls only work for TCP at present, update when that has changed - TcpAddress next; - if (transport == Broker::TCP_TRANSPORT && urls.next(next) && - (next.host != host || next.port != port)) { - links->changeAddress(TcpAddress(host, port), next); + Address next; + if (urls.next(next) && + (next.host != host || next.port != port || next.protocol != transport)) { + links->changeAddress(Address(transport, host, port), next); QPID_LOG(debug, "Link failing over to " << host << ":" << port); return true; } else { diff --git a/cpp/src/qpid/broker/Link.h b/cpp/src/qpid/broker/Link.h index 318eb5bd32..9da610076b 100644 --- a/cpp/src/qpid/broker/Link.h +++ b/cpp/src/qpid/broker/Link.h @@ -115,7 +115,7 @@ namespace qpid { void established(); // Called when connection is created void closed(int, std::string); // Called when connection goes away void setConnection(Connection*); // Set pointer to the AMQP Connection - void reconnect(const TcpAddress&); //called by LinkRegistry + void reconnect(const Address&); //called by LinkRegistry string getAuthMechanism() { return authMechanism; } string getUsername() { return username; } diff --git a/cpp/src/qpid/broker/LinkRegistry.cpp b/cpp/src/qpid/broker/LinkRegistry.cpp index f32587dd68..592b64449d 100644 --- a/cpp/src/qpid/broker/LinkRegistry.cpp +++ b/cpp/src/qpid/broker/LinkRegistry.cpp @@ -95,14 +95,14 @@ void LinkRegistry::periodicMaintenance () reMappings.clear(); } -void LinkRegistry::changeAddress(const qpid::TcpAddress& oldAddress, const qpid::TcpAddress& newAddress) +void LinkRegistry::changeAddress(const qpid::Address& oldAddress, const qpid::Address& newAddress) { //done on periodic maintenance thread; hold changes in separate //map to avoid modifying the link map that is iterated over reMappings[createKey(oldAddress)] = newAddress; } -bool LinkRegistry::updateAddress(const std::string& oldKey, const qpid::TcpAddress& newAddress) +bool LinkRegistry::updateAddress(const std::string& oldKey, const qpid::Address& newAddress) { std::string newKey = createKey(newAddress); if (links.find(newKey) != links.end()) { @@ -133,9 +133,7 @@ pair<Link::shared_ptr, bool> LinkRegistry::declare(string& host, { Mutex::ScopedLock locker(lock); - stringstream keystream; - keystream << host << ":" << port; - string key = string(keystream.str()); + string key = createKey(host, port); LinkMap::iterator i = links.find(key); if (i == links.end()) @@ -168,12 +166,10 @@ pair<Bridge::shared_ptr, bool> LinkRegistry::declare(std::string& host, Mutex::ScopedLock locker(lock); QPID_LOG(debug, "Bridge declared " << host << ": " << port << " from " << src << " to " << dest << " (" << key << ")"); - stringstream keystream; - keystream << host << ":" << port; - string linkKey = string(keystream.str()); - - keystream << "!" << src << "!" << dest << "!" << key; - string bridgeKey = string(keystream.str()); + string linkKey = createKey(host, port); + stringstream keystream; + keystream << linkKey << "!" << src << "!" << dest << "!" << key; + string bridgeKey = keystream.str(); LinkMap::iterator l = links.find(linkKey); if (l == links.end()) @@ -210,9 +206,7 @@ pair<Bridge::shared_ptr, bool> LinkRegistry::declare(std::string& host, void LinkRegistry::destroy(const string& host, const uint16_t port) { Mutex::ScopedLock locker(lock); - stringstream keystream; - keystream << host << ":" << port; - string key = string(keystream.str()); + string key = createKey(host, port); LinkMap::iterator i = links.find(key); if (i != links.end()) @@ -231,16 +225,15 @@ void LinkRegistry::destroy(const std::string& host, const std::string& key) { Mutex::ScopedLock locker(lock); - stringstream keystream; - keystream << host << ":" << port; - string linkKey = string(keystream.str()); + string linkKey = createKey(host, port); + stringstream keystream; + keystream << linkKey << "!" << src << "!" << dest << "!" << key; + string bridgeKey = keystream.str(); LinkMap::iterator l = links.find(linkKey); if (l == links.end()) return; - keystream << "!" << src << "!" << dest << "!" << key; - string bridgeKey = string(keystream.str()); BridgeMap::iterator b = bridges.find(bridgeKey); if (b == bridges.end()) return; @@ -328,11 +321,18 @@ std::string LinkRegistry::getAuthIdentity(const std::string& key) } -std::string LinkRegistry::createKey(const qpid::TcpAddress& a) -{ - stringstream keystream; - keystream << a.host << ":" << a.port; - return string(keystream.str()); +std::string LinkRegistry::createKey(const qpid::Address& a) { + // TODO aconway 2010-05-11: key should also include protocol/transport to + // be unique. Requires refactor of LinkRegistry interface. + return createKey(a.host, a.port); +} + +std::string LinkRegistry::createKey(const std::string& host, uint16_t port) { + // TODO aconway 2010-05-11: key should also include protocol/transport to + // be unique. Requires refactor of LinkRegistry interface. + stringstream keystream; + keystream << host << ":" << port; + return keystream.str(); } void LinkRegistry::setPassive(bool p) diff --git a/cpp/src/qpid/broker/LinkRegistry.h b/cpp/src/qpid/broker/LinkRegistry.h index 09a89298b6..52ab700cfc 100644 --- a/cpp/src/qpid/broker/LinkRegistry.h +++ b/cpp/src/qpid/broker/LinkRegistry.h @@ -53,7 +53,7 @@ namespace broker { typedef std::map<std::string, boost::shared_ptr<Link> > LinkMap; typedef std::map<std::string, Bridge::shared_ptr> BridgeMap; - typedef std::map<std::string, TcpAddress> AddressMap; + typedef std::map<std::string, Address> AddressMap; LinkMap links; LinkMap linksToDestroy; @@ -72,9 +72,10 @@ namespace broker { std::string realm; void periodicMaintenance (); - bool updateAddress(const std::string& oldKey, const TcpAddress& newAddress); + bool updateAddress(const std::string& oldKey, const Address& newAddress); boost::shared_ptr<Link> findLink(const std::string& key); - static std::string createKey(const TcpAddress& address); + static std::string createKey(const Address& address); + static std::string createKey(const std::string& host, uint16_t port); public: LinkRegistry (); // Only used in store tests @@ -135,7 +136,7 @@ namespace broker { /** * Called by links failing over to new address */ - void changeAddress(const TcpAddress& oldAddress, const TcpAddress& newAddress); + void changeAddress(const Address& oldAddress, const Address& newAddress); /** * Called to alter passive state. In passive state the links * and bridges managed by a link registry will be recorded and diff --git a/cpp/src/qpid/broker/RetryList.cpp b/cpp/src/qpid/broker/RetryList.cpp index 8f600c086d..b0477dd0f7 100644 --- a/cpp/src/qpid/broker/RetryList.cpp +++ b/cpp/src/qpid/broker/RetryList.cpp @@ -31,20 +31,16 @@ void RetryList::reset(const std::vector<Url>& u) urlIndex = addressIndex = 0;//reset indices } -bool RetryList::next(TcpAddress& address) +bool RetryList::next(Address& address) { while (urlIndex < urls.size()) { - while (addressIndex < urls[urlIndex].size()) { - const TcpAddress* tcp = urls[urlIndex][addressIndex++].get<TcpAddress>(); - if (tcp) { - address = *tcp; - return true; - } + if (addressIndex < urls[urlIndex].size()) { + address = urls[urlIndex][addressIndex++]; + return true; } urlIndex++; addressIndex = 0; } - urlIndex = addressIndex = 0;//reset indices return false; } diff --git a/cpp/src/qpid/broker/RetryList.h b/cpp/src/qpid/broker/RetryList.h index f87adf2c8d..242a7d2122 100644 --- a/cpp/src/qpid/broker/RetryList.h +++ b/cpp/src/qpid/broker/RetryList.h @@ -38,7 +38,7 @@ class RetryList public: QPID_BROKER_EXTERN RetryList(); QPID_BROKER_EXTERN void reset(const std::vector<Url>& urls); - QPID_BROKER_EXTERN bool next(TcpAddress& address); + QPID_BROKER_EXTERN bool next(Address& address); private: std::vector<Url> urls; size_t urlIndex; diff --git a/cpp/src/qpid/broker/windows/SslProtocolFactory.cpp b/cpp/src/qpid/broker/windows/SslProtocolFactory.cpp index 0db8fb5713..62122cbaa9 100644 --- a/cpp/src/qpid/broker/windows/SslProtocolFactory.cpp +++ b/cpp/src/qpid/broker/windows/SslProtocolFactory.cpp @@ -52,7 +52,7 @@ struct SslServerOptions : qpid::Options SslServerOptions() : qpid::Options("SSL Options"),
certStore("My"), port(5671), clientAuth(false)
{
- qpid::TcpAddress me;
+ qpid::Address me;
if (qpid::sys::SystemInfo::getLocalHostname(me))
certName = me.host;
else
diff --git a/cpp/src/qpid/client/Connection.cpp b/cpp/src/qpid/client/Connection.cpp index 6d2fd1d760..2882ef5d42 100644 --- a/cpp/src/qpid/client/Connection.cpp +++ b/cpp/src/qpid/client/Connection.cpp @@ -71,19 +71,18 @@ void Connection::open(const Url& url, const ConnectionSettings& settings) { throw Exception(QPID_MSG("Attempt to open URL with no addresses.")); Url::const_iterator i = url.begin(); do { - const TcpAddress* tcp = i->get<TcpAddress>(); + const Address& addr = *i; i++; - if (tcp) { - try { - ConnectionSettings cs(settings); - cs.host = tcp->host; - cs.port = tcp->port; - open(cs); - break; - } - catch (const Exception& /*e*/) { - if (i == url.end()) throw; - } + try { + ConnectionSettings cs(settings); + cs.protocol = addr.protocol; + cs.host = addr.host; + cs.port = addr.port; + open(cs); + break; + } + catch (const Exception& /*e*/) { + if (i == url.end()) throw; } } while (i != url.end()); } diff --git a/cpp/src/qpid/client/ConnectionSettings.cpp b/cpp/src/qpid/client/ConnectionSettings.cpp index b0c9477223..60b2eac2e8 100644 --- a/cpp/src/qpid/client/ConnectionSettings.cpp +++ b/cpp/src/qpid/client/ConnectionSettings.cpp @@ -31,7 +31,7 @@ namespace client { ConnectionSettings::ConnectionSettings() : protocol("tcp"), host("localhost"), - port(TcpAddress::DEFAULT_PORT), + port(5672), locale("en_US"), heartbeat(0), maxChannels(32767), diff --git a/cpp/src/qpid/client/Connector.cpp b/cpp/src/qpid/client/Connector.cpp index a06184fa27..c71dd9ecb6 100644 --- a/cpp/src/qpid/client/Connector.cpp +++ b/cpp/src/qpid/client/Connector.cpp @@ -20,7 +20,7 @@ */ #include "qpid/client/Connector.h" - +#include "qpid/Url.h" #include "qpid/Exception.h" #include "qpid/log/Statement.h" #include "qpid/sys/SecurityLayer.h" @@ -61,6 +61,7 @@ void Connector::registerFactory(const std::string& proto, Factory* connectorFact QPID_LOG(error, "Tried to register protocol: " << proto << " more than once"); } theProtocolRegistry()[proto] = connectorFactory; + Url::addProtocol(proto); } void Connector::activateSecurityLayer(std::auto_ptr<qpid::sys::SecurityLayer>) diff --git a/cpp/src/qpid/client/FailoverManager.cpp b/cpp/src/qpid/client/FailoverManager.cpp index 81f71eb7df..9405765b47 100644 --- a/cpp/src/qpid/client/FailoverManager.cpp +++ b/cpp/src/qpid/client/FailoverManager.cpp @@ -104,12 +104,11 @@ void FailoverManager::attempt(Connection& c, ConnectionSettings s, std::vector<U } else { for (std::vector<Url>::const_iterator i = urls.begin(); i != urls.end() && !c.isOpen(); ++i) { for (Url::const_iterator j = i->begin(); j != i->end() && !c.isOpen(); ++j) { - const TcpAddress* tcp = j->get<TcpAddress>(); - if (tcp) { - s.host = tcp->host; - s.port = tcp->port; - attempt(c, s); - } + const Address& addr = *j; + s.protocol = addr.protocol; + s.host = addr.host; + s.port = addr.port; + attempt(c, s); } } } diff --git a/cpp/src/qpid/cluster/MemberSet.cpp b/cpp/src/qpid/cluster/MemberSet.cpp index 0fdf4a8f96..97748947b3 100644 --- a/cpp/src/qpid/cluster/MemberSet.cpp +++ b/cpp/src/qpid/cluster/MemberSet.cpp @@ -20,6 +20,7 @@ */ #include "MemberSet.h" #include <ostream> +#include <iterator> #include <algorithm> namespace qpid { diff --git a/cpp/src/qpid/sys/posix/SystemInfo.cpp b/cpp/src/qpid/sys/posix/SystemInfo.cpp index 3c11b04d29..a19ab6885c 100755 --- a/cpp/src/qpid/sys/posix/SystemInfo.cpp +++ b/cpp/src/qpid/sys/posix/SystemInfo.cpp @@ -50,7 +50,7 @@ long SystemInfo::concurrency() { #endif } -bool SystemInfo::getLocalHostname (TcpAddress &address) { +bool SystemInfo::getLocalHostname (Address &address) { char name[HOST_NAME_MAX]; if (::gethostname(name, sizeof(name)) != 0) return false; @@ -59,6 +59,7 @@ bool SystemInfo::getLocalHostname (TcpAddress &address) { } static const string LOCALHOST("127.0.0.1"); +static const string TCP("tcp"); void SystemInfo::getLocalIpAddresses (uint16_t port, std::vector<Address> &addrList) { @@ -83,7 +84,7 @@ void SystemInfo::getLocalIpAddresses (uint16_t port, } string addr(dispName); if (addr != LOCALHOST) { - addrList.push_back(TcpAddress(addr, port)); + addrList.push_back(Address(TCP, addr, port)); } break; } @@ -97,7 +98,7 @@ void SystemInfo::getLocalIpAddresses (uint16_t port, freeifaddrs(ifaddr); if (addrList.empty()) { - addrList.push_back(TcpAddress(LOCALHOST, port)); + addrList.push_back(Address(TCP, LOCALHOST, port)); } } diff --git a/cpp/src/qpid/sys/solaris/SystemInfo.cpp b/cpp/src/qpid/sys/solaris/SystemInfo.cpp index 0075a89021..765e5a7eb0 100755 --- a/cpp/src/qpid/sys/solaris/SystemInfo.cpp +++ b/cpp/src/qpid/sys/solaris/SystemInfo.cpp @@ -49,7 +49,7 @@ long SystemInfo::concurrency() { return sysconf(_SC_NPROCESSORS_ONLN); } -bool SystemInfo::getLocalHostname(TcpAddress &address) { +bool SystemInfo::getLocalHostname(Address &address) { char name[MAXHOSTNAMELEN]; if (::gethostname(name, sizeof(name)) != 0) return false; @@ -58,6 +58,7 @@ bool SystemInfo::getLocalHostname(TcpAddress &address) { } static const string LOCALHOST("127.0.0.1"); +static const string TCP("tcp"); void SystemInfo::getLocalIpAddresses(uint16_t port, std::vector<Address> &addrList) { @@ -71,10 +72,10 @@ void SystemInfo::getLocalIpAddresses(uint16_t port, struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.lifr_addr; std::string addr(inet_ntoa(sin->sin_addr)); if (addr != LOCALHOST) - addrList.push_back(TcpAddress(addr, port)); + addrList.push_back(Address(TCP, addr, port)); } if (addrList.empty()) { - addrList.push_back(TcpAddress(LOCALHOST, port)); + addrList.push_back(Address(TCP, LOCALHOST, port)); } close (s); } diff --git a/cpp/src/qpid/sys/ssl/util.cpp b/cpp/src/qpid/sys/ssl/util.cpp index 53326e2f55..3078e894df 100644 --- a/cpp/src/qpid/sys/ssl/util.cpp +++ b/cpp/src/qpid/sys/ssl/util.cpp @@ -38,13 +38,15 @@ namespace qpid { namespace sys { namespace ssl { +static const std::string LOCALHOST("127.0.0.1"); + std::string defaultCertName() { - TcpAddress address; + Address address; if (SystemInfo::getLocalHostname(address)) { return address.host; } else { - return "localhost"; + return LOCALHOST; } } diff --git a/cpp/src/qpid/sys/windows/SystemInfo.cpp b/cpp/src/qpid/sys/windows/SystemInfo.cpp index ea53fc199c..4da440bdd4 100755 --- a/cpp/src/qpid/sys/windows/SystemInfo.cpp +++ b/cpp/src/qpid/sys/windows/SystemInfo.cpp @@ -51,7 +51,7 @@ long SystemInfo::concurrency() { return activeProcessors; } -bool SystemInfo::getLocalHostname (TcpAddress &address) { +bool SystemInfo::getLocalHostname (Address &address) { char name[HOST_NAME_MAX]; if (::gethostname(name, sizeof(name)) != 0) { errno = WSAGetLastError(); @@ -61,10 +61,12 @@ bool SystemInfo::getLocalHostname (TcpAddress &address) { return true; } +static const std::string LOCALHOST("127.0.0.1"); +static const std::string TCP("tcp"); + void SystemInfo::getLocalIpAddresses (uint16_t port, std::vector<Address> &addrList) { enum { MAX_URL_INTERFACES = 100 }; - static const std::string LOCALHOST("127.0.0.1"); SOCKET s = socket (PF_INET, SOCK_STREAM, 0); if (s != INVALID_SOCKET) { @@ -84,7 +86,7 @@ void SystemInfo::getLocalIpAddresses (uint16_t port, if (interfaces[i].iiFlags & IFF_UP) { std::string addr(inet_ntoa(interfaces[i].iiAddress.AddressIn.sin_addr)); if (addr != LOCALHOST) - addrList.push_back(TcpAddress(addr, port)); + addrList.push_back(Address(TCP, addr, port)); } } closesocket (s); diff --git a/cpp/src/tests/ClusterFixture.cpp b/cpp/src/tests/ClusterFixture.cpp index b7e8e88abf..04a5d35b06 100644 --- a/cpp/src/tests/ClusterFixture.cpp +++ b/cpp/src/tests/ClusterFixture.cpp @@ -153,7 +153,7 @@ std::set<int> knownBrokerPorts(qpid::client::Connection& c, int n) { } std::set<int> s; for (std::vector<qpid::Url>::const_iterator i = urls.begin(); i != urls.end(); ++i) - s.insert((*i)[0].get<qpid::TcpAddress>()->port); + s.insert((*i)[0].port); return s; } diff --git a/cpp/src/tests/RetryList.cpp b/cpp/src/tests/RetryList.cpp index d1d22348a3..50cd5edfe8 100644 --- a/cpp/src/tests/RetryList.cpp +++ b/cpp/src/tests/RetryList.cpp @@ -33,7 +33,7 @@ struct RetryListFixture { RetryList list; std::vector<Url> urls; - std::vector<TcpAddress> expected; + std::vector<Address> expected; void addUrl(const std::string& s) { @@ -42,15 +42,15 @@ struct RetryListFixture void addExpectation(const std::string& host, uint16_t port) { - expected.push_back(TcpAddress(host, port)); + expected.push_back(Address("tcp", host, port)); } void check() { list.reset(urls); for (int t = 0; t < 2; t++) { - TcpAddress next; - for (std::vector<TcpAddress>::const_iterator i = expected.begin(); i != expected.end(); ++i) { + Address next; + for (std::vector<Address>::const_iterator i = expected.begin(); i != expected.end(); ++i) { BOOST_CHECK(list.next(next)); BOOST_CHECK_EQUAL(i->host, next.host); BOOST_CHECK_EQUAL(i->port, next.port); diff --git a/cpp/src/tests/Url.cpp b/cpp/src/tests/Url.cpp index cd5169a21d..df1852dd1d 100644 --- a/cpp/src/tests/Url.cpp +++ b/cpp/src/tests/Url.cpp @@ -37,38 +37,37 @@ QPID_AUTO_TEST_SUITE(UrlTestSuite) QPID_AUTO_TEST_CASE(TestParseTcp) { URL_CHECK_STR("amqp:tcp:host:42"); URL_CHECK_STR("amqp:tcp:host-._~%ff%23:42"); // unreserved chars and pct encoded hex. - // Check defaults BOOST_CHECK_EQUAL(Url("amqp:host:42").str(), "amqp:tcp:host:42"); BOOST_CHECK_EQUAL(Url("amqp:tcp:host").str(), "amqp:tcp:host:5672"); + BOOST_CHECK_EQUAL(Url("host").str(), "amqp:tcp:host:5672"); +} +QPID_AUTO_TEST_CASE(TestParseInvalid) { //host is required: URL_CHECK_INVALID("amqp:tcp:"); URL_CHECK_INVALID("amqp:"); URL_CHECK_INVALID("amqp::42"); + URL_CHECK_INVALID(""); - URL_CHECK_INVALID("amqp::badHost!#$#"); - URL_CHECK_INVALID("amqp::host:badPort"); + // Port must be numeric + URL_CHECK_INVALID("host:badPort"); } -QPID_AUTO_TEST_CASE(TestParseExample) { - URL_CHECK_STR("amqp:example:x"); - URL_CHECK_INVALID("amqp:example:badExample"); +QPID_AUTO_TEST_CASE(TestParseXyz) { + Url::addProtocol("xyz"); + URL_CHECK_STR("amqp:xyz:host:123"); + BOOST_CHECK_EQUAL(Url("xyz:host").str(), "amqp:xyz:host:5672"); } QPID_AUTO_TEST_CASE(TestParseMultiAddress) { - URL_CHECK_STR("amqp:tcp:host:0,example:y,tcp:foo:0,example:1"); - URL_CHECK_STR("amqp:example:z,tcp:foo:0"); + Url::addProtocol("xyz"); + URL_CHECK_STR("amqp:tcp:host:0,xyz:foo:123,tcp:foo:0,xyz:bar:1"); + URL_CHECK_STR("amqp:xyz:foo:222,tcp:foo:0"); URL_CHECK_INVALID("amqp:tcp:h:0,"); URL_CHECK_INVALID(",amqp:tcp:h"); } - -QPID_AUTO_TEST_CASE(TestInvalidAddress) { - URL_CHECK_INVALID("xxxx"); - URL_CHECK_INVALID(""); -} - QPID_AUTO_TEST_SUITE_END() }} // namespace qpid::tests diff --git a/cpp/src/tests/ssl_test b/cpp/src/tests/ssl_test index 36b332f868..1564abb5f5 100755 --- a/cpp/src/tests/ssl_test +++ b/cpp/src/tests/ssl_test @@ -78,5 +78,11 @@ export QPID_NO_MODULE_DIR=1 export QPID_LOAD_MODULE=$SSLCONNECTOR_LIB export QPID_SSL_CERT_DB=${CERT_DIR} export QPID_SSL_CERT_PASSWORD_FILE=${CERT_PW_FILE} +# Test connection via connection settings ./perftest --count ${COUNT} --port ${PORT} -P ssl -b $TEST_HOSTNAME --summary +# Test connection with a URL +URL=amqp:ssl:$TEST_HOSTNAME:$PORT +./qpid_send -b $URL --content-string=hello -a "foo;{create:always}" +MSG=`./qpid_receive -b $URL -a "foo;{create:always}" --messages 1` +test "$MSG" = "hello" || { echo "receive failed '$MSG' != 'hello'"; exit 1; } |