summaryrefslogtreecommitdiff
path: root/src/libs/network/connect/listen.c
blob: 4a7a65c30759c9c37d46f33e80788747d54a164b (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
/*
 *  Copyright 2006  Serge van den Boom <svdb@stack.nl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define PORT_WANT_ERRNO
#include "port.h"
#include "../netport.h"

#define LISTEN_INTERNAL
#define SOCKET_INTERNAL
#include "listen.h"

#include "resolve.h"
#include "../socket/socket.h"
#include "../netmanager/netmanager.h"
#include "libs/misc.h"
#include "libs/log.h"

#include <assert.h>
#include <errno.h>
#ifdef USE_WINSOCK
#	include <winsock2.h>
#	include <ws2tcpip.h>
#	include "../wspiapiwrap.h"
#else
#	include <netdb.h>
#endif
#include <stdlib.h>
#include <string.h>

#define DEBUG_LISTEN_REF
#ifdef DEBUG_LISTEN_REF
#	include "types.h"
#endif


static void acceptCallback(NetDescriptor *nd);
static void doListenErrorCallback(ListenState *listenState,
		const ListenError *error);


static ListenState *
ListenState_alloc(void) {
	return (ListenState *) malloc(sizeof (ListenState));
}

static void
ListenState_free(ListenState *listenState) {
	free(listenState);
}

static void
ListenState_delete(ListenState *listenState) {
	assert(listenState->nds == NULL);
	ListenState_free(listenState);
}

void
ListenState_incRef(ListenState *listenState) {
	assert(listenState->refCount < REFCOUNT_MAX);
	listenState->refCount++;
#ifdef DEBUG_LISTEN_REF
	log_add(log_Debug, "ListenState %08" PRIxPTR ": ref++ (%d)",
			(uintptr_t) listenState, listenState->refCount);
#endif
}

bool
ListenState_decRef(ListenState *listenState) {
	assert(listenState->refCount > 0);
	listenState->refCount--;
#ifdef DEBUG_LISTEN_REF
	log_add(log_Debug, "ListenState %08" PRIxPTR ": ref-- (%d)",
			(uintptr_t) listenState, listenState->refCount);
#endif
	if (listenState->refCount == 0) {
		ListenState_delete(listenState);
		return true;
	}
	return false;
}

// Decrements ref count byh 1
void
ListenState_close(ListenState *listenState) {
	if (listenState->resolveState != NULL) {
		Resolve_close(listenState->resolveState);
		listenState->resolveState = NULL;
	}
	if (listenState->nds != NULL) {
		while (listenState->numNd > 0) {
			listenState->numNd--;
			NetDescriptor_close(listenState->nds[listenState->numNd]);
		}
		free(listenState->nds);
		listenState->nds = NULL;
	}
	listenState->state = Listen_closed;
	ListenState_decRef(listenState);
}

void
ListenState_setExtra(ListenState *listenState, void *extra) {
	listenState->extra = extra;
}

void *
ListenState_getExtra(ListenState *listenState) {
	return listenState->extra;
}

static NetDescriptor *
listenPortSingle(struct ListenState *listenState, struct addrinfo *info) {
	Socket *sock;
	int bindResult;
	int listenResult;
	NetDescriptor *nd;

	sock = Socket_openNative(info->ai_family, info->ai_socktype,
			info->ai_protocol);
	if (sock == Socket_noSocket) {
		int savedErrno = errno;
		log_add(log_Error, "socket() failed: %s.", strerror(errno));
		errno = savedErrno;
		return NULL;
	}

	(void) Socket_setReuseAddr(sock);
			// Ignore errors; it's not a big deal.
	if (Socket_setNonBlocking(sock) == -1) {
		int savedErrno = errno;
		// Error message is already printed.
		Socket_close(sock);
		errno = savedErrno;
		return NULL;
	}
	
	bindResult = Socket_bind(sock, info->ai_addr, info->ai_addrlen);
	if (bindResult == -1) {
		int savedErrno = errno;
		if (errno == EADDRINUSE) {
#ifdef DEBUG
			log_add(log_Warning, "bind() failed: %s.", strerror(errno));
#endif
		} else
			log_add(log_Error, "bind() failed: %s.", strerror(errno));
		Socket_close(sock);
		errno = savedErrno;
		return NULL;
	}

	listenResult = Socket_listen(sock, listenState->flags.backlog);
	if (listenResult == -1) {
		int savedErrno = errno;
		log_add(log_Error, "listen() failed: %s.", strerror(errno));
		Socket_close(sock);
		errno = savedErrno;
		return NULL;
	}

	nd = NetDescriptor_new(sock, (void *) listenState);
	if (nd == NULL) {
		int savedErrno = errno;
		log_add(log_Error, "NetDescriptor_new() failed: %s.",
				strerror(errno));
		Socket_close(sock);
		errno = savedErrno;
		return NULL;
	}
		
	NetDescriptor_setReadCallback(nd, acceptCallback);

	return nd;
}

static void
listenPortMulti(struct ListenState *listenState, struct addrinfo *info) {
	struct addrinfo *addrPtr;
	size_t addrCount;
	size_t addrOkCount;
	NetDescriptor **nds;

	// Count how many addresses we've got.
	addrCount = 0;
	for (addrPtr = info; addrPtr != NULL; addrPtr = addrPtr->ai_next)
		addrCount++;

	// This is where we intend to store the file descriptors of the
	// listening sockets.
	nds = malloc(addrCount * sizeof listenState->nds[0]);

	// Bind to each address.
	addrOkCount = 0;
	for (addrPtr = info; addrPtr != NULL; addrPtr = addrPtr->ai_next) {
		NetDescriptor *nd;
		nd = listenPortSingle(listenState, addrPtr);
		if (nd == NULL) {
			// Failed. On to the next.
			// An error message is already printed for serious errors.
			// If the address is already in use, we here also print
			// a message when we are not already listening on one of
			// the other addresses.
			// This is because on some IPv6 capabable systems (like Linux),
			// IPv6 sockets also handle IPv4 connections, which means
			// that a separate IPv4 socket won't be able to bind to the
			// port.
			// BUG: if the IPv4 socket is in the list before the
			// IPv6 socket, it will be the IPv6 which will fail to bind,
			// so only IPv4 connections will be handled, as v4 sockets can't
			// accept v6 connections.
			// In practice, on Linux, I haven't seen it happen, but
			// it's a real possibility.
			if (errno == EADDRINUSE && addrOkCount == 0) {
				log_add(log_Error, "Error while preparing a network socket "
						"for incoming connections: %s", strerror(errno));
			}
			continue;
		}
		
		nds[addrOkCount] = nd;
		addrOkCount++;
	}

	freeaddrinfo(info);

	listenState->nds =
			realloc(nds, addrOkCount * sizeof listenState->nds[0]);
	listenState->numNd = addrOkCount;

	if (addrOkCount == 0) {
		// Could not listen on any port.
		ListenError error;
		error.state = Listen_listening;
		error.err = EIO;
				// Nothing better to offer.
		doListenErrorCallback(listenState, &error);
		return;
	}
}

static void
listenPortResolveCallback(ResolveState *resolveState,
		struct addrinfo *result) {
	ListenState *listenState =
			(ListenState *) ResolveState_getExtra(resolveState);
	Resolve_close(listenState->resolveState);
	listenState->resolveState = NULL;
	listenState->state = Listen_listening;
	listenPortMulti(listenState, result);
}

static void
listenPortResolveErrorCallback(ResolveState *resolveState,
		const ResolveError *resolveError) {
	ListenState *listenState =
			(ListenState *) ResolveState_getExtra(resolveState);
	ListenError listenError;

	assert(resolveError->gaiRes != 0);

	listenError.state = Listen_resolving;
	listenError.resolveError = resolveError;
	listenError.err = resolveError->err;
	doListenErrorCallback(listenState, &listenError);
}

// 'proto' is one of IPProto_tcp or IPProto_udp.
ListenState *
listenPort(const char *service, Protocol proto, const ListenFlags *flags,
		ListenConnectCallback connectCallback,
		ListenErrorCallback errorCallback, void *extra) {
	struct addrinfo	hints;
	ListenState *listenState;
	ResolveFlags resolveFlags;
			// Structure is empty (for now).

	assert(flags->familyDemand == PF_inet ||
			flags->familyDemand == PF_inet6 ||
			flags->familyDemand == PF_unspec);
	assert(flags->familyPrefer == PF_inet ||
			flags->familyPrefer == PF_inet6 ||
			flags->familyPrefer == PF_unspec);
	assert(proto == IPProto_tcp || proto == IPProto_udp);

	// Acquire a list of addresses to bind to.
	memset(&hints, '\0', sizeof hints);
	hints.ai_family = protocolFamilyTranslation[flags->familyDemand];
	hints.ai_protocol = protocolTranslation[proto];

	if (proto == IPProto_tcp) {
		hints.ai_socktype = SOCK_STREAM;
	} else {
		assert(proto == IPProto_udp);
		hints.ai_socktype = SOCK_DGRAM;
	}
	hints.ai_flags = AI_PASSIVE;

	listenState = ListenState_alloc();
	listenState->refCount = 1;
#ifdef DEBUG_LISTEN_REF
	log_add(log_Debug, "ListenState %08" PRIxPTR ": ref=1 (%d)",
			(uintptr_t) listenState, listenState->refCount);
#endif
	listenState->state = Listen_resolving;
	listenState->flags = *flags;
	listenState->connectCallback = connectCallback;
	listenState->errorCallback = errorCallback;
	listenState->extra = extra;
	listenState->nds = NULL;
	listenState->numNd = 0;

	listenState->resolveState = getaddrinfoAsync(NULL, service, &hints,
			&resolveFlags, listenPortResolveCallback,
			listenPortResolveErrorCallback,
			(ResolveCallbackArg) listenState);

	return listenState;
}

// NB: The callback function becomes the owner of newNd.
static void
doListenConnectCallback(ListenState *listenState, NetDescriptor *listenNd,
		NetDescriptor *newNd,
		const struct sockaddr *addr, SOCKLEN_T addrLen) {
	assert(listenState->connectCallback != NULL);

	ListenState_incRef(listenState);
	// No need to increment listenNd, as there's guaranteed to be one
	// reference from listenState. And no need to increment newNd,
	// as the callback function takes over ownership.
	(*listenState->connectCallback)(listenState, listenNd, newNd,
			addr, (socklen_t) addrLen);
	ListenState_decRef(listenState);
}

static void
doListenErrorCallback(ListenState *listenState, const ListenError *error) {
	assert(listenState->errorCallback != NULL);

	ListenState_incRef(listenState);
	(*listenState->errorCallback)(listenState, error);
	ListenState_decRef(listenState);
}

static void
acceptSingleConnection(ListenState *listenState, NetDescriptor *nd) {
	Socket *sock;
	Socket *acceptResult;
	struct sockaddr_storage addr;
	socklen_t addrLen;
	NetDescriptor *newNd;

	sock = NetDescriptor_getSocket(nd);
	addrLen = sizeof (addr);
	acceptResult = Socket_accept(sock, (struct sockaddr *) &addr, &addrLen);
	if (acceptResult == Socket_noSocket) {
		switch (errno) {
			case EWOULDBLOCK:
			case ECONNABORTED:
				// Nothing serious. Keep listening.
				return;
			case EMFILE:
			case ENFILE:
			case ENOBUFS:
			case ENOMEM:
#ifdef ENOSR
			case ENOSR:
#endif
				// Serious problems, but future connections may still
				// be possible.
				log_add(log_Warning, "accept() reported '%s'",
						strerror(errno));
				return;
			default:
				// Should not happen.
				log_add(log_Fatal, "Internal error: accept() reported "
						"'%s'", strerror(errno));
				explode();
		}
	}

	(void) Socket_setReuseAddr(acceptResult);
			// Ignore errors; it's not a big deal.
	if (Socket_setNonBlocking(acceptResult) == -1) {
		int savedErrno = errno;
		log_add(log_Error, "Could not make socket non-blocking: %s.",
				strerror(errno));
		Socket_close(acceptResult);
		errno = savedErrno;
		return;
	}
	(void) Socket_setInlineOOB(acceptResult);
			// Ignore errors; it's not a big deal as the other
			// party is not not supposed to send any OOB data.
	(void) Socket_setKeepAlive(sock);
			// Ignore errors; it's not a big deal.

#ifdef DEBUG
	{
		char hostname[NI_MAXHOST];
		int gniRes;

		gniRes = getnameinfo((struct sockaddr *) &addr, addrLen,
				hostname, sizeof hostname, NULL, 0, 0);
		if (gniRes != 0) {
			log_add(log_Error, "Error while performing hostname "
					"lookup for incoming connection: %s",
					(gniRes == EAI_SYSTEM) ? strerror(errno) :
					gai_strerror(gniRes));
		} else {
			log_add(log_Debug, "Accepted incoming connection from '%s'.",
					hostname);
		}
	}
#endif
	
	newNd = NetDescriptor_new(acceptResult, NULL);
	if (newNd == NULL) {
		int savedErrno = errno;
		log_add(log_Error, "NetDescriptor_new() failed: %s.",
				strerror(errno));
		Socket_close(acceptResult);
		errno = savedErrno;
		return;
	}

	doListenConnectCallback(listenState, nd, newNd,
			(struct sockaddr *) &addr, addrLen);
	// NB: newNd is now handed over to the callback function, and should
	//     no longer be referenced from here.
}

// Called when select() has indicated readability on a listening socket,
// i.e. when a connection is in the queue.
static void
acceptCallback(NetDescriptor *nd) {
	ListenState *listenState = (ListenState *) NetDescriptor_getExtra(nd);

	acceptSingleConnection(listenState, nd);
}