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 197 : IPEndpoint() : _address(), _port(PORT_DEFAULT_VALUE), _flags(0U) {}
25 :
26 485 : IPEndpoint(IPAddress const& addr, uint16_t const port)
27 485 : : _address(addr), _port(port), _flags(PORT_IS_SET)
28 485 : {}
29 :
30 : IPEndpoint(IPEndpoint const& other) = default;
31 :
32 12 : bool isSet() const
33 : {
34 12 : bool const isPortSet = (_flags & PORT_IS_SET) != 0;
35 12 : return isPortSet && (!isUnspecified(_address));
36 : }
37 :
38 2 : void clear()
39 : {
40 2 : _address = IPAddress();
41 2 : _port = PORT_DEFAULT_VALUE;
42 2 : _flags = 0U;
43 2 : }
44 :
45 162 : IPAddress const& getAddress() const { return _address; }
46 :
47 4 : void setAddress(IPAddress const& addr) { _address = addr; }
48 :
49 155 : uint16_t getPort() const { return _port; }
50 :
51 4 : void setPort(uint16_t const port)
52 : {
53 4 : _port = port;
54 4 : _flags |= PORT_IS_SET;
55 4 : }
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 211 : inline bool operator==(IPEndpoint const& lhs, IPEndpoint const& rhs)
72 : {
73 211 : return (lhs._address == rhs._address) && (lhs._port == rhs._port) && (lhs._flags == rhs._flags);
74 : }
75 :
76 1 : 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
|