btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / net / AddressUtilities.h
blobbae11fac2c3048173b10f1ec6080111aa640113c
1 /*
2 * Copyright 2007-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Hugo Santos, hugosantos@gmail.com
7 */
8 #ifndef NET_ADDRESS_UTILITIES_H
9 #define NET_ADDRESS_UTILITIES_H
12 #include <NetUtilities.h>
13 #include <net_datalink.h>
15 #include <sys/socket.h>
18 class SocketAddress {
19 public:
20 SocketAddress(net_address_module_info* module, sockaddr* address)
22 fModule(module),
23 fAddress(address)
27 SocketAddress(net_address_module_info* module, sockaddr_storage* address)
29 fModule(module),
30 fAddress((sockaddr*)address)
34 SocketAddress(const SocketAddress& address)
36 fModule(address.fModule),
37 fAddress(address.fAddress)
41 void SetAddressTo(sockaddr* address)
43 fAddress = address;
46 bool IsEmpty(bool checkPort) const
48 return fModule->is_empty_address(fAddress, checkPort);
51 uint32 HashPair(const sockaddr* second) const
53 return fModule->hash_address_pair(fAddress, second);
56 bool EqualTo(const sockaddr* address, bool checkPort = false) const
58 if (checkPort)
59 return fModule->equal_addresses_and_ports(fAddress, address);
61 return fModule->equal_addresses(fAddress, address);
64 bool EqualTo(const SocketAddress& second, bool checkPort = false) const
66 return EqualTo(second.fAddress, checkPort);
69 bool EqualPorts(const sockaddr* second) const
71 return fModule->equal_ports(fAddress, second);
74 bool MatchMasked(const sockaddr* address, const sockaddr* mask) const
76 return fModule->equal_masked_addresses(fAddress, address, mask);
79 uint16 Port() const
81 return fModule->get_port(fAddress);
84 status_t SetTo(const sockaddr* from)
86 return fModule->set_to(fAddress, from);
89 status_t SetTo(const sockaddr_storage* from)
91 return SetTo((sockaddr*)from);
94 status_t CopyTo(sockaddr* to) const
96 return fModule->set_to(to, fAddress);
99 status_t CopyTo(sockaddr_storage* to) const
101 return CopyTo((sockaddr*)to);
104 status_t SetPort(uint16 port)
106 return fModule->set_port(fAddress, port);
109 status_t SetToEmpty()
111 return fModule->set_to_empty_address(fAddress);
114 status_t UpdateTo(const sockaddr* from)
116 return fModule->update_to(fAddress, from);
119 AddressString AsString(bool printPort = false) const
121 return AddressString(fModule, fAddress, printPort);
124 const char* AsString(char* buffer, size_t bufferSize,
125 bool printPort = false) const
127 fModule->print_address_buffer(fAddress, buffer, bufferSize, printPort);
128 return buffer;
131 const sockaddr* operator*() const { return fAddress; }
132 sockaddr* operator*() { return fAddress; }
134 net_address_module_info* Module() const { return fModule; }
136 private:
137 net_address_module_info* fModule;
138 sockaddr* fAddress;
142 class ConstSocketAddress {
143 public:
144 ConstSocketAddress(net_address_module_info* module,
145 const sockaddr* address)
147 fModule(module),
148 fAddress(address)
152 ConstSocketAddress(net_address_module_info* module,
153 const sockaddr_storage* address)
155 fModule(module),
156 fAddress((sockaddr*)address)
160 ConstSocketAddress(const ConstSocketAddress& address)
162 fModule(address.fModule),
163 fAddress(address.fAddress)
167 ConstSocketAddress(const SocketAddress& address)
169 fModule(address.Module()),
170 fAddress(*address)
174 void SetAddressTo(const sockaddr* address)
176 fAddress = address;
179 bool IsEmpty(bool checkPort) const
181 return fModule->is_empty_address(fAddress, checkPort);
184 uint32 HashPair(const sockaddr* second) const
186 return fModule->hash_address_pair(fAddress, second);
189 bool EqualTo(const sockaddr* address, bool checkPort = false) const
191 if (checkPort)
192 return fModule->equal_addresses_and_ports(fAddress, address);
194 return fModule->equal_addresses(fAddress, address);
197 bool EqualPorts(const sockaddr* second) const
199 return fModule->equal_ports(fAddress, second);
202 bool MatchMasked(const sockaddr* address, const sockaddr* mask) const
204 return fModule->equal_masked_addresses(fAddress, address, mask);
207 uint16 Port() const
209 return fModule->get_port(fAddress);
212 status_t CopyTo(sockaddr* to) const
214 return fModule->set_to(to, fAddress);
217 status_t CopyTo(sockaddr_storage* to) const
219 return CopyTo((sockaddr*)to);
222 AddressString AsString(bool printPort = false) const
224 return AddressString(fModule, fAddress, printPort);
227 const char* AsString(char* buffer, size_t bufferSize,
228 bool printPort = false) const
230 fModule->print_address_buffer(fAddress, buffer, bufferSize, printPort);
231 return buffer;
234 const sockaddr* operator*() const { return fAddress; }
236 private:
237 net_address_module_info* fModule;
238 const sockaddr* fAddress;
242 class SocketAddressStorage : public SocketAddress {
243 public:
244 SocketAddressStorage(net_address_module_info* module)
246 SocketAddress(module, &fAddressStorage)
250 private:
251 sockaddr_storage fAddressStorage;
255 #endif // NET_ADDRESS_UTILITIES_H