summaryrefslogtreecommitdiffstats
path: root/src/shared/sockhelper.c
blob: ab34aa1ca1454954d333663b2811f53af1434edb (plain) (blame)
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
#include "sockhelper.h"
#include "log.h"
#include <arpa/inet.h> // inet_ntop
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>

#define MAXLISTEN 20

struct _poll_list {
	int count;
	struct pollfd entry[MAXLISTEN];
};

int sock_connect(const dnbd3_host_t * const addr, const int connect_ms, const int rw_ms)
{
	// TODO: Move out of here, this unit should contain general socket functions
	// TODO: Abstract away from sockaddr_in* like the rest of the functions here do,
	// so WITH_IPV6 can finally be removed as everything is transparent. b- but how?
	struct sockaddr_storage ss;
	int proto, addrlen;
	memset( &ss, 0, sizeof ss );
	if ( addr->type == HOST_IP4 ) {
		// Set host (IPv4)
		struct sockaddr_in *addr4 = (struct sockaddr_in*)&ss;
		addr4->sin_family = AF_INET;
		memcpy( &addr4->sin_addr, addr->addr, 4 );
		addr4->sin_port = addr->port;
		proto = PF_INET;
		addrlen = sizeof *addr4;
	}
#ifdef WITH_IPV6
	else if ( addr->type == HOST_IP6 ) {
		// Set host (IPv6)
		struct sockaddr_in6 *addr6 = (struct sockaddr_in6*)&ss;
		addr6->sin6_family = AF_INET6;
		memcpy( &addr6->sin6_addr, addr->addr, 16 );
		addr6->sin6_port = addr->port;
		proto = PF_INET6;
		addrlen = sizeof *addr6;
	}
#endif
	else {
		logadd( LOG_DEBUG1, "Unsupported address type: %d\n", (int)addr->type );
		return -1;
	}
	int client_sock = socket( proto, SOCK_STREAM, IPPROTO_TCP );
	if ( client_sock == -1 ) return -1;
	// Apply connect timeout
	if ( connect_ms == -1 ) {
		sock_set_nonblock( client_sock );
	} else {
		sock_setTimeout( client_sock, connect_ms );
	}
	for ( int i = 0; i < 5; ++i ) {
		int ret = connect( client_sock, (struct sockaddr *)&ss, addrlen );
		if ( ret != -1 || errno == EINPROGRESS || errno == EISCONN ) break;
		if ( errno == EINTR ) {
			// http://www.madore.org/~david/computers/connect-intr.html
#ifdef __linux__
			continue;
#else
			struct pollfd unix_really_sucks = { .fd = client_sock, .events = POLLOUT | POLLIN };
			while ( i-- > 0 ) {
				int pr = poll( &unix_really_sucks, 1, connect_ms == 0 ? -1 : connect_ms );
				if ( pr == 1 && ( unix_really_sucks.revents & POLLOUT ) ) break;
				if ( pr == -1 && errno == EINTR ) continue;
				close( client_sock );
				return -1;
			}
			sockaddr_storage junk;
			socklen_t more_junk = sizeof(junk);
			if ( getpeername( client_sock, (struct sockaddr*)&junk, &more_junk ) == -1 ) {
				close( client_sock );
				return -1;
			}
			break;
#endif
		} // EINTR
		close( client_sock );
		return -1;
	}
	if ( connect_ms != -1 && connect_ms != rw_ms ) {
		// Apply read/write timeout
		sock_setTimeout( client_sock, rw_ms );
	}
	return client_sock;
}

