1 | /* $Id: server.c 16 2024-09-13 15:09:52Z nishi $ */
|
---|
2 |
|
---|
3 | #define SOURCE
|
---|
4 |
|
---|
5 | #include "tw_server.h"
|
---|
6 |
|
---|
7 | #include "tw_ssl.h"
|
---|
8 | #include "tw_config.h"
|
---|
9 | #include "tw_http.h"
|
---|
10 |
|
---|
11 | #include <unistd.h>
|
---|
12 | #include <string.h>
|
---|
13 | #include <stdbool.h>
|
---|
14 |
|
---|
15 | #include <cm_log.h>
|
---|
16 |
|
---|
17 | #ifdef __MINGW32__
|
---|
18 | #include <winsock2.h>
|
---|
19 | #include <process.h>
|
---|
20 | #define NO_IPV6
|
---|
21 | #else
|
---|
22 | #include <sys/select.h>
|
---|
23 | #include <sys/socket.h>
|
---|
24 | #include <arpa/inet.h>
|
---|
25 | #include <netinet/in.h>
|
---|
26 | #include <netinet/tcp.h>
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | extern struct tw_config config;
|
---|
30 |
|
---|
31 | fd_set fdset;
|
---|
32 | int sockcount = 0;
|
---|
33 |
|
---|
34 | #ifdef NO_IPV6
|
---|
35 | #define SOCKADDR struct sockaddr_in
|
---|
36 | #else
|
---|
37 | #define SOCKADDR struct sockaddr_in6
|
---|
38 | #endif
|
---|
39 | SOCKADDR addresses[MAX_PORTS];
|
---|
40 | int sockets[MAX_PORTS];
|
---|
41 |
|
---|
42 | void close_socket(int sock) {
|
---|
43 | #ifdef __MINGW32__
|
---|
44 | closesocket(sock);
|
---|
45 | #else
|
---|
46 | close(sock);
|
---|
47 | #endif
|
---|
48 | }
|
---|
49 |
|
---|
50 | int tw_server_init(void) {
|
---|
51 | int i;
|
---|
52 | #ifdef __MINGW32__
|
---|
53 | WSADATA wsa;
|
---|
54 | WSAStartup(MAKEWORD(2, 0), &wsa);
|
---|
55 | #endif
|
---|
56 | for(i = 0; config.ports[i] != -1; i++)
|
---|
57 | ;
|
---|
58 | sockcount = i;
|
---|
59 | for(i = 0; config.ports[i] != -1; i++) {
|
---|
60 | #ifdef NO_IPV6
|
---|
61 | int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
62 | #else
|
---|
63 | int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
---|
64 | #endif
|
---|
65 | #ifdef __MINGW32__
|
---|
66 | if(sock == INVALID_SOCKET)
|
---|
67 | #else
|
---|
68 | if(sock < 0)
|
---|
69 | #endif
|
---|
70 | {
|
---|
71 | cm_log("Server", "Socket creation failure");
|
---|
72 | return 1;
|
---|
73 | }
|
---|
74 | int yes = 1;
|
---|
75 | if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
|
---|
76 | close_socket(sock);
|
---|
77 | cm_log("Server", "setsockopt failure (reuseaddr)");
|
---|
78 | return 1;
|
---|
79 | }
|
---|
80 | if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
|
---|
81 | close_socket(sock);
|
---|
82 | cm_log("Server", "setsockopt failure (nodelay)");
|
---|
83 | return 1;
|
---|
84 | }
|
---|
85 | #ifndef NO_IPV6
|
---|
86 | int no = 0;
|
---|
87 | if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
|
---|
88 | close_socket(sock);
|
---|
89 | cm_log("Server", "setsockopt failure (IPv6)");
|
---|
90 | return 1;
|
---|
91 | }
|
---|
92 | #endif
|
---|
93 | memset(&addresses[i], 0, sizeof(addresses[i]));
|
---|
94 | #ifdef NO_IPV6
|
---|
95 | addresses[i].sin_family = AF_INET;
|
---|
96 | addresses[i].sin_addr.s_addr = INADDR_ANY;
|
---|
97 | addresses[i].sin_port = htons(config.ports[i]);
|
---|
98 | #else
|
---|
99 | addresses[i].sin6_family = AF_INET6;
|
---|
100 | addresses[i].sin6_addr = in6addr_any;
|
---|
101 | addresses[i].sin6_port = htons(config.ports[i]);
|
---|
102 | #endif
|
---|
103 | if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
|
---|
104 | close_socket(sock);
|
---|
105 | cm_log("Server", "Bind failure");
|
---|
106 | return 1;
|
---|
107 | }
|
---|
108 | if(listen(sock, 128) < 0) {
|
---|
109 | close_socket(sock);
|
---|
110 | cm_log("Server", "Listen failure");
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 | sockets[i] = sock;
|
---|
114 | }
|
---|
115 | return 0;
|
---|
116 | }
|
---|
117 |
|
---|
118 | size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
|
---|
119 | if(ssl == NULL) {
|
---|
120 | return recv(s, data, len, 0);
|
---|
121 | } else {
|
---|
122 | return SSL_read(ssl, data, len);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
|
---|
127 | if(ssl == NULL) {
|
---|
128 | return send(s, data, len, 0);
|
---|
129 | } else {
|
---|
130 | return SSL_write(ssl, data, len);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | #ifdef __MINGW32__
|
---|
135 | struct pass_entry {
|
---|
136 | int sock;
|
---|
137 | int port;
|
---|
138 | bool ssl;
|
---|
139 | };
|
---|
140 |
|
---|
141 | unsigned int WINAPI tw_server_pass(void* ptr) {
|
---|
142 | int sock = ((struct pass_entry*)ptr)->sock;
|
---|
143 | bool ssl = ((struct pass_entry*)ptr)->ssl;
|
---|
144 | int port = ((struct pass_entry*)ptr)->port;
|
---|
145 | free(ptr);
|
---|
146 | #else
|
---|
147 | void tw_server_pass(int sock, bool ssl, int port) {
|
---|
148 | #endif
|
---|
149 | char* name = config.hostname;
|
---|
150 |
|
---|
151 | SSL_CTX* ctx = NULL;
|
---|
152 | SSL* s = NULL;
|
---|
153 | bool sslworks = false;
|
---|
154 | if(ssl) {
|
---|
155 | ctx = tw_create_ssl_ctx(port);
|
---|
156 | s = SSL_new(ctx);
|
---|
157 | SSL_set_fd(s, sock);
|
---|
158 | if(SSL_accept(s) <= 0) goto cleanup;
|
---|
159 | sslworks = true;
|
---|
160 | }
|
---|
161 | struct tw_http_request req;
|
---|
162 | int ret = tw_http_parse(s, sock, &req);
|
---|
163 | cleanup:
|
---|
164 | if(sslworks) {
|
---|
165 | SSL_shutdown(s);
|
---|
166 | }
|
---|
167 | SSL_free(s);
|
---|
168 | close_socket(sock);
|
---|
169 | #ifdef __MINGW32__
|
---|
170 | _endthreadex(0);
|
---|
171 | #endif
|
---|
172 | }
|
---|
173 |
|
---|
174 | void tw_server_loop(void) {
|
---|
175 | struct timeval tv;
|
---|
176 | while(1) {
|
---|
177 | FD_ZERO(&fdset);
|
---|
178 | int i;
|
---|
179 | for(i = 0; i < sockcount; i++) {
|
---|
180 | FD_SET(sockets[i], &fdset);
|
---|
181 | }
|
---|
182 | tv.tv_sec = 1;
|
---|
183 | tv.tv_usec = 0;
|
---|
184 | int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
|
---|
185 | if(ret == -1) {
|
---|
186 | break;
|
---|
187 | } else if(ret > 0) {
|
---|
188 | /* connection */
|
---|
189 | int i;
|
---|
190 | for(i = 0; i < sockcount; i++) {
|
---|
191 | if(FD_ISSET(sockets[i], &fdset)) {
|
---|
192 | SOCKADDR claddr;
|
---|
193 | int clen = sizeof(claddr);
|
---|
194 | int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
|
---|
195 | cm_log("Server", "New connection accepted");
|
---|
196 | #ifdef __MINGW32__
|
---|
197 | HANDLE thread;
|
---|
198 | struct pass_entry* e = malloc(sizeof(*e));
|
---|
199 | e->sock = sock;
|
---|
200 | e->ssl = config.ports[i] & (1ULL << 32);
|
---|
201 | e->port = config.ports[i];
|
---|
202 | thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
|
---|
203 | #else
|
---|
204 | pid_t pid = fork();
|
---|
205 | if(pid == 0) {
|
---|
206 | tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i]);
|
---|
207 | _exit(0);
|
---|
208 | } else {
|
---|
209 | close_socket(sock);
|
---|
210 | }
|
---|
211 | #endif
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|