summaryrefslogtreecommitdiff
path: root/usr/src/cmd/lms/tools/ATNetworkTool.h
blob: 00cda38d0841107b1e41c1ef96f49f3b6c5fea6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*******************************************************************************
 * Copyright (C) 2004-2008 Intel Corp. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  - Neither the name of Intel Corp. nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#ifndef _AT_NETWORK_TOOL_H_
#define _AT_NETWORK_TOOL_H_

#include <string>
#include <set>
#include <vector>
#include <map>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

class ATAddress
{
private:
	struct sockaddr_storage ip;

public:

	ATAddress()
	{
		memset(&(this->ip), 0, sizeof(struct sockaddr_storage));
	};

	ATAddress(const ATAddress &y)
	{
		memcpy(&(this->ip), y.addr(), sizeof(struct sockaddr_storage));
	};

	ATAddress(const struct sockaddr *yip)
	{
		memset(&(this->ip), 0, sizeof(struct sockaddr_storage));
		memcpy(&(this->ip), yip, saSize(yip));
	};

	ATAddress(const ATAddress &y, in_port_t yport)
	{
		memcpy(&(this->ip), y.addr(), sizeof(struct sockaddr_storage));
		ATAddress::saSetPort((const struct sockaddr *)&(this->ip), yport);
	};

	ATAddress(const struct sockaddr *yip, in_port_t yport)
	{
		memset(&(this->ip), 0, sizeof(struct sockaddr_storage));
		memcpy(&(this->ip), yip, saSize(yip));
		ATAddress::saSetPort((const struct sockaddr *)&(this->ip), yport);
	};

	static void saSetPort(const struct sockaddr *ip, in_port_t yport)
	{
		switch (ip->sa_family) {
		case AF_INET6:
			((struct sockaddr_in6 *)ip)->sin6_port = htons(yport);
			break;

		case AF_INET:
			((struct sockaddr_in *)ip)->sin_port = htons(yport);
			break;
		}
	}

	static bool saIsInet(const struct sockaddr *ip)
	{
		return ((ip->sa_family == AF_INET) || (ip->sa_family == AF_INET6));
	};

	static unsigned int saSize(const struct sockaddr *ip)
	{
		switch (ip->sa_family) {
		case AF_INET6:
			return sizeof(struct sockaddr_in6);
			break;

		case AF_INET:
			return sizeof(struct sockaddr_in);
			break;

		default:
			return 0;
		}
	};

	static in_port_t saInPort(const struct sockaddr *ip)
	{
		switch (ip->sa_family) {
		case AF_INET6:
			return ntohs(((struct sockaddr_in6 *)ip)->sin6_port);
			break;

		case AF_INET:
			return ntohs(((struct sockaddr_in *)ip)->sin_port);
			break;

		default:
			return 0;
		}
	};

	static const void *saInAddr(const struct sockaddr *ip, size_t &asize)
	{
		switch (ip->sa_family) {
		case AF_INET6:
			asize = sizeof(in6_addr);
			return (const void *)&(((struct sockaddr_in6 *)ip)->sin6_addr);
			break;

		case AF_INET:
			asize = sizeof(in_addr);
			return (const void *)&(((struct sockaddr_in *)ip)->sin_addr);
			break;

		default:
			asize = 0;
			return NULL;
		}
	};

	static const char *saInNtoP(const struct sockaddr *ip, char *buf, size_t buflen)
	{
		if (!ATAddress::saIsInet(ip)) {
			return NULL;
		}

		size_t asize;
		const void *src = ATAddress::saInAddr(ip, asize);
		if (NULL == src) {
			return NULL;
		}
		return inet_ntop(ip->sa_family, src, buf, buflen);
	};

	sa_family_t family() const { return ip.ss_family; };

	bool isInet() const { return ((ip.ss_family == AF_INET) || (ip.ss_family == AF_INET6)); };

	unsigned int size() const
	{
		return ATAddress::saSize((const struct sockaddr *)&(this->ip));
	};

	const struct sockaddr *addr() const
	{
		return (const struct sockaddr *)&ip;
	};

	in_port_t inPort() const
	{
		return ATAddress::saInPort((const struct sockaddr *)&(this->ip));
	};

	const void *inAddr(size_t &asize) const
	{
		return ATAddress::saInAddr((const struct sockaddr *)&(this->ip), asize);
	};

	const char *inNtoP(char *buf, size_t buflen)
	{
		return ATAddress::saInNtoP((const struct sockaddr *)&(this->ip), buf, buflen);
	};

	ATAddress &operator=(const ATAddress &y)
	{
		if (this != &y) {
			memcpy(&(this->ip), y.addr(), sizeof(struct sockaddr_storage));
		}
		return *this;
	};

	ATAddress &operator=(const struct sockaddr &yip)
	{
		memset(&(this->ip), 0, sizeof(struct sockaddr_storage));
		memcpy(&(this->ip), &yip, saSize(&yip));
		return *this;
	};