// TODO: Pretty much same as in server/*
int sock_resolveToDnbd3Host(const char * const address, dnbd3_host_t * const dest, const int count)
{
	if ( count <= 0 )
		return 0;
	struct addrinfo hints, *res, *ptr;
	char bufferAddr[100], bufferPort[6];
	char *addr = bufferAddr;
	const char *portStr = NULL;
	int addCount = 0;

	// See if we have a port
	snprintf( bufferAddr, sizeof bufferAddr, "%s", address );
	char *c1, *c2;
	c1 = strchr( addr, ':' );
	if ( c1 != NULL ) {
		c2 = strchr( c1 + 1, ':' );
		if ( c2 == NULL ) {
			*c1 = '\0';
			portStr = c1 + 1;
		} else if ( *addr == '[' ) {
			// IPv6 - support [1:2::3]:123
			do {
				c1 = strchr( c2 + 1, ':' );
				if ( c1 != NULL ) c2 = c1;
			} while ( c1 != NULL );
			if ( *(c2 - 1 ) == ']' ) {
				*( c2 - 1 ) = '\0';
				*c2 = '\0';
				addr += 1;
				portStr = c2 + 1;
			}
		}
	}
	if ( portStr == NULL ) {
		portStr = bufferPort;
		snprintf( bufferPort, sizeof bufferPort, "%d", (int)PORT );
	}

	// Set hints for local addresses.
	memset( &hints, 0, sizeof( hints ) );
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	if ( getaddrinfo( addr, portStr, &hints, &res ) != 0 || res == NULL ) {
		return 0;
	}
	for ( ptr = res; ptr != NULL && count > 0; ptr = ptr->ai_next ) {
		if ( sock_sockaddrToDnbd3( ptr->ai_addr, &dest[addCount] ) ) {
			addCount += 1;
		}
	}

	freeaddrinfo( res );
	return addCount;
}

bool sock_sockaddrToDnbd3(struct sockaddr* sa, dnbd3_host_t *host)
{
	if ( sa->sa_family == AF_INET ) {
		// Set host (IPv4)
		struct sockaddr_in *addr4 = (struct sockaddr_in*)sa;
		host->type = HOST_IP4;
		host->port = addr4->sin_port;
		memcpy( host->addr, &addr4->sin_addr, 4 );
		return true;
	}
#ifdef WITH_IPV6
	if ( sa->sa_family == AF_INET6 ) {
		// Set host (IPv6)
		struct sockaddr_in6 *addr6 = (struct sockaddr_in6*)sa;
		host->type = HOST_IP6;
		host->port = addr6->sin6_port;
		memcpy( host->addr, &addr6->sin6_addr, 16 );
		return true;
	}
#endif
	return false;
}

void sock_setTimeout(const int sockfd, const int milliseconds)
{
	struct timeval tv;
	tv.tv_sec = milliseconds / 1000;
	tv.tv_usec = (milliseconds * 1000) % 1000000;
	setsockopt( sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv) );
	setsockopt( sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv) );
}

poll_list_t* sock_newPollList()
{
	poll_list_t *list = (poll_list_t*)malloc( sizeof( poll_list_t ) );
	list->count = 0;
	return list;
}

void sock_destroyPollList(poll_list_t *list)
{
	for ( int i = 0; i < list->count; ++i ) {
		if ( list->entry[i].fd >= 0 ) close( list->entry[i].fd );
	}
	free( list );
}

size_t sock_printHost(const dnbd3_host_t * const host, char * const buffer, const size_t len)
{
	// Worst case: Port 5 chars, ':' to separate ip and port 1 char, terminating null 1 char = 7, [] for IPv6
	if ( len < 10 ) return 0;
	char *output = buffer;
	if ( host->type == HOST_IP6 ) {
		*output++ = '[';
		inet_ntop( AF_INET6, host->addr, output, (socklen_t)( len - 10 ) );
		output += strlen( output );
		*output++ = ']';
	} else if ( host->type == HOST_IP4 ) {
		inet_ntop( AF_INET, host->addr, output, (socklen_t)( len - 8 ) );
		output += strlen( output );
	} else {
		int ret = snprintf( output, len, "<?addrtype=%d>", (int)host->type );
		if ( ret <= 0 ) return 0;
		return MIN( (size_t)ret, len-1 );
	}
	*output = '\0';
	if ( host->port != 0 ) {
		// There are still at least 7 bytes left in the buffer, port is at most 5 bytes + ':' + '\0' = 7
		int ret = snprintf( output, 7, ":%d", (int)ntohs( host->port ) );
		if ( ret < 0 ) ret = 0;
		output += MIN( ret, 6 );
	}
	return output - buffer;
}

