sockwrap.c 14.1 KB
Newer Older
1
2
3
4
5
6
/* Berkley/WinSock socket API wrappers */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8
 *																			*
9
10
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
11
12
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
13
14
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
15
16
17
18
19
20
21
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

deuce's avatar
deuce committed
22
#include <ctype.h>		/* isdigit */
23
#include <stdlib.h>		/* alloca/free on FreeBSD */
24
#include <string.h>		/* bzero (for FD_ZERO) on FreeBSD */
25
#include <errno.h>		/* ENOMEM */
26
#include <stdio.h>		/* SEEK_SET */
27
#include <string.h>
28
29
30
#if defined(_WIN32)
 #include <malloc.h>	/* alloca() on Win32 */
#endif
31

32
#include "genwrap.h"	/* SLEEP */
deuce's avatar
deuce committed
33
#include "gen_defs.h"	/* BOOL/LOG_WARNING */
34
35
36
#include "sockwrap.h"	/* sendsocket */
#include "filewrap.h"	/* filelength */

37
static socket_option_t socket_options[] = {
38
	{ "TYPE",				0,				SOL_SOCKET,		SO_TYPE				},
39
	{ "ERROR",				0,				SOL_SOCKET,		SO_ERROR			},
40
41
42
43
	{ "DEBUG",				0,				SOL_SOCKET,		SO_DEBUG			},
	{ "LINGER",				SOCK_STREAM,	SOL_SOCKET,		SO_LINGER			},
	{ "SNDBUF",				0,				SOL_SOCKET,		SO_SNDBUF			},
	{ "RCVBUF",				0,				SOL_SOCKET,		SO_RCVBUF			},
44
45

#ifndef _WINSOCKAPI_	/* Defined, but not supported, by WinSock */
46
47
48
49
	{ "SNDLOWAT",			0,				SOL_SOCKET,		SO_SNDLOWAT			},
	{ "RCVLOWAT",			0,				SOL_SOCKET,		SO_RCVLOWAT			},
	{ "SNDTIMEO",			0,				SOL_SOCKET,		SO_SNDTIMEO			},
	{ "RCVTIMEO",			0,				SOL_SOCKET,		SO_RCVTIMEO			},
50
51
52
#ifdef SO_USELOOPBACK	/* SunOS */
	{ "USELOOPBACK",		0,				SOL_SOCKET,		SO_USELOOPBACK		},
#endif
53
#endif
54

55
	{ "REUSEADDR",			0,				SOL_SOCKET,		SO_REUSEADDR		},	
deuce's avatar
deuce committed
56
57
#ifdef SO_REUSEPORT	/* BSD */
	{ "REUSEPORT",			0,				SOL_SOCKET,		SO_REUSEPORT		},	
58
59
60
#endif
#ifdef SO_EXCLUSIVEADDRUSE /* WinSock */
	{ "EXCLUSIVEADDRUSE",	0,				SOL_SOCKET,		SO_EXCLUSIVEADDRUSE },
deuce's avatar
deuce committed
61
#endif
62
63
64
65
	{ "KEEPALIVE",			SOCK_STREAM,	SOL_SOCKET,		SO_KEEPALIVE		},
	{ "DONTROUTE",			0,				SOL_SOCKET,		SO_DONTROUTE		},
	{ "BROADCAST",			SOCK_DGRAM,		SOL_SOCKET,		SO_BROADCAST		},
	{ "OOBINLINE",			SOCK_STREAM,	SOL_SOCKET,		SO_OOBINLINE		},
66

67
#ifdef SO_ACCEPTCONN											
68
	{ "ACCEPTCONN",			SOCK_STREAM,	SOL_SOCKET,		SO_ACCEPTCONN		},
69
#endif
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifdef SO_PRIORITY		/* Linux */
	{ "PRIORITY",			0,				SOL_SOCKET,		SO_PRIORITY			},
#endif
#ifdef SO_NO_CHECK		/* Linux */
	{ "NO_CHECK",			0,				SOL_SOCKET,		SO_NO_CHECK			},
#endif
#ifdef SO_PROTOTYPE		/* SunOS */
	{ "PROTOTYPE",			0,				SOL_SOCKET,		SO_PROTOTYPE		},
#endif
#ifdef SO_MAX_MSG_SIZE	/* WinSock2 */
	{ "MAX_MSG_SIZE",		SOCK_DGRAM,		SOL_SOCKET,		SO_MAX_MSG_SIZE		},
#endif
#ifdef SO_CONNECT_TIME	/* WinSock2 */
	{ "CONNECT_TIME",		SOCK_STREAM,	SOL_SOCKET,		SO_CONNECT_TIME		},
#endif
85
86

	/* IPPROTO-level socket options */
87
	{ "TCP_NODELAY",		SOCK_STREAM,	IPPROTO_TCP,	TCP_NODELAY			},
88
89
	/* The following are platform-specific */					
#ifdef TCP_MAXSEG											
90
	{ "TCP_MAXSEG",			SOCK_STREAM,	IPPROTO_TCP,	TCP_MAXSEG			},
91
92
#endif															
#ifdef TCP_CORK													
93
	{ "TCP_CORK",			SOCK_STREAM,	IPPROTO_TCP,	TCP_CORK			},
94
95
#endif															
#ifdef TCP_KEEPIDLE												
96
	{ "TCP_KEEPIDLE",		SOCK_STREAM,	IPPROTO_TCP,	TCP_KEEPIDLE		},
97
98
#endif															
#ifdef TCP_KEEPINTVL											
99
	{ "TCP_KEEPINTVL",		SOCK_STREAM,	IPPROTO_TCP,	TCP_KEEPINTVL		},
100
101
#endif															
#ifdef TCP_KEEPCNT												
102
	{ "TCP_KEEPCNT",		SOCK_STREAM,	IPPROTO_TCP,	TCP_KEEPCNT			},
103
#endif															
104
105
106
#ifdef TCP_KEEPALIVE	/* SunOS */
	{ "TCP_KEEPALIVE",		SOCK_STREAM,	IPPROTO_TCP,	TCP_KEEPALIVE		},
#endif															
107
#ifdef TCP_SYNCNT												
108
	{ "TCP_SYNCNT",			SOCK_STREAM,	IPPROTO_TCP,	TCP_SYNCNT			},
109
110
#endif															
#ifdef TCP_LINGER2												
111
	{ "TCP_LINGER2",		SOCK_STREAM,	IPPROTO_TCP,	TCP_LINGER2			},
112
113
#endif														
#ifdef TCP_DEFER_ACCEPT										
114
	{ "TCP_DEFER_ACCEPT",	SOCK_STREAM,	IPPROTO_TCP,	TCP_DEFER_ACCEPT	},
115
116
#endif															
#ifdef TCP_WINDOW_CLAMP											
117
	{ "TCP_WINDOW_CLAMP",	SOCK_STREAM,	IPPROTO_TCP,	TCP_WINDOW_CLAMP	},
118
119
#endif														
#ifdef TCP_QUICKACK											
120
	{ "TCP_QUICKACK",		SOCK_STREAM,	IPPROTO_TCP,	TCP_QUICKACK		},
121
122
#endif						
#ifdef TCP_NOPUSH			
123
	{ "TCP_NOPUSH",			SOCK_STREAM,	IPPROTO_TCP,	TCP_NOPUSH			},
124
125
#endif						
#ifdef TCP_NOOPT			
126
	{ "TCP_NOOPT",			SOCK_STREAM,	IPPROTO_TCP,	TCP_NOOPT			},
127
128
129
#endif
#if defined(IPV6_V6ONLY) && defined(IPPROTO_IPV6)
	{ "IPV6_V6ONLY",		0,				IPPROTO_IPV6,	IPV6_V6ONLY			},
130
131
132
133
#endif
	{ NULL }
};

