Skip to content

Commit bb0a6ff

Browse files
Fix PR issues.
1 parent 53199cc commit bb0a6ff

2 files changed

Lines changed: 44 additions & 32 deletions

File tree

include/tscpp/util/ts_ip.h

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ class IPAddrPair
3737

3838
IPAddrPair() = default; ///< Default construct empty pair.
3939

40+
/** Construct with IPv4 address.
41+
*
42+
* @param a4 Address.
43+
*/
44+
IPAddrPair(swoc::IP4Addr a4);
45+
46+
/** Construct with IPv6 address.
47+
*
48+
* @param a6 Address.
49+
*/
50+
IPAddrPair(swoc::IP6Addr a6);
51+
4052
/// @return @c true if either address is present.
4153
bool has_value() const;
4254

@@ -82,6 +94,9 @@ class IPAddrPair
8294
std::optional<swoc::IP6Addr> _ip6;
8395
};
8496

97+
inline IPAddrPair::IPAddrPair(swoc::IP4Addr a4) : _ip4(a4) {}
98+
inline IPAddrPair::IPAddrPair(swoc::IP6Addr a6) : _ip6(a6) {}
99+
85100
inline bool
86101
IPAddrPair::has_value() const
87102
{
@@ -134,6 +149,7 @@ IPAddrPair::operator=(swoc::IPAddr const &addr) -> self_type &
134149
} else if (addr.is_ip6()) {
135150
_ip6 = addr.ip6();
136151
}
152+
137153
return *this;
138154
}
139155

@@ -161,14 +177,14 @@ class IPSrvPair
161177
* @param a4 IPv4 address
162178
* @param port Port.
163179
*/
164-
explicit IPSrvPair(swoc::IP4Addr const &a4, in_port_t port = 0);
180+
IPSrvPair(swoc::IP4Addr const &a4, in_port_t port = 0);
165181

166182
/** Construct from IPv6 address and optional port.
167183
*
168184
* @param a6 IPv6 address
169185
* @param port Port.
170186
*/
171-
explicit IPSrvPair(swoc::IP6Addr const &a6, in_port_t port = 0);
187+
IPSrvPair(swoc::IP6Addr const &a6, in_port_t port = 0);
172188

173189
/** Construct from an address pair and optional port.
174190
*
@@ -238,7 +254,9 @@ inline IPSrvPair::IPSrvPair(IPAddrPair const &a, in_port_t port)
238254
{
239255
if (a.has_ip4()) {
240256
_ip4 = swoc::IP4Srv(a.ip4(), port);
241-
} else if (a.has_ip6()) {
257+
}
258+
259+
if (a.has_ip6()) {
242260
_ip6 = swoc::IP6Srv(a.ip6(), port);
243261
}
244262
}
@@ -301,54 +319,46 @@ IPSrvPair::operator=(swoc::IPSrv const &srv) -> self_type &
301319
/** Get the best address info for @a name.
302320
303321
* @param name Address / host.
304-
* @param ip4 [out] Best IPv4 result, if any.
305-
* @param ip6 [out] Best IPv6 result, if any.
306-
* @return @c true if an address was found, @c false if not.
322+
* @return An address pair.
307323
*
308324
* If @a name is a valid IP address it is interpreted as such. Otherwise it is presumed
309325
* to be a host name suitable for resolution using @c getaddrinfo. The "best" address is
310-
* selected by ranking the types of addresss in the order
326+
* selected by ranking the types of addresses in the order
311327
*
312328
* - Global, multi-cast, non-routable (private), link local, loopback
313329
*
314-
* For a host name, both IPv4 and IPv6 addresses may be returned. Each is judged
315-
* independently.
316-
*
317-
* Either @a ip4 or @ip6 can be @c nullptr in which that result is discarded.
318-
*
319-
* @note If @name is known or required to be an IP address, use a standard address conversion
320-
* instead, e.g. @c IPAddr::load .
330+
* For a host name, an IPv4 and IPv6 address may be returned. The "best" is computed independently
331+
* for each family.
321332
*
322333
* @see getaddrinfo
334+
* @see ts::getbestsrvinfo
323335
*/
324336
IPAddrPair getbestaddrinfo(swoc::TextView name);
325337

326-
/** Get the best address info for @a name.
338+
/** Get the best address and port info for @a name.
327339
328340
* @param name Address / host.
329-
* @param ip4 [out] Best IPv4 result, if any.
330-
* @param ip6 [out] Best IPv6 result, if any.
331-
* @return @c true if an address was found, @c false if not.
341+
* @return An address pair.
332342
*
333-
* If @a name is a valid IP address it is interpreted as such. Otherwise it is presumed
334-
* to be a host name suitable for resolution using @c getaddrinfo. The "best" address is
335-
* selected by ranking the types of addresss in the order
343+
* If @a name is a valid IP address (with optional port) it is interpreted as such. Otherwise it is
344+
* presumed to be a host name (with optional port) suitable for resolution using @c getaddrinfo. The "best" address is
345+
* selected by ranking the types of addresses in the order
336346
*
337347
* - Global, multi-cast, non-routable (private), link local, loopback
338348
*
339-
* For a host name, both IPv4 and IPv6 addresses may be returned. Each is judged
340-
* independently.
341-
*
342-
* Either @a ip4 or @ip6 can be @c nullptr in which that result is discarded.
343-
*
344-
* @note If @name is known or required to be an IP address, use a standard address conversion
345-
* instead, e.g. @c IPAddr::load .
349+
* For a host name, an IPv4 and IPv6 service may be returned. The "best" is computed independently
350+
* for each family. The port, if present, is the same for all returned services.
346351
*
347352
* @see getaddrinfo
353+
* @see ts::getbestaddrinfo
348354
*/
349355
IPSrvPair getbestsrvinfo(swoc::TextView name);
350356

351-
/** An IPSpace that only tracks the presence of IP addresses.
357+
/** An IPSpace that contains only addresses.
358+
*
359+
* This is to @c IPSpace as @c std::set is to @c std::map. The @c value_type is removed from the API
360+
* and only the keys are visible. This suits use cases where the goal is to track the presence of
361+
* addresses without any additional data.
352362
*
353363
* @note Because there is only one value stored, there is no difference between @c mark and @c fill.
354364
*/

src/tscpp/util/ts_ip.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ namespace ts
3333
IPAddrPair
3434
getbestaddrinfo(swoc::TextView name)
3535
{
36+
// If @a name parses as a valid address, return it as that address.
37+
3638
if (swoc::IP4Addr addr; addr.load(name)) {
37-
return {};
39+
return addr;
3840
}
3941

4042
if (swoc::IP6Addr addr; addr.load(name)) {
41-
return {};
43+
return addr;
4244
}
4345

44-
// Copy to guarantee C string
46+
// Presume it is a host name, make a copy to guarantee C string.
4547
char *tmp = static_cast<char *>(alloca(name.size() + 1));
4648
memcpy(tmp, name.data(), name.size());
4749
tmp[name.size()] = 0;

0 commit comments

Comments
 (0)