SocketAddress.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // SocketAddress.cpp
  3. //
  4. // $Id: //poco/Main/Net/src/SocketAddress.cpp#13 $
  5. //
  6. // Library: Net
  7. // Package: NetCore
  8. // Module: SocketAddress
  9. //
  10. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // Permission is hereby granted, free of charge, to any person or organization
  14. // obtaining a copy of the software and accompanying documentation covered by
  15. // this license (the "Software") to use, reproduce, display, distribute,
  16. // execute, and transmit the Software, and to prepare derivative works of the
  17. // Software, and to permit third-parties to whom the Software is furnished to
  18. // do so, all subject to the following:
  19. //
  20. // The copyright notices in the Software and this entire statement, including
  21. // the above license grant, this restriction and the following disclaimer,
  22. // must be included in all copies of the Software, in whole or in part, and
  23. // all derivative works of the Software, unless such copies or derivative
  24. // works are solely in the form of machine-executable object code generated by
  25. // a source language processor.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  30. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  31. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  32. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. //
  35. #include "Poco/Net/SocketAddress.h"
  36. #include "Poco/Net/IPAddress.h"
  37. #include "Poco/Net/NetException.h"
  38. #include "Poco/Net/DNS.h"
  39. #include "Poco/RefCountedObject.h"
  40. #include "Poco/NumberParser.h"
  41. #include "Poco/NumberFormatter.h"
  42. #include <algorithm>
  43. #include <cstring>
  44. using Poco::RefCountedObject;
  45. using Poco::NumberParser;
  46. using Poco::NumberFormatter;
  47. using Poco::UInt16;
  48. using Poco::InvalidArgumentException;
  49. namespace Poco {
  50. namespace Net {
  51. //
  52. // SocketAddressImpl
  53. //
  54. class SocketAddressImpl: public RefCountedObject
  55. {
  56. public:
  57. virtual IPAddress host() const = 0;
  58. virtual UInt16 port() const = 0;
  59. virtual poco_socklen_t length() const = 0;
  60. virtual const struct sockaddr* addr() const = 0;
  61. virtual int af() const = 0;
  62. protected:
  63. SocketAddressImpl()
  64. {
  65. }
  66. virtual ~SocketAddressImpl()
  67. {
  68. }
  69. private:
  70. SocketAddressImpl(const SocketAddressImpl&);
  71. SocketAddressImpl& operator = (const SocketAddressImpl&);
  72. };
  73. class IPv4SocketAddressImpl: public SocketAddressImpl
  74. {
  75. public:
  76. IPv4SocketAddressImpl()
  77. {
  78. std::memset(&_addr, 0, sizeof(_addr));
  79. _addr.sin_family = AF_INET;
  80. poco_set_sin_len(&_addr);
  81. }
  82. IPv4SocketAddressImpl(const struct sockaddr_in* addr)
  83. {
  84. std::memcpy(&_addr, addr, sizeof(_addr));
  85. }
  86. IPv4SocketAddressImpl(const void* addr, UInt16 port)
  87. {
  88. std::memset(&_addr, 0, sizeof(_addr));
  89. _addr.sin_family = AF_INET;
  90. std::memcpy(&_addr.sin_addr, addr, sizeof(_addr.sin_addr));
  91. _addr.sin_port = port;
  92. }
  93. IPAddress host() const
  94. {
  95. return IPAddress(&_addr.sin_addr, sizeof(_addr.sin_addr));
  96. }
  97. UInt16 port() const
  98. {
  99. return _addr.sin_port;
  100. }
  101. poco_socklen_t length() const
  102. {
  103. return sizeof(_addr);
  104. }
  105. const struct sockaddr* addr() const
  106. {
  107. return reinterpret_cast<const struct sockaddr*>(&_addr);
  108. }
  109. int af() const
  110. {
  111. return _addr.sin_family;
  112. }
  113. private:
  114. struct sockaddr_in _addr;
  115. };
  116. #if defined(POCO_HAVE_IPv6)
  117. class IPv6SocketAddressImpl: public SocketAddressImpl
  118. {
  119. public:
  120. IPv6SocketAddressImpl(const struct sockaddr_in6* addr)
  121. {
  122. std::memcpy(&_addr, addr, sizeof(_addr));
  123. }
  124. IPv6SocketAddressImpl(const void* addr, UInt16 port)
  125. {
  126. std::memset(&_addr, 0, sizeof(_addr));
  127. _addr.sin6_family = AF_INET6;
  128. poco_set_sin6_len(&_addr);
  129. std::memcpy(&_addr.sin6_addr, addr, sizeof(_addr.sin6_addr));
  130. _addr.sin6_port = port;
  131. }
  132. IPAddress host() const
  133. {
  134. return IPAddress(&_addr.sin6_addr, sizeof(_addr.sin6_addr));
  135. }
  136. UInt16 port() const
  137. {
  138. return _addr.sin6_port;
  139. }
  140. poco_socklen_t length() const
  141. {
  142. return sizeof(_addr);
  143. }
  144. const struct sockaddr* addr() const
  145. {
  146. return reinterpret_cast<const struct sockaddr*>(&_addr);
  147. }
  148. int af() const
  149. {
  150. return _addr.sin6_family;
  151. }
  152. private:
  153. struct sockaddr_in6 _addr;
  154. };
  155. #endif // POCO_HAVE_IPv6
  156. //
  157. // SocketAddress
  158. //
  159. SocketAddress::SocketAddress()
  160. {
  161. _pImpl = new IPv4SocketAddressImpl;
  162. }
  163. SocketAddress::SocketAddress(const IPAddress& addr, Poco::UInt16 port)
  164. {
  165. init(addr, port);
  166. }
  167. SocketAddress::SocketAddress(const std::string& addr, Poco::UInt16 port)
  168. {
  169. init(addr, port);
  170. }
  171. SocketAddress::SocketAddress(const std::string& addr, const std::string& port)
  172. {
  173. init(addr, resolveService(port));
  174. }
  175. SocketAddress::SocketAddress(const std::string& hostAndPort)
  176. {
  177. poco_assert (!hostAndPort.empty());
  178. std::string host;
  179. std::string port;
  180. std::string::const_iterator it = hostAndPort.begin();
  181. std::string::const_iterator end = hostAndPort.end();
  182. if (*it == '[')
  183. {
  184. ++it;
  185. while (it != end && *it != ']') host += *it++;
  186. if (it == end) throw InvalidArgumentException("Malformed IPv6 address");
  187. ++it;
  188. }
  189. else
  190. {
  191. while (it != end && *it != ':') host += *it++;
  192. }
  193. if (it != end && *it == ':')
  194. {
  195. ++it;
  196. while (it != end) port += *it++;
  197. }
  198. else throw InvalidArgumentException("Missing port number");
  199. init(host, resolveService(port));
  200. }
  201. SocketAddress::SocketAddress(const SocketAddress& addr)
  202. {
  203. _pImpl = addr._pImpl;
  204. _pImpl->duplicate();
  205. }
  206. SocketAddress::SocketAddress(const struct sockaddr* addr, poco_socklen_t length)
  207. {
  208. if (length == sizeof(struct sockaddr_in))
  209. _pImpl = new IPv4SocketAddressImpl(reinterpret_cast<const struct sockaddr_in*>(addr));
  210. #if defined(POCO_HAVE_IPv6)
  211. else if (length == sizeof(struct sockaddr_in6))
  212. _pImpl = new IPv6SocketAddressImpl(reinterpret_cast<const struct sockaddr_in6*>(addr));
  213. #endif
  214. else throw Poco::InvalidArgumentException("Invalid address length passed to SocketAddress()");
  215. }
  216. SocketAddress::~SocketAddress()
  217. {
  218. _pImpl->release();
  219. }
  220. SocketAddress& SocketAddress::operator = (const SocketAddress& addr)
  221. {
  222. if (&addr != this)
  223. {
  224. _pImpl->release();
  225. _pImpl = addr._pImpl;
  226. _pImpl->duplicate();
  227. }
  228. return *this;
  229. }
  230. void SocketAddress::swap(SocketAddress& addr)
  231. {
  232. std::swap(_pImpl, addr._pImpl);
  233. }
  234. IPAddress SocketAddress::host() const
  235. {
  236. return _pImpl->host();
  237. }
  238. Poco::UInt16 SocketAddress::port() const
  239. {
  240. return ntohs(_pImpl->port());
  241. }
  242. poco_socklen_t SocketAddress::length() const
  243. {
  244. return _pImpl->length();
  245. }
  246. const struct sockaddr* SocketAddress::addr() const
  247. {
  248. return _pImpl->addr();
  249. }
  250. int SocketAddress::af() const
  251. {
  252. return _pImpl->af();
  253. }
  254. std::string SocketAddress::toString() const
  255. {
  256. std::string result;
  257. if (host().family() == IPAddress::IPv6)
  258. result.append("[");
  259. result.append(host().toString());
  260. if (host().family() == IPAddress::IPv6)
  261. result.append("]");
  262. result.append(":");
  263. result.append(NumberFormatter::format(port()));
  264. return result;
  265. }
  266. void SocketAddress::init(const IPAddress& host, Poco::UInt16 port)
  267. {
  268. if (host.family() == IPAddress::IPv4)
  269. _pImpl = new IPv4SocketAddressImpl(host.addr(), htons(port));
  270. #if defined(POCO_HAVE_IPv6)
  271. else if (host.family() == IPAddress::IPv6)
  272. _pImpl = new IPv6SocketAddressImpl(host.addr(), htons(port));
  273. #endif
  274. else throw Poco::NotImplementedException("unsupported IP address family");
  275. }
  276. void SocketAddress::init(const std::string& host, Poco::UInt16 port)
  277. {
  278. IPAddress ip;
  279. if (IPAddress::tryParse(host, ip))
  280. {
  281. init(ip, port);
  282. }
  283. else
  284. {
  285. HostEntry he = DNS::hostByName(host);
  286. if (he.addresses().size() > 0)
  287. init(he.addresses()[0], port);
  288. else throw HostNotFoundException("No address found for host", host);
  289. }
  290. }
  291. Poco::UInt16 SocketAddress::resolveService(const std::string& service)
  292. {
  293. unsigned port;
  294. if (NumberParser::tryParseUnsigned(service, port) && port <= 0xFFFF)
  295. {
  296. return (UInt16) port;
  297. }
  298. else
  299. {
  300. struct servent* se = getservbyname(service.c_str(), NULL);
  301. if (se)
  302. return ntohs(se->s_port);
  303. else
  304. throw ServiceNotFoundException(service);
  305. }
  306. }
  307. } } // namespace Poco::Net