2 d3a08f4d 2021-01-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 d3a08f4d 2021-01-17 op * Permission to use, copy, modify, and distribute this software for any
5 d3a08f4d 2021-01-17 op * purpose with or without fee is hereby granted, provided that the above
6 d3a08f4d 2021-01-17 op * copyright notice and this permission notice appear in all copies.
8 d3a08f4d 2021-01-17 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d3a08f4d 2021-01-17 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d3a08f4d 2021-01-17 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d3a08f4d 2021-01-17 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d3a08f4d 2021-01-17 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d3a08f4d 2021-01-17 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d3a08f4d 2021-01-17 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 d3a08f4d 2021-01-17 op #include <sys/mman.h>
18 d3a08f4d 2021-01-17 op #include <sys/stat.h>
20 d3a08f4d 2021-01-17 op #include <netdb.h>
22 d3a08f4d 2021-01-17 op #include <assert.h>
23 d3a08f4d 2021-01-17 op #include <errno.h>
24 d3a08f4d 2021-01-17 op #include <fcntl.h>
25 b4d409cf 2021-01-21 op #include <fnmatch.h>
26 d3a08f4d 2021-01-17 op #include <string.h>
28 d3a08f4d 2021-01-17 op #include "gmid.h"
30 d3a08f4d 2021-01-17 op int connected_clients;
33 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
35 d3a08f4d 2021-01-17 op struct stat sb;
37 d3a08f4d 2021-01-17 op assert(path != NULL);
38 d3a08f4d 2021-01-17 op if ((*fd = openat(c->host->dirfd, *path ? path : ".",
39 f87bf597 2021-01-17 op O_RDONLY | O_NOFOLLOW)) == -1) {
40 d3a08f4d 2021-01-17 op return FILE_MISSING;
43 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
44 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
45 d3a08f4d 2021-01-17 op return FILE_MISSING;
48 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
49 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
51 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
52 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
54 d3a08f4d 2021-01-17 op return FILE_EXISTS;
58 0be51733 2021-01-20 op open_file(struct pollfd *fds, struct client *c)
60 0be51733 2021-01-20 op switch (check_path(c, c->iri.path, &c->fd)) {
61 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
62 0be51733 2021-01-20 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi))
63 0be51733 2021-01-20 op return start_cgi(c->iri.path, "", c->iri.query, fds, c);
65 d3a08f4d 2021-01-17 op /* fallthrough */
67 d3a08f4d 2021-01-17 op case FILE_EXISTS:
68 d3a08f4d 2021-01-17 op if ((c->len = filesize(c->fd)) == -1) {
69 0be51733 2021-01-20 op LOGE(c, "failed to get file size for %s", c->iri.path);
70 d3a08f4d 2021-01-17 op goodbye(fds, c);
74 d3a08f4d 2021-01-17 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
75 d3a08f4d 2021-01-17 op c->fd, 0)) == MAP_FAILED) {
76 0be51733 2021-01-20 op LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
77 d3a08f4d 2021-01-17 op goodbye(fds, c);
80 d3a08f4d 2021-01-17 op c->i = c->buf;
81 0be51733 2021-01-20 op if (!start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path)))
83 0be51733 2021-01-20 op send_file(fds, c);
86 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
89 0be51733 2021-01-20 op send_dir(fds, c);
92 d3a08f4d 2021-01-17 op case FILE_MISSING:
93 0be51733 2021-01-20 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi))
94 0be51733 2021-01-20 op return check_for_cgi(c->iri.path, c->iri.query, fds, c);
96 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
98 d3a08f4d 2021-01-17 op goodbye(fds, c);
102 d3a08f4d 2021-01-17 op /* unreachable */
109 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
110 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
111 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
112 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
113 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
116 d3a08f4d 2021-01-17 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
119 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
121 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
123 d3a08f4d 2021-01-17 op while (end > path) {
124 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
125 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
126 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
127 d3a08f4d 2021-01-17 op while (*end != '/')
131 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
132 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
133 d3a08f4d 2021-01-17 op return start_cgi(path, end+1, query, fds,c);
134 d3a08f4d 2021-01-17 op case FILE_MISSING:
145 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
147 d3a08f4d 2021-01-17 op goodbye(fds, c);
152 d3a08f4d 2021-01-17 op mark_nonblock(int fd)
156 d3a08f4d 2021-01-17 op if ((flags = fcntl(fd, F_GETFL)) == -1)
157 d3a08f4d 2021-01-17 op fatal("fcntl(F_GETFL): %s", strerror(errno));
158 d3a08f4d 2021-01-17 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
159 d3a08f4d 2021-01-17 op fatal("fcntl(F_SETFL): %s", strerror(errno));
163 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
165 d3a08f4d 2021-01-17 op struct vhost *h;
166 d3a08f4d 2021-01-17 op const char *servname;
168 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
169 d3a08f4d 2021-01-17 op case 0: /* success */
170 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
172 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
173 d3a08f4d 2021-01-17 op fds->events = POLLIN;
175 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
176 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
179 d3a08f4d 2021-01-17 op /* unreachable */
183 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
185 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
186 ce79c944 2021-01-21 op if (!strcmp(h->domain, "*"))
189 b4d409cf 2021-01-21 op if (servname != NULL && !fnmatch(h->domain, servname, 0))
193 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
194 d3a08f4d 2021-01-17 op c->state = S_OPEN;
196 d3a08f4d 2021-01-17 op handle_open_conn(fds, c);
200 0ab65593 2021-01-21 op if (servname != NULL)
201 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
203 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
205 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
207 d3a08f4d 2021-01-17 op goodbye(fds, c);
211 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
213 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
215 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
216 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
218 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
220 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
221 d3a08f4d 2021-01-17 op goodbye(fds, c);
224 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
225 d3a08f4d 2021-01-17 op fds->events = POLLIN;
228 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
229 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
233 0be51733 2021-01-20 op if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
234 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, BAD_REQUEST, parse_err))
236 d3a08f4d 2021-01-17 op goodbye(fds, c);
240 0be51733 2021-01-20 op /* XXX: we should check that the SNI matches the requested host */
241 0be51733 2021-01-20 op if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
242 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, PROXY_REFUSED, "won't proxy request"))
244 d3a08f4d 2021-01-17 op goodbye(fds, c);
248 0be51733 2021-01-20 op open_file(fds, c);
252 df79b4c1 2021-01-19 op start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
254 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
257 df79b4c1 2021-01-19 op c->code = code;
258 df79b4c1 2021-01-19 op c->meta = meta;
259 df79b4c1 2021-01-19 op c->state = S_INITIALIZING;
261 05c23a54 2021-01-19 op snprintf(buf, sizeof(buf), "%d ", code);
262 df79b4c1 2021-01-19 op strlcat(buf, meta, sizeof(buf));
263 df79b4c1 2021-01-19 op if (!strcmp(meta, "text/gemini") && c->host->lang != NULL) {
264 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
265 df79b4c1 2021-01-19 op strlcat(buf, c->host->lang, sizeof(buf));
268 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
269 0be51733 2021-01-20 op assert(len < sizeof(buf));
271 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
272 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
273 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
275 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
276 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
279 0be51733 2021-01-20 op log_request(c, buf, sizeof(buf));
285 d3a08f4d 2021-01-17 op start_cgi(const char *spath, const char *relpath, const char *query,
286 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
288 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
289 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
292 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
293 d3a08f4d 2021-01-17 op addr, sizeof(addr),
295 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
299 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
300 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
301 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
302 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
304 d3a08f4d 2021-01-17 op ruser = NULL;
305 d3a08f4d 2021-01-17 op cissuer = NULL;
306 d3a08f4d 2021-01-17 op chash = NULL;
309 d3a08f4d 2021-01-17 op if (!send_string(exfd, spath)
310 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
311 d3a08f4d 2021-01-17 op || !send_string(exfd, query)
312 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
313 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
314 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
315 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
316 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
319 d3a08f4d 2021-01-17 op close(c->fd);
320 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
321 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
323 d3a08f4d 2021-01-17 op goodbye(fds, c);
326 d3a08f4d 2021-01-17 op c->child = 1;
327 d3a08f4d 2021-01-17 op c->state = S_SENDING;
328 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
329 0be51733 2021-01-20 op c->code = -1;
330 d3a08f4d 2021-01-17 op /* handle_cgi(fds, c); */
334 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
335 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
339 0be51733 2021-01-20 op send_file(struct pollfd *fds, struct client *c)
341 d3a08f4d 2021-01-17 op ssize_t ret, len;
343 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
345 d3a08f4d 2021-01-17 op while (len > 0) {
346 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
348 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
349 d3a08f4d 2021-01-17 op goodbye(fds, c);
352 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
353 d3a08f4d 2021-01-17 op fds->events = POLLIN;
356 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
357 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
367 d3a08f4d 2021-01-17 op goodbye(fds, c);
371 0be51733 2021-01-20 op send_dir(struct pollfd *fds, struct client *c)
375 0be51733 2021-01-20 op /* guard against a re-entrant call:
377 0be51733 2021-01-20 op * open_file -> send_dir -> open_file -> send_dir
379 0be51733 2021-01-20 op * this can happen only if:
381 0be51733 2021-01-20 op * - user requested a dir, say foo/
382 0be51733 2021-01-20 op * - we try to serve foo/index.gmi
383 0be51733 2021-01-20 op * - foo/index.gmi is a directory.
385 0be51733 2021-01-20 op * It's an unlikely case, but can happen. We then redirect
386 0be51733 2021-01-20 op * to foo/index.gmi
388 0be51733 2021-01-20 op if (c->iri.path == c->sbuf) {
389 0be51733 2021-01-20 op if (!start_reply(fds, c, TEMP_REDIRECT, c->sbuf))
391 0be51733 2021-01-20 op goodbye(fds, c);
395 0be51733 2021-01-20 op len = strlen(c->iri.path);
396 0be51733 2021-01-20 op if (len > 0 && c->iri.path[len-1] != '/') {
397 0be51733 2021-01-20 op /* redirect to url with the trailing / */
398 0be51733 2021-01-20 op strlcpy(c->sbuf, c->iri.path, sizeof(c->sbuf));
399 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
400 0be51733 2021-01-20 op if (!start_reply(fds, c, TEMP_REDIRECT, c->sbuf))
402 0be51733 2021-01-20 op goodbye(fds, c);
406 0be51733 2021-01-20 op strlcpy(c->sbuf, c->iri.path, sizeof(c->sbuf));
407 0be51733 2021-01-20 op if (len != 0)
408 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
409 0be51733 2021-01-20 op len = strlcat(c->sbuf, "index.gmi", sizeof(c->sbuf));
411 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
412 0be51733 2021-01-20 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
414 0be51733 2021-01-20 op goodbye(fds, c);
418 0be51733 2021-01-20 op close(c->fd);
419 0be51733 2021-01-20 op c->iri.path = c->sbuf;
420 0be51733 2021-01-20 op open_file(fds, c);
424 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
428 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
430 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
432 d3a08f4d 2021-01-17 op fds->events = POLLIN;
434 d3a08f4d 2021-01-17 op fd = fds->fd;
435 d3a08f4d 2021-01-17 op fds->fd = c->fd;
440 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
444 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
446 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
448 d3a08f4d 2021-01-17 op fd = fds->fd;
449 d3a08f4d 2021-01-17 op fds->fd = c->fd;
454 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
458 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
459 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
462 d3a08f4d 2021-01-17 op if (c->len == 0) {
463 d3a08f4d 2021-01-17 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
465 d3a08f4d 2021-01-17 op if (r == -1) {
466 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
467 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
475 0be51733 2021-01-20 op /* XXX: if we haven't still read a whole
476 0be51733 2021-01-20 op * reply line, we should go back to poll! */
477 0be51733 2021-01-20 op if (c->code == -1) {
479 0be51733 2021-01-20 op log_request(c, c->sbuf, sizeof(c->sbuf));
483 d3a08f4d 2021-01-17 op while (c->len > 0) {
484 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
488 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
489 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
492 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
493 d3a08f4d 2021-01-17 op fds->events = POLLIN;
505 d3a08f4d 2021-01-17 op goodbye(fds, c);
509 d3a08f4d 2021-01-17 op goodbye(struct pollfd *pfd, struct client *c)
511 d3a08f4d 2021-01-17 op c->state = S_CLOSING;
513 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
514 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
515 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
517 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
518 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
522 d3a08f4d 2021-01-17 op connected_clients--;
524 d3a08f4d 2021-01-17 op tls_free(c->ctx);
525 d3a08f4d 2021-01-17 op c->ctx = NULL;
527 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
528 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
530 d3a08f4d 2021-01-17 op if (c->fd != -1)
531 d3a08f4d 2021-01-17 op close(c->fd);
533 d3a08f4d 2021-01-17 op close(pfd->fd);
534 d3a08f4d 2021-01-17 op pfd->fd = -1;
538 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
541 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
542 d3a08f4d 2021-01-17 op socklen_t len;
544 d3a08f4d 2021-01-17 op len = sizeof(addr);
545 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
546 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
548 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
551 d3a08f4d 2021-01-17 op mark_nonblock(fd);
553 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
554 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
555 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
556 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
557 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
559 d3a08f4d 2021-01-17 op fds[i].fd = fd;
560 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
562 d3a08f4d 2021-01-17 op clients[i].state = S_HANDSHAKE;
563 d3a08f4d 2021-01-17 op clients[i].fd = -1;
564 d3a08f4d 2021-01-17 op clients[i].child = 0;
565 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
566 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
567 d3a08f4d 2021-01-17 op clients[i].addr = addr;
569 d3a08f4d 2021-01-17 op connected_clients++;
578 d3a08f4d 2021-01-17 op handle(struct pollfd *fds, struct client *client)
580 d3a08f4d 2021-01-17 op switch (client->state) {
581 d3a08f4d 2021-01-17 op case S_HANDSHAKE:
582 d3a08f4d 2021-01-17 op handle_handshake(fds, client);
586 d3a08f4d 2021-01-17 op handle_open_conn(fds, client);
589 d3a08f4d 2021-01-17 op case S_INITIALIZING:
590 d3a08f4d 2021-01-17 op if (!start_reply(fds, client, client->code, client->meta))
593 d3a08f4d 2021-01-17 op if (client->code != SUCCESS) {
594 d3a08f4d 2021-01-17 op /* we don't need a body */
595 d3a08f4d 2021-01-17 op goodbye(fds, client);
599 d3a08f4d 2021-01-17 op client->state = S_SENDING;
601 d3a08f4d 2021-01-17 op /* fallthrough */
603 d3a08f4d 2021-01-17 op case S_SENDING:
604 d3a08f4d 2021-01-17 op if (client->child)
605 d3a08f4d 2021-01-17 op handle_cgi(fds, client);
607 0be51733 2021-01-20 op send_file(fds, client);
610 d3a08f4d 2021-01-17 op case S_CLOSING:
611 d3a08f4d 2021-01-17 op goodbye(fds, client);
615 d3a08f4d 2021-01-17 op /* unreachable */
621 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
624 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
625 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
627 d3a08f4d 2021-01-17 op connected_clients = 0;
629 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
630 d3a08f4d 2021-01-17 op fds[i].fd = -1;
631 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
632 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
635 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
636 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
639 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
640 d3a08f4d 2021-01-17 op if (errno == EINTR) {
641 132cae8c 2021-01-18 op fprintf(stderr, "connected clients: %d\n",
642 d3a08f4d 2021-01-17 op connected_clients);
645 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
648 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
649 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
652 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
653 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
654 d3a08f4d 2021-01-17 op strerror(errno));
656 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
657 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
658 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
659 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
660 d3a08f4d 2021-01-17 op goodbye(&fds[i], &clients[i]);
665 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
666 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
667 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
668 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
670 d3a08f4d 2021-01-17 op handle(&fds[i], &clients[i]);