134
int getSocketOptionByName(const char* name, int* level)
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
	int i;

	if(level!=NULL)
		*level=SOL_SOCKET;	/* default option level */
	for(i=0;socket_options[i].name;i++) {
		if(stricmp(name,socket_options[i].name)==0) {
			if(level!=NULL)
				*level = socket_options[i].level;
			return(socket_options[i].value);
		}
	}
	if(!isdigit(*name))	/* unknown option name */
		return(-1);
	return(strtol(name,NULL,0));
}

152
socket_option_t* getSocketOptionList(void)
153
154
155
156
{
	return(socket_options);
}

157
off_t sendfilesocket(int sock, int file, off_t *offset, off_t count)
158
{
159
	char		buf[1024*16];
160
	off_t		len;
161
162
163
164
	ssize_t		rd;
	ssize_t		wr=0;
	off_t		total=0;
	ssize_t		i;
165

166
167
168
/* sendfile() on Linux may or may not work with non-blocking sockets ToDo */
	len=filelength(file);

169
170
171
172
	if(offset!=NULL)
		if(lseek(file,*offset,SEEK_SET)<0)
			return(-1);

173
	if(count<1 || count>len) {
174
		count=len;
175
176
		count-=tell(file);		/* don't try to read beyond EOF */
	}
177
178
179
180
181
182
#if USE_SENDFILE
	while((i=sendfile(file,sock,(offset==NULL?0:*offset)+total,count-total,NULL,&wr,0))==-1 && errno==EAGAIN)  {
		total+=wr;
		SLEEP(1);
	}
	if(i==0)
183
		return(count);
184
#endif
185

186
187
188
189
190
191
	if(count<0) {
		errno=EINVAL;
		return(-1);
	}

	while(total<count) {
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
		rd=read(file,buf,sizeof(buf));
		if(rd==-1)
			return(-1);
		if(rd==0)
			break;
		for(i=wr=0;i<rd;i+=wr) {
			wr=sendsocket(sock,buf+i,rd-i);
			if(wr>0)
				continue;
			if(wr==SOCKET_ERROR && ERROR_VALUE==EWOULDBLOCK) {
				wr=0;
				SLEEP(1);
				continue;
			}
			return(wr);
207
		}
208
209
210
		if(i!=rd)
			return(-1);
		total+=rd;
211
212
	}

213
	if(offset!=NULL)
214
215
216
		(*offset)+=total;

	return(total);
217
218
}

