Line data Source code
1 : // Copyright 2025 Accenture. 2 : 3 : #pragma once 4 : 5 : #include "ip/IPAddress.h" 6 : 7 : namespace ip 8 : { 9 : /** 10 : * An IPEndpoint combines an IPAddress and a port to represent 11 : * a complete endpoint. 12 : */ 13 : class IPEndpoint 14 : { 15 : public: 16 4 : IPEndpoint() : _address(), _port(PORT_DEFAULT_VALUE), _flags(0U) {} 17 : 18 13 : IPEndpoint(IPAddress const& addr, uint16_t const port) 19 13 : : _address(addr), _port(port), _flags(PORT_IS_SET) 20 : {} 21 : 22 : IPEndpoint(IPEndpoint const& other) = default; 23 : 24 10 : bool isSet() const 25 : { 26 10 : bool const isPortSet = (_flags & PORT_IS_SET) != 0; 27 10 : return isPortSet && (!isUnspecified(_address)); 28 : } 29 : 30 2 : void clear() 31 : { 32 2 : _address = IPAddress(); 33 2 : _port = PORT_DEFAULT_VALUE; 34 2 : _flags = 0U; 35 : } 36 : 37 6 : IPAddress const& getAddress() const { return _address; } 38 : 39 4 : void setAddress(IPAddress const& addr) { _address = addr; } 40 : 41 9 : uint16_t getPort() const { return _port; } 42 : 43 4 : void setPort(uint16_t const port) 44 : { 45 4 : _port = port; 46 4 : _flags |= PORT_IS_SET; 47 : } 48 : 49 : IPEndpoint& operator=(IPEndpoint const& other) = default; 50 : 51 : friend bool operator==(IPEndpoint const& lhs, IPEndpoint const& rhs); 52 : friend bool operator!=(IPEndpoint const& lhs, IPEndpoint const& rhs); 53 : friend bool operator<(IPEndpoint const& lhs, IPEndpoint const& rhs); 54 : 55 : private: 56 : static uint16_t const PORT_DEFAULT_VALUE = 0U; 57 : static uint8_t const PORT_IS_SET = 0x01U; 58 : IPAddress _address; 59 : uint16_t _port; 60 : uint8_t _flags; 61 : }; 62 : 63 9 : inline bool operator==(IPEndpoint const& lhs, IPEndpoint const& rhs) 64 : { 65 9 : return (lhs._address == rhs._address) && (lhs._port == rhs._port) && (lhs._flags == rhs._flags); 66 : } 67 : 68 1 : inline bool operator!=(IPEndpoint const& lhs, IPEndpoint const& rhs) { return !(lhs == rhs); } 69 : 70 3 : inline bool operator<(IPEndpoint const& lhs, IPEndpoint const& rhs) 71 : { 72 3 : if (lhs._flags != rhs._flags) 73 : { 74 1 : return lhs._flags < rhs._flags; 75 : } 76 : 77 2 : if (lhs._port != rhs._port) 78 : { 79 1 : return lhs._port < rhs._port; 80 : } 81 : 82 1 : return IPAddressCompareLess()(lhs._address, rhs._address); 83 : } 84 : 85 : } // namespace ip