@@ -85,29 +85,29 @@ class IP4Srv {
85
85
*/
86
86
bool load (swoc::TextView text);
87
87
88
- /* * Change the address.
88
+ /* * Assign an IPv4 address.
89
89
*
90
90
* @param addr Address to assign.
91
91
* @return @a this
92
92
*/
93
93
self_type &assign (IP4Addr const &addr);
94
94
95
- /* * Change the host_order_port .
95
+ /* * Assign a port .
96
96
*
97
- * @param port Port to assign.
97
+ * @param port Port to assign (host order)
98
98
* @return @a this.
99
99
*/
100
100
self_type &assign (in_port_t port);
101
101
102
- /* * Change the address and port.
102
+ /* * Assign an address and port.
103
103
*
104
104
* @param addr Address to assign.
105
- * @param port Port to assign.
105
+ * @param port Port to assign (host order) .
106
106
* @return @a this
107
107
*/
108
108
self_type &assign (IP4Addr const &addr, in_port_t port);
109
109
110
- /* * Change the address and port.
110
+ /* * Assign an address and port from an IPv4 socket address .
111
111
*
112
112
* @param s A socket address.
113
113
* @return @a this
@@ -116,7 +116,7 @@ class IP4Srv {
116
116
117
117
protected:
118
118
IP4Addr _addr; // /< Address.
119
- in_port_t _port = 0 ; // /< Port.
119
+ in_port_t _port = 0 ; // /< Port [host order] .
120
120
};
121
121
122
122
// / An IPv6 address and host_order_port, modeled on an SRV type for DNS.
@@ -193,17 +193,17 @@ class IP6Srv {
193
193
*/
194
194
self_type &assign (IP6Addr const &addr);
195
195
196
- /* * Change the host_order_port .
196
+ /* * Assign a port .
197
197
*
198
- * @param port Port to assign .
198
+ * @param port Port [host order] .
199
199
* @return @a this.
200
200
*/
201
201
self_type &assign (in_port_t port);
202
202
203
- /* * Change the address and host_order_port .
203
+ /* * Assign an address and port .
204
204
*
205
- * @param addr Address to assign .
206
- * @param port Port to assign .
205
+ * @param addr Address.
206
+ * @param port Port [host order] .
207
207
* @return @a this
208
208
*/
209
209
self_type &assign (IP6Addr const &addr, in_port_t port);
@@ -217,7 +217,7 @@ class IP6Srv {
217
217
218
218
protected:
219
219
IP6Addr _addr; // /< Address.
220
- in_port_t _port = 0 ; // /< Port.
220
+ in_port_t _port = 0 ; // /< Port [host order]
221
221
};
222
222
223
223
// / An IP address and host_order_port, modeled on an SRV type for DNS.
@@ -228,11 +228,17 @@ class IPSrv {
228
228
public:
229
229
IPSrv () = default ; // /< Default constructor.
230
230
explicit IPSrv (IP4Addr addr, in_port_t port = 0 ) : _srv(IP4Srv{addr, port}), _family(addr.family()) {}
231
+ // / Construct for IPv6 address and port.
231
232
explicit IPSrv (IP6Addr addr, in_port_t port = 0 ) : _srv(IP6Srv{addr, port}), _family(addr.family()) {}
233
+ // / Construct from generic address and port.
232
234
explicit IPSrv (IPAddr addr, in_port_t port = 0 );
235
+ // / Construct from socket address.
233
236
explicit IPSrv (sockaddr const *sa);
237
+ // / Construct IPv4 service from socket address.
234
238
explicit IPSrv (sockaddr_in const *s);
239
+ // / Construct IPv6 service from socket address.
235
240
explicit IPSrv (sockaddr_in6 const *s);
241
+ // / Construct from Endpoint.
236
242
explicit IPSrv (IPEndpoint const &ep);
237
243
238
244
/* * Construct from a string.
@@ -259,21 +265,17 @@ class IPSrv {
259
265
// / @return The protocol of the current value.
260
266
constexpr sa_family_t family () const ;
261
267
268
+ // / @return @c true if this is a valid service, @c false if not.
269
+ bool is_valid () const ;
262
270
// / @return @c true if the data is IPv4, @c false if not.
263
271
bool is_ip4 () const ;
264
272
// / @return @c true if hte data is IPv6, @c false if not.
265
273
bool is_ip6 () const ;
266
274
267
275
// / @return The IPv4 data.
268
- IP4Srv const &
269
- ip4 () const {
270
- return _srv._ip4 ;
271
- }
276
+ IP4Srv const & ip4 () const ;
272
277
// / @return The IPv6 data.
273
- IP6Srv const &
274
- ip6 () const {
275
- return _srv._ip6 ;
276
- }
278
+ IP6Srv const & ip6 () const ;
277
279
278
280
/* * Change the address.
279
281
*
@@ -289,88 +291,91 @@ class IPSrv {
289
291
*/
290
292
self_type &assign (IP6Addr const &addr);
291
293
292
- /* * Change the address.
294
+ /* * Assign an address.
293
295
*
294
- * @param addr Address to assign .
296
+ * @param addr Address.
295
297
* @return @a this
296
298
*
297
- * If @a addr isn't valid then no assignment is made.
299
+ * If @a addr isn't valid then no assignment is made, otherwise the family is changed to that of
300
+ * @a addr.
298
301
*/
299
302
self_type &assign (IPAddr const &addr);
300
303
301
- /* * Change the host_order_port .
304
+ /* * Assign port .
302
305
*
303
- * @param port Port in host order.
306
+ * @param port Port [ host order] .
304
307
* @return @a this.
305
308
*/
306
309
self_type &assign (in_port_t port);
307
310
308
- /* * Change the address and port.
311
+ /* * Assign an IPv4 address and port.
309
312
*
310
- * @param addr Address to assign .
311
- * @param port Port to assign .
313
+ * @param addr Address.
314
+ * @param port Port [host order] .
312
315
* @return @a this
313
316
*/
314
317
self_type &assign (IP4Addr const &addr, in_port_t port);
315
318
316
- /* * Change the address and port.
319
+ /* * Assign an IPv6 address and port.
317
320
*
318
- * @param addr Address to assign .
319
- * @param port Port to assign .
321
+ * @param addr Address.
322
+ * @param port Port [host order] .
320
323
* @return @a this
321
324
*/
322
325
self_type &assign (IP6Addr const &addr, in_port_t port);
323
326
324
- /* * Change the address and port.
327
+ /* * Assogm address amd [prt/
325
328
*
326
329
* @param sa Socket address.
327
330
* @return @a this
331
+ *
332
+ * The assignment is ignored if @a sa is not a valid IP family, otherwise the family is changed
333
+ * to that of @a sa.
328
334
*/
329
335
self_type &assign (sockaddr const *sa);
330
336
331
- /* * Change the address and port.
337
+ /* * Assign an IPv4 address and port.
332
338
*
333
339
* @param s Socket address.
334
340
* @return @a this
335
341
*/
336
342
self_type &assign (sockaddr_in const *s);
337
343
338
- /* * Change the address and port.
344
+ /* * Assign an IPv6 address and port.
339
345
*
340
346
* @param s Socket address.
341
347
* @return @a this
342
348
*/
343
349
self_type &assign (sockaddr_in6 const *s);
344
350
345
- /* * Change the address and host_order_port .
351
+ /* * Assign an address and port .
346
352
*
347
- * @param addr Address to assign .
348
- * @param port Port to assign .
353
+ * @param addr Address.
354
+ * @param port Port [host order] .
349
355
* @return @a this
350
356
*
351
- * If @a addr isn't valid then no assignment is made.
357
+ * If @a addr isn't valid then no assignment is made, otherwise the family is changed to match
358
+ * @a addr.
352
359
*/
353
360
self_type &assign (IPAddr const &addr, in_port_t port);
354
361
362
+ // / Copy assignment.
355
363
self_type &operator =(self_type const &that) = default ;
364
+ // / Assign from IPv4.
356
365
self_type &operator =(IP4Srv const &that);
366
+ // / Assign from IPv6.
357
367
self_type &operator =(IP6Srv const &that);
358
- self_type &
359
- operator =(sockaddr const *sa) {
360
- return this ->assign (sa);
361
- }
362
- self_type &
363
- operator =(sockaddr_in const *s) {
364
- return this ->assign (s);
365
- }
366
- self_type &
367
- operator =(sockaddr_in6 const *s) {
368
- return this ->assign (s);
369
- }
368
+ // / Assign from generic socket address.
369
+ self_type & operator =(sockaddr const *sa);
370
+ // / Assign from IPv4 socket address.
371
+ self_type & operator =(sockaddr_in const *s);
372
+ // / Assign from IPv6 socket address.
373
+ self_type & operator =(sockaddr_in6 const *s);
370
374
371
375
protected:
372
376
// / Family specialized data.
373
377
union data {
378
+ std::monostate _nil; // /< Nil / invalid state.
374
379
IP4Srv _ip4; // /< IPv4 address (host)
375
380
IP6Srv _ip6; // /< IPv6 address (host)
376
381
@@ -555,19 +560,42 @@ inline IPAddr
555
560
IPSrv::addr () const {
556
561
return _srv.addr (_family);
557
562
}
563
+
558
564
inline constexpr sa_family_t
559
565
IPSrv::family () const {
560
566
return _family;
561
567
}
568
+
569
+ inline bool IPSrv::is_valid () const { return AF_INET == _family || AF_INET6 == _family; }
570
+
562
571
inline bool
563
572
IPSrv::is_ip4 () const {
564
573
return _family == AF_INET;
565
574
}
575
+
566
576
inline bool
567
577
IPSrv::is_ip6 () const {
568
578
return _family == AF_INET6;
569
579
}
570
580
581
+ inline IP4Srv const & IPSrv::ip4 () const {
582
+ return _srv._ip4 ;
583
+ }
584
+
585
+ inline IP6Srv const & IPSrv::ip6 () const {
586
+ return _srv._ip6 ;
587
+ }
588
+
589
+ inline constexpr in_port_t
590
+ IPSrv::host_order_port () const {
591
+ return _srv.port (_family);
592
+ }
593
+
594
+ inline in_port_t
595
+ IPSrv::network_order_port () const {
596
+ return ntohs (_srv.port (_family));
597
+ }
598
+
571
599
inline auto
572
600
IPSrv::assign (IP6Addr const &addr) -> self_type & {
573
601
_srv._ip6 .assign (addr, this ->host_order_port ());
@@ -656,14 +684,16 @@ IPSrv::assign(sockaddr_in6 const *s) -> self_type & {
656
684
return *this ;
657
685
}
658
686
659
- inline constexpr in_port_t
660
- IPSrv::host_order_port () const {
661
- return _srv.port (_family);
687
+ inline IPSrv::self_type & IPSrv::operator =(sockaddr const *sa) {
688
+ return this ->assign (sa);
662
689
}
663
690
664
- inline in_port_t
665
- IPSrv::network_order_port () const {
666
- return ntohs (_srv.port (_family));
691
+ inline IPSrv::self_type & IPSrv::operator =(sockaddr_in const *s) {
692
+ return this ->assign (s);
693
+ }
694
+
695
+ inline IPSrv::self_type & IPSrv::operator =(sockaddr_in6 const *s) {
696
+ return this ->assign (s);
667
697
}
668
698
669
699
inline IPAddr
@@ -675,6 +705,7 @@ constexpr inline in_port_t
675
705
IPSrv::data::port (sa_family_t f) const {
676
706
return (f == AF_INET) ? _ip4.host_order_port () : (f == AF_INET6) ? _ip6.host_order_port () : 0 ;
677
707
}
708
+
678
709
// --- Independent comparisons.
679
710
680
711
inline bool
0 commit comments