1 /* 2 * Copyright 2007-2008, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Hugo Santos, hugosantos@gmail.com 7 */ 8 9 #ifndef NET_ADDRESS_UTILITIES_H 10 #define NET_ADDRESS_UTILITIES_H 11 12 13 #include <NetUtilities.h> 14 #include <net_datalink.h> 15 16 #include <sys/socket.h> 17 18 19 class SocketAddress { 20 public: 21 SocketAddress(net_address_module_info *module, sockaddr *address) 22 : fModule(module), fAddress(address) {} 23 24 SocketAddress(net_address_module_info *module, sockaddr_storage *address) 25 : fModule(module), fAddress((sockaddr *)address) {} 26 27 SocketAddress(const SocketAddress &address) 28 : fModule(address.fModule), fAddress(address.fAddress) {} 29 30 void SetAddressTo(sockaddr *address) 31 { 32 fAddress = address; 33 } 34 35 bool IsEmpty(bool checkPort) const 36 { 37 return fModule->is_empty_address(fAddress, checkPort); 38 } 39 40 uint32 HashPair(const sockaddr *second) const 41 { 42 return fModule->hash_address_pair(fAddress, second); 43 } 44 45 bool EqualTo(const sockaddr *address, bool checkPort = false) const 46 { 47 if (checkPort) 48 return fModule->equal_addresses_and_ports(fAddress, address); 49 else 50 return fModule->equal_addresses(fAddress, address); 51 } 52 53 bool EqualTo(const SocketAddress &second, bool checkPort = false) const 54 { 55 return EqualTo(second.fAddress, checkPort); 56 } 57 58 bool EqualPorts(const sockaddr *second) const 59 { 60 return fModule->equal_ports(fAddress, second); 61 } 62 63 bool MatchMasked(const sockaddr *address, const sockaddr *mask) const 64 { 65 return fModule->equal_masked_addresses(fAddress, address, mask); 66 } 67 68 uint16 Port() const 69 { 70 return fModule->get_port(fAddress); 71 } 72 73 status_t SetTo(const sockaddr *from) 74 { 75 return fModule->set_to(fAddress, from); 76 } 77 78 status_t SetTo(const sockaddr_storage *from) 79 { 80 return SetTo((sockaddr *)from); 81 } 82 83 status_t CopyTo(sockaddr *to) const 84 { 85 return fModule->set_to(to, fAddress); 86 } 87 88 status_t CopyTo(sockaddr_storage *to) const 89 { 90 return CopyTo((sockaddr *)to); 91 } 92 93 status_t SetPort(uint16 port) 94 { 95 return fModule->set_port(fAddress, port); 96 } 97 98 status_t SetToEmpty() 99 { 100 return fModule->set_to_empty_address(fAddress); 101 } 102 103 status_t UpdateTo(const sockaddr *from) 104 { 105 return fModule->update_to(fAddress, from); 106 } 107 108 AddressString AsString(bool printPort = false) const 109 { 110 return AddressString(fModule, fAddress, printPort); 111 } 112 113 const char *AsString(char *buffer, size_t bufferSize, 114 bool printPort = false) const { 115 fModule->print_address_buffer(fAddress, buffer, bufferSize, printPort); 116 return buffer; 117 } 118 119 const sockaddr *operator *() const { return fAddress; } 120 sockaddr *operator *() { return fAddress; } 121 122 net_address_module_info *Module() const { return fModule; } 123 124 private: 125 net_address_module_info *fModule; 126 sockaddr *fAddress; 127 }; 128 129 class ConstSocketAddress { 130 public: 131 ConstSocketAddress(net_address_module_info *module, 132 const sockaddr *address) 133 : fModule(module), fAddress(address) {} 134 135 ConstSocketAddress(net_address_module_info *module, 136 const sockaddr_storage *address) 137 : fModule(module), fAddress((sockaddr *)address) {} 138 139 ConstSocketAddress(const ConstSocketAddress &address) 140 : fModule(address.fModule), fAddress(address.fAddress) {} 141 142 ConstSocketAddress(const SocketAddress &address) 143 : fModule(address.Module()), fAddress(*address) {} 144 145 void SetAddressTo(const sockaddr *address) 146 { 147 fAddress = address; 148 } 149 150 bool IsEmpty(bool checkPort) const 151 { 152 return fModule->is_empty_address(fAddress, checkPort); 153 } 154 155 uint32 HashPair(const sockaddr *second) const 156 { 157 return fModule->hash_address_pair(fAddress, second); 158 } 159 160 bool EqualTo(const sockaddr *address, bool checkPort = false) const 161 { 162 if (checkPort) 163 return fModule->equal_addresses_and_ports(fAddress, address); 164 165 return fModule->equal_addresses(fAddress, address); 166 } 167 168 bool EqualPorts(const sockaddr *second) const 169 { 170 return fModule->equal_ports(fAddress, second); 171 } 172 173 bool MatchMasked(const sockaddr *address, const sockaddr *mask) const 174 { 175 return fModule->equal_masked_addresses(fAddress, address, mask); 176 } 177 178 uint16 Port() const 179 { 180 return fModule->get_port(fAddress); 181 } 182 183 status_t CopyTo(sockaddr *to) const 184 { 185 return fModule->set_to(to, fAddress); 186 } 187 188 status_t CopyTo(sockaddr_storage *to) const 189 { 190 return CopyTo((sockaddr *)to); 191 } 192 193 AddressString AsString(bool printPort = false) const 194 { 195 return AddressString(fModule, fAddress, printPort); 196 } 197 198 const char *AsString(char *buffer, size_t bufferSize, 199 bool printPort = false) const 200 { 201 fModule->print_address_buffer(fAddress, buffer, bufferSize, printPort); 202 return buffer; 203 } 204 205 const sockaddr *operator *() const { return fAddress; } 206 207 private: 208 net_address_module_info *fModule; 209 const sockaddr *fAddress; 210 }; 211 212 class SocketAddressStorage : public SocketAddress { 213 public: 214 SocketAddressStorage(net_address_module_info *module) 215 : SocketAddress(module, &fAddressStorage) 216 {} 217 218 private: 219 sockaddr_storage fAddressStorage; 220 }; 221 222 #endif // NET_ADDRESS_UTILITIES_H 223