219
off_t recvfilesocket(int sock, int file, off_t *offset, off_t count)
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
{
	/* Writes a file from a socket -
	 *
	 * sock		- Socket to read from
	 * file		- File descriptior to write to
	 *				MUST be open and writeable
	 * offset	- pointer to file offset to start writing at
	 *				is set to offset writing STOPPED
	 *				on return
	 * count	- number of bytes to read/write
	 *
	 * returns -1 if an error occurse, otherwise
	 * returns number ob bytes written and sets offset
	 * to the new offset
	 */
	 
	char*	buf;
237
238
	ssize_t	rd;
	ssize_t	wr;
239
240
241
242
243
244

	if(count<1) {
		errno=ERANGE;
		return(-1);
	}
		
245
	if((buf=(char*)malloc((size_t)count))==NULL) {
246
247
248
249
		errno=ENOMEM;
		return(-1);
	}

250
251
252
	if(offset!=NULL) {
		if(lseek(file,*offset,SEEK_SET)<0) {
			free(buf);
253
			return(-1);
254
255
		}
	}
256

257
	rd=read(sock,buf,(size_t)count);
258
259
	if(rd!=count) {
		free(buf);
260
		return(-1);
261
	}
262
263
264
265
266
267

	wr=write(file,buf,rd);

	if(offset!=NULL)
		(*offset)+=wr;

268
	free(buf);
269
270
271
272
	return(wr);
}


273
/* Return true if connected, optionally sets *rd_p to true if read data available */
274
BOOL socket_check(SOCKET sock, BOOL* rd_p, BOOL* wr_p, DWORD timeout)
275
276
277
{
	char	ch;
	int		i,rd;
278
	fd_set	rd_set;
279
	fd_set*	rd_set_p=&rd_set;
280
281
	fd_set	wr_set;
	fd_set*	wr_set_p=NULL;
282
283
284
285
286
	struct	timeval tv;

	if(rd_p!=NULL)
		*rd_p=FALSE;

287
288
289
	if(wr_p!=NULL)
		*wr_p=FALSE;

290
291
292
	if(sock==INVALID_SOCKET)
		return(FALSE);

293
294
295
296
297
298
	FD_ZERO(&rd_set);
	FD_SET(sock,&rd_set);
	if(wr_p!=NULL) {
		wr_set_p=&wr_set;
		FD_ZERO(wr_set_p);
		FD_SET(sock,wr_set_p);
299
		if(rd_p==NULL)
300
			rd_set_p=NULL;
301
	}
302

303
304
305
	/* Convert timeout from ms to sec/usec */
	tv.tv_sec=timeout/1000;
	tv.tv_usec=(timeout%1000)*1000;
306

307
	i=select(sock+1,rd_set_p,wr_set_p,NULL,&tv);
308
309
310
311
312
313
	if(i==SOCKET_ERROR)
		return(FALSE);

	if(i==0) 
		return(TRUE);

314
315
316
317
318
319
	if(wr_p!=NULL && FD_ISSET(sock,wr_set_p)) {
		*wr_p=TRUE;
		if(i==1)
			return(TRUE);
	}

320
	if(rd_p !=NULL || wr_p==NULL)  {
321
322
323
324
325
326
327
		rd=recv(sock,&ch,1,MSG_PEEK);
		if(rd==1 
			|| (rd==SOCKET_ERROR && ERROR_VALUE==EMSGSIZE)) {
			if(rd_p!=NULL)
				*rd_p=TRUE;
			return(TRUE);
		}
328
329
330
331
	}

	return(FALSE);
}
deuce's avatar
deuce committed
332

