2 * Copyright 2007-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Hugo Santos, hugosantos@gmail.com
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>
20 SocketAddress(net_address_module_info
* module
, sockaddr
* address
)
27 SocketAddress(net_address_module_info
* module
, sockaddr_storage
* address
)
30 fAddress((sockaddr
*)address
)
34 SocketAddress(const SocketAddress
& address
)
36 fModule(address
.fModule
),
37 fAddress(address
.fAddress
)
41 void SetAddressTo(sockaddr
* 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
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
);
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
);
131 const sockaddr
* operator*() const { return fAddress
; }
132 sockaddr
* operator*() { return fAddress
; }
134 net_address_module_info
* Module() const { return fModule
; }
137 net_address_module_info
* fModule
;
142 class ConstSocketAddress
{
144 ConstSocketAddress(net_address_module_info
* module
,
145 const sockaddr
* address
)
152 ConstSocketAddress(net_address_module_info
* module
,
153 const sockaddr_storage
* address
)
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()),
174 void SetAddressTo(const sockaddr
* 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
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
);
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
);
234 const sockaddr
* operator*() const { return fAddress
; }
237 net_address_module_info
* fModule
;
238 const sockaddr
* fAddress
;
242 class SocketAddressStorage
: public SocketAddress
{
244 SocketAddressStorage(net_address_module_info
* module
)
246 SocketAddress(module
, &fAddressStorage
)
251 sockaddr_storage fAddressStorage
;
255 #endif // NET_ADDRESS_UTILITIES_H