	int compare(const ATAddress &y) const
	{
		if (this->family() != y.family()) {
			return (this->family() - y.family());
		}

		size_t asize = 0;
		const void *a = this->inAddr(asize);
		const void *b = y.inAddr(asize);
		if ((0 != asize) && (NULL != a) && (NULL != b)) {
			int adiff = memcmp(a, b, asize);
			if (adiff != 0) {
				return adiff;
			}
		}

		in_port_t ap = this->inPort();
		in_port_t bp = y.inPort();
		if ((ap == 0) || (bp == 0)) {
			return 0;
		}
		if (ap != bp) {
			return (ap - bp);
		}

		return memcmp(&(this->ip), y.addr(), this->size());
	};

	bool operator<(const ATAddress &y) const
	{
		if (this == &y) {
			return false;
		}
		return (this->compare(y) < 0);
	};

	bool operator>(const ATAddress &y) const
	{
		if (this == &y) {
			return false;
		}
		return (this->compare(y) > 0);
	};

	bool operator==(const ATAddress &y) const
	{
		if (this == &y) {
			return true;
		}
		if (this->family() != y.family()) {
			return false;
		}
		return (memcmp(&(this->ip), y.addr(), this->size()) == 0);
	};

	bool operator!=(const ATAddress &y) const
	{
		if (this == &y) {
			return false;
		}
		if (this->family() != y.family()) {
			return true;
		}
		return (memcmp(&(this->ip), y.addr(), this->size()) != 0);
	};

	static bool IsAddressIP(const char *address, int family = AF_INET)
	{
		struct sockaddr_storage inaddr;

		if (address == NULL) {
			return false;
		}
		return (0 < inet_pton(family, address, &inaddr));
	};
};


typedef std::set<ATAddress> ATAddressList;
typedef std::map<ATAddress, std::string> ATDomainMap;
typedef std::vector<int> ATSocketList;

class ATNetworkTool
{
public:
	static const int DefaultBacklog = 5;
	static const int DefaultFamily = AF_INET;
	static const int AF_XINETX = AF_MAX + 10;

	/* Gets Domain name from Hostname
	 * @param name hostname
	 * @param domain [out] domain name
	 * @return bool true if success, false if domain unknown
	 */
	static bool GetHostNameDomain(const char *name, std::string &domain);

	/* Gets Domain name from IP
	 * @param ip address
	 * @param domain [out] domain name
	 * @param error [out] error code
	 * @return bool true if success, false if domain unknown
	 */
	static bool GetIPDomain(const ATAddress &ip, std::string &domain, int &error);

	/* Gets Domain name from host entry
	 * @param hent pointer to host entry structure
	 * @param domain [out] domain name
	 * @return bool true if success, false if domain unknown
	 */
	static bool GetHentDomain(struct hostent *hent, std::string &domain);

	/* Gets Domain name from socket
	 * @param sock checked socket
	 * @param domain [out] domain name
	 * @param error [out] error code
	 * @return int ==1 if success, <0 on error, ==0 if no domain
	 */
	static int GetSockDomain(int sock, std::string &domain, int &error);

	/* Gets all local (IPv4/6) from local running network interfaces
	 * @param addresses [out] set of local IP addresses
	 * @param error [out] error code
	 * @param family filtered address family
	 * @param withloopback true if get loopback addresses too
	 * @return int ==0 if success, !=0 on error
	 */
	static int GetLocalIPs(ATAddressList &addresses, int &error,
				int family = ATNetworkTool::AF_XINETX,
				bool withloopback = false);

	/* Gets all local domains from local running network interfaces
	 * @param domains [out] map of <local IP address> => <domain name>
	 * @param error [out] error code
	 * @param family filtered address family
	 * @return int ==0 if success, !=0 on error
	 */
	static int GetLocalNetDomains(ATDomainMap &domains, int &error,
				int family = ATNetworkTool::AF_XINETX);

	/* Gets all (IPv4/6) network interfaces of socket peer
	 * @param addresses [out] set of peer IP addresses
	 * @param error [out] error code
	 * @param family filtered address family
	 * @param zeroport set port to 0 in result list
	 * @return int ==0 if success, !=0 on error
	 */
	static int GetSockPeerIPs(int sock, ATAddressList &addresses,
				int &error,
				int family = ATNetworkTool::AF_XINETX,
				bool zeroport = false);

	/* Checks if socket peer is (IPv4/6) local address
	 * @param sock checked socket
	 * @param error [out] error code
	 * @param family filtered address family
	 * @return int ==1 if peer is local, ==0 if remote, <0 on error
	 */
	static int IsSockPeerLocal(int sock, int &error,
				int family = ATNetworkTool::AF_XINETX);

	/* Closes socket
	 * @param s socket to close
	 * @return int ==0 if success, !=0 on error
	 */
	static int CloseSocket(int s);

	/* Sets/removes O_NONBLOCKING flag to socket
	 * @param s socket
	 * @param block - true to set flag, false to remove flag
	 * @return int ==0 if success, !=0 on error
	 */
	static int SetNonBlocking(int s, bool block = true);