size_t sock_printable(const struct sockaddr * const addr, const socklen_t addrLen, char *output, const size_t len)
{
	char host[100], port[10];
	int outlen = 0;
	int ret = getnameinfo( addr, addrLen, host, sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV );
	if ( ret == 0 ) {
		if ( addr->sa_family == AF_INET ) {
			outlen = snprintf( output, len, "%s:%s", host, port );
		} else {
			outlen = snprintf( output, len, "[%s]:%s", host, port );
		}
	}
	if ( outlen <= 0 ) return 0;
	return MIN( (size_t)outlen, len-1 );
}

bool sock_listen(poll_list_t* list, char* bind_addr, uint16_t port)
{
	if ( list->count >= MAXLISTEN ) return false;
	struct addrinfo hints, *res = NULL, *ptr;
	char portStr[6];
	const int on = 1;
	int openCount = 0;
	// Set hints for local addresses.
	memset( &hints, 0, sizeof(hints) );
	hints.ai_flags = AI_PASSIVE;
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	snprintf( portStr, sizeof portStr, "%d", (int)port );
	if ( getaddrinfo( bind_addr, portStr, &hints, &res ) != 0 || res == NULL ) return false;
	// Attempt to bind to all of the addresses as long as there's room in the poll list
	for( ptr = res; ptr != NULL; ptr = ptr->ai_next ) {
		char bla[100];
		if ( !sock_printable( (struct sockaddr*)ptr->ai_addr, ptr->ai_addrlen, bla, 100 ) ) snprintf( bla, 100, "[invalid]" );
		logadd( LOG_DEBUG1, "Binding to %s...", bla );
		int sock = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
		if ( sock < 0 ) {
			logadd( LOG_WARNING, "(Bind to %s): cannot socket(), errno=%d", bla, errno );
			continue;
		}
		setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
		if ( ptr->ai_family == PF_INET6 ) setsockopt( sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on) );
		if ( bind( sock, ptr->ai_addr, ptr->ai_addrlen ) == -1 ) {
			logadd( LOG_WARNING, "(Bind to %s): cannot bind(), errno=%d", bla, errno );
			close( sock );
			continue;
		}
		if ( listen( sock, 20 ) == -1 ) {
			logadd( LOG_WARNING, "(Bind to %s): cannot listen(), errno=%d", bla, errno );
			close( sock );
			continue;
		}
		list->entry[list->count].fd = sock;
		list->entry[list->count].events = POLLIN | POLLRDHUP;
		list->count++;
		openCount++;
		if ( list->count >= MAXLISTEN ) break;
	}
	freeaddrinfo( res );
	return openCount > 0;
}

bool sock_listenAny(poll_list_t* list, uint16_t port)
{
	return sock_listen( list, NULL, port );
}

int sock_multiConnect(poll_list_t* list, const dnbd3_host_t* host, int connect_ms, int rw_ms)
{
	// Nonblocking connect seems to be hard to get right in a portable fashion
	// that's why you might see some weird checks here and there. For now there's
	// only Linux and FreeBSD, but let's try to not make this code fall on its nose
	// should dnbd3 be ported to other platforms.
	if ( list->count < MAXLISTEN && host != NULL ) {
		int sock = sock_connect( host, -1, -1 );
		if ( sock != -1 ) {
			list->entry[list->count].fd = sock;
			list->entry[list->count].events = POLLIN | POLLOUT | POLLRDHUP;
			list->count++;
		}
	}
	if ( list->count == 0 ) {
		return -2;
	}
	int ret, tries = 5;
	do {
		ret = poll( list->entry, list->count, connect_ms );
		if ( ret > 0 ) break;
		if ( ret == 0 ) return -1;
		if ( ret == -1 && ( errno == EINTR || errno == EAGAIN ) ) {
			if ( --tries == 0 ) return -1;
			if ( connect_ms > 1 ) connect_ms /= 2; // Maybe properly account time one day
			continue;
		}
		return -1;
	} while ( true );
	for ( int i = list->count - 1; i >= 0; --i ) {
		int fd = -1;
		if ( list->entry[i].revents & ( POLLIN | POLLOUT ) ) {
			struct sockaddr_storage tmp;
			socklen_t len = sizeof(tmp);
			fd = list->entry[i].fd;
			if ( getpeername( fd, (struct sockaddr*)&tmp, &len ) == -1 ) { // More portable then SO_ERROR ...
				close( fd );
				fd = -1;
			}
		} else if ( list->entry[i].revents != 0 ) {
			close( list->entry[i].fd );
		} else {
			continue;
		}
		// Either error or connect success
		list->count--;
		if ( i != list->count ) list->entry[i] = list->entry[list->count];
		if ( fd != -1 ) {
			sock_set_block( fd );
			if ( rw_ms != -1 && rw_ms != connect_ms ) {
				sock_setTimeout( fd, rw_ms );
			}
			return fd;
		}
	}
	return -1;
}

