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