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
21namespace Poco {
22namespace Net {
23namespace Impl {
24
25
26//
27// SocketAddressImpl
28//
29
30
31SocketAddressImpl::SocketAddressImpl()
32{
33}
34
35
36SocketAddressImpl::~SocketAddressImpl()
37{
38}
39
40
41//
42// IPv4SocketAddressImpl
43//
44
45
46IPv4SocketAddressImpl::IPv4SocketAddressImpl()
47{
48 std::memset(&_addr, 0, sizeof(_addr));
49 _addr.sin_family = AF_INET;
50 poco_set_sin_len(&_addr);
51}
52
53
54IPv4SocketAddressImpl::IPv4SocketAddressImpl(const struct sockaddr_in* addr)
55{
56 std::memcpy(&_addr, addr, sizeof(_addr));
57}
58
59
60IPv4SocketAddressImpl::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
70std::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
88IPv6SocketAddressImpl::IPv6SocketAddressImpl(const struct sockaddr_in6* addr)
89{
90 std::memcpy(&_addr, addr, sizeof(_addr));
91}
92
93
94IPv6SocketAddressImpl::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
104IPv6SocketAddressImpl::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
115std::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
138LocalSocketAddressImpl::LocalSocketAddressImpl(const struct sockaddr_un* addr, poco_socklen_t length)
139{
140 _pAddr = new sockaddr_un;
141 std::memcpy(_pAddr, addr, sizeof(struct sockaddr_un));
142 _addressLength = length ? length : sizeof(struct sockaddr_un);
143}
144
145
146LocalSocketAddressImpl::LocalSocketAddressImpl(const std::string& path)
147{
148 poco_assert(path.length() < sizeof(_pAddr->sun_path) && !path.empty());
149
150 _pAddr = new sockaddr_un;
151 std::memset(_pAddr, 0, sizeof(struct sockaddr_un));
152
153 _pAddr->sun_family = AF_UNIX;
154 std::memcpy(_pAddr->sun_path, path.data(), path.size());
155
156 _addressLength = path.size() + offsetof(struct sockaddr_un, sun_path);
157 if (path[0] != 0) _addressLength++;
158 poco_set_sun_len(_pAddr, _addressLength);
159}
160
161
162LocalSocketAddressImpl::~LocalSocketAddressImpl()
163{
164 delete _pAddr;
165}
166
167
168std::string LocalSocketAddressImpl::toString() const
169{
170 std::size_t len = _addressLength - offsetof(struct sockaddr_un, sun_path);
171 if (path()[0] != 0) len--;
172 return std::string(path(), len);
173}
174
175
176#endif // POCO_OS_FAMILY_UNIX
177
178
179} } } // namespace Poco::Net::Impl
180