Coverage Report

Created: 2025-10-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Net/include/Poco/Net/SocketAddressImpl.h
Line
Count
Source
1
//
2
// SocketAddressImpl.h
3
//
4
// Library: Net
5
// Package: NetCore
6
// Module:  SocketAddressImpl
7
//
8
// Definition of the SocketAddressImpl class.
9
//
10
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Net_SocketAddressImpl_INCLUDED
18
#define Net_SocketAddressImpl_INCLUDED
19
20
21
#include "Poco/Net/Net.h"
22
#include "Poco/Net/SocketDefs.h"
23
#include "Poco/Net/IPAddress.h"
24
#include "Poco/RefCountedObject.h"
25
26
27
namespace Poco {
28
namespace Net {
29
namespace Impl {
30
31
32
class Net_API SocketAddressImpl : public Poco::RefCountedObject
33
{
34
public:
35
  using Family = AddressFamily::Family;
36
37
  virtual ~SocketAddressImpl();
38
39
  virtual IPAddress host() const = 0;
40
  virtual UInt16 port() const = 0;
41
  virtual poco_socklen_t length() const = 0;
42
  virtual const struct sockaddr* addr() const = 0;
43
  virtual int af() const = 0;
44
  virtual Family family() const = 0;
45
  virtual std::string toString() const = 0;
46
47
protected:
48
  SocketAddressImpl();
49
50
private:
51
  SocketAddressImpl(const SocketAddressImpl&);
52
  SocketAddressImpl& operator = (const SocketAddressImpl&);
53
};
54
55
56
class Net_API IPv4SocketAddressImpl: public SocketAddressImpl
57
{
58
public:
59
  IPv4SocketAddressImpl();
60
  IPv4SocketAddressImpl(const struct sockaddr_in* addr);
61
  IPv4SocketAddressImpl(const void* addr, UInt16 port);
62
  IPAddress host() const;
63
  UInt16 port() const;
64
  poco_socklen_t length() const;
65
  const struct sockaddr* addr() const;
66
  int af() const;
67
  Family family() const;
68
  std::string toString() const;
69
70
private:
71
  struct sockaddr_in _addr;
72
};
73
74
75
//
76
// inlines
77
//
78
79
inline IPAddress IPv4SocketAddressImpl::host() const
80
0
{
81
0
  return IPAddress(&_addr.sin_addr, sizeof(_addr.sin_addr));
82
0
}
83
84
85
inline UInt16 IPv4SocketAddressImpl::port() const
86
0
{
87
0
  return _addr.sin_port;
88
0
}
89
90
91
inline poco_socklen_t IPv4SocketAddressImpl::length() const
92
0
{
93
0
  return sizeof(_addr);
94
0
}
95
96
97
inline const struct sockaddr* IPv4SocketAddressImpl::addr() const
98
0
{
99
0
  return reinterpret_cast<const struct sockaddr*>(&_addr);
100
0
}
101
102
103
inline int IPv4SocketAddressImpl::af() const
104
0
{
105
0
  return _addr.sin_family;
106
0
}
107
108
109
inline SocketAddressImpl::Family IPv4SocketAddressImpl::family() const
110
0
{
111
0
  return AddressFamily::IPv4;
112
0
}
113
114
115
#if defined(POCO_HAVE_IPv6)
116
117
118
class Net_API IPv6SocketAddressImpl: public SocketAddressImpl
119
{
120
public:
121
  IPv6SocketAddressImpl(const struct sockaddr_in6* addr);
122
  IPv6SocketAddressImpl(const void* addr, UInt16 port);
123
  IPv6SocketAddressImpl(const void* addr, UInt16 port, UInt32 scope);
124
  IPAddress host() const;
125
  UInt16 port() const;
126
  poco_socklen_t length() const;
127
  const struct sockaddr* addr() const;
128
  int af() const;
129
  Family family() const;
130
  std::string toString() const;
131
132
private:
133
  struct sockaddr_in6 _addr;
134
};
135
136
137
//
138
// inlines
139
//
140
141
inline IPAddress IPv6SocketAddressImpl::host() const
142
0
{
143
0
  return IPAddress(&_addr.sin6_addr, sizeof(_addr.sin6_addr), _addr.sin6_scope_id);
144
0
}
145
146
147
inline UInt16 IPv6SocketAddressImpl::port() const
148
0
{
149
0
  return _addr.sin6_port;
150
0
}
151
152
153
inline poco_socklen_t IPv6SocketAddressImpl::length() const
154
0
{
155
0
  return sizeof(_addr);
156
0
}
157
158
159
inline const struct sockaddr* IPv6SocketAddressImpl::addr() const
160
0
{
161
0
  return reinterpret_cast<const struct sockaddr*>(&_addr);
162
0
}
163
164
165
inline int IPv6SocketAddressImpl::af() const
166
0
{
167
0
  return _addr.sin6_family;
168
0
}
169
170
171
inline SocketAddressImpl::Family IPv6SocketAddressImpl::family() const
172
0
{
173
0
  return AddressFamily::IPv6;
174
0
}
175
176
177
#endif // POCO_HAVE_IPv6
178
179
180
#if defined(POCO_HAS_UNIX_SOCKET)
181
182
183
class Net_API LocalSocketAddressImpl: public SocketAddressImpl
184
{
185
public:
186
  LocalSocketAddressImpl(const struct sockaddr_un* addr);
187
  LocalSocketAddressImpl(const char* path);
188
  LocalSocketAddressImpl(const char* path, std::size_t length);
189
  ~LocalSocketAddressImpl();
190
  IPAddress host() const;
191
  UInt16 port() const;
192
  poco_socklen_t length() const;
193
  const struct sockaddr* addr() const;
194
  int af() const;
195
  Family family() const;
196
  const char* path() const;
197
  std::string toString() const;
198
199
private:
200
  struct sockaddr_un* _pAddr;
201
    // Note: We allocate struct sockaddr_un on the heap, otherwise we would
202
    // waste a lot of memory due to small object optimization in SocketAddress.
203
};
204
205
206
//
207
// inlines
208
//
209
210
inline IPAddress LocalSocketAddressImpl::host() const
211
0
{
212
0
  throw Poco::InvalidAccessException("local socket address does not have host IP address");
213
0
}
214
215
216
inline UInt16 LocalSocketAddressImpl::port() const
217
0
{
218
0
  throw Poco::InvalidAccessException("local socket address does not have port number");
219
0
}
220
221
222
inline poco_socklen_t LocalSocketAddressImpl::length() const
223
0
{
224
0
  return sizeof(struct sockaddr_un);
225
0
}
226
227
228
inline const struct sockaddr* LocalSocketAddressImpl::addr() const
229
0
{
230
0
  return reinterpret_cast<const struct sockaddr*>(_pAddr);
231
0
}
232
233
234
inline int LocalSocketAddressImpl::af() const
235
0
{
236
0
  return _pAddr->sun_family;
237
0
}
238
239
240
inline SocketAddressImpl::Family LocalSocketAddressImpl::family() const
241
0
{
242
0
  return AddressFamily::UNIX_LOCAL;
243
0
}
244
245
246
inline const char* LocalSocketAddressImpl::path() const
247
0
{
248
0
  return _pAddr->sun_path;
249
0
}
250
251
252
#endif // POCO_OS_FAMILY_UNIX
253
254
255
} } } // namespace Poco::Net::Impl
256
257
258
#endif // Net_SocketAddressImpl_INCLUDED