333
int retry_bind(SOCKET s, const struct sockaddr *addr, socklen_t addrlen
334
335
			   ,uint retries, uint wait_secs
			   ,const char* prot
336
			   ,int (*lprintf)(int level, const char *fmt, ...))
deuce's avatar
deuce committed
337
{
338
	char	port_str[128];
339
	char	err[256];
deuce's avatar
deuce committed
340
	int		result=-1;
341
	uint	i;
deuce's avatar
deuce committed
342

343
	if(addr->sa_family==AF_INET)
deuce's avatar
deuce committed
344
		SAFEPRINTF(port_str," to port %u",ntohs(((SOCKADDR_IN *)(addr))->sin_port));
345
346
	else
		port_str[0]=0;
deuce's avatar
deuce committed
347
	for(i=0;i<=retries;i++) {
348
		if((result=bind(s,addr,addrlen))==0)
deuce's avatar
deuce committed
349
			break;
350
		if(lprintf!=NULL)
351
			lprintf(i<retries ? LOG_WARNING:LOG_CRIT
352
				,"%04d !ERROR %d binding %s socket%s: %s", s, ERROR_VALUE, prot, port_str, socket_strerror(socket_errno, err, sizeof(err)));
353
354
		if(i<retries) {
			if(lprintf!=NULL)
355
356
				lprintf(LOG_WARNING,"%04d Will retry in %u seconds (%u of %u)"
					,s, wait_secs, i+1, retries);
357
358
			SLEEP(wait_secs*1000);
		}
deuce's avatar
deuce committed
359
360
361
	}
	return(result);
}
362

363
int nonblocking_connect(SOCKET sock, struct sockaddr* addr, size_t size, unsigned timeout)
364
365
366
367
368
{
	int result;

	result=connect(sock, addr, size);

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
	if(result==SOCKET_ERROR) {
		result=ERROR_VALUE;
		if(result==EWOULDBLOCK || result==EINPROGRESS) {
			fd_set		wsocket_set;
			fd_set		esocket_set;
			struct		timeval tv;
			socklen_t	optlen=sizeof(result);
			tv.tv_sec = timeout;
			tv.tv_usec = 0;
			FD_ZERO(&wsocket_set);
			FD_SET(sock,&wsocket_set);
			FD_ZERO(&esocket_set);
			FD_SET(sock,&esocket_set);
			switch(select(sock+1,NULL,&wsocket_set,&esocket_set,&tv)) {
				case 1:
					if(getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&result, &optlen)==SOCKET_ERROR)
						result=ERROR_VALUE;
					break;
				case 0:
					break;
				case SOCKET_ERROR:
					result=ERROR_VALUE;
					break;
			}
		}
394
395
396
	}
	return result;
}
397

deuce's avatar
deuce committed
398

399
union xp_sockaddr* inet_ptoaddr(char *addr_str, union xp_sockaddr *addr, size_t size)
deuce's avatar
deuce committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
{
    struct addrinfo hints = {0};
    struct addrinfo *res, *cur;

    hints.ai_flags = AI_NUMERICHOST|AI_PASSIVE;
    if(getaddrinfo(addr_str, NULL, &hints, &res))
        return NULL;
    
    for(cur = res; cur; cur++) {
        if(cur->ai_addr->sa_family == AF_INET6)
            break;
    }
    if(!cur) {
        freeaddrinfo(res);
        return NULL;
    }
416
417
418
419
420
421
    if (size < sizeof(struct sockaddr_in6)) {
        freeaddrinfo(res);
        return NULL;
	}
	size = sizeof(struct sockaddr_in6);
    memcpy(addr, ((struct sockaddr_in6 *)(cur->ai_addr)), size);
deuce's avatar
deuce committed
422
423
424
425
    freeaddrinfo(res);
    return addr;
}

426
const char* inet_addrtop(union xp_sockaddr *addr, char *dest, size_t size)
427
{
428
#ifdef _WIN32
deuce's avatar
deuce committed
429
	if(getnameinfo(&addr->addr, xp_sockaddr_len(addr), dest, size, NULL, 0, NI_NUMERICHOST))
430
		safe_snprintf(dest, size, "<Error %u converting address, family=%u>", WSAGetLastError(), addr->addr.sa_family);
431
432
	return dest;
#else
433
	switch(addr->addr.sa_family) {
434
		case AF_INET:
435
			return inet_ntop(addr->in.sin_family, &addr->in.sin_addr, dest, size);
436
		case AF_INET6:
437
			return inet_ntop(addr->in6.sin6_family, &addr->in6.sin6_addr, dest, size);
438
439
440
441
		case AF_UNIX:
			strncpy(dest, addr->un.sun_path, size);
			dest[size-1]=0;
			return dest;
442
		default:
443
			safe_snprintf(dest, size, "<unknown address family: %u>", addr->addr.sa_family);
444
445
			return NULL;
	}
446
#endif
447
448
}

