xref: /lighttpd1.4/src/network.c (revision d147673d)
1 #include "first.h"
2 
3 #include "network.h"
4 #include "fdevent.h"
5 #include "log.h"
6 #include "connections.h"
7 #include "plugin.h"
8 #include "joblist.h"
9 #include "configfile.h"
10 
11 #include "network_backends.h"
12 #include "sys-mmap.h"
13 #include "sys-socket.h"
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 
26 #ifdef USE_OPENSSL
27 # include <openssl/ssl.h>
28 # include <openssl/err.h>
29 # include <openssl/rand.h>
30 # ifndef OPENSSL_NO_DH
31 #  include <openssl/dh.h>
32 # endif
33 # include <openssl/bn.h>
34 
35 # if OPENSSL_VERSION_NUMBER >= 0x0090800fL
36 #  ifndef OPENSSL_NO_ECDH
37 # include <openssl/ecdh.h>
38 #  endif
39 # endif
40 #endif
41 
42 #ifdef USE_OPENSSL
43 static void ssl_info_callback(const SSL *ssl, int where, int ret) {
44 	UNUSED(ret);
45 
46 	if (0 != (where & SSL_CB_HANDSHAKE_START)) {
47 		connection *con = SSL_get_app_data(ssl);
48 		++con->renegotiations;
49 	}
50 }
51 #endif
52 
53 void
54 network_accept_tcp_nagle_disable (const int fd)
55 {
56     static int noinherit_tcpnodelay = -1;
57     int opt;
58 
59     if (!noinherit_tcpnodelay) /* TCP_NODELAY inherited from listen socket */
60         return;
61 
62     if (noinherit_tcpnodelay < 0) {
63         socklen_t optlen = sizeof(opt);
64         if (0 == getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen)) {
65             noinherit_tcpnodelay = !opt;
66             if (opt)           /* TCP_NODELAY inherited from listen socket */
67                 return;
68         }
69     }
70 
71     opt = 1;
72     (void)setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
73 }
74 
75 static handler_t network_server_handle_fdevent(server *srv, void *context, int revents) {
76 	server_socket *srv_socket = (server_socket *)context;
77 	connection *con;
78 	int loops = 0;
79 
80 	UNUSED(context);
81 
82 	if (0 == (revents & FDEVENT_IN)) {
83 		log_error_write(srv, __FILE__, __LINE__, "sdd",
84 				"strange event for server socket",
85 				srv_socket->fd,
86 				revents);
87 		return HANDLER_ERROR;
88 	}
89 
90 	/* accept()s at most 100 connections directly
91 	 *
92 	 * we jump out after 100 to give the waiting connections a chance */
93 	for (loops = 0; loops < 100 && NULL != (con = connection_accept(srv, srv_socket)); loops++) {
94 		connection_state_machine(srv, con);
95 	}
96 	return HANDLER_GO_ON;
97 }
98 
99 #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT
100 static int network_ssl_servername_callback(SSL *ssl, int *al, server *srv) {
101 	const char *servername;
102 	connection *con = (connection *) SSL_get_app_data(ssl);
103 	UNUSED(al);
104 
105 	buffer_copy_string(con->uri.scheme, "https");
106 
107 	if (NULL == (servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name))) {
108 #if 0
109 		/* this "error" just means the client didn't support it */
110 		log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
111 				"failed to get TLS server name");
112 #endif
113 		return SSL_TLSEXT_ERR_NOACK;
114 	}
115 	buffer_copy_string(con->tlsext_server_name, servername);
116 	buffer_to_lower(con->tlsext_server_name);
117 
118 	/* Sometimes this is still set, confusing COMP_HTTP_HOST */
119 	buffer_reset(con->uri.authority);
120 
121 	config_cond_cache_reset(srv, con);
122 	config_setup_connection(srv, con);
123 
124 	con->conditional_is_valid[COMP_SERVER_SOCKET] = 1;
125 	con->conditional_is_valid[COMP_HTTP_SCHEME] = 1;
126 	con->conditional_is_valid[COMP_HTTP_HOST] = 1;
127 	config_patch_connection(srv, con);
128 
129 	if (NULL == con->conf.ssl_pemfile_x509 || NULL == con->conf.ssl_pemfile_pkey) {
130 		/* x509/pkey available <=> pemfile was set <=> pemfile got patched: so this should never happen, unless you nest $SERVER["socket"] */
131 		log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
132 			"no certificate/private key for TLS server name", con->tlsext_server_name);
133 		return SSL_TLSEXT_ERR_ALERT_FATAL;
134 	}
135 
136 	/* first set certificate! setting private key checks whether certificate matches it */
137 	if (!SSL_use_certificate(ssl, con->conf.ssl_pemfile_x509)) {
138 		log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
139 			"failed to set certificate for TLS server name", con->tlsext_server_name,
140 			ERR_error_string(ERR_get_error(), NULL));
141 		return SSL_TLSEXT_ERR_ALERT_FATAL;
142 	}
143 
144 	if (!SSL_use_PrivateKey(ssl, con->conf.ssl_pemfile_pkey)) {
145 		log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
146 			"failed to set private key for TLS server name", con->tlsext_server_name,
147 			ERR_error_string(ERR_get_error(), NULL));
148 		return SSL_TLSEXT_ERR_ALERT_FATAL;
149 	}
150 
151 	if (con->conf.ssl_verifyclient) {
152 		if (NULL == con->conf.ssl_ca_file_cert_names) {
153 			log_error_write(srv, __FILE__, __LINE__, "ssb:s", "SSL:",
154 				"can't verify client without ssl.ca-file for TLS server name", con->tlsext_server_name,
155 				ERR_error_string(ERR_get_error(), NULL));
156 			return SSL_TLSEXT_ERR_ALERT_FATAL;
157 		}
158 
159 		SSL_set_client_CA_list(ssl, SSL_dup_CA_list(con->conf.ssl_ca_file_cert_names));
160 		/* forcing verification here is really not that useful - a client could just connect without SNI */
161 		SSL_set_verify(
162 			ssl,
163 			SSL_VERIFY_PEER | (con->conf.ssl_verifyclient_enforce ? SSL_VERIFY_FAIL_IF_NO_PEER_CERT : 0),
164 			NULL
165 		);
166 		SSL_set_verify_depth(ssl, con->conf.ssl_verifyclient_depth);
167 	} else {
168 		SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
169 	}
170 
171 	return SSL_TLSEXT_ERR_OK;
172 }
173 #endif
174 
175 static int network_server_init(server *srv, buffer *host_token, specific_config *s) {
176 	int val;
177 	socklen_t addr_len;
178 	server_socket *srv_socket;
179 	unsigned int port = 0;
180 	const char *host;
181 	buffer *b;
182 	int err;
183 
184 #ifdef __WIN32
185 	WORD wVersionRequested;
186 	WSADATA wsaData;
187 
188 	wVersionRequested = MAKEWORD( 2, 2 );
189 
190 	err = WSAStartup( wVersionRequested, &wsaData );
191 	if ( err != 0 ) {
192 		    /* Tell the user that we could not find a usable */
193 		    /* WinSock DLL.                                  */
194 		    return -1;
195 	}
196 #endif
197 	err = -1;
198 
199 	srv_socket = calloc(1, sizeof(*srv_socket));
200 	force_assert(NULL != srv_socket);
201 	srv_socket->addr.plain.sa_family = AF_INET; /* default */
202 	srv_socket->fd = -1;
203 	srv_socket->fde_ndx = -1;
204 
205 	srv_socket->srv_token = buffer_init();
206 	buffer_copy_buffer(srv_socket->srv_token, host_token);
207 
208 	b = buffer_init();
209 	buffer_copy_buffer(b, host_token);
210 
211 	host = b->ptr;
212 
213 	if (host[0] == '/') {
214 		/* host is a unix-domain-socket */
215 #ifdef HAVE_SYS_UN_H
216 		srv_socket->addr.plain.sa_family = AF_UNIX;
217 #else
218 		log_error_write(srv, __FILE__, __LINE__, "s",
219 				"ERROR: Unix Domain sockets are not supported.");
220 		goto error_free_socket;
221 #endif
222 	} else {
223 		/* ipv4:port
224 		 * [ipv6]:port
225 		 */
226 		size_t len = buffer_string_length(b);
227 		char *sp = NULL;
228 		if (0 == len) {
229 			log_error_write(srv, __FILE__, __LINE__, "s", "value of $SERVER[\"socket\"] must not be empty");
230 			goto error_free_socket;
231 		}
232 		if ((b->ptr[0] == '[' && b->ptr[len-1] == ']') || NULL == (sp = strrchr(b->ptr, ':'))) {
233 			/* use server.port if set in config, or else default from config_set_defaults() */
234 			port = srv->srvconf.port;
235 			sp = b->ptr + len; /* point to '\0' at end of string so end of IPv6 address can be found below */
236 		} else {
237 			/* found ip:port separator at *sp; port doesn't end in ']', so *sp hopefully doesn't split an IPv6 address */
238 			*sp = '\0';
239 			port = strtol(sp+1, NULL, 10);
240 		}
241 
242 		/* check for [ and ] */
243 		if (b->ptr[0] == '[' && *(sp-1) == ']') {
244 			*(sp-1) = '\0';
245 			host++;
246 
247 			s->use_ipv6 = 1;
248 		}
249 
250 		if (port == 0 || port > 65535) {
251 			log_error_write(srv, __FILE__, __LINE__, "sd", "port not set or out of range:", port);
252 
253 			goto error_free_socket;
254 		}
255 	}
256 
257 	if (*host == '\0') host = NULL;
258 
259 #ifdef HAVE_IPV6
260 	if (s->use_ipv6) {
261 		srv_socket->addr.plain.sa_family = AF_INET6;
262 	}
263 #endif
264 
265 	switch(srv_socket->addr.plain.sa_family) {
266 #ifdef HAVE_IPV6
267 	case AF_INET6:
268 		memset(&srv_socket->addr, 0, sizeof(struct sockaddr_in6));
269 		srv_socket->addr.ipv6.sin6_family = AF_INET6;
270 		if (host == NULL) {
271 			srv_socket->addr.ipv6.sin6_addr = in6addr_any;
272 			log_error_write(srv, __FILE__, __LINE__, "s", "warning: please use server.use-ipv6 only for hostnames, not without server.bind / empty address; your config will break if the kernel default for IPV6_V6ONLY changes");
273 		} else {
274 			struct addrinfo hints, *res;
275 			int r;
276 
277 			memset(&hints, 0, sizeof(hints));
278 
279 			hints.ai_family   = AF_INET6;
280 			hints.ai_socktype = SOCK_STREAM;
281 			hints.ai_protocol = IPPROTO_TCP;
282 
283 			if (0 != (r = getaddrinfo(host, NULL, &hints, &res))) {
284 				log_error_write(srv, __FILE__, __LINE__,
285 						"sssss", "getaddrinfo failed: ",
286 						gai_strerror(r), "'", host, "'");
287 
288 				goto error_free_socket;
289 			}
290 
291 			memcpy(&(srv_socket->addr), res->ai_addr, res->ai_addrlen);
292 
293 			freeaddrinfo(res);
294 		}
295 		srv_socket->addr.ipv6.sin6_port = htons(port);
296 		addr_len = sizeof(struct sockaddr_in6);
297 		break;
298 #endif
299 	case AF_INET:
300 		memset(&srv_socket->addr, 0, sizeof(struct sockaddr_in));
301 		srv_socket->addr.ipv4.sin_family = AF_INET;
302 		if (host == NULL) {
303 			srv_socket->addr.ipv4.sin_addr.s_addr = htonl(INADDR_ANY);
304 		} else {
305 			struct hostent *he;
306 			if (NULL == (he = gethostbyname(host))) {
307 				log_error_write(srv, __FILE__, __LINE__,
308 						"sds", "gethostbyname failed: ",
309 						h_errno, host);
310 				goto error_free_socket;
311 			}
312 
313 			if (he->h_addrtype != AF_INET) {
314 				log_error_write(srv, __FILE__, __LINE__, "sd", "addr-type != AF_INET: ", he->h_addrtype);
315 				goto error_free_socket;
316 			}
317 
318 			if (he->h_length != sizeof(struct in_addr)) {
319 				log_error_write(srv, __FILE__, __LINE__, "sd", "addr-length != sizeof(in_addr): ", he->h_length);
320 				goto error_free_socket;
321 			}
322 
323 			memcpy(&(srv_socket->addr.ipv4.sin_addr.s_addr), he->h_addr_list[0], he->h_length);
324 		}
325 		srv_socket->addr.ipv4.sin_port = htons(port);
326 		addr_len = sizeof(struct sockaddr_in);
327 		break;
328 #ifdef HAVE_SYS_UN_H
329 	case AF_UNIX:
330 		memset(&srv_socket->addr, 0, sizeof(struct sockaddr_un));
331 		srv_socket->addr.un.sun_family = AF_UNIX;
332 		{
333 			size_t hostlen = strlen(host) + 1;
334 			if (hostlen > sizeof(srv_socket->addr.un.sun_path)) {
335 				log_error_write(srv, __FILE__, __LINE__, "sS", "unix socket filename too long:", host);
336 				goto error_free_socket;
337 			}
338 			memcpy(srv_socket->addr.un.sun_path, host, hostlen);
339 
340 #if defined(SUN_LEN)
341 			addr_len = SUN_LEN(&srv_socket->addr.un);
342 #else
343 			/* stevens says: */
344 			addr_len = hostlen + sizeof(srv_socket->addr.un.sun_family);
345 #endif
346 		}
347 
348 		break;
349 #endif
350 	default:
351 		goto error_free_socket;
352 	}
353 
354 	if (srv->srvconf.preflight_check) {
355 		err = 0;
356 		goto error_free_socket;
357 	}
358 
359 	if (srv->sockets_disabled) { /* lighttpd -1 (one-shot mode) */
360 #ifdef USE_OPENSSL
361 		if (s->ssl_enabled) srv_socket->ssl_ctx = s->ssl_ctx;
362 #endif
363 		goto srv_sockets_append;
364 	}
365 
366 #ifdef HAVE_SYS_UN_H
367 	if (AF_UNIX == srv_socket->addr.plain.sa_family) {
368 		/* check if the socket exists and try to connect to it. */
369 		force_assert(host); /*(static analysis hint)*/
370 		if (-1 == (srv_socket->fd = socket(srv_socket->addr.plain.sa_family, SOCK_STREAM, 0))) {
371 			log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed:", strerror(errno));
372 			goto error_free_socket;
373 		}
374 		if (0 == connect(srv_socket->fd, (struct sockaddr *) &(srv_socket->addr), addr_len)) {
375 			log_error_write(srv, __FILE__, __LINE__, "ss",
376 				"server socket is still in use:",
377 				host);
378 
379 
380 			goto error_free_socket;
381 		}
382 
383 		/* connect failed */
384 		switch(errno) {
385 		case ECONNREFUSED:
386 			unlink(host);
387 			break;
388 		case ENOENT:
389 			break;
390 		default:
391 			log_error_write(srv, __FILE__, __LINE__, "sds",
392 				"testing socket failed:",
393 				host, strerror(errno));
394 
395 			goto error_free_socket;
396 		}
397 	} else
398 #endif
399 	{
400 		if (-1 == (srv_socket->fd = socket(srv_socket->addr.plain.sa_family, SOCK_STREAM, IPPROTO_TCP))) {
401 			log_error_write(srv, __FILE__, __LINE__, "ss", "socket failed:", strerror(errno));
402 			goto error_free_socket;
403 		}
404 
405 #ifdef HAVE_IPV6
406 		if (AF_INET6 == srv_socket->addr.plain.sa_family
407 		    && host != NULL) {
408 			if (s->set_v6only) {
409 				val = 1;
410 				if (-1 == setsockopt(srv_socket->fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val))) {
411 					log_error_write(srv, __FILE__, __LINE__, "ss", "socketsockopt(IPV6_V6ONLY) failed:", strerror(errno));
412 					goto error_free_socket;
413 				}
414 			} else {
415 				log_error_write(srv, __FILE__, __LINE__, "s", "warning: server.set-v6only will be removed soon, update your config to have different sockets for ipv4 and ipv6");
416 			}
417 		}
418 #endif
419 	}
420 
421 	/* set FD_CLOEXEC now, fdevent_fcntl_set is called later; needed for pipe-logger forks */
422 	fd_close_on_exec(srv_socket->fd);
423 
424 	/* */
425 	srv->cur_fds = srv_socket->fd;
426 
427 	val = 1;
428 	if (setsockopt(srv_socket->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
429 		log_error_write(srv, __FILE__, __LINE__, "ss", "socketsockopt(SO_REUSEADDR) failed:", strerror(errno));
430 		goto error_free_socket;
431 	}
432 
433 	if (srv_socket->addr.plain.sa_family != AF_UNIX) {
434 		val = 1;
435 		if (setsockopt(srv_socket->fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) < 0) {
436 			log_error_write(srv, __FILE__, __LINE__, "ss", "socketsockopt(TCP_NODELAY) failed:", strerror(errno));
437 			goto error_free_socket;
438 		}
439 	}
440 
441 	if (0 != bind(srv_socket->fd, (struct sockaddr *) &(srv_socket->addr), addr_len)) {
442 		switch(srv_socket->addr.plain.sa_family) {
443 		case AF_UNIX:
444 			log_error_write(srv, __FILE__, __LINE__, "sds",
445 					"can't bind to socket:",
446 					host, strerror(errno));
447 			break;
448 		default:
449 			log_error_write(srv, __FILE__, __LINE__, "ssds",
450 					"can't bind to port:",
451 					host, port, strerror(errno));
452 			break;
453 		}
454 		goto error_free_socket;
455 	}
456 
457 	if (-1 == listen(srv_socket->fd, s->listen_backlog)) {
458 		log_error_write(srv, __FILE__, __LINE__, "ss", "listen failed: ", strerror(errno));
459 		goto error_free_socket;
460 	}
461 
462 	if (s->ssl_enabled) {
463 #ifdef USE_OPENSSL
464 		if (NULL == (srv_socket->ssl_ctx = s->ssl_ctx)) {
465 			log_error_write(srv, __FILE__, __LINE__, "s", "ssl.pemfile has to be set");
466 			goto error_free_socket;
467 		}
468 #else
469 
470 		log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
471 				"ssl requested but openssl support is not compiled in");
472 
473 		goto error_free_socket;
474 #endif
475 #ifdef TCP_DEFER_ACCEPT
476 	} else if (s->defer_accept) {
477 		int v = s->defer_accept;
478 		if (-1 == setsockopt(srv_socket->fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &v, sizeof(v))) {
479 			log_error_write(srv, __FILE__, __LINE__, "ss", "can't set TCP_DEFER_ACCEPT: ", strerror(errno));
480 		}
481 #endif
482 #if defined(__FreeBSD__) || defined(__NetBSD__) \
483  || defined(__OpenBSD__) || defined(__DragonflyBSD__)
484 	} else if (!buffer_is_empty(s->bsd_accept_filter)
485 		   && (buffer_is_equal_string(s->bsd_accept_filter, CONST_STR_LEN("httpready"))
486 			|| buffer_is_equal_string(s->bsd_accept_filter, CONST_STR_LEN("dataready")))) {
487 #ifdef SO_ACCEPTFILTER
488 		/* FreeBSD accf_http filter */
489 		struct accept_filter_arg afa;
490 		memset(&afa, 0, sizeof(afa));
491 		strncpy(afa.af_name, s->bsd_accept_filter->ptr, sizeof(afa.af_name));
492 		if (setsockopt(srv_socket->fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa)) < 0) {
493 			if (errno != ENOENT) {
494 				log_error_write(srv, __FILE__, __LINE__, "SBss", "can't set accept-filter '", s->bsd_accept_filter, "':", strerror(errno));
495 			}
496 		}
497 #endif
498 #endif
499 	}
500 
501 srv_sockets_append:
502 	srv_socket->is_ssl = s->ssl_enabled;
503 
504 	if (srv->srv_sockets.size == 0) {
505 		srv->srv_sockets.size = 4;
506 		srv->srv_sockets.used = 0;
507 		srv->srv_sockets.ptr = malloc(srv->srv_sockets.size * sizeof(server_socket*));
508 		force_assert(NULL != srv->srv_sockets.ptr);
509 	} else if (srv->srv_sockets.used == srv->srv_sockets.size) {
510 		srv->srv_sockets.size += 4;
511 		srv->srv_sockets.ptr = realloc(srv->srv_sockets.ptr, srv->srv_sockets.size * sizeof(server_socket*));
512 		force_assert(NULL != srv->srv_sockets.ptr);
513 	}
514 
515 	srv->srv_sockets.ptr[srv->srv_sockets.used++] = srv_socket;
516 
517 	buffer_free(b);
518 
519 	return 0;
520 
521 error_free_socket:
522 	if (srv_socket->fd != -1) {
523 		/* check if server fd are already registered */
524 		if (srv_socket->fde_ndx != -1) {
525 			fdevent_event_del(srv->ev, &(srv_socket->fde_ndx), srv_socket->fd);
526 			fdevent_unregister(srv->ev, srv_socket->fd);
527 		}
528 
529 		close(srv_socket->fd);
530 	}
531 	buffer_free(srv_socket->srv_token);
532 	free(srv_socket);
533 
534 	buffer_free(b);
535 
536 	return err; /* -1 if error; 0 if srv->srvconf.preflight_check successful */
537 }
538 
539 int network_close(server *srv) {
540 	size_t i;
541 	for (i = 0; i < srv->srv_sockets.used; i++) {
542 		server_socket *srv_socket = srv->srv_sockets.ptr[i];
543 
544 		if (srv_socket->fd != -1) {
545 			/* check if server fd are already registered */
546 			if (srv_socket->fde_ndx != -1) {
547 				fdevent_event_del(srv->ev, &(srv_socket->fde_ndx), srv_socket->fd);
548 				fdevent_unregister(srv->ev, srv_socket->fd);
549 			}
550 
551 			close(srv_socket->fd);
552 		}
553 
554 		buffer_free(srv_socket->srv_token);
555 
556 		free(srv_socket);
557 	}
558 
559 	free(srv->srv_sockets.ptr);
560 
561 	return 0;
562 }
563 
564 typedef enum {
565 	NETWORK_BACKEND_UNSET,
566 	NETWORK_BACKEND_WRITE,
567 	NETWORK_BACKEND_WRITEV,
568 	NETWORK_BACKEND_SENDFILE,
569 } network_backend_t;
570 
571 #ifdef USE_OPENSSL
572 static X509* x509_load_pem_file(server *srv, const char *file) {
573 	BIO *in;
574 	X509 *x = NULL;
575 
576 	in = BIO_new(BIO_s_file());
577 	if (NULL == in) {
578 		log_error_write(srv, __FILE__, __LINE__, "S", "SSL: BIO_new(BIO_s_file()) failed");
579 		goto error;
580 	}
581 
582 	if (BIO_read_filename(in,file) <= 0) {
583 		log_error_write(srv, __FILE__, __LINE__, "SSS", "SSL: BIO_read_filename('", file,"') failed");
584 		goto error;
585 	}
586 	x = PEM_read_bio_X509(in, NULL, NULL, NULL);
587 
588 	if (NULL == x) {
589 		log_error_write(srv, __FILE__, __LINE__, "SSS", "SSL: couldn't read X509 certificate from '", file,"'");
590 		goto error;
591 	}
592 
593 	BIO_free(in);
594 	return x;
595 
596 error:
597 	if (NULL != in) BIO_free(in);
598 	return NULL;
599 }
600 
601 static EVP_PKEY* evp_pkey_load_pem_file(server *srv, const char *file) {
602 	BIO *in;
603 	EVP_PKEY *x = NULL;
604 
605 	in=BIO_new(BIO_s_file());
606 	if (NULL == in) {
607 		log_error_write(srv, __FILE__, __LINE__, "s", "SSL: BIO_new(BIO_s_file()) failed");
608 		goto error;
609 	}
610 
611 	if (BIO_read_filename(in,file) <= 0) {
612 		log_error_write(srv, __FILE__, __LINE__, "SSS", "SSL: BIO_read_filename('", file,"') failed");
613 		goto error;
614 	}
615 	x = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
616 
617 	if (NULL == x) {
618 		log_error_write(srv, __FILE__, __LINE__, "SSS", "SSL: couldn't read private key from '", file,"'");
619 		goto error;
620 	}
621 
622 	BIO_free(in);
623 	return x;
624 
625 error:
626 	if (NULL != in) BIO_free(in);
627 	return NULL;
628 }
629 
630 static int network_openssl_load_pemfile(server *srv, size_t ndx) {
631 	specific_config *s = srv->config_storage[ndx];
632 
633 #ifdef OPENSSL_NO_TLSEXT
634 	{
635 		data_config *dc = (data_config *)srv->config_context->data[ndx];
636 		if ((ndx > 0 && (COMP_SERVER_SOCKET != dc->comp || dc->cond != CONFIG_COND_EQ))
637 			|| !s->ssl_enabled) {
638 			log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
639 					"ssl.pemfile only works in SSL socket binding context as openssl version does not support TLS extensions");
640 			return -1;
641 		}
642 	}
643 #endif
644 
645 	if (NULL == (s->ssl_pemfile_x509 = x509_load_pem_file(srv, s->ssl_pemfile->ptr))) return -1;
646 	if (NULL == (s->ssl_pemfile_pkey = evp_pkey_load_pem_file(srv, s->ssl_pemfile->ptr))) return -1;
647 
648 	if (!X509_check_private_key(s->ssl_pemfile_x509, s->ssl_pemfile_pkey)) {
649 		log_error_write(srv, __FILE__, __LINE__, "sssb", "SSL:",
650 				"Private key does not match the certificate public key, reason:",
651 				ERR_error_string(ERR_get_error(), NULL),
652 				s->ssl_pemfile);
653 		return -1;
654 	}
655 
656 	return 0;
657 }
658 #endif
659 
660 int network_init(server *srv) {
661 	buffer *b;
662 	size_t i, j;
663 	network_backend_t backend;
664 
665 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
666 #ifndef OPENSSL_NO_ECDH
667 	EC_KEY *ecdh;
668 	int nid;
669 #endif
670 #endif
671 
672 #ifdef USE_OPENSSL
673 # ifndef OPENSSL_NO_DH
674 	DH *dh;
675 # endif
676 	BIO *bio;
677 
678        /* 1024-bit MODP Group with 160-bit prime order subgroup (RFC5114)
679 	* -----BEGIN DH PARAMETERS-----
680 	* MIIBDAKBgQCxC4+WoIDgHd6S3l6uXVTsUsmfvPsGo8aaap3KUtI7YWBz4oZ1oj0Y
681 	* mDjvHi7mUsAT7LSuqQYRIySXXDzUm4O/rMvdfZDEvXCYSI6cIZpzck7/1vrlZEc4
682 	* +qMaT/VbzMChUa9fDci0vUW/N982XBpl5oz9p21NpwjfH7K8LkpDcQKBgQCk0cvV
683 	* w/00EmdlpELvuZkF+BBN0lisUH/WQGz/FCZtMSZv6h5cQVZLd35pD1UE8hMWAhe0
684 	* sBuIal6RVH+eJ0n01/vX07mpLuGQnQ0iY/gKdqaiTAh6CR9THb8KAWm2oorWYqTR
685 	* jnOvoy13nVkY0IvIhY9Nzvl8KiSFXm7rIrOy5QICAKA=
686 	* -----END DH PARAMETERS-----
687 	*/
688 
689 	static const unsigned char dh1024_p[]={
690 		0xB1,0x0B,0x8F,0x96,0xA0,0x80,0xE0,0x1D,0xDE,0x92,0xDE,0x5E,
691 		0xAE,0x5D,0x54,0xEC,0x52,0xC9,0x9F,0xBC,0xFB,0x06,0xA3,0xC6,
692 		0x9A,0x6A,0x9D,0xCA,0x52,0xD2,0x3B,0x61,0x60,0x73,0xE2,0x86,
693 		0x75,0xA2,0x3D,0x18,0x98,0x38,0xEF,0x1E,0x2E,0xE6,0x52,0xC0,
694 		0x13,0xEC,0xB4,0xAE,0xA9,0x06,0x11,0x23,0x24,0x97,0x5C,0x3C,
695 		0xD4,0x9B,0x83,0xBF,0xAC,0xCB,0xDD,0x7D,0x90,0xC4,0xBD,0x70,
696 		0x98,0x48,0x8E,0x9C,0x21,0x9A,0x73,0x72,0x4E,0xFF,0xD6,0xFA,
697 		0xE5,0x64,0x47,0x38,0xFA,0xA3,0x1A,0x4F,0xF5,0x5B,0xCC,0xC0,
698 		0xA1,0x51,0xAF,0x5F,0x0D,0xC8,0xB4,0xBD,0x45,0xBF,0x37,0xDF,
699 		0x36,0x5C,0x1A,0x65,0xE6,0x8C,0xFD,0xA7,0x6D,0x4D,0xA7,0x08,
700 		0xDF,0x1F,0xB2,0xBC,0x2E,0x4A,0x43,0x71,
701 	};
702 
703 	static const unsigned char dh1024_g[]={
704 		0xA4,0xD1,0xCB,0xD5,0xC3,0xFD,0x34,0x12,0x67,0x65,0xA4,0x42,
705 		0xEF,0xB9,0x99,0x05,0xF8,0x10,0x4D,0xD2,0x58,0xAC,0x50,0x7F,
706 		0xD6,0x40,0x6C,0xFF,0x14,0x26,0x6D,0x31,0x26,0x6F,0xEA,0x1E,
707 		0x5C,0x41,0x56,0x4B,0x77,0x7E,0x69,0x0F,0x55,0x04,0xF2,0x13,
708 		0x16,0x02,0x17,0xB4,0xB0,0x1B,0x88,0x6A,0x5E,0x91,0x54,0x7F,
709 		0x9E,0x27,0x49,0xF4,0xD7,0xFB,0xD7,0xD3,0xB9,0xA9,0x2E,0xE1,
710 		0x90,0x9D,0x0D,0x22,0x63,0xF8,0x0A,0x76,0xA6,0xA2,0x4C,0x08,
711 		0x7A,0x09,0x1F,0x53,0x1D,0xBF,0x0A,0x01,0x69,0xB6,0xA2,0x8A,
712 		0xD6,0x62,0xA4,0xD1,0x8E,0x73,0xAF,0xA3,0x2D,0x77,0x9D,0x59,
713 		0x18,0xD0,0x8B,0xC8,0x85,0x8F,0x4D,0xCE,0xF9,0x7C,0x2A,0x24,
714 		0x85,0x5E,0x6E,0xEB,0x22,0xB3,0xB2,0xE5,
715 	};
716 #endif
717 
718 	struct nb_map {
719 		network_backend_t nb;
720 		const char *name;
721 	} network_backends[] = {
722 		/* lowest id wins */
723 #if defined USE_SENDFILE
724 		{ NETWORK_BACKEND_SENDFILE,   "sendfile" },
725 #endif
726 #if defined USE_LINUX_SENDFILE
727 		{ NETWORK_BACKEND_SENDFILE,   "linux-sendfile" },
728 #endif
729 #if defined USE_FREEBSD_SENDFILE
730 		{ NETWORK_BACKEND_SENDFILE,   "freebsd-sendfile" },
731 #endif
732 #if defined USE_SOLARIS_SENDFILEV
733 		{ NETWORK_BACKEND_SENDFILE,   "solaris-sendfilev" },
734 #endif
735 #if defined USE_WRITEV
736 		{ NETWORK_BACKEND_WRITEV,     "writev" },
737 #endif
738 		{ NETWORK_BACKEND_WRITE,      "write" },
739 		{ NETWORK_BACKEND_UNSET,       NULL }
740 	};
741 
742 #ifdef USE_OPENSSL
743 	/* load SSL certificates */
744 	for (i = 0; i < srv->config_context->used; i++) {
745 		specific_config *s = srv->config_storage[i];
746 #ifndef SSL_OP_NO_COMPRESSION
747 # define SSL_OP_NO_COMPRESSION 0
748 #endif
749 #ifndef SSL_MODE_RELEASE_BUFFERS    /* OpenSSL >= 1.0.0 */
750 #define SSL_MODE_RELEASE_BUFFERS 0
751 #endif
752 		long ssloptions =
753 			SSL_OP_ALL | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | SSL_OP_NO_COMPRESSION;
754 
755 		if (buffer_string_is_empty(s->ssl_pemfile) && buffer_string_is_empty(s->ssl_ca_file)) continue;
756 
757 		if (srv->ssl_is_init == 0) {
758 			SSL_load_error_strings();
759 			SSL_library_init();
760 			OpenSSL_add_all_algorithms();
761 			srv->ssl_is_init = 1;
762 
763 			if (0 == RAND_status()) {
764 				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
765 						"not enough entropy in the pool");
766 				return -1;
767 			}
768 		}
769 
770 		if (!buffer_string_is_empty(s->ssl_pemfile)) {
771 #ifdef OPENSSL_NO_TLSEXT
772 			data_config *dc = (data_config *)srv->config_context->data[i];
773 			if (COMP_HTTP_HOST == dc->comp) {
774 				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
775 						"can't use ssl.pemfile with $HTTP[\"host\"], openssl version does not support TLS extensions");
776 				return -1;
777 			}
778 #endif
779 			if (network_openssl_load_pemfile(srv, i)) return -1;
780 		}
781 
782 
783 		if (!buffer_string_is_empty(s->ssl_ca_file)) {
784 			s->ssl_ca_file_cert_names = SSL_load_client_CA_file(s->ssl_ca_file->ptr);
785 			if (NULL == s->ssl_ca_file_cert_names) {
786 				log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
787 						ERR_error_string(ERR_get_error(), NULL), s->ssl_ca_file);
788 			}
789 		}
790 
791 		if (buffer_string_is_empty(s->ssl_pemfile) || !s->ssl_enabled) continue;
792 
793 		if (NULL == (s->ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) {
794 			log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
795 					ERR_error_string(ERR_get_error(), NULL));
796 			return -1;
797 		}
798 
799 		/* completely useless identifier; required for client cert verification to work with sessions */
800 		if (0 == SSL_CTX_set_session_id_context(s->ssl_ctx, (const unsigned char*) CONST_STR_LEN("lighttpd"))) {
801 			log_error_write(srv, __FILE__, __LINE__, "ss:s", "SSL:",
802 				"failed to set session context",
803 				ERR_error_string(ERR_get_error(), NULL));
804 			return -1;
805 		}
806 
807 		if (s->ssl_empty_fragments) {
808 #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
809 			ssloptions &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
810 #else
811 			ssloptions &= ~0x00000800L; /* hardcode constant */
812 			log_error_write(srv, __FILE__, __LINE__, "ss", "WARNING: SSL:",
813 					"'insert empty fragments' not supported by the openssl version used to compile lighttpd with");
814 #endif
815 		}
816 
817 		SSL_CTX_set_options(s->ssl_ctx, ssloptions);
818 		SSL_CTX_set_info_callback(s->ssl_ctx, ssl_info_callback);
819 
820 		if (!s->ssl_use_sslv2) {
821 			/* disable SSLv2 */
822 			if ((SSL_OP_NO_SSLv2 & SSL_CTX_set_options(s->ssl_ctx, SSL_OP_NO_SSLv2)) != SSL_OP_NO_SSLv2) {
823 				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
824 						ERR_error_string(ERR_get_error(), NULL));
825 				return -1;
826 			}
827 		}
828 
829 		if (!s->ssl_use_sslv3) {
830 			/* disable SSLv3 */
831 			if ((SSL_OP_NO_SSLv3 & SSL_CTX_set_options(s->ssl_ctx, SSL_OP_NO_SSLv3)) != SSL_OP_NO_SSLv3) {
832 				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
833 						ERR_error_string(ERR_get_error(), NULL));
834 				return -1;
835 			}
836 		}
837 
838 		if (!buffer_string_is_empty(s->ssl_cipher_list)) {
839 			/* Disable support for low encryption ciphers */
840 			if (SSL_CTX_set_cipher_list(s->ssl_ctx, s->ssl_cipher_list->ptr) != 1) {
841 				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
842 						ERR_error_string(ERR_get_error(), NULL));
843 				return -1;
844 			}
845 
846 			if (s->ssl_honor_cipher_order) {
847 				SSL_CTX_set_options(s->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
848 			}
849 		}
850 
851 #ifndef OPENSSL_NO_DH
852 		/* Support for Diffie-Hellman key exchange */
853 		if (!buffer_string_is_empty(s->ssl_dh_file)) {
854 			/* DH parameters from file */
855 			bio = BIO_new_file((char *) s->ssl_dh_file->ptr, "r");
856 			if (bio == NULL) {
857 				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL: Unable to open file", s->ssl_dh_file->ptr);
858 				return -1;
859 			}
860 			dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
861 			BIO_free(bio);
862 			if (dh == NULL) {
863 				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL: PEM_read_bio_DHparams failed", s->ssl_dh_file->ptr);
864 				return -1;
865 			}
866 		} else {
867 			BIGNUM *dh_p, *dh_g;
868 			/* Default DH parameters from RFC5114 */
869 			dh = DH_new();
870 			if (dh == NULL) {
871 				log_error_write(srv, __FILE__, __LINE__, "s", "SSL: DH_new () failed");
872 				return -1;
873 			}
874 			dh_p = BN_bin2bn(dh1024_p,sizeof(dh1024_p), NULL);
875 			dh_g = BN_bin2bn(dh1024_g,sizeof(dh1024_g), NULL);
876 			if ((dh_p == NULL) || (dh_g == NULL)) {
877 				DH_free(dh);
878 				log_error_write(srv, __FILE__, __LINE__, "s", "SSL: BN_bin2bn () failed");
879 				return -1;
880 			}
881 		      #if OPENSSL_VERSION_NUMBER < 0x10100000L \
882 			|| defined(LIBRESSL_VERSION_NUMBER)
883 			dh->p = dh_p;
884 			dh->g = dh_g;
885 			dh->length = 160;
886 		      #else
887 			DH_set0_pqg(dh, dh_p, NULL, dh_g);
888 			DH_set_length(dh, 160);
889 		      #endif
890 		}
891 		SSL_CTX_set_tmp_dh(s->ssl_ctx,dh);
892 		SSL_CTX_set_options(s->ssl_ctx,SSL_OP_SINGLE_DH_USE);
893 		DH_free(dh);
894 #else
895 		if (!buffer_string_is_empty(s->ssl_dh_file)) {
896 			log_error_write(srv, __FILE__, __LINE__, "ss", "SSL: openssl compiled without DH support, can't load parameters from", s->ssl_dh_file->ptr);
897 		}
898 #endif
899 
900 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
901 #ifndef OPENSSL_NO_ECDH
902 		/* Support for Elliptic-Curve Diffie-Hellman key exchange */
903 		if (!buffer_string_is_empty(s->ssl_ec_curve)) {
904 			/* OpenSSL only supports the "named curves" from RFC 4492, section 5.1.1. */
905 			nid = OBJ_sn2nid((char *) s->ssl_ec_curve->ptr);
906 			if (nid == 0) {
907 				log_error_write(srv, __FILE__, __LINE__, "ss", "SSL: Unknown curve name", s->ssl_ec_curve->ptr);
908 				return -1;
909 			}
910 		} else {
911 			/* Default curve */
912 			nid = OBJ_sn2nid("prime256v1");
913 		}
914 		ecdh = EC_KEY_new_by_curve_name(nid);
915 		if (ecdh == NULL) {
916 			log_error_write(srv, __FILE__, __LINE__, "ss", "SSL: Unable to create curve", s->ssl_ec_curve->ptr);
917 			return -1;
918 		}
919 		SSL_CTX_set_tmp_ecdh(s->ssl_ctx,ecdh);
920 		SSL_CTX_set_options(s->ssl_ctx,SSL_OP_SINGLE_ECDH_USE);
921 		EC_KEY_free(ecdh);
922 #endif
923 #endif
924 
925 		/* load all ssl.ca-files specified in the config into each SSL_CTX to be prepared for SNI */
926 		for (j = 0; j < srv->config_context->used; j++) {
927 			specific_config *s1 = srv->config_storage[j];
928 
929 			if (!buffer_string_is_empty(s1->ssl_ca_file)) {
930 				if (1 != SSL_CTX_load_verify_locations(s->ssl_ctx, s1->ssl_ca_file->ptr, NULL)) {
931 					log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
932 							ERR_error_string(ERR_get_error(), NULL), s1->ssl_ca_file);
933 					return -1;
934 				}
935 			}
936 		}
937 
938 		if (s->ssl_verifyclient) {
939 			if (NULL == s->ssl_ca_file_cert_names) {
940 				log_error_write(srv, __FILE__, __LINE__, "s",
941 					"SSL: You specified ssl.verifyclient.activate but no ca_file"
942 				);
943 				return -1;
944 			}
945 			SSL_CTX_set_client_CA_list(s->ssl_ctx, SSL_dup_CA_list(s->ssl_ca_file_cert_names));
946 			SSL_CTX_set_verify(
947 				s->ssl_ctx,
948 				SSL_VERIFY_PEER | (s->ssl_verifyclient_enforce ? SSL_VERIFY_FAIL_IF_NO_PEER_CERT : 0),
949 				NULL
950 			);
951 			SSL_CTX_set_verify_depth(s->ssl_ctx, s->ssl_verifyclient_depth);
952 		}
953 
954 		if (SSL_CTX_use_certificate(s->ssl_ctx, s->ssl_pemfile_x509) < 0) {
955 			log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
956 					ERR_error_string(ERR_get_error(), NULL), s->ssl_pemfile);
957 			return -1;
958 		}
959 
960 		if (SSL_CTX_use_PrivateKey(s->ssl_ctx, s->ssl_pemfile_pkey) < 0) {
961 			log_error_write(srv, __FILE__, __LINE__, "ssb", "SSL:",
962 					ERR_error_string(ERR_get_error(), NULL), s->ssl_pemfile);
963 			return -1;
964 		}
965 
966 		if (SSL_CTX_check_private_key(s->ssl_ctx) != 1) {
967 			log_error_write(srv, __FILE__, __LINE__, "sssb", "SSL:",
968 					"Private key does not match the certificate public key, reason:",
969 					ERR_error_string(ERR_get_error(), NULL),
970 					s->ssl_pemfile);
971 			return -1;
972 		}
973 		SSL_CTX_set_default_read_ahead(s->ssl_ctx, 1);
974 		SSL_CTX_set_mode(s->ssl_ctx,  SSL_CTX_get_mode(s->ssl_ctx)
975 					    | SSL_MODE_ENABLE_PARTIAL_WRITE
976 					    | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
977 					    | SSL_MODE_RELEASE_BUFFERS);
978 
979 # ifndef OPENSSL_NO_TLSEXT
980 		if (!SSL_CTX_set_tlsext_servername_callback(s->ssl_ctx, network_ssl_servername_callback) ||
981 		    !SSL_CTX_set_tlsext_servername_arg(s->ssl_ctx, srv)) {
982 			log_error_write(srv, __FILE__, __LINE__, "ss", "SSL:",
983 					"failed to initialize TLS servername callback, openssl library does not support TLS servername extension");
984 			return -1;
985 		}
986 # endif
987 	}
988 #endif
989 
990 	b = buffer_init();
991 
992 	buffer_copy_buffer(b, srv->srvconf.bindhost);
993 	buffer_append_string_len(b, CONST_STR_LEN(":"));
994 	buffer_append_int(b, srv->srvconf.port);
995 
996 	if (0 != network_server_init(srv, b, srv->config_storage[0])) {
997 		buffer_free(b);
998 		return -1;
999 	}
1000 	buffer_free(b);
1001 
1002 #ifdef USE_OPENSSL
1003 	srv->network_ssl_backend_write = network_write_chunkqueue_openssl;
1004 #endif
1005 
1006 	/* get a usefull default */
1007 	backend = network_backends[0].nb;
1008 
1009 	/* match name against known types */
1010 	if (!buffer_string_is_empty(srv->srvconf.network_backend)) {
1011 		for (i = 0; network_backends[i].name; i++) {
1012 			/**/
1013 			if (buffer_is_equal_string(srv->srvconf.network_backend, network_backends[i].name, strlen(network_backends[i].name))) {
1014 				backend = network_backends[i].nb;
1015 				break;
1016 			}
1017 		}
1018 		if (NULL == network_backends[i].name) {
1019 			/* we don't know it */
1020 
1021 			log_error_write(srv, __FILE__, __LINE__, "sb",
1022 					"server.network-backend has a unknown value:",
1023 					srv->srvconf.network_backend);
1024 
1025 			return -1;
1026 		}
1027 	}
1028 
1029 	switch(backend) {
1030 	case NETWORK_BACKEND_WRITE:
1031 		srv->network_backend_write = network_write_chunkqueue_write;
1032 		break;
1033 #if defined(USE_WRITEV)
1034 	case NETWORK_BACKEND_WRITEV:
1035 		srv->network_backend_write = network_write_chunkqueue_writev;
1036 		break;
1037 #endif
1038 #if defined(USE_SENDFILE)
1039 	case NETWORK_BACKEND_SENDFILE:
1040 		srv->network_backend_write = network_write_chunkqueue_sendfile;
1041 		break;
1042 #endif
1043 	default:
1044 		return -1;
1045 	}
1046 
1047 	/* check for $SERVER["socket"] */
1048 	for (i = 1; i < srv->config_context->used; i++) {
1049 		data_config *dc = (data_config *)srv->config_context->data[i];
1050 		specific_config *s = srv->config_storage[i];
1051 
1052 		/* not our stage */
1053 		if (COMP_SERVER_SOCKET != dc->comp) continue;
1054 
1055 		if (dc->cond != CONFIG_COND_EQ) continue;
1056 
1057 		/* check if we already know this socket,
1058 		 * if yes, don't init it */
1059 		for (j = 0; j < srv->srv_sockets.used; j++) {
1060 			if (buffer_is_equal(srv->srv_sockets.ptr[j]->srv_token, dc->string)) {
1061 				break;
1062 			}
1063 		}
1064 
1065 		if (j == srv->srv_sockets.used) {
1066 			if (0 != network_server_init(srv, dc->string, s)) return -1;
1067 		}
1068 	}
1069 
1070 	return 0;
1071 }
1072 
1073 int network_register_fdevents(server *srv) {
1074 	size_t i;
1075 
1076 	if (-1 == fdevent_reset(srv->ev)) {
1077 		return -1;
1078 	}
1079 
1080 	if (srv->sockets_disabled) return 0; /* lighttpd -1 (one-shot mode) */
1081 
1082 	/* register fdevents after reset */
1083 	for (i = 0; i < srv->srv_sockets.used; i++) {
1084 		server_socket *srv_socket = srv->srv_sockets.ptr[i];
1085 
1086 		fdevent_register(srv->ev, srv_socket->fd, network_server_handle_fdevent, srv_socket);
1087 		fdevent_event_set(srv->ev, &(srv_socket->fde_ndx), srv_socket->fd, FDEVENT_IN);
1088 	}
1089 	return 0;
1090 }
1091 
1092 int network_write_chunkqueue(server *srv, connection *con, chunkqueue *cq, off_t max_bytes) {
1093 	int ret = -1;
1094 	off_t written = 0;
1095 #ifdef TCP_CORK
1096 	int corked = 0;
1097 #endif
1098 	server_socket *srv_socket = con->srv_socket;
1099 
1100 	if (con->conf.global_kbytes_per_second) {
1101 		off_t limit = con->conf.global_kbytes_per_second * 1024 - *(con->conf.global_bytes_per_second_cnt_ptr);
1102 		if (limit <= 0) {
1103 			/* we reached the global traffic limit */
1104 			con->traffic_limit_reached = 1;
1105 
1106 			return 1;
1107 		} else {
1108 			if (max_bytes > limit) max_bytes = limit;
1109 		}
1110 	}
1111 
1112 	if (con->conf.kbytes_per_second) {
1113 		off_t limit = con->conf.kbytes_per_second * 1024 - con->bytes_written_cur_second;
1114 		if (limit <= 0) {
1115 			/* we reached the traffic limit */
1116 			con->traffic_limit_reached = 1;
1117 
1118 			return 1;
1119 		} else {
1120 			if (max_bytes > limit) max_bytes = limit;
1121 		}
1122 	}
1123 
1124 	written = cq->bytes_out;
1125 
1126 #ifdef TCP_CORK
1127 	/* Linux: put a cork into the socket as we want to combine the write() calls
1128 	 * but only if we really have multiple chunks
1129 	 */
1130 	if (cq->first && cq->first->next) {
1131 		corked = 1;
1132 		(void)setsockopt(con->fd, IPPROTO_TCP, TCP_CORK, &corked, sizeof(corked));
1133 	}
1134 #endif
1135 
1136 	if (srv_socket->is_ssl) {
1137 #ifdef USE_OPENSSL
1138 		ret = srv->network_ssl_backend_write(srv, con, con->ssl, cq, max_bytes);
1139 #endif
1140 	} else {
1141 		ret = srv->network_backend_write(srv, con, con->fd, cq, max_bytes);
1142 	}
1143 
1144 	if (ret >= 0) {
1145 		chunkqueue_remove_finished_chunks(cq);
1146 		ret = chunkqueue_is_empty(cq) ? 0 : 1;
1147 	}
1148 
1149 #ifdef TCP_CORK
1150 	if (corked) {
1151 		corked = 0;
1152 		(void)setsockopt(con->fd, IPPROTO_TCP, TCP_CORK, &corked, sizeof(corked));
1153 	}
1154 #endif
1155 
1156 	written = cq->bytes_out - written;
1157 	con->bytes_written += written;
1158 	con->bytes_written_cur_second += written;
1159 
1160 	*(con->conf.global_bytes_per_second_cnt_ptr) += written;
1161 
1162 	return ret;
1163 }
1164