| 1 | // |
| 2 | // SocketAddressImpl.cpp |
| 3 | // |
| 4 | // Library: Net |
| 5 | // Package: NetCore |
| 6 | // Module: SocketAddressImpl |
| 7 | // |
| 8 | // Copyright (c) 2005-2011, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Net/SocketAddressImpl.h" |
| 16 | #include "Poco/Net/SocketDefs.h" |
| 17 | #include "Poco/NumberFormatter.h" |
| 18 | #include <cstring> |
| 19 | |
| 20 | |
| 21 | namespace Poco { |
| 22 | namespace Net { |
| 23 | namespace Impl { |
| 24 | |
| 25 | |
| 26 | // |
| 27 | // SocketAddressImpl |
| 28 | // |
| 29 | |
| 30 | |
| 31 | SocketAddressImpl::SocketAddressImpl() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | |
| 36 | SocketAddressImpl::~SocketAddressImpl() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | |
| 41 | // |
| 42 | // IPv4SocketAddressImpl |
| 43 | // |
| 44 | |
| 45 | |
| 46 | IPv4SocketAddressImpl::IPv4SocketAddressImpl() |
| 47 | { |
| 48 | std::memset(&_addr, 0, sizeof(_addr)); |
| 49 | _addr.sin_family = AF_INET; |
| 50 | poco_set_sin_len(&_addr); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | IPv4SocketAddressImpl::IPv4SocketAddressImpl(const struct sockaddr_in* addr) |
| 55 | { |
| 56 | std::memcpy(&_addr, addr, sizeof(_addr)); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | IPv4SocketAddressImpl::IPv4SocketAddressImpl(const void* addr, UInt16 port) |
| 61 | { |
| 62 | std::memset(&_addr, 0, sizeof(_addr)); |
| 63 | _addr.sin_family = AF_INET; |
| 64 | poco_set_sin_len(&_addr); |
| 65 | std::memcpy(&_addr.sin_addr, addr, sizeof(_addr.sin_addr)); |
| 66 | _addr.sin_port = port; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | std::string IPv4SocketAddressImpl::toString() const |
| 71 | { |
| 72 | std::string result; |
| 73 | result.append(host().toString()); |
| 74 | result.append(":" ); |
| 75 | NumberFormatter::append(result, ntohs(port())); |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | #if defined(POCO_HAVE_IPv6) |
| 81 | |
| 82 | |
| 83 | // |
| 84 | // IPv6SocketAddressImpl |
| 85 | // |
| 86 | |
| 87 | |
| 88 | IPv6SocketAddressImpl::IPv6SocketAddressImpl(const struct sockaddr_in6* addr) |
| 89 | { |
| 90 | std::memcpy(&_addr, addr, sizeof(_addr)); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | IPv6SocketAddressImpl::IPv6SocketAddressImpl(const void* addr, UInt16 port) |
| 95 | { |
| 96 | std::memset(&_addr, 0, sizeof(_addr)); |
| 97 | _addr.sin6_family = AF_INET6; |
| 98 | poco_set_sin6_len(&_addr); |
| 99 | std::memcpy(&_addr.sin6_addr, addr, sizeof(_addr.sin6_addr)); |
| 100 | _addr.sin6_port = port; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | IPv6SocketAddressImpl::IPv6SocketAddressImpl(const void* addr, UInt16 port, UInt32 scope) |
| 105 | { |
| 106 | std::memset(&_addr, 0, sizeof(_addr)); |
| 107 | _addr.sin6_family = AF_INET6; |
| 108 | poco_set_sin6_len(&_addr); |
| 109 | std::memcpy(&_addr.sin6_addr, addr, sizeof(_addr.sin6_addr)); |
| 110 | _addr.sin6_port = port; |
| 111 | _addr.sin6_scope_id = scope; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | std::string IPv6SocketAddressImpl::toString() const |
| 116 | { |
| 117 | std::string result; |
| 118 | result.append("[" ); |
| 119 | result.append(host().toString()); |
| 120 | result.append("]" ); |
| 121 | result.append(":" ); |
| 122 | NumberFormatter::append(result, ntohs(port())); |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | #endif // POCO_HAVE_IPv6 |
| 128 | |
| 129 | |
| 130 | #if defined(POCO_OS_FAMILY_UNIX) |
| 131 | |
| 132 | |
| 133 | // |
| 134 | // LocalSocketAddressImpl |
| 135 | // |
| 136 | |
| 137 | |
| 138 | LocalSocketAddressImpl::LocalSocketAddressImpl(const struct sockaddr_un* addr) |
| 139 | { |
| 140 | _pAddr = new sockaddr_un; |
| 141 | std::memcpy(_pAddr, addr, sizeof(struct sockaddr_un)); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | LocalSocketAddressImpl::LocalSocketAddressImpl(const char* path) |
| 146 | { |
| 147 | _pAddr = new sockaddr_un; |
| 148 | poco_set_sun_len(_pAddr, std::strlen(path) + sizeof(struct sockaddr_un) - sizeof(_pAddr->sun_path) + 1); |
| 149 | _pAddr->sun_family = AF_UNIX; |
| 150 | std::strcpy(_pAddr->sun_path, path); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | LocalSocketAddressImpl::~LocalSocketAddressImpl() |
| 155 | { |
| 156 | delete _pAddr; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | std::string LocalSocketAddressImpl::toString() const |
| 161 | { |
| 162 | std::string result(path()); |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | #endif // POCO_OS_FAMILY_UNIX |
| 168 | |
| 169 | |
| 170 | } } } // namespace Poco::Net::Impl |
| 171 | |