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 7560 : IPEndpoint() : _address(), _port(PORT_DEFAULT_VALUE), _flags(0U) {}
25 :
26 1526 : IPEndpoint(IPAddress const& addr, uint16_t const port)
27 1526 : : _address(addr), _port(port), _flags(PORT_IS_SET)
28 1526 : {}
29 :
30 : IPEndpoint(IPEndpoint const& other) = default;
31 : IPEndpoint(IPEndpoint&& other) = default;
32 : ~IPEndpoint() = default;
33 :
34 525 : bool isSet() const
35 : {
36 525 : bool const isPortSet = (_flags & PORT_IS_SET) != 0;
37 525 : return isPortSet && (!isUnspecified(_address));
38 : }
39 :
40 127 : void clear()
41 : {
42 508 : _address = IPAddress();
43 127 : _port = PORT_DEFAULT_VALUE;
44 127 : _flags = 0U;
45 127 : }
46 :
47 495 : IPAddress const& getAddress() const { return _address; }
48 :
49 162 : void setAddress(IPAddress const& addr) { _address = addr; }
50 :
51 298 : uint16_t getPort() const { return _port; }
52 :
53 163 : void setPort(uint16_t const port)
54 : {
55 163 : _port = port;
56 163 : _flags |= PORT_IS_SET;
57 163 : }
58 :
59 : IPEndpoint& operator=(IPEndpoint const& other) = default;
60 : IPEndpoint& operator=(IPEndpoint&& other) = default;
61 :
62 : friend bool operator==(IPEndpoint const& lhs, IPEndpoint const& rhs);
63 : friend bool operator!=(IPEndpoint const& lhs, IPEndpoint const& rhs);
64 : friend bool operator<(IPEndpoint const& lhs, IPEndpoint const& rhs);
65 :
66 : private:
67 : static uint16_t const PORT_DEFAULT_VALUE = 0U;
68 : static uint8_t const PORT_IS_SET = 0x01U;
69 : IPAddress _address;
70 : uint16_t _port;
71 : uint8_t _flags;
72 : };
73 :
74 894 : inline bool operator==(IPEndpoint const& lhs, IPEndpoint const& rhs)
75 : {
76 894 : return (lhs._address == rhs._address) && (lhs._port == rhs._port) && (lhs._flags == rhs._flags);
77 : }
78 :
79 7 : inline bool operator!=(IPEndpoint const& lhs, IPEndpoint const& rhs) { return !(lhs == rhs); }
80 :
81 3 : inline bool operator<(IPEndpoint const& lhs, IPEndpoint const& rhs)
82 : {
83 3 : if (lhs._flags != rhs._flags)
84 : {
85 1 : return lhs._flags < rhs._flags;
86 : }
87 :
88 2 : if (lhs._port != rhs._port)
89 : {
90 1 : return lhs._port < rhs._port;
91 : }
92 :
93 1 : return IPAddressCompareLess()(lhs._address, rhs._address);
94 : }
95 :
96 : } // namespace ip
|