449
uint16_t inet_addrport(union xp_sockaddr *addr)
450
{
451
	switch(addr->addr.sa_family) {
452
		case AF_INET:
453
			return ntohs(addr->in.sin_port);
454
		case AF_INET6:
455
			return ntohs(addr->in6.sin6_port);
456
457
458
459
		default:
			return 0;
	}
}
deuce's avatar
deuce committed
460

461
void inet_setaddrport(union xp_sockaddr *addr, uint16_t port)
deuce's avatar
deuce committed
462
463
464
465
466
467
468
469
470
471
{
	switch(addr->addr.sa_family) {
		case AF_INET:
			addr->in.sin_port = htons(port);
			break;
		case AF_INET6:
			addr->in6.sin6_port = htons(port);
			break;
	}
}
472
473

/* Return TRUE if the 2 addresses are the same host (type and address) */
474
BOOL inet_addrmatch(union xp_sockaddr* addr1, union xp_sockaddr* addr2)
475
476
477
478
479
480
481
482
483
484
485
486
{
	if(addr1->addr.sa_family != addr2->addr.sa_family)
		return FALSE;

	switch(addr1->addr.sa_family) {
		case AF_INET:
			return memcmp(&addr1->in.sin_addr, &addr2->in.sin_addr, sizeof(addr1->in.sin_addr)) == 0;
		case AF_INET6:
			return memcmp(&addr1->in6.sin6_addr, &addr2->in6.sin6_addr, sizeof(addr1->in6.sin6_addr)) == 0;
	}
	return FALSE;
}
487
488

/* Return the current socket error description (for Windows), like strerror() does for errno */
489
DLLEXPORT char* socket_strerror(int error_number, char* buf, size_t buflen)
490
{
491
#if defined(_WINSOCKAPI_)
492
493
	strncpy(buf, "Unknown error", buflen);
	buf[buflen - 1] = 0;
494
495
	if(error_number > 0 && error_number < WSABASEERR)
		error_number += WSABASEERR;
496
	if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,	// dwFlags
497
498
499
		NULL,			// lpSource
		error_number,	// dwMessageId
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),    // dwLanguageId
500
501
		buf,
		buflen,
502
503
		NULL))
		safe_snprintf(buf, buflen, "Error %d getting error description", GetLastError());
504
505
506
507
	truncsp(buf);
	return buf;
#else
	return safe_strerror(error_number, buf, buflen);
508
#endif
509
}
Deucе's avatar
Deucе committed
510

511
512
513
514
515
516
517
518
519
DLLEXPORT void set_socket_errno(int err)
{
#if defined(_WINSOCKAPI_)
	WSASetLastError(err);
#else
	errno = err;
#endif
}

520
DLLEXPORT int xp_inet_pton(int af, const char *src, void *dst)
Deucе's avatar
Deucе committed
521
522
523
524
525
{
	struct addrinfo hints = {0};
	struct addrinfo *res, *cur;

	if (af != AF_INET && af != AF_INET6) {
526
		set_socket_errno(EAFNOSUPPORT);
Deucе's avatar
Deucе committed
527
528
529
530
		return -1;
	}

	hints.ai_flags = AI_NUMERICHOST|AI_PASSIVE;
531
	if(getaddrinfo(src, NULL, &hints, &res))
Deucе's avatar
Deucе committed
532
533
534
535
536
537
538
539
540
541
542
543
		return -1;

	for(cur = res; cur; cur++) {
		if(cur->ai_addr->sa_family == af)
			break;
	}
	if(!cur) {
		freeaddrinfo(res);
		return 0;
	}
	switch(af) {
		case AF_INET:
544
			memcpy(dst, &(((struct sockaddr_in *)cur)->sin_addr), sizeof(((struct sockaddr_in *)cur)->sin_addr));
Deucе's avatar
Deucе committed
545
546
			break;
		case AF_INET6:
547
			memcpy(dst, &(((struct sockaddr_in6 *)cur)->sin6_addr), sizeof(((struct sockaddr_in6 *)cur)->sin6_addr));
Deucе's avatar
Deucе committed
548
549
550
			break;
	}
	freeaddrinfo(res);
551
	return 1;
Deucе's avatar
Deucе committed
552
}