	/* Creates socket
	 * @param addr socket parameters
	 * @param error [out] error code
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int CreateSocket(const struct addrinfo *addr, int &error);

	/* Creates socket
	 * @param addr socket address
	 * @param addrlen socket address length
	 * @param error [out] error code
	 * @param family socket expected family
	 * @param socktype socket type
	 * @param protocol socket protocol
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int CreateSocket(const struct sockaddr *addr, socklen_t addrlen,
				int &error,
				int family = ATNetworkTool::DefaultFamily,
				int socktype = SOCK_STREAM, int protocol = 0);

	/* Creates server listening socket
	 * @param addr socket parameters
	 * @param error [out] error code
	 * @param nonblocking true for nonblocking socket
	 * @param backlog listening backlog
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int CreateServerSocket(const struct addrinfo *addr,
				int &error, bool nonblocking = true,
				int backlog = ATNetworkTool::DefaultBacklog);

	/* Creates server listening sockets
	 * @param sockets [out] list of created server listening sockets
	 * @param port listening port
	 * @param error [out] error code
	 * @param loopback true to listen only on loopback
	 * @param nonblocking true for nonblocking sockets
	 * @param family sockets expected family
	 * @param socktype sockets type
	 * @param protocol sockets protocol
	 * @param backlog listening backlog
	 * @param one true if want create only one socket
	 * @return int >=0 number of created server sockets, ==-1 on error
	 */
	static int CreateServerSockets(ATSocketList &sockets, in_port_t port,
				int &error,
				bool loopback = false, bool nonblocking = true,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0,
				int backlog = ATNetworkTool::DefaultBacklog,
				bool one = false);
	static int CreateServerSockets(ATSocketList &sockets, const char *port,
				int &error,
				bool loopback = false, bool nonblocking = true,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0,
				int backlog = ATNetworkTool::DefaultBacklog,
				bool one = false);

	/* Creates one server listening socket
	 * @param port listening port
	 * @param error [out] error code
	 * @param loopback true to listen only on loopback
	 * @param nonblocking true for nonblocking sockets
	 * @param family sockets expected family
	 * @param socktype sockets type
	 * @param protocol sockets protocol
	 * @param backlog listening backlog
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int CreateServerSocket(in_port_t port,
				int &error,
				bool loopback = false, bool nonblocking = true,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0,
				int backlog = ATNetworkTool::DefaultBacklog);
	static int CreateServerSocket(const char *port,
				int &error,
				bool loopback = false, bool nonblocking = true,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0,
				int backlog = ATNetworkTool::DefaultBacklog);

	/* Connects to local socket
	 * @param sock socket to connect to
	 * @param error [out] error code
	 * @param loopback true to use loopback, false to use any local address
	 * @param socktype sockets type
	 * @param protocol sockets protocol
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int ConnectToSocket(int sock,
				int &error, bool loopback = true,
				int socktype = SOCK_STREAM, int protocol = 0);

	/* Connects to address
	 * @param addr destination address parameters
	 * @param error [out] error code
	 * @param loopback true to use loopback, false to use any local address
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int ConnectSocket(struct addrinfo *addr,
				int &error, bool loopback = false);

	/* Connects to address
	 * @param addr destination address
	 * @param addrlen socket address length
	 * @param error [out] error code
	 * @param loopback true to use loopback, false to use any local address
	 * @param family sockets expected family
	 * @param socktype sockets type
	 * @param protocol sockets protocol
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int ConnectSocket(const struct sockaddr *addr, socklen_t addrlen,
				int &error, bool loopback = false,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0);

	/* Connects to address
	 * @param hostname name of destination host
	 * @param port destination port
	 * @param error [out] error code
	 * @param family sockets expected family
	 * @param socktype sockets type
	 * @param protocol sockets protocol
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int Connect(const char *host, in_port_t port,
				int &error,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0);
	static int Connect(const char *host, const char *port,
				int &error,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0);

	/* Connects to loopback port
	 * @param port destination port
	 * @param error [out] error code
	 * @param family sockets expected family
	 * @param socktype sockets type
	 * @param protocol sockets protocol
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int ConnectLoopback(in_port_t port,
				int &error,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0);
	static int ConnectLoopback(const char *port,
				int &error,
				int family = ATNetworkTool::AF_XINETX,
				int socktype = SOCK_STREAM, int protocol = 0);

	/* Returns local port associated with socket
	 * @param sock socket
	 * @return unsigned int port number, or 0 - on error
	 */
	static unsigned int GetLocalPort(int sock);

	/* Accepts connection on sockets - returns address or error
	 * @param s socket accepting connection
	 * @param address [out] peer address of accepted connection
	 * @param error [out] error code
	 * @param nonblocking true for nonblocking sockets
	 * @return int !=-1 socket, ==-1 on error
	 */
	static int Accept(int s, ATAddress &address,
			  int &error, bool nonblocking = true);
};

#endif //_AT_NETWORK_TOOL_H_