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