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 d3a08f4d 2021-01-17 op #include <string.h>
27 d3a08f4d 2021-01-17 op #include "gmid.h"
29 d3a08f4d 2021-01-17 op int connected_clients;
32 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
34 d3a08f4d 2021-01-17 op struct stat sb;
36 d3a08f4d 2021-01-17 op assert(path != NULL);
37 d3a08f4d 2021-01-17 op if ((*fd = openat(c->host->dirfd, *path ? path : ".",
38 f87bf597 2021-01-17 op O_RDONLY | O_NOFOLLOW)) == -1) {
39 d3a08f4d 2021-01-17 op return FILE_MISSING;
42 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
43 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
44 d3a08f4d 2021-01-17 op return FILE_MISSING;
47 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
48 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
50 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
51 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
53 d3a08f4d 2021-01-17 op return FILE_EXISTS;
57 d3a08f4d 2021-01-17 op open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
59 d3a08f4d 2021-01-17 op switch (check_path(c, fpath, &c->fd)) {
60 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
61 d3a08f4d 2021-01-17 op if (c->host->cgi != NULL && starts_with(fpath, c->host->cgi))
62 d3a08f4d 2021-01-17 op return start_cgi(fpath, "", query, fds, c);
64 d3a08f4d 2021-01-17 op /* fallthrough */
66 d3a08f4d 2021-01-17 op case FILE_EXISTS:
67 d3a08f4d 2021-01-17 op if ((c->len = filesize(c->fd)) == -1) {
68 d3a08f4d 2021-01-17 op LOGE(c, "failed to get file size for %s", fpath);
69 d3a08f4d 2021-01-17 op goodbye(fds, c);
73 d3a08f4d 2021-01-17 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
74 d3a08f4d 2021-01-17 op c->fd, 0)) == MAP_FAILED) {
75 132cae8c 2021-01-18 op LOGW(c, "mmap: %s: %s", fpath, strerror(errno));
76 d3a08f4d 2021-01-17 op goodbye(fds, c);
79 d3a08f4d 2021-01-17 op c->i = c->buf;
80 d3a08f4d 2021-01-17 op return start_reply(fds, c, SUCCESS, mime(fpath));
82 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
83 d3a08f4d 2021-01-17 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
86 d3a08f4d 2021-01-17 op send_dir(fpath, fds, c);
89 d3a08f4d 2021-01-17 op case FILE_MISSING:
90 d3a08f4d 2021-01-17 op if (c->host->cgi != NULL && starts_with(fpath, c->host->cgi))
91 d3a08f4d 2021-01-17 op return check_for_cgi(fpath, query, fds, c);
93 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
95 d3a08f4d 2021-01-17 op goodbye(fds, c);
99 d3a08f4d 2021-01-17 op /* unreachable */
106 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
107 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
108 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
109 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
110 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
113 d3a08f4d 2021-01-17 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
116 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
118 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
120 d3a08f4d 2021-01-17 op while (end > path) {
121 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
122 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
123 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
124 d3a08f4d 2021-01-17 op while (*end != '/')
128 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
129 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
130 d3a08f4d 2021-01-17 op return start_cgi(path, end+1, query, fds,c);
131 d3a08f4d 2021-01-17 op case FILE_MISSING:
142 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
144 d3a08f4d 2021-01-17 op goodbye(fds, c);
149 d3a08f4d 2021-01-17 op mark_nonblock(int fd)
153 d3a08f4d 2021-01-17 op if ((flags = fcntl(fd, F_GETFL)) == -1)
154 d3a08f4d 2021-01-17 op fatal("fcntl(F_GETFL): %s", strerror(errno));
155 d3a08f4d 2021-01-17 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
156 d3a08f4d 2021-01-17 op fatal("fcntl(F_SETFL): %s", strerror(errno));
160 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
162 d3a08f4d 2021-01-17 op struct vhost *h;
163 d3a08f4d 2021-01-17 op const char *servname;
165 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
166 d3a08f4d 2021-01-17 op case 0: /* success */
167 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
169 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
170 d3a08f4d 2021-01-17 op fds->events = POLLIN;
172 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
173 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
176 d3a08f4d 2021-01-17 op /* unreachable */
180 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
181 d3a08f4d 2021-01-17 op if (servname == NULL)
182 d3a08f4d 2021-01-17 op goto hostnotfound;
184 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
185 d3a08f4d 2021-01-17 op if (!strcmp(h->domain, servname) || !strcmp(h->domain, "*"))
189 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
190 d3a08f4d 2021-01-17 op c->state = S_OPEN;
192 d3a08f4d 2021-01-17 op handle_open_conn(fds, c);
196 d3a08f4d 2021-01-17 op hostnotfound:
197 d3a08f4d 2021-01-17 op /* XXX: check the correct response */
198 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
200 d3a08f4d 2021-01-17 op goodbye(fds, c);
204 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
206 d3a08f4d 2021-01-17 op char buf[GEMINI_URL_LEN];
207 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
208 d3a08f4d 2021-01-17 op struct iri iri;
210 d3a08f4d 2021-01-17 op bzero(buf, sizeof(buf));
212 d3a08f4d 2021-01-17 op switch (tls_read(c->ctx, buf, sizeof(buf)-1)) {
214 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
215 d3a08f4d 2021-01-17 op goodbye(fds, c);
218 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
219 d3a08f4d 2021-01-17 op fds->events = POLLIN;
222 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
223 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
227 d3a08f4d 2021-01-17 op if (!trim_req_iri(buf) || !parse_iri(buf, &iri, &parse_err)) {
228 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, BAD_REQUEST, parse_err))
230 d3a08f4d 2021-01-17 op goodbye(fds, c);
234 d3a08f4d 2021-01-17 op if (strcmp(iri.schema, "gemini") || iri.port_no != conf.port) {
235 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, PROXY_REFUSED, "won't proxy request"))
237 d3a08f4d 2021-01-17 op goodbye(fds, c);
241 d3a08f4d 2021-01-17 op LOGI(c, "GET %s%s%s",
242 d3a08f4d 2021-01-17 op *iri.path ? iri.path : "/",
243 d3a08f4d 2021-01-17 op *iri.query ? "?" : "",
244 d3a08f4d 2021-01-17 op *iri.query ? iri.query : "");
246 d3a08f4d 2021-01-17 op send_file(iri.path, iri.query, fds, c);
250 d3a08f4d 2021-01-17 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
252 d3a08f4d 2021-01-17 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
255 d3a08f4d 2021-01-17 op client->code = code;
256 d3a08f4d 2021-01-17 op client->meta = reason;
257 d3a08f4d 2021-01-17 op client->state = S_INITIALIZING;
259 d3a08f4d 2021-01-17 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
260 d3a08f4d 2021-01-17 op assert(len < (int)sizeof(buf));
262 d3a08f4d 2021-01-17 op switch (tls_write(client->ctx, buf, len)) {
263 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
264 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
266 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
267 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
275 d3a08f4d 2021-01-17 op start_cgi(const char *spath, const char *relpath, const char *query,
276 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
278 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
279 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
282 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
283 d3a08f4d 2021-01-17 op addr, sizeof(addr),
285 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
289 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
290 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
291 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
292 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
294 d3a08f4d 2021-01-17 op ruser = NULL;
295 d3a08f4d 2021-01-17 op cissuer = NULL;
296 d3a08f4d 2021-01-17 op chash = NULL;
299 d3a08f4d 2021-01-17 op if (!send_string(exfd, spath)
300 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
301 d3a08f4d 2021-01-17 op || !send_string(exfd, query)
302 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
303 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
304 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
305 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
306 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
309 d3a08f4d 2021-01-17 op close(c->fd);
310 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
311 d3a08f4d 2021-01-17 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
313 d3a08f4d 2021-01-17 op goodbye(fds, c);
316 d3a08f4d 2021-01-17 op c->child = 1;
317 d3a08f4d 2021-01-17 op c->state = S_SENDING;
318 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
319 d3a08f4d 2021-01-17 op /* handle_cgi(fds, c); */
323 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
324 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
328 d3a08f4d 2021-01-17 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
330 d3a08f4d 2021-01-17 op ssize_t ret, len;
332 d3a08f4d 2021-01-17 op if (c->fd == -1) {
333 d3a08f4d 2021-01-17 op if (!open_file(path, query, fds, c))
335 d3a08f4d 2021-01-17 op c->state = S_SENDING;
338 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
340 d3a08f4d 2021-01-17 op while (len > 0) {
341 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
343 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
344 d3a08f4d 2021-01-17 op goodbye(fds, c);
347 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
348 d3a08f4d 2021-01-17 op fds->events = POLLIN;
351 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
352 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
362 d3a08f4d 2021-01-17 op goodbye(fds, c);
366 d3a08f4d 2021-01-17 op send_dir(char *path, struct pollfd *fds, struct client *client)
368 d3a08f4d 2021-01-17 op char fpath[PATHBUF];
371 d3a08f4d 2021-01-17 op bzero(fpath, PATHBUF);
373 d3a08f4d 2021-01-17 op if (path[0] != '.')
374 d3a08f4d 2021-01-17 op fpath[0] = '.';
376 d3a08f4d 2021-01-17 op /* this cannot fail since sizeof(fpath) > maxlen of path */
377 d3a08f4d 2021-01-17 op strlcat(fpath, path, PATHBUF);
378 d3a08f4d 2021-01-17 op len = strlen(fpath);
380 d3a08f4d 2021-01-17 op /* add a trailing / in case. */
381 d3a08f4d 2021-01-17 op if (fpath[len-1] != '/') {
382 d3a08f4d 2021-01-17 op fpath[len] = '/';
385 d3a08f4d 2021-01-17 op strlcat(fpath, "index.gmi", sizeof(fpath));
387 d3a08f4d 2021-01-17 op send_file(fpath, NULL, fds, client);
391 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
395 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
397 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
399 d3a08f4d 2021-01-17 op fds->events = POLLIN;
401 d3a08f4d 2021-01-17 op fd = fds->fd;
402 d3a08f4d 2021-01-17 op fds->fd = c->fd;
407 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
411 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
413 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
415 d3a08f4d 2021-01-17 op fd = fds->fd;
416 d3a08f4d 2021-01-17 op fds->fd = c->fd;
421 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
425 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
426 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
429 d3a08f4d 2021-01-17 op if (c->len == 0) {
430 d3a08f4d 2021-01-17 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
432 d3a08f4d 2021-01-17 op if (r == -1) {
433 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
434 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
443 d3a08f4d 2021-01-17 op while (c->len > 0) {
444 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
448 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
449 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
452 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
453 d3a08f4d 2021-01-17 op fds->events = POLLIN;
465 d3a08f4d 2021-01-17 op goodbye(fds, c);
469 d3a08f4d 2021-01-17 op goodbye(struct pollfd *pfd, struct client *c)
471 d3a08f4d 2021-01-17 op c->state = S_CLOSING;
473 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
474 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
475 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
477 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
478 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
482 d3a08f4d 2021-01-17 op connected_clients--;
484 d3a08f4d 2021-01-17 op tls_free(c->ctx);
485 d3a08f4d 2021-01-17 op c->ctx = NULL;
487 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
488 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
490 d3a08f4d 2021-01-17 op if (c->fd != -1)
491 d3a08f4d 2021-01-17 op close(c->fd);
493 d3a08f4d 2021-01-17 op close(pfd->fd);
494 d3a08f4d 2021-01-17 op pfd->fd = -1;
498 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
501 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
502 d3a08f4d 2021-01-17 op socklen_t len;
504 d3a08f4d 2021-01-17 op len = sizeof(addr);
505 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
506 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
508 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
511 d3a08f4d 2021-01-17 op mark_nonblock(fd);
513 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
514 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
515 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
516 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
517 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
519 d3a08f4d 2021-01-17 op fds[i].fd = fd;
520 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
522 d3a08f4d 2021-01-17 op clients[i].state = S_HANDSHAKE;
523 d3a08f4d 2021-01-17 op clients[i].fd = -1;
524 d3a08f4d 2021-01-17 op clients[i].child = 0;
525 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
526 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
527 d3a08f4d 2021-01-17 op clients[i].af = AF_INET;
528 d3a08f4d 2021-01-17 op clients[i].addr = addr;
530 d3a08f4d 2021-01-17 op connected_clients++;
539 d3a08f4d 2021-01-17 op handle(struct pollfd *fds, struct client *client)
541 d3a08f4d 2021-01-17 op switch (client->state) {
542 d3a08f4d 2021-01-17 op case S_HANDSHAKE:
543 d3a08f4d 2021-01-17 op handle_handshake(fds, client);
547 d3a08f4d 2021-01-17 op handle_open_conn(fds, client);
550 d3a08f4d 2021-01-17 op case S_INITIALIZING:
551 d3a08f4d 2021-01-17 op if (!start_reply(fds, client, client->code, client->meta))
554 d3a08f4d 2021-01-17 op if (client->code != SUCCESS) {
555 d3a08f4d 2021-01-17 op /* we don't need a body */
556 d3a08f4d 2021-01-17 op goodbye(fds, client);
560 d3a08f4d 2021-01-17 op client->state = S_SENDING;
562 d3a08f4d 2021-01-17 op /* fallthrough */
564 d3a08f4d 2021-01-17 op case S_SENDING:
565 d3a08f4d 2021-01-17 op if (client->child)
566 d3a08f4d 2021-01-17 op handle_cgi(fds, client);
568 d3a08f4d 2021-01-17 op send_file(NULL, NULL, fds, client);
571 d3a08f4d 2021-01-17 op case S_CLOSING:
572 d3a08f4d 2021-01-17 op goodbye(fds, client);
576 d3a08f4d 2021-01-17 op /* unreachable */
582 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
585 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
586 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
588 d3a08f4d 2021-01-17 op connected_clients = 0;
590 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
591 d3a08f4d 2021-01-17 op fds[i].fd = -1;
592 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
593 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
596 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
597 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
600 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
601 d3a08f4d 2021-01-17 op if (errno == EINTR) {
602 132cae8c 2021-01-18 op fprintf(stderr, "connected clients: %d\n",
603 d3a08f4d 2021-01-17 op connected_clients);
606 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
609 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
610 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
613 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
614 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
615 d3a08f4d 2021-01-17 op strerror(errno));
617 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
618 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
619 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
620 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
621 d3a08f4d 2021-01-17 op goodbye(&fds[i], &clients[i]);
626 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
627 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
628 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
629 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
631 d3a08f4d 2021-01-17 op handle(&fds[i], &clients[i]);