aboutsummaryrefslogtreecommitdiff
path: root/gmid.c
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2021-01-10 22:56:33 +0000
committerOmar Polo <op@omarpolo.com>2021-01-10 22:56:33 +0000
commit33756bd2353b645a2c046a0807103c309d6d7215 (patch)
tree5b778ab5640b9bd876ccbc7a9ccdf60321eda092 /gmid.c
parent709d6e5ead07ce64dd6625eef05deaedadd8f095 (diff)
accept both ipv4 and ipv6
Diffstat (limited to 'gmid.c')
-rw-r--r--gmid.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/gmid.c b/gmid.c
index df6c9a7..e858c7b 100644
--- a/gmid.c
+++ b/gmid.c
@@ -771,7 +771,7 @@ goodbye(struct pollfd *pfd, struct client *c)
}
void
-loop(struct tls *ctx, int sock)
+loop(struct tls *ctx, int sock4, int sock6)
{
int i;
struct client clients[MAX_USERS];
@@ -783,7 +783,8 @@ loop(struct tls *ctx, int sock)
bzero(&clients[i], sizeof(struct client));
}
- fds[0].fd = sock;
+ fds[0].fd = sock4;
+ fds[1].fd = sock6;
for (;;) {
if (poll(fds, MAX_USERS, INFTIM) == -1) {
@@ -812,12 +813,12 @@ loop(struct tls *ctx, int sock)
}
}
- if (i == 0) { /* new client */
- do_accept(sock, ctx, fds, clients);
- continue;
- }
-
- handle(&fds[i], &clients[i]);
+ if (fds[i].fd == sock4)
+ do_accept(sock4, ctx, fds, clients);
+ else if (fds[i].fd == sock6)
+ do_accept(sock6, ctx, fds, clients);
+ else
+ handle(&fds[i], &clients[i]);
}
}
}
@@ -852,7 +853,7 @@ main(int argc, char **argv)
const char *cert = "cert.pem", *key = "key.pem";
struct tls *ctx = NULL;
struct tls_config *conf;
- int sock, ch;
+ int sock4, sock6, ch;
connected_clients = 0;
if ((dir = absolutify_path("docs")) == NULL)
@@ -944,7 +945,8 @@ main(int argc, char **argv)
if (tls_configure(ctx, conf) == -1)
errx(1, "tls_configure: %s", tls_error(ctx));
- sock = make_socket(port, AF_INET);
+ sock4 = make_socket(port, AF_INET);
+ sock6 = make_socket(port, AF_INET6);
if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
err(1, "open: %s", dir);
@@ -962,9 +964,10 @@ main(int argc, char **argv)
if (cgi == NULL && pledge("stdio rpath inet", NULL) == -1)
err(1, "pledge");
- loop(ctx, sock);
+ loop(ctx, sock4, sock6);
- close(sock);
+ close(sock4);
+ close(sock6);
tls_free(ctx);
tls_config_free(conf);
}