2 months ago
add shutdown cleanup (listener, log, threadpool)
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
41
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
1 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
29 | #include "../public/nsapi.h" |
1 | 30 | |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
33 | |
1 | 34 | #include <sys/shm.h> |
35 | #include <sys/ipc.h> | |
36 | #include <sys/file.h> | |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
37 | #include <netinet/tcp.h> |
1 | 38 | #include <stdio.h> |
39 | #include <stdlib.h> | |
40 | #include <fcntl.h> | |
41 | #include <unistd.h> | |
42 | #include <strings.h> | |
43 | #include <stdbool.h> | |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
44 | #include <signal.h> |
1 | 45 | #include <pthread.h> |
46 | ||
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
47 | #include <stdio.h> |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
48 | #include <stdlib.h> |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
49 | |
557
e35829a3a6d8
add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
554
diff
changeset
|
50 | #include <openssl/rand.h> /* RAND_bytes, connection id */ |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
398
diff
changeset
|
51 | |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
398
diff
changeset
|
52 | #include <cx/hash_map.h> |
92
382bff43c6eb
fixed some includes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
87
diff
changeset
|
53 | |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
38
diff
changeset
|
54 | #include "../util/atomic.h" |
1 | 55 | #include "httplistener.h" |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
169
diff
changeset
|
56 | #include "netsite.h" |
1 | 57 | |
58 | #include "session.h" | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
59 | #include "configmanager.h" |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
60 | #include "log.h" |
1 | 61 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
398
diff
changeset
|
62 | #define LISTENER_MAX_PROTOCOL_TOKENS 1024 |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
398
diff
changeset
|
63 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
64 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
65 | #define LISTENER_PROTO_IPV4 "ipv4" |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
66 | #define LISTENER_PROTO_IPV6 "ipv6" |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
67 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
68 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
69 | /* |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
70 | * key: string format: <protocol>:<port> |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
71 | * value: WSSocket* |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
72 | * |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
73 | * protocol: ipv4 | ipv6 |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
74 | * port: short |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
75 | */ |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
76 | static CxMap *listener_socket_map; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
77 | |
453
4586d534f9b5
fix build on macos
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
449
diff
changeset
|
78 | static pthread_mutex_t listener_mutex = PTHREAD_MUTEX_INITIALIZER; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
79 | |
577
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
80 | static void wssocket_free(WSSocket *s) { |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
81 | close(s->socket); |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
82 | free(s); |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
83 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
84 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
85 | int http_listener_global_init(void) { |
490 | 86 | listener_socket_map = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 4); |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
87 | if(!listener_socket_map) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
88 | return 1; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
89 | } |
577
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
90 | listener_socket_map->simple_destructor = (cx_destructor_func)wssocket_free; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
91 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
92 | return 0; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
93 | } |
1 | 94 | |
577
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
95 | void http_listener_global_shutdown(void) { |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
96 | cxMapDestroy(listener_socket_map); |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
97 | } |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
98 | |
1 | 99 | int start_all_listener() { |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
100 | ServerConfiguration *conf = cfgmgr_get_server_config(); |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
398
diff
changeset
|
101 | CxList *ls = conf->listeners; |
490 | 102 | CxIterator iter = cxListIterator(ls); |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
398
diff
changeset
|
103 | cx_foreach(HttpListener *, listener, iter) { |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
104 | http_listener_start(listener); |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
105 | } |
1 | 106 | |
107 | return 0; | |
108 | } | |
109 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
110 | static HttpSSL* create_http_ssl(ListenerConfig *conf) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
111 | SSL_CTX *ctx = SSL_CTX_new(SSLv23_server_method()); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
112 | if(!ctx) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
113 | return NULL; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
114 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
115 | SSL_CTX_set_options( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
116 | ctx, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
117 | SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv3); |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
494
diff
changeset
|
118 | SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
494
diff
changeset
|
119 | SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
120 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
121 | int error = 0; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
122 | if(conf->disable_proto.ptr) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
123 | cxstring *plist = NULL; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
124 | ssize_t n = cx_strsplit_a(cxDefaultAllocator, conf->disable_proto, cx_str(","), LISTENER_MAX_PROTOCOL_TOKENS, &plist); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
125 | if(plist) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
126 | for(int i=0;i<n;i++) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
127 | cxstring proto = plist[i]; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
128 | log_ereport( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
129 | LOG_VERBOSE, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
130 | "Listener %s: Disable protocol %s", |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
131 | conf->name.ptr, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
132 | proto.ptr); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
133 | if(!cx_strcasecmp(cx_strtrim(proto), cx_str("SSLv2"))) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
134 | SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
135 | } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("SSLv3"))) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
136 | SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
137 | } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1"))) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
138 | SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
139 | } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.1"))) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
140 | #ifdef SSL_OP_NO_TLSv1_1 |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
141 | SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
142 | #else |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
143 | log_ereport( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
144 | LOG_WARN, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
145 | "Listener: %s: TLSv1.1 not supported", |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
146 | conf->name.ptr); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
147 | #endif |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
148 | } else if(cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.2"))) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
149 | #ifdef SSL_OP_NO_TLSv1_2 |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
150 | SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
151 | #else |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
152 | log_ereport( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
153 | LOG_WARN, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
154 | "Listener: %s: TLSv1.2 not supported", |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
155 | conf->name.ptr); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
156 | #endif |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
157 | } else if(cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.3"))) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
158 | #ifdef SSL_OP_NO_TLSv1_3 |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
159 | SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_3); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
160 | #else |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
161 | log_ereport( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
162 | LOG_WARN, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
163 | "Listener: %s: TLSv1.3 not supported", |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
164 | conf->name.ptr); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
165 | #endif |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
166 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
167 | error = 1; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
168 | log_ereport( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
169 | LOG_MISCONFIG, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
170 | "Listener: %s: Unknown protocol %s", |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
171 | conf->name.ptr, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
172 | proto.ptr); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
173 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
174 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
175 | free(plist); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
176 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
177 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
178 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
179 | if(error) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
180 | SSL_CTX_free(ctx); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
181 | return NULL; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
182 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
183 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
184 | int ret; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
185 | char errbuf[512]; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
186 | |
441
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
187 | // get TLS cert |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
188 | error = 0; |
441
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
189 | if(conf->chainfile.ptr) { |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
190 | ret = SSL_CTX_use_certificate_chain_file(ctx, conf->chainfile.ptr); |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
191 | if(!ret) { |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
192 | ERR_error_string(ERR_get_error(), errbuf); |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
193 | log_ereport(LOG_MISCONFIG, "Cannot load ssl cert file: %s", errbuf); |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
194 | error = 1; |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
195 | } |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
196 | } else if(conf->certfile.ptr) { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
197 | ret = SSL_CTX_use_certificate_file(ctx, conf->certfile.ptr, SSL_FILETYPE_PEM); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
198 | if(!ret) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
199 | ERR_error_string(ERR_get_error(), errbuf); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
200 | log_ereport(LOG_MISCONFIG, "Cannot load ssl chain file: %s", errbuf); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
201 | error = 1; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
202 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
203 | } else { |
441
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
204 | log_ereport(LOG_MISCONFIG, "Listener %s: no CertChain/Cert specified", conf->name.ptr); |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
205 | error = 1; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
206 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
207 | |
441
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
208 | // get private key |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
209 | ret = SSL_CTX_use_PrivateKey_file(ctx, conf->privkeyfile.ptr, SSL_FILETYPE_PEM); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
210 | if(!ret) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
211 | ERR_error_string(ERR_get_error(), errbuf); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
212 | log_ereport(LOG_MISCONFIG, "Cannot load ssl key file: %s", errbuf); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
213 | error = 1; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
214 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
215 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
216 | if(error) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
217 | SSL_CTX_free(ctx); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
218 | return NULL; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
219 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
220 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
221 | HttpSSL *ssl = pool_malloc(conf->cfg->pool, sizeof(HttpSSL)); |
441
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
222 | if(!ssl) { |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
223 | SSL_CTX_free(ctx); |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
224 | return NULL; |
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
225 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
226 | ZERO(ssl, sizeof(HttpSSL)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
227 | ssl->sslctx = ctx; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
228 | |
441
797aeb31a2c6
fix listener ssl initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
229 | return ssl; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
230 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
231 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
232 | static WSSocket* create_socket(ListenerConfig *conf, const char *protocol) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
233 | WSBool ipv4 = !strcmp(protocol, "ipv4") ? TRUE : FALSE; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
234 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
235 | int s = -1; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
236 | if(ipv4) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
237 | // ipv4 socket |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
238 | s = socket(AF_INET, SOCK_STREAM, 0); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
239 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
240 | // ipv6 socket |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
241 | s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
242 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
243 | if(s < 0) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
244 | log_ereport( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
245 | LOG_FAILURE, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
246 | "cannot create socket: protocol: %s port: %d error: %s", |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
247 | protocol, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
248 | conf->port, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
249 | strerror(errno)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
250 | return NULL; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
251 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
252 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
253 | // socket options |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
254 | int o = 1; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
255 | setsockopt( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
256 | s, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
257 | SOL_SOCKET, SO_REUSEADDR, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
258 | &o, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
259 | sizeof(int)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
260 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
261 | #ifdef LINUX |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
262 | if(!ipv4) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
263 | o = 1; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
264 | setsockopt( |
454
e03d4a00427a
fix build on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
453
diff
changeset
|
265 | s, |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
266 | IPPROTO_IPV6, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
267 | IPV6_V6ONLY, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
268 | &o, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
269 | sizeof(int)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
270 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
271 | #endif |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
272 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
273 | // bind server socket to address |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
274 | union ws_socketaddr addr; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
275 | struct sockaddr *servaddr; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
276 | size_t servaddr_size; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
277 | if(ipv4) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
278 | // ipv4 |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
279 | memset(&addr.addr4, 0, sizeof(addr.addr4)); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
280 | addr.addr4.sin_family = AF_INET; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
281 | addr.addr4.sin_addr.s_addr = htonl(INADDR_ANY); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
282 | addr.addr4.sin_port = htons(conf->port); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
283 | servaddr = (struct sockaddr *)&addr.addr4; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
284 | servaddr_size = sizeof(addr.addr4); |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
285 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
286 | // ipv6 |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
287 | memset(&addr.addr6, 0, sizeof(addr.addr6)); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
288 | addr.addr6.sin6_family = AF_INET6; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
289 | addr.addr6.sin6_addr = in6addr_any; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
290 | addr.addr6.sin6_port = htons(conf->port); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
291 | servaddr = (struct sockaddr *)&addr.addr6; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
292 | servaddr_size = sizeof(addr.addr6); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
293 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
294 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
295 | if(bind(s, servaddr, servaddr_size)) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
296 | log_ereport( |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
297 | LOG_FAILURE, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
298 | "cannot bind socket: protocol: %s port: %d error: %s", |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
299 | protocol, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
300 | conf->port, |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
301 | strerror(errno)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
302 | close(s); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
303 | return NULL; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
304 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
305 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
306 | // everything is ok, create WSSocket object |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
307 | WSSocket *wssocket = malloc(sizeof(WSSocket)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
308 | if(!wssocket) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
309 | close(s); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
310 | return NULL; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
311 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
312 | ZERO(wssocket, sizeof(WSSocket)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
313 | wssocket->socket = s; |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
314 | wssocket->addr = addr; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
315 | if(ipv4) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
316 | wssocket->sockaddr = (struct sockaddr *)&wssocket->addr.addr4; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
317 | } else { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
318 | wssocket->sockaddr = (struct sockaddr *)&wssocket->addr.addr6; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
319 | } |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
320 | wssocket->sockaddr_size = servaddr_size; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
321 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
322 | return wssocket; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
323 | } |
1 | 324 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
325 | static WSSocket* get_socket(ListenerConfig *conf, const char *protocol) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
326 | char key_data[32]; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
327 | size_t key_len = snprintf(key_data, 32, "%s:%d", protocol, conf->port); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
328 | CxHashKey key = cx_hash_key(key_data, key_len); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
329 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
330 | WSSocket *sock = cxMapGet(listener_socket_map, key); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
331 | if(!sock) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
332 | sock = create_socket(conf, protocol); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
333 | if(sock) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
334 | cxMapPut(listener_socket_map, key, sock); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
335 | } |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
336 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
337 | return sock; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
338 | } |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
339 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
340 | /* |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
341 | * returns HttpSSL of socket1 or socket2 if ssl is available |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
342 | */ |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
343 | static HttpSSL* socket_get_ssl(WSSocket *socket1, WSSocket *socket2) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
344 | if(socket1 && socket1->ssl) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
345 | return socket1->ssl; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
346 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
347 | if(socket2 && socket2->ssl) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
348 | return socket2->ssl; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
349 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
350 | return NULL; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
351 | } |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
352 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
353 | static HttpListener* listener_create(ListenerConfig *conf) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
354 | pool_handle_t *pool = conf->cfg->pool; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
355 | HttpListener *listener = pool_malloc(pool, sizeof(HttpListener)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
356 | if(!listener) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
357 | return NULL; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
358 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
359 | ZERO(listener, sizeof(HttpListener)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
360 | |
74
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
361 | listener->running = 0; |
38
d07810b02147
added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
362 | listener->cfg = conf->cfg; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
363 | listener->name = cx_strdup_a(pool_allocator(pool), cx_strcast(conf->name)); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
364 | listener->default_vs.vs_name = pool_strdup(pool, conf->vs.ptr); |
81
d25825f37967
preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
78
diff
changeset
|
365 | listener->threadpool = NULL; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
366 | if(conf->threadpool.ptr != NULL) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
398
diff
changeset
|
367 | listener->threadpool = get_threadpool(cx_strcast(conf->threadpool)); |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
368 | } |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
369 | if(listener->threadpool == NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
370 | listener->threadpool = get_default_threadpool(); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
371 | } |
134
44415e4399ce
adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
372 | if(conf->blockingio) { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
373 | listener->session_handler = create_basic_session_handler(pool); |
134
44415e4399ce
adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
374 | } else { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
375 | listener->session_handler = create_event_session_handler(pool); |
134
44415e4399ce
adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
376 | } |
1 | 377 | listener->nacceptors = conf->nacceptors; |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
378 | listener->port = conf->port; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
379 | listener->next = NULL; |
492
07452a54a22b
fix ssl settings not stored in the listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
490
diff
changeset
|
380 | //listener->ssl = NULL; |
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
381 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
382 | // create sockets |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
383 | listener->server_socket = get_socket(conf, LISTENER_PROTO_IPV4); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
384 | listener->server_socket6 = get_socket(conf, LISTENER_PROTO_IPV6); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
385 | if(!listener->server_socket && !listener->server_socket6) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
386 | log_ereport(LOG_FAILURE, "Listener %s: no server socket", conf->name.ptr); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
387 | return NULL; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
388 | } |
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
389 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
390 | if(listener->server_socket) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
391 | wssocket_ref(listener->server_socket); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
392 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
393 | if(listener->server_socket6) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
394 | wssocket_ref(listener->server_socket6); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
395 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
396 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
397 | // create SSL context |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
398 | if(conf->ssl) { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
399 | HttpSSL *ssl = socket_get_ssl(listener->server_socket, listener->server_socket6); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
400 | if(!ssl) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
401 | ssl = create_http_ssl(conf); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
402 | if(!ssl) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
403 | log_ereport(LOG_FAILURE, "Listener %s: cannot create SSL context", conf->name.ptr); |
129
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
404 | return NULL; |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
405 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
406 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
407 | if(listener->server_socket) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
408 | listener->server_socket->ssl = ssl; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
409 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
410 | if(listener->server_socket6) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
411 | listener->server_socket6->ssl = ssl; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
412 | } |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
413 | } |
1 | 414 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
415 | // create acceptors |
1 | 416 | listener->acceptors = calloc(listener->nacceptors, sizeof(void*)); |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
417 | listener->acceptors6 = calloc(listener->nacceptors, sizeof(void*)); |
1 | 418 | for (int i=0;i<listener->nacceptors;i++) { |
419 | listener->acceptors[i] = acceptor_new(listener); | |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
420 | listener->acceptors6[i] = acceptor_new(listener); |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
421 | listener->acceptors6[i]->ipv6 = TRUE; |
1 | 422 | } |
423 | ||
424 | return listener; | |
425 | } | |
426 | ||
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
427 | HttpListener* http_listener_create(ListenerConfig *conf) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
428 | pthread_mutex_lock(&listener_mutex); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
429 | HttpListener *listener = listener_create(conf); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
430 | pthread_mutex_unlock(&listener_mutex); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
431 | return listener; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
432 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
433 | |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
434 | void http_listener_destroy(HttpListener *listener) { |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
435 | log_ereport(LOG_DEBUG, "destroy http listener: %s config: %p", listener->name.ptr, listener->cfg); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
436 | |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
437 | if(listener->shutdown) { |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
438 | pthread_mutex_destroy(&listener->shutdown_mutex); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
439 | pthread_cond_destroy(&listener->shutdown_cond); |
577
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
440 | |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
441 | free(listener->acceptors); |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
442 | free(listener->acceptors6); |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
443 | } |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
444 | } |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
445 | |
1 | 446 | int http_listener_start(HttpListener *listener) { |
74
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
447 | if(listener->running) { |
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
448 | return 0; |
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
449 | } |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
450 | log_ereport(LOG_INFORM, "start listener on port %d", listener->port); |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
451 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
452 | WSBool ipv4 = listener->server_socket ? TRUE : FALSE; |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
453 | WSBool ipv6 = listener->server_socket6 ? TRUE: FALSE; |
1 | 454 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
455 | if (ipv4 && !listener->server_socket->listening && listen(listener->server_socket->socket, 256) == -1) { |
398
83234bc3bee9
use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
396
diff
changeset
|
456 | log_ereport(LOG_FAILURE, "http_listener_start: listen failed: %s", strerror(errno)); |
1 | 457 | return -1; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
458 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
459 | listener->server_socket->listening = TRUE; |
1 | 460 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
461 | if (ipv6 && !listener->server_socket6->listening && listen(listener->server_socket6->socket, 256) == -1) { |
398
83234bc3bee9
use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
396
diff
changeset
|
462 | log_ereport(LOG_FAILURE, "http_listener_start: listen v6 failed: %s", strerror(errno)); |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
463 | return -1; |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
464 | } else { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
465 | listener->server_socket6->listening = TRUE; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
466 | } |
1 | 467 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
468 | // start acceptor threads |
1 | 469 | for (int i=0;i<listener->nacceptors;i++) { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
470 | if(ipv4) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
471 | acceptor_start(listener->acceptors[i]); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
472 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
473 | if(ipv6) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
474 | acceptor_start(listener->acceptors6[i]); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
475 | } |
1 | 476 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
477 | listener->running = TRUE; |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
478 | |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
479 | return 0; |
1 | 480 | } |
481 | ||
440
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
482 | int http_listener_socket_eq(HttpListener *l1, HttpListener *l2) { |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
483 | if(l1->server_socket && l2->server_socket && l1->server_socket == l2->server_socket) { |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
484 | return TRUE; |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
485 | } |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
486 | if(l1->server_socket6 && l2->server_socket6 && l1->server_socket6 == l2->server_socket6) { |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
487 | return TRUE; |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
488 | } |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
489 | return FALSE; |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
490 | } |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
491 | |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
492 | void http_listener_set_next(HttpListener *listener, HttpListener *next) { |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
493 | while(listener->next) { |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
494 | listener = listener->next; |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
495 | } |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
496 | listener->next = next; |
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
497 | } |
1 | 498 | |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
499 | int http_listener_connect(HttpListener *listener, WSBool ipv6) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
500 | int domain = ipv6 ? AF_INET6 : AF_INET; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
501 | int client = socket(domain, SOCK_STREAM, 0); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
502 | if(client < 0) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
503 | return -1; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
504 | } |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
505 | |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
506 | struct sockaddr *sockaddr; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
507 | size_t sockaddr_size; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
508 | if(ipv6) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
509 | sockaddr = listener->server_socket6->sockaddr; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
510 | sockaddr_size = listener->server_socket6->sockaddr_size; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
511 | } else { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
512 | sockaddr = listener->server_socket->sockaddr; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
513 | sockaddr_size = listener->server_socket->sockaddr_size; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
514 | } |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
515 | |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
516 | if(connect(client, sockaddr, sockaddr_size) < 0) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
517 | close(client); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
518 | return -1; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
519 | } |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
520 | |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
521 | return client; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
522 | } |
1 | 523 | |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
524 | |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
525 | void http_listener_shutdown_acceptors(HttpListener *listener) { |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
526 | if(pthread_mutex_init(&listener->shutdown_mutex, NULL)) { |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
527 | log_ereport(LOG_FAILURE, "http_listener_shutdown_acceptors: pthread_mutex_init failed: %s", strerror(errno)); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
528 | return; |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
529 | } |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
530 | if(pthread_cond_init(&listener->shutdown_cond, NULL)) { |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
531 | log_ereport(LOG_FAILURE, "http_listener_shutdown_acceptors: pthread_cond_init failed: %s", strerror(errno)); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
532 | return; |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
533 | } |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
534 | listener->shutdown = TRUE; |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
535 | |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
536 | log_ereport(LOG_INFORM, "shutdown http listener %s", listener->name.ptr); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
537 | |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
538 | pthread_mutex_lock(&listener->shutdown_mutex); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
539 | |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
540 | // shut down acceptors by connecting to the server socket, to make sure |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
541 | // accept() returns |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
542 | // the acceptor will handle this as a normal connection, but this should |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
543 | // not be a problem |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
544 | for(int i=0;i<listener->nacceptors;i++) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
545 | listener->acceptors[i]->exit = TRUE; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
546 | int client4 = http_listener_connect(listener, FALSE); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
547 | if(client4 < 0) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
548 | log_ereport(LOG_FAILURE, "http_listener_shutdown_acceptors: cannot connect to ipv4 server socket: %s", strerror(errno)); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
549 | } else { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
550 | close(client4); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
551 | } |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
552 | |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
553 | listener->acceptors6[i]->exit = TRUE; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
554 | int client6 = http_listener_connect(listener, TRUE); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
555 | if(client6 < 0) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
556 | log_ereport(LOG_FAILURE, "http_listener_shutdown_acceptors: cannot connect to ipv6 server socket: %s", strerror(errno)); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
557 | } else { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
558 | close(client6); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
559 | } |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
560 | } |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
561 | |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
562 | // The last acceptor will notify listener_shutdown.cond |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
563 | // It is not really necessary to wait for acceptors to shut down, |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
564 | // we do it mostly for nicer log messages. The timeout is there |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
565 | // for making sure, this function never blocks forever. |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
566 | struct timespec ts; |
571
0fa595aefd0f
shutdown acceptor threads on ws shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
568
diff
changeset
|
567 | clock_gettime(CLOCK_REALTIME, &ts); |
0fa595aefd0f
shutdown acceptor threads on ws shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
568
diff
changeset
|
568 | ts.tv_sec += 60; |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
569 | pthread_cond_timedwait(&listener->shutdown_cond, &listener->shutdown_mutex, &ts); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
570 | |
577
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
571 | |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
572 | pthread_mutex_unlock(&listener->shutdown_mutex); |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
573 | } |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
574 | |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
575 | |
1 | 576 | Acceptor* acceptor_new(HttpListener *listener) { |
577 | Acceptor *acceptor = malloc(sizeof(Acceptor)); | |
578 | acceptor->listener = listener; | |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
579 | acceptor->ipv6 = WS_FALSE; |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
580 | acceptor->exit = WS_FALSE; |
1 | 581 | return acceptor; |
582 | } | |
583 | ||
584 | void acceptor_start(Acceptor *a) { | |
444
96d2ba2f28db
fix configuration referencing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
441
diff
changeset
|
585 | cfg_ref(a->listener->cfg); |
1 | 586 | if(pthread_create( |
587 | &a->tid, | |
588 | NULL, | |
589 | (void*(*)(void*))acceptor_thread, | |
590 | a) != 0) | |
591 | { | |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
592 | log_ereport(LOG_FAILURE, "Listener %s: acceptor_start: %s acceptor", a->listener->name.ptr, strerror(errno)); |
444
96d2ba2f28db
fix configuration referencing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
441
diff
changeset
|
593 | cfg_unref(a->listener->cfg); |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
594 | } else { |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
595 | (void)pthread_detach(a->tid); |
1 | 596 | } |
597 | } | |
598 | ||
599 | void* acceptor_thread(Acceptor *acceptor) { | |
87
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
600 | WS_ASSERT(acceptor); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
601 | WS_ASSERT(acceptor->listener); |
444
96d2ba2f28db
fix configuration referencing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
441
diff
changeset
|
602 | WS_ASSERT(acceptor->listener->cfg); |
87
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
603 | WS_ASSERT(acceptor->listener->session_handler); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
604 | WS_ASSERT(acceptor->listener->session_handler->enqueue_connection); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
605 | |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
606 | acceptor->running = TRUE; |
1 | 607 | HttpListener *listener = acceptor->listener; |
444
96d2ba2f28db
fix configuration referencing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
441
diff
changeset
|
608 | |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
609 | int server_socket; |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
610 | uint32_t *acceptors_running = &listener->nacceptors_running; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
611 | |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
612 | ConnectionAddr ca; |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
613 | struct sockaddr *ca_ptr; |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
614 | socklen_t ca_length; |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
615 | ConnectionAddrType addr_type; |
492
07452a54a22b
fix ssl settings not stored in the listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
490
diff
changeset
|
616 | HttpSSL *socket_ssl = NULL; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
617 | if(acceptor->ipv6) { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
618 | server_socket = listener->server_socket6->socket; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
619 | ca_ptr = (struct sockaddr*)&ca.address_v6; |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
620 | ca_length = sizeof(ca.address_v6); |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
621 | addr_type = CONN_ADDR_IPV6; |
492
07452a54a22b
fix ssl settings not stored in the listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
490
diff
changeset
|
622 | socket_ssl = listener->server_socket6->ssl; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
623 | } else { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
624 | server_socket = listener->server_socket->socket; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
625 | ca_ptr = (struct sockaddr*)&ca.address_v4; |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
626 | ca_length = sizeof(ca.address_v4); |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
627 | addr_type = CONN_ADDR_IPV4; |
492
07452a54a22b
fix ssl settings not stored in the listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
490
diff
changeset
|
628 | socket_ssl = listener->server_socket->ssl; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
629 | } |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
630 | |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
631 | log_ereport(LOG_DEBUG, "acceptor: %p listener: %p start", acceptor, acceptor->listener); |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
632 | ws_atomic_inc32(acceptors_running); |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
633 | |
568
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
634 | // in case the system is too busy and we get EMFILE (Too many open files) |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
635 | // we wait for busy_sleep_time seconds |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
636 | // the sleep time is increased after 5 attempts |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
637 | int busy_sleep_time = 5; |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
638 | int sleep_counter = 0; |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
639 | |
1 | 640 | for (;;) { |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
641 | // accept connections |
1 | 642 | int clientfd; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
643 | socklen_t length = ca_length; |
1 | 644 | |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
645 | // accept a connection |
1 | 646 | clientfd = accept( |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
647 | server_socket, |
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
648 | ca_ptr, |
1 | 649 | &length); |
440
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
650 | log_ereport(LOG_DEBUG, "acceptor: %p listener: %p: accept(): %d", acceptor, acceptor->listener, clientfd); |
1 | 651 | if (clientfd == -1) { |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
652 | log_ereport(LOG_FAILURE, "accept %s failed: %s", acceptor->ipv6 ? "ipv6" : "ipv4", strerror(errno)); |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
653 | |
568
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
654 | switch(errno) { |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
655 | case EBADF: acceptor->exit = 1; break; |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
656 | case EINTR: continue; |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
657 | case EINVAL: acceptor->exit = 1; break; |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
658 | case EMFILE: { |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
659 | sleep(busy_sleep_time); |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
660 | sleep_counter++; |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
661 | if(sleep_counter >= 5) { |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
662 | sleep_counter = 0; |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
663 | busy_sleep_time += 5; |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
664 | } |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
665 | } |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
666 | } |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
667 | |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
668 | if(acceptor->exit) { |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
669 | log_ereport(LOG_VERBOSE, "acceptor thread %p: listener: %p exit", acceptor, acceptor->listener); |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
670 | break; |
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
671 | } |
568
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
672 | |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
673 | |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
674 | |
1 | 675 | continue; |
676 | } | |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
677 | |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
678 | //if(http_listener_apply_keep_alive_settings(listener, clientfd)) { |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
679 | // close(clientfd); |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
680 | // continue; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
681 | //} |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
682 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
683 | // check listener |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
684 | HttpListener *ls = listener; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
685 | int acceptor_exit = 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
686 | while(ls->next) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
687 | ls = ls->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
688 | acceptor_exit = 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
689 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
690 | |
440
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
691 | // create Connection object |
1 | 692 | Connection *conn = malloc(sizeof(Connection)); |
557
e35829a3a6d8
add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
554
diff
changeset
|
693 | RAND_bytes((unsigned char*)&conn->id, sizeof(conn->id)); |
e35829a3a6d8
add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
554
diff
changeset
|
694 | conn->id <<= 16; |
1 | 695 | conn->address = ca; |
396
77d81f2bb9f7
add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
696 | conn->addr_type = addr_type; |
1 | 697 | conn->fd = clientfd; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
698 | conn->listener = ls; |
194
6345f50208d5
fixes keep-alive with ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
193
diff
changeset
|
699 | conn->ssl_accepted = 0; |
494
f7f624cfe80a
fix missing connection ssl_error initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
492
diff
changeset
|
700 | conn->ssl_error = 0; |
557
e35829a3a6d8
add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
554
diff
changeset
|
701 | log_ereport(LOG_DEBUG, "trace reqid: %016llx connection accepted", (unsigned long long int)conn->id); |
492
07452a54a22b
fix ssl settings not stored in the listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
490
diff
changeset
|
702 | if(socket_ssl) { |
191
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
703 | // SSL connections are always non-blocking |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
704 | // set socket non blocking |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
705 | int flags; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
706 | if((flags = fcntl(conn->fd, F_GETFL, 0)) == -1) { |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
707 | flags = 0; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
708 | } |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
709 | if(fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK)) { |
557
e35829a3a6d8
add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
554
diff
changeset
|
710 | log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | fcntl error", (unsigned long long int)conn->id); |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
711 | log_ereport(LOG_FAILURE, "acceptor: fcntl failed: %s", strerror(errno)); |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
712 | close(clientfd); |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
713 | free(conn); |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
714 | conn = NULL; |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
715 | } else { |
492
07452a54a22b
fix ssl settings not stored in the listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
490
diff
changeset
|
716 | SSL *ssl = SSL_new(socket_ssl->sslctx); |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
717 | if(ssl) { |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
718 | SSL_set_fd(ssl, clientfd); |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
719 | |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
720 | conn->ssl = ssl; |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
721 | conn->read = connection_ssl_read; |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
722 | conn->write = connection_ssl_write; |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
723 | conn->close = connection_ssl_close; |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
724 | } else { |
557
e35829a3a6d8
add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
554
diff
changeset
|
725 | log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | SSL init error", (unsigned long long int)conn->id); |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
726 | log_ereport(LOG_FAILURE, "acceptor: %p listener: %p SSL_new() failed", acceptor, acceptor->listener); |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
727 | free(conn); |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
728 | close(clientfd); |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
729 | conn = NULL; |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
730 | } |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
731 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
732 | } else { |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
733 | conn->ssl = NULL; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
734 | conn->read = connection_read; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
735 | conn->write = connection_write; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
736 | conn->close = connection_close; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
737 | } |
61
c858850f3d3a
improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
738 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
739 | if(conn) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
740 | cfg_ref(ls->cfg); |
1 | 741 | |
440
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
742 | // enqueue the connection |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
743 | ls->session_handler->enqueue_connection( |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
744 | ls->session_handler, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
745 | conn); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
746 | } |
1 | 747 | |
440
d77b8f3e14e2
implement successor listener, fix webserver_reconfig
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
438
diff
changeset
|
748 | // ready for new connection |
38
d07810b02147
added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
749 | |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
750 | if(acceptor_exit || acceptor->exit) { |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
751 | // this acceptor is outdated |
446
240ed6f945ca
implement acceptor thread shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
445
diff
changeset
|
752 | log_ereport(LOG_VERBOSE, "acceptor thread %p: listener: %p exit", acceptor, acceptor->listener); |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
753 | break; |
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
754 | } |
568
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
755 | |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
756 | busy_sleep_time = 5; // reset sleep time |
3f8c587734aa
add error handling in case accept fails
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
557
diff
changeset
|
757 | sleep_counter = 0; |
1 | 758 | } |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
759 | |
449
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
760 | if(ws_atomic_dec32(acceptors_running) == 0) { |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
761 | // notify |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
762 | if(listener->shutdown) { |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
763 | log_ereport(LOG_DEBUG, "last acceptor shutdown: notify cfgmgr"); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
764 | pthread_mutex_lock(&listener->shutdown_mutex); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
765 | pthread_cond_signal(&listener->shutdown_cond); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
766 | pthread_mutex_unlock(&listener->shutdown_mutex); |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
767 | } |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
768 | } |
a28a5ccc894b
improve http listener migration / shutdown of old listener
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
446
diff
changeset
|
769 | |
445
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
770 | acceptor->running = FALSE; |
834351da593b
improve acceptor error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
444
diff
changeset
|
771 | |
444
96d2ba2f28db
fix configuration referencing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
441
diff
changeset
|
772 | cfg_unref(acceptor->listener->cfg); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
773 | |
577
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
774 | free(acceptor); |
4f5ccaea4a92
add shutdown cleanup (listener, log, threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
571
diff
changeset
|
775 | |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
776 | return NULL; |
1 | 777 | } |
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
778 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
779 | void wssocket_ref(WSSocket *ws) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
780 | ws_atomic_inc32(&ws->ref); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
781 | } |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
782 | |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
783 | void wssocket_unref(WSSocket *ws) { |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
784 | // does nothing yet, because maybe it is not a good idea to destroy |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
785 | // a socket |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
786 | ws_atomic_dec32(&ws->ref); |
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
787 | } |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
788 | |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
789 | |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
790 | int http_listener_apply_keep_alive_settings(HttpListener *listener, int fd) { |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
791 | // TODO: all these values should be configurable |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
792 | int optval = 1; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
793 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval))) { |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
794 | log_ereport(LOG_FAILURE, "listener: cannot set SO_KEEPALIVE: %s", strerror(errno)); |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
795 | return 1; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
796 | } |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
797 | |
554
e0a6b761ddbc
add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
798 | int keepidle = 240; |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
799 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepidle, sizeof(keepidle))) { |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
800 | log_ereport(LOG_FAILURE, "listener: cannot set TCP_KEEPIDLE to value %d: %s", keepidle, strerror(errno)); |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
801 | return 1; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
802 | } |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
803 | |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
804 | int keepintvl = 10; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
805 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &keepintvl, sizeof(keepintvl))) { |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
806 | log_ereport(LOG_FAILURE, "listener: cannot set TCP_KEEPINTVL to value %d: %s", keepintvl, strerror(errno)); |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
807 | return 1; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
808 | } |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
809 | |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
810 | int keepcnt = 3; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
811 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof(keepcnt))) { |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
812 | log_ereport(LOG_FAILURE, "listener: cannot set TCP_KEEPCNT to value %d: %s", keepcnt, strerror(errno)); |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
813 | return 1; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
814 | } |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
815 | |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
816 | return 0; |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
817 | } |