Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2025 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #pragma once
12 :
13 : #include "ip/IPAddress.h"
14 :
15 : namespace ip
16 : {
17 : /**
18 : * An IPEndpoint combines an IPAddress and a port to represent
19 : * a complete endpoint.
20 : */
21 : class IPEndpoint
22 : {
23 : public:
24 7512 : IPEndpoint() : _address(), _port(PORT_DEFAULT_VALUE), _flags(0U) {}
25 :
26 1515 : IPEndpoint(IPAddress const& addr, uint16_t const port)
27 1515 : : _address(addr), _port(port), _flags(PORT_IS_SET)
28 1515 : {}
29 :
30 : IPEndpoint(IPEndpoint const& other) = default;
31 :
32 525 : bool isSet() const
33 : {
34 525 : bool const isPortSet = (_flags & PORT_IS_SET) != 0;
35 525 : return isPortSet && (!isUnspecified(_address));
36 : }
37 :
38 127 : void clear()
39 : {
40 508 : _address = IPAddress();
41 127 : _port = PORT_DEFAULT_VALUE;
42 127 : _flags = 0U;
43 127 : }
44 :
45 493 : IPAddress const& getAddress() const { return _address; }
46 :
47 162 : void setAddress(IPAddress const& addr) { _address = addr; }
48 :
49 295 : uint16_t getPort() const { return _port; }
50 :
51 163 : void setPort(uint16_t const port)
52 : {
53 163 : _port = port;
54 163 : _flags |= PORT_IS_SET;
55 163 : }
56 :
57 : IPEndpoint& operator=(IPEndpoint const& other) = default;
58 :
59 : friend bool operator==(IPEndpoint const& lhs, IPEndpoint const& rhs);
60 : friend bool operator!=(IPEndpoint const& lhs, IPEndpoint const& rhs);
61 : friend bool operator<(IPEndpoint const& lhs, IPEndpoint const& rhs);
62 :
63 : private:
64 : static uint16_t const PORT_DEFAULT_VALUE = 0U;
65 : static uint8_t const PORT_IS_SET = 0x01U;
66 : IPAddress _address;
67 : uint16_t _port;
68 : uint8_t _flags;
69 : };
70 :
71 887 : inline bool operator==(IPEndpoint const& lhs, IPEndpoint const& rhs)
72 : {
73 887 : return (lhs._address == rhs._address) && (lhs._port == rhs._port) && (lhs._flags == rhs._flags);
74 : }
75 :
76 7 : inline bool operator!=(IPEndpoint const& lhs, IPEndpoint const& rhs) { return !(lhs == rhs); }
77 :
78 3 : inline bool operator<(IPEndpoint const& lhs, IPEndpoint const& rhs)
79 : {
80 3 : if (lhs._flags != rhs._flags)
81 : {
82 1 : return lhs._flags < rhs._flags;
83 : }
84 :
85 2 : if (lhs._port != rhs._port)
86 : {
87 1 : return lhs._port < rhs._port;
88 : }
89 :
90 1 : return IPAddressCompareLess()(lhs._address, rhs._address);
91 : }
92 :
93 : } // namespace ip
|