src/server/daemon/httplistener.c

Wed, 09 Nov 2022 11:51:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 09 Nov 2022 11:51:19 +0100
changeset 422
76f2f5d532d0
parent 415
d938228c382e
child 438
22eca559aded
permissions
-rw-r--r--

extend serverconfig tokenizer to create separate tokens for brackets

1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
29 #include "../public/nsapi.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdlib.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <fcntl.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <sys/shm.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <sys/types.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include <sys/ipc.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <sys/socket.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include <sys/file.h>
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
39 #include <arpa/inet.h>
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include <netinet/in.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include <netdb.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include <stdio.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include <stdlib.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include <fcntl.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #include <unistd.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 #include <strings.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #include <stdbool.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 #include <pthread.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
50 #include <arpa/inet.h>
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
51 #include <netinet/in.h>
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
52 #include <stdio.h>
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
53 #include <stdlib.h>
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
54 #include <sys/socket.h>
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
55 #include <unistd.h>
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
56
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
57
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
58 #include <cx/hash_map.h>
92
382bff43c6eb fixed some includes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 87
diff changeset
59
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 38
diff changeset
60 #include "../util/atomic.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 #include "httplistener.h"
171
af7e2d80dee6 adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 169
diff changeset
62 #include "netsite.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 #include "session.h"
19
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
65 #include "configmanager.h"
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
66 #include "log.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
68 #define LISTENER_MAX_PROTOCOL_TOKENS 1024
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
69
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
70 CxMap *listener_map = NULL;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 int start_all_listener() {
19
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
73 ServerConfiguration *conf = cfgmgr_get_server_config();
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
74 CxList *ls = conf->listeners;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
75 CxIterator iter = cxListIterator(ls, 0);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
76 cx_foreach(HttpListener *, listener, iter) {
19
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
77 http_listener_start(listener);
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
78 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 return 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
83 HttpListener* http_listener_create(ListenerConfig *conf) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
84 if(listener_map == NULL) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
85 listener_map = cxHashMapCreate(cxDefaultAllocator, 16);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
86 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
87
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
88 HttpListener *fl = cxMapGet(listener_map, cx_hash_key(conf->name.ptr, conf->name.length));
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
89 if(fl == NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
90 return http_listener_new(conf);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
91 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
92
111
c93be34fde76 fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
93 HttpListener* newls = calloc(1, sizeof(HttpListener));
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
94 if(newls == NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
95 // TODO: error
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
96 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
97
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
98 newls->name = conf->name;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
99 newls->cfg = conf->cfg;
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
100 newls->nacceptors = conf->nacceptors;
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
101 newls->default_vs.vs_name = conf->vs.ptr;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
102 newls->port = fl->port;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
103 newls->server_socket = fl->server_socket;
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
104 newls->server_socket6 = fl->server_socket6;
74
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
105 newls->running = 1;
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
106 newls->threadpool = NULL;
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
107 newls->ref = 2; // 1 reference is fl->next
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
108
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
109 newls->session_handler = fl->session_handler; // TODO
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
110
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
111 // the listener threadpool might be changed
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
112 if(conf->threadpool.ptr != NULL) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
113 newls->threadpool = get_threadpool(cx_strcast(conf->threadpool));
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
114 }
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
115 if(newls->threadpool == NULL) {
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
116 newls->threadpool = get_default_threadpool();
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
117 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
118
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
119 // create acceptor threads
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
120 newls->acceptors = calloc(newls->nacceptors, sizeof(void*));
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
121 for (int i=0;i<newls->nacceptors;i++) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
122 newls->acceptors[i] = acceptor_new(newls);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
123 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
124
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
125 newls->acceptors6 = calloc(newls->nacceptors, sizeof(void*));
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
126 for (int i=0;i<newls->nacceptors;i++) {
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
127 newls->acceptors6[i] = acceptor_new(newls);
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
128 newls->acceptors6[i]->ipv6 = TRUE;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
129 }
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
130
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
131 // fl hold one reference of newls
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
132 fl->next = newls;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
133
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
134
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
135 cxMapPut(listener_map, cx_hash_key(newls->name.ptr, newls->name.length), newls);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
136
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
137 for (int i=0;i<newls->nacceptors;i++) {
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
138 //acceptor_start(newls->acceptors[i]);
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
139 acceptor_start(newls->acceptors6[i]);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
140 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
141
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
142 // check if a restart is required to apply all changes
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
143
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
144 if(newls->port != conf->port) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
145 // TODO: log
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
146 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
147
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
148 return newls;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
149 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 HttpListener* http_listener_new(ListenerConfig *conf) {
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
152 // TODO: remove
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
153 if(listener_map == NULL) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
154 listener_map = cxHashMapCreate(cxDefaultAllocator, 16);
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
155 }
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
156
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
157 HttpListener *fl = cxMapGet(listener_map, cx_hash_key(conf->name.ptr, conf->name.length));
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
158 if(fl != NULL) {
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
159 return fl;
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
160 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
161 // end remove
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
162
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 HttpListener *listener = malloc(sizeof(HttpListener));
74
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
164 listener->running = 0;
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
165 listener->cfg = conf->cfg;
19
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
166 listener->name = conf->name;
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
167 listener->default_vs.vs_name = conf->vs.ptr;
81
d25825f37967 preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
168 listener->threadpool = NULL;
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
169 if(conf->threadpool.ptr != NULL) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
170 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
171 }
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
172 if(listener->threadpool == NULL) {
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
173 listener->threadpool = get_default_threadpool();
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
174 }
134
44415e4399ce adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
175 if(conf->blockingio) {
44415e4399ce adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
176 listener->session_handler = create_basic_session_handler();
44415e4399ce adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
177 } else {
44415e4399ce adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
178 listener->session_handler = create_event_session_handler();
44415e4399ce adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
179 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 listener->nacceptors = conf->nacceptors;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
181 listener->port = conf->port;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
182 listener->ref = 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
183 listener->next = NULL;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
184 listener->ssl = NULL;
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
185
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
186 int error = 0;
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
187
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
188 if(conf->ssl) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
189 listener->ssl = malloc(sizeof(HttpSSL));
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
190
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
191 SSL_CTX *ctx = SSL_CTX_new(SSLv23_server_method());
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
192 SSL_CTX_set_options(
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
193 ctx,
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
194 SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv3);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
195 if(conf->disable_proto.ptr) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
196 cxstring *plist = NULL;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
197 ssize_t n = cx_strsplit_a(cxDefaultAllocator, conf->disable_proto, cx_str(","), LISTENER_MAX_PROTOCOL_TOKENS, &plist);
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
198 if(plist) {
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
199 for(int i=0;i<n;i++) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
200 cxstring proto = plist[i];
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
201 log_ereport(
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
202 LOG_VERBOSE,
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
203 "Listener %s: Disable protocol %s",
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
204 listener->name.ptr,
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
205 proto.ptr);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
206 if(!cx_strcasecmp(cx_strtrim(proto), cx_str("SSLv2"))) {
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
207 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
208 } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("SSLv3"))) {
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
209 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
210 } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1"))) {
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
211 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
212 } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.1"))) {
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
213 #ifdef SSL_OP_NO_TLSv1_1
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
214 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
215 #else
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
216 log_ereport(
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
217 LOG_WARN,
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
218 "Listener: %s: TLSv1.1 already not supported",
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
219 listener->name.ptr);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
220 #endif
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
221 } else if(cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.2"))) {
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
222 #ifdef SSL_OP_NO_TLSv1_2
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
223 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
224 #else
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
225 log_ereport(
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
226 LOG_WARN,
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
227 "Listener: %s: TLSv1.2 already not supported",
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
228 listener->name.ptr);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
229 #endif
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
230 } else if(cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.3"))) {
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
231 #ifdef SSL_OP_NO_TLSv1_3
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
232 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_3);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
233 #else
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
234 log_ereport(
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
235 LOG_WARN,
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
236 "Listener: %s: TLSv1.3 already not supported",
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
237 listener->name.ptr);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
238 #endif
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
239 } else {
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
240 error = 1;
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
241 log_ereport(
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
242 LOG_MISCONFIG,
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
243 "Listener: %s: Unknown protocol %s",
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
244 listener->name.ptr,
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
245 proto.ptr);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
246 }
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
247 }
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
248 free(plist);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
249 }
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
250 }
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
251
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
252 if(error) {
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
253 return NULL;
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
254 }
129
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
255 // TODO: cleanup on error
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
256
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
257 int ret;
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
258 char errbuf[512];
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
259
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
260 if(!conf->chainfile.ptr) {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 194
diff changeset
261 ret = SSL_CTX_use_certificate_file(ctx, conf->certfile.ptr, SSL_FILETYPE_PEM);
129
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
262 if(!ret) {
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
263 ERR_error_string(ERR_get_error(), errbuf);
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
264 log_ereport(LOG_MISCONFIG, "Cannot load ssl chain file: %s", errbuf);
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
265 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
266 }
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
267 } else {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 194
diff changeset
268 ret = SSL_CTX_use_certificate_chain_file(ctx, conf->chainfile.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
269 if(!ret) {
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
270 ERR_error_string(ERR_get_error(), errbuf);
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
271 log_ereport(LOG_MISCONFIG, "Cannot load ssl cert file: %s", errbuf);
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
272 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
273 }
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
274 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
275
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 194
diff changeset
276 ret = SSL_CTX_use_PrivateKey_file(ctx, conf->privkeyfile.ptr, SSL_FILETYPE_PEM);
129
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
277 if(!ret) {
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
278 ERR_error_string(ERR_get_error(), errbuf);
fd324464f56f adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
279 log_ereport(LOG_MISCONFIG, "Cannot load ssl key file: %s", errbuf);
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
280 return NULL;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
281 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
282
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
283 // TODO: chain
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
284 listener->ssl->sslctx = ctx;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
285 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
286
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
287
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 398
diff changeset
288 cxMapPut(listener_map, cx_hash_key(listener->name.ptr, listener->name.length), listener);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 struct sockaddr_in servaddr; /* server address */
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
291 struct sockaddr_in6 servaddr6;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 /* init address structure */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 memset(&servaddr, 0, sizeof(servaddr));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 servaddr.sin_family = AF_INET;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 servaddr.sin_port = htons(conf->port);
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
298
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
299 memset(&servaddr6, 0, sizeof(servaddr6));
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
300 servaddr6.sin6_family = AF_INET6;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
301 servaddr6.sin6_addr = in6addr_any;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
302 servaddr6.sin6_port = htons(conf->port);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 /* create socket */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 if((listener->server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 perror("Error: http_listener_new: socket");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 }
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
309
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
310 if((listener->server_socket6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) == -1) {
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
311 perror("Error: http_listener_new: socket v6");
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
312 return NULL;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
313 }
398
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
314
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 int o = 1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 setsockopt(
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 listener->server_socket,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 SOL_SOCKET, SO_REUSEADDR,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 &o,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 sizeof(int));
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
321 o = 1;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
322 setsockopt(
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
323 listener->server_socket6,
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
324 SOL_SOCKET, SO_REUSEADDR,
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
325 &o,
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
326 sizeof(int));
398
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
327
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
328 #ifdef LINUX
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
329 o = 1;
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
330 setsockopt(
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
331 listener->server_socket6,
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
332 IPPROTO_IPV6,
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
333 IPV6_V6ONLY,
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
334 &o,
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
335 sizeof(int));
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
336 #endif
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 /* bind server socket to address */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 if(bind(listener->server_socket, (struct sockaddr*)&servaddr, sizeof(servaddr))){
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 111
diff changeset
340 log_ereport(LOG_FAILURE, "http_listener_new: bind failed. Port: %d", conf->port);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 }
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
343
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
344 if(bind(listener->server_socket6, (struct sockaddr*)&servaddr6, sizeof(servaddr6))){
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
345 log_ereport(LOG_FAILURE, "http_listener_new: bind v6 failed. Port: %d: %s", conf->port, strerror(errno));
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
346 return NULL;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
347 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 /* create acceptors */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 listener->acceptors = calloc(listener->nacceptors, sizeof(void*));
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
351 listener->acceptors6 = calloc(listener->nacceptors, sizeof(void*));
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 for (int i=0;i<listener->nacceptors;i++) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 listener->acceptors[i] = acceptor_new(listener);
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
354 listener->acceptors6[i] = acceptor_new(listener);
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
355 listener->acceptors6[i]->ipv6 = TRUE;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 return listener;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 int http_listener_start(HttpListener *listener) {
74
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
362 if(listener->running) {
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
363 return 0;
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
364 }
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
365 log_ereport(LOG_INFORM, "start listener on port %d", listener->port);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366
21
627b09ee74e4 New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
367 if (listen(listener->server_socket, 256) == -1) {
398
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
368 log_ereport(LOG_FAILURE, "http_listener_start: listen failed: %s", strerror(errno));
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 return -1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 }
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
371 if (listen(listener->server_socket6, 256) == -1) {
398
83234bc3bee9 use IPV6_V6ONLY socket option on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 396
diff changeset
372 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
373 return -1;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
374 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 /* start acceptor threads */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 for (int i=0;i<listener->nacceptors;i++) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 acceptor_start(listener->acceptors[i]);
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
379 acceptor_start(listener->acceptors6[i]);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 }
19
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
381
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
382 return 0;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
385 void http_listener_ref(HttpListener *listener) {
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 38
diff changeset
386 ws_atomic_inc32(&listener->ref);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
387 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
388
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
389 void http_listener_unref(HttpListener *listener) {
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 38
diff changeset
390 uint32_t ref = ws_atomic_dec32(&listener->ref);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
391 if(ref == 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
392 free(listener->acceptors);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
393 // TODO: unref cfg
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
394 // TODO: unref session handler
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
395 free(listener);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
396 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
397 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
398
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 Acceptor* acceptor_new(HttpListener *listener) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 Acceptor *acceptor = malloc(sizeof(Acceptor));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 acceptor->listener = listener;
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
404 acceptor->ipv6 = WS_FALSE;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 return acceptor;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 void acceptor_start(Acceptor *a) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 if(pthread_create(
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 &a->tid,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 NULL,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 (void*(*)(void*))acceptor_thread,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 a) != 0)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 perror("Error: acceptor_start: pthread_create");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 void* acceptor_thread(Acceptor *acceptor) {
87
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
420 WS_ASSERT(acceptor);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
421 WS_ASSERT(acceptor->listener);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
422 WS_ASSERT(acceptor->listener->session_handler);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
423 WS_ASSERT(acceptor->listener->session_handler->enqueue_connection);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
424
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 HttpListener *listener = acceptor->listener;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
427 int server_socket;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
428
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
429 ConnectionAddr ca;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
430 struct sockaddr *ca_ptr;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
431 socklen_t ca_length;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
432 ConnectionAddrType addr_type;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
433 if(acceptor->ipv6) {
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
434 server_socket = listener->server_socket6;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
435 ca_ptr = (struct sockaddr*)&ca.address_v6;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
436 ca_length = sizeof(ca.address_v6);
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
437 addr_type = CONN_ADDR_IPV6;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
438 } else {
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
439 server_socket = listener->server_socket;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
440 ca_ptr = (struct sockaddr*)&ca.address_v4;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
441 ca_length = sizeof(ca.address_v4);
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
442 addr_type = CONN_ADDR_IPV4;
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
443 }
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
444
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
445
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 for (;;) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 /* accept connections */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448 int clientfd;
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
449 socklen_t length = ca_length;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 /* accept a connection */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 clientfd = accept(
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
453 server_socket,
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
454 ca_ptr,
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 &length);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456 if (clientfd == -1) {
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
457 log_ereport(LOG_FAILURE, "accept %s failed: %s", acceptor->ipv6 ? "ipv6" : "ipv4", strerror(errno));
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458 continue;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 }
145
1c93281ca4bf fixes memory leaks in request_stat_path and send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 134
diff changeset
460
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
461 // check listener
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
462 HttpListener *ls = listener;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
463 int acceptor_exit = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
464 while(ls->next) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
465 ls = ls->next;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
466 acceptor_exit = 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
467 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
468
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 /* create Connection object */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 Connection *conn = malloc(sizeof(Connection));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 conn->address = ca;
396
77d81f2bb9f7 add initial ipv6 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
472 conn->addr_type = addr_type;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 conn->fd = clientfd;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
474 conn->listener = ls;
194
6345f50208d5 fixes keep-alive with ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
475 conn->ssl_accepted = 0;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
476 if(ls->ssl) {
191
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
477 // 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
478 // set socket non blocking
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
479 int flags;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
480 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
481 flags = 0;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
482 }
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
483 if(fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK)) {
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
484 perror("Error: acceptor_thread: fcntl");
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
485 // TODO: error
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
486 }
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
487
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
488 SSL *ssl = SSL_new(ls->ssl->sslctx);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
489 SSL_set_fd(ssl, clientfd);
188
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
490
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
491 conn->ssl = ssl;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
492 conn->read = connection_ssl_read;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
493 conn->write = connection_ssl_write;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
494 conn->close = connection_ssl_close;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
495 } else {
111
c93be34fde76 fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
496 conn->ssl = NULL;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
497 conn->read = connection_read;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
498 conn->write = connection_write;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
499 conn->close = connection_close;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
500 }
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
501
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
502 if(conn) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
503 cfg_ref(ls->cfg);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
505 /* enqueue the connection */
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
506 ls->session_handler->enqueue_connection(
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
507 ls->session_handler,
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
508 conn);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 101
diff changeset
509 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
511 /* ready for new connection */
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
512
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
513 if(acceptor_exit) {
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
514 // this acceptor is outdated
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
515 break;
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
516 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
517 }
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
518
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
519 http_listener_unref(listener->next);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
520 http_listener_unref(listener);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
521
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
522 return NULL;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 }

mercurial