int sock_accept(poll_list_t *list, struct sockaddr_storage *addr, socklen_t *length_ptr)
{
	int ret = poll( list->entry, list->count, -1 );
	if ( ret < 0 ) {
		return -1;
	}
	for ( int i = list->count - 1; i >= 0; --i ) {
		if ( list->entry[i].revents == 0 ) continue;
		if ( list->entry[i].revents == POLLIN ) return accept( list->entry[i].fd, (struct sockaddr *)addr, length_ptr );
		if ( list->entry[i].revents & ( POLLNVAL | POLLHUP | POLLERR | POLLRDHUP ) ) {
			logadd( LOG_DEBUG1, "poll fd revents=%d for index=%d and fd=%d", (int)list->entry[i].revents, i, list->entry[i].fd );
			close( list->entry[i].fd );
			list->count--;
			if ( i != list->count ) list->entry[i] = list->entry[list->count];
		}
	}
	return -1;
}

void sock_set_nonblock(int sock)
{
	int flags = fcntl( sock, F_GETFL, 0 );
	if ( flags == -1 ) flags = 0;
	fcntl( sock, F_SETFL, flags | O_NONBLOCK );
}

void sock_set_block(int sock)
{
	int flags = fcntl( sock, F_GETFL, 0 );
	if ( flags == -1 ) flags = 0;
	fcntl( sock, F_SETFL, flags & ~(int)O_NONBLOCK );
}

bool sock_append(poll_list_t *list, const int sock, bool wantRead, bool wantWrite)
{
	if ( sock == -1 || list->count >= MAXLISTEN ) return false;
	list->entry[list->count++].fd = sock;
	list->entry[list->count++].events = (short)( ( wantRead ? POLLIN : 0 ) | ( wantWrite ? POLLOUT : 0 ) | POLLRDHUP );
	list->count++;
	return true;
}

ssize_t sock_sendAll(const int sock, const void *buffer, const size_t len, int maxtries)
{
	size_t done = 0;
	ssize_t ret = 0;
	while ( done < len ) {
		if ( maxtries >= 0 && --maxtries == -1 ) break;
		ret = send( sock, (const uint8_t*)buffer + done, len - done, MSG_NOSIGNAL );
		if ( ret == -1 ) {
			if ( errno == EINTR ) continue;
			if ( errno == EAGAIN || errno == EWOULDBLOCK ) {
				continue;
			}
			break;
		}
		if ( ret == 0 ) break;
		done += ret;
	}
	if ( done == 0 ) return ret;
	return done;
}

ssize_t sock_recv(const int sock, void *buffer, const size_t len)
{
	size_t done = 0;
	ssize_t ret = 0;
	int intrs = 0;
	while ( done < len ) {
		ret = recv( sock, (char*)buffer + done, len - done, MSG_NOSIGNAL );
		if ( ret == -1 ) {
			if ( errno == EINTR && ++intrs < 10 ) continue;
			break;
		}
		if ( ret == 0 ) break;
		done += ret;
	}
	if ( done == 0 ) return ret;
	return done;
}