2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 5e11c00c 2021-03-02 op * - move the various
20 5e11c00c 2021-03-02 op * imsg_compose(...);
21 5e11c00c 2021-03-02 op * imsg_flush(...);
22 5e11c00c 2021-03-02 op * to something more asynchronous
25 5e11c00c 2021-03-02 op #include <telescope.h>
27 5e11c00c 2021-03-02 op #include <sys/types.h>
28 5e11c00c 2021-03-02 op #include <sys/socket.h>
30 5e11c00c 2021-03-02 op #include <netinet/in.h>
32 5e11c00c 2021-03-02 op #include <assert.h>
33 5e11c00c 2021-03-02 op #include <ctype.h>
34 5e11c00c 2021-03-02 op #include <errno.h>
35 5e11c00c 2021-03-02 op #include <event.h>
36 5e11c00c 2021-03-02 op #include <netdb.h>
37 5e11c00c 2021-03-02 op #include <stdarg.h>
38 5e11c00c 2021-03-02 op #include <stdio.h>
39 5e11c00c 2021-03-02 op #include <stdlib.h>
40 5e11c00c 2021-03-02 op #include <string.h>
41 5e11c00c 2021-03-02 op #include <tls.h>
42 5e11c00c 2021-03-02 op #include <unistd.h>
44 5e11c00c 2021-03-02 op static struct tls_config *tlsconf;
45 5e11c00c 2021-03-02 op static struct imsgbuf *ibuf;
49 5e11c00c 2021-03-02 op static void die(void) __attribute__((__noreturn__));
50 5e11c00c 2021-03-02 op static char *xasprintf(const char*, ...);
51 5e11c00c 2021-03-02 op static int conn_towards(struct url*, char**);
53 5e11c00c 2021-03-02 op static void close_with_err(struct req*, const char *err);
54 5e11c00c 2021-03-02 op static struct req *req_by_id(uint32_t);
56 5e11c00c 2021-03-02 op static void do_handshake(int, short, void*);
57 5e11c00c 2021-03-02 op static void write_request(int, short, void*);
58 5e11c00c 2021-03-02 op static void read_reply(int, short, void*);
59 5e11c00c 2021-03-02 op static void parse_reply(struct req*);
60 5e11c00c 2021-03-02 op static void copy_body(int, short, void*);
62 5e11c00c 2021-03-02 op static void check_special_page(struct req*, const char*);
64 5e11c00c 2021-03-02 op static void handle_get(struct imsg*, size_t);
65 5e11c00c 2021-03-02 op static void handle_cert_status(struct imsg*, size_t);
66 0972d8b2 2021-03-02 op static void handle_proceed(struct imsg*, size_t);
67 5e11c00c 2021-03-02 op static void handle_stop(struct imsg*, size_t);
68 5e11c00c 2021-03-02 op static void handle_quit(struct imsg*, size_t);
70 723bee54 2021-03-09 op /* TODO: making this customizable */
71 723bee54 2021-03-09 op struct timeval timeout_for_handshake = { 5, 0 };
73 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
74 5e11c00c 2021-03-02 op [IMSG_GET] = handle_get,
75 5e11c00c 2021-03-02 op [IMSG_CERT_STATUS] = handle_cert_status,
76 0972d8b2 2021-03-02 op [IMSG_PROCEED] = handle_proceed,
77 5e11c00c 2021-03-02 op [IMSG_STOP] = handle_stop,
78 5e11c00c 2021-03-02 op [IMSG_QUIT] = handle_quit,
81 5e11c00c 2021-03-02 op typedef void (*statefn)(int, short, void*);
83 5e11c00c 2021-03-02 op TAILQ_HEAD(, req) reqhead;
84 5e11c00c 2021-03-02 op /* a pending request */
86 5e11c00c 2021-03-02 op struct event ev;
87 5e11c00c 2021-03-02 op struct url url;
90 5e11c00c 2021-03-02 op struct tls *ctx;
91 5e11c00c 2021-03-02 op char buf[1024];
93 5e11c00c 2021-03-02 op TAILQ_ENTRY(req) reqs;
96 5e11c00c 2021-03-02 op static inline void
97 5e11c00c 2021-03-02 op yield_r(struct req *req, statefn fn, struct timeval *tv)
99 5e11c00c 2021-03-02 op event_once(req->fd, EV_READ, fn, req, tv);
102 5e11c00c 2021-03-02 op static inline void
103 5e11c00c 2021-03-02 op yield_w(struct req *req, statefn fn, struct timeval *tv)
105 5e11c00c 2021-03-02 op event_once(req->fd, EV_WRITE, fn, req, tv);
108 5e11c00c 2021-03-02 op static inline void
109 5e11c00c 2021-03-02 op advance_buf(struct req *req, size_t len)
111 5e11c00c 2021-03-02 op assert(len <= req->off);
113 5e11c00c 2021-03-02 op req->off -= len;
114 5e11c00c 2021-03-02 op memmove(req->buf, req->buf + len, req->off);
117 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
120 5e11c00c 2021-03-02 op abort(); /* TODO */
123 5e11c00c 2021-03-02 op static char *
124 5e11c00c 2021-03-02 op xasprintf(const char *fmt, ...)
129 5e11c00c 2021-03-02 op va_start(ap, fmt);
130 5e11c00c 2021-03-02 op if (vasprintf(&s, fmt, ap) == -1)
138 5e11c00c 2021-03-02 op conn_towards(struct url *url, char **err)
140 5e11c00c 2021-03-02 op struct addrinfo hints, *servinfo, *p;
141 5e11c00c 2021-03-02 op int status, sock;
142 5e11c00c 2021-03-02 op const char *proto = "1965";
146 5e11c00c 2021-03-02 op if (*url->port != '\0')
147 5e11c00c 2021-03-02 op proto = url->port;
149 5e11c00c 2021-03-02 op memset(&hints, 0, sizeof(hints));
150 5e11c00c 2021-03-02 op hints.ai_family = AF_UNSPEC;
151 5e11c00c 2021-03-02 op hints.ai_socktype = SOCK_STREAM;
153 5e11c00c 2021-03-02 op if ((status = getaddrinfo(url->host, proto, &hints, &servinfo))) {
154 5e11c00c 2021-03-02 op *err = xasprintf("failed to resolve %s: %s",
155 5e11c00c 2021-03-02 op url->host, gai_strerror(status));
160 5e11c00c 2021-03-02 op for (p = servinfo; p != NULL; p = p->ai_next) {
161 5e11c00c 2021-03-02 op if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
163 5e11c00c 2021-03-02 op if (connect(sock, p->ai_addr, p->ai_addrlen) != -1)
168 5e11c00c 2021-03-02 op if (sock == -1)
169 5e11c00c 2021-03-02 op *err = xasprintf("couldn't connect to %s", url->host);
171 5e11c00c 2021-03-02 op mark_nonblock(sock);
173 5e11c00c 2021-03-02 op freeaddrinfo(servinfo);
177 5e11c00c 2021-03-02 op static struct req *
178 5e11c00c 2021-03-02 op req_by_id(uint32_t id)
180 5e11c00c 2021-03-02 op struct req *r;
182 5e11c00c 2021-03-02 op TAILQ_FOREACH(r, &reqhead, reqs) {
183 5e11c00c 2021-03-02 op if (r->id == id)
191 5e11c00c 2021-03-02 op close_conn(int fd, short ev, void *d)
193 5e11c00c 2021-03-02 op struct req *req = d;
195 5e11c00c 2021-03-02 op if (req->ctx != NULL) {
196 5e11c00c 2021-03-02 op switch (tls_close(req->ctx)) {
197 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
198 5e11c00c 2021-03-02 op yield_r(req, close_conn, NULL);
200 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
201 5e11c00c 2021-03-02 op yield_w(req, close_conn, NULL);
205 5e11c00c 2021-03-02 op tls_free(req->ctx);
208 5e11c00c 2021-03-02 op TAILQ_REMOVE(&reqhead, req, reqs);
209 5e11c00c 2021-03-02 op if (req->fd != -1)
210 5e11c00c 2021-03-02 op close(req->fd);
215 5e11c00c 2021-03-02 op close_with_err(struct req *req, const char *err)
217 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_ERR, req->id, 0, -1, err, strlen(err)+1);
218 5e11c00c 2021-03-02 op imsg_flush(ibuf);
219 5e11c00c 2021-03-02 op close_conn(0, 0, req);
223 5e11c00c 2021-03-02 op do_handshake(int fd, short ev, void *d)
225 5e11c00c 2021-03-02 op struct req *req = d;
226 5e11c00c 2021-03-02 op const char *hash;
228 723bee54 2021-03-09 op if (ev == EV_TIMEOUT) {
229 723bee54 2021-03-09 op close_with_err(req, "Timeout loading page");
233 5e11c00c 2021-03-02 op switch (tls_handshake(req->ctx)) {
234 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
235 5e11c00c 2021-03-02 op yield_r(req, do_handshake, NULL);
237 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
238 5e11c00c 2021-03-02 op yield_w(req, do_handshake, NULL);
242 5e11c00c 2021-03-02 op hash = tls_peer_cert_hash(req->ctx);
243 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CHECK_CERT, req->id, 0, -1, hash, strlen(hash)+1);
244 5e11c00c 2021-03-02 op imsg_flush(ibuf);
248 5e11c00c 2021-03-02 op write_request(int fd, short ev, void *d)
250 5e11c00c 2021-03-02 op struct req *req = d;
253 5e11c00c 2021-03-02 op char buf[1024], *err;
255 5e11c00c 2021-03-02 op strlcpy(buf, "gemini://", sizeof(buf));
256 5e11c00c 2021-03-02 op strlcat(buf, req->url.host, sizeof(buf));
257 5e11c00c 2021-03-02 op strlcat(buf, "/", sizeof(buf));
258 5e11c00c 2021-03-02 op strlcat(buf, req->url.path, sizeof(buf));
260 5e11c00c 2021-03-02 op if (req->url.query[0] != '\0') {
261 5e11c00c 2021-03-02 op strlcat(buf, "?", sizeof(buf));
262 5e11c00c 2021-03-02 op strlcat(buf, req->url.query, sizeof(buf));
265 5e11c00c 2021-03-02 op len = strlcat(buf, "\r\n", sizeof(buf));
267 5e11c00c 2021-03-02 op assert(len <= sizeof(buf));
269 5e11c00c 2021-03-02 op switch (r = tls_write(req->ctx, buf, len)) {
271 5e11c00c 2021-03-02 op err = xasprintf("tls_write: %s", tls_error(req->ctx));
272 5e11c00c 2021-03-02 op close_with_err(req, err);
275 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
276 5e11c00c 2021-03-02 op yield_r(req, write_request, NULL);
278 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
279 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
282 5e11c00c 2021-03-02 op /* assume r == len */
284 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
290 5e11c00c 2021-03-02 op read_reply(int fd, short ev, void *d)
292 5e11c00c 2021-03-02 op struct req *req = d;
295 5e11c00c 2021-03-02 op char *buf, *e;
297 5e11c00c 2021-03-02 op buf = req->buf + req->off;
298 5e11c00c 2021-03-02 op len = sizeof(req->buf) - req->off;
300 5e11c00c 2021-03-02 op switch (r = tls_read(req->ctx, buf, len)) {
302 5e11c00c 2021-03-02 op e = xasprintf("tls_read: %s", tls_error(req->ctx));
303 5e11c00c 2021-03-02 op close_with_err(req, e);
306 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
307 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
309 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
310 5e11c00c 2021-03-02 op yield_w(req, read_reply, NULL);
313 5e11c00c 2021-03-02 op req->off += r;
315 5e11c00c 2021-03-02 op /* TODO: really watch for \r\n not \n alone */
316 5e11c00c 2021-03-02 op if ((e = telescope_strnchr(req->buf, '\n', req->off)) != NULL)
317 5e11c00c 2021-03-02 op parse_reply(req);
318 5e11c00c 2021-03-02 op else if (req->off == sizeof(req->buf))
319 5e11c00c 2021-03-02 op close_with_err(req, "invalid response");
321 5e11c00c 2021-03-02 op yield_r(req, read_reply, NULL);
327 5e11c00c 2021-03-02 op parse_reply(struct req *req)
333 5e11c00c 2021-03-02 op if (req->off < 4)
336 5e11c00c 2021-03-02 op if (!isdigit(req->buf[0]) || !isdigit(req->buf[1]))
339 5e11c00c 2021-03-02 op code = (req->buf[0] - '0')*10 + (req->buf[1] - '0');
341 5e11c00c 2021-03-02 op if (!isspace(req->buf[2]))
344 5e11c00c 2021-03-02 op advance_buf(req, 3);
345 5e11c00c 2021-03-02 op if ((e = telescope_strnchr(req->buf, '\r', req->off)) == NULL)
350 5e11c00c 2021-03-02 op len = e - req->buf;
351 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_CODE, req->id, 0, -1, &code, sizeof(code));
352 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GOT_META, req->id, 0, -1,
353 5e11c00c 2021-03-02 op req->buf, len);
354 5e11c00c 2021-03-02 op imsg_flush(ibuf);
356 723bee54 2021-03-09 op if (code != 20)
357 723bee54 2021-03-09 op close_conn(0, 0, req);
362 5e11c00c 2021-03-02 op close_with_err(req, "malformed request");
366 5e11c00c 2021-03-02 op copy_body(int fd, short ev, void *d)
368 5e11c00c 2021-03-02 op struct req *req = d;
369 5e11c00c 2021-03-02 op char buf[BUFSIZ];
373 5e11c00c 2021-03-02 op switch (r = tls_read(req->ctx, buf, sizeof(buf))) {
374 5e11c00c 2021-03-02 op case TLS_WANT_POLLIN:
375 5e11c00c 2021-03-02 op yield_r(req, copy_body, NULL);
377 5e11c00c 2021-03-02 op case TLS_WANT_POLLOUT:
378 5e11c00c 2021-03-02 op yield_w(req, copy_body, NULL);
382 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_EOF, req->id, 0, -1, NULL, 0);
383 5e11c00c 2021-03-02 op imsg_flush(ibuf);
384 5e11c00c 2021-03-02 op close_conn(0, 0, req);
387 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_BUF, req->id, 0, -1, buf, r);
388 5e11c00c 2021-03-02 op imsg_flush(ibuf);
395 5e11c00c 2021-03-02 op handle_get(struct imsg *imsg, size_t datalen)
397 5e11c00c 2021-03-02 op struct req *req;
398 5e11c00c 2021-03-02 op const char *e;
399 5e11c00c 2021-03-02 op char *data, *err = NULL;
401 5e11c00c 2021-03-02 op data = imsg->data;
403 5e11c00c 2021-03-02 op if (data[datalen-1] != '\0')
406 5e11c00c 2021-03-02 op if ((req = calloc(1, sizeof(*req))) == NULL)
409 5e11c00c 2021-03-02 op req->id = imsg->hdr.peerid;
411 5e11c00c 2021-03-02 op if (!url_parse(imsg->data, &req->url, &e)) {
412 5e11c00c 2021-03-02 op fprintf(stderr, "failed to parse url: %s\n", e);
413 5e11c00c 2021-03-02 op close_with_err(req, e);
417 5e11c00c 2021-03-02 op if ((req->fd = conn_towards(&req->url, &err)) == -1)
419 5e11c00c 2021-03-02 op if ((req->ctx = tls_client()) == NULL)
421 5e11c00c 2021-03-02 op if (tls_configure(req->ctx, tlsconf) == -1) {
422 5e11c00c 2021-03-02 op err = xasprintf("tls_configure: %s", tls_error(req->ctx));
425 5e11c00c 2021-03-02 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
426 5e11c00c 2021-03-02 op err = xasprintf("tls_connect_socket: %s", tls_error(req->ctx));
430 5e11c00c 2021-03-02 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
431 723bee54 2021-03-09 op yield_w(req, do_handshake, &timeout_for_handshake);
435 5e11c00c 2021-03-02 op close_with_err(req, err);
440 5e11c00c 2021-03-02 op handle_cert_status(struct imsg *imsg, size_t datalen)
442 5e11c00c 2021-03-02 op struct req *req;
445 5e11c00c 2021-03-02 op req = req_by_id(imsg->hdr.peerid);
447 5e11c00c 2021-03-02 op if (datalen < sizeof(is_ok))
449 5e11c00c 2021-03-02 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
452 5e11c00c 2021-03-02 op yield_w(req, write_request, NULL);
454 5e11c00c 2021-03-02 op close_conn(0, 0, req);
458 0972d8b2 2021-03-02 op handle_proceed(struct imsg *imsg, size_t datalen)
460 0972d8b2 2021-03-02 op struct req *req;
462 0972d8b2 2021-03-02 op req = req_by_id(imsg->hdr.peerid);
463 0972d8b2 2021-03-02 op yield_r(req, copy_body, NULL);
467 5e11c00c 2021-03-02 op handle_stop(struct imsg *imsg, size_t datalen)
469 5e11c00c 2021-03-02 op struct req *req;
471 5e11c00c 2021-03-02 op req = req_by_id(imsg->hdr.peerid);
472 5e11c00c 2021-03-02 op close_conn(0, 0, req);
476 5e11c00c 2021-03-02 op handle_quit(struct imsg *imsg, size_t datalen)
478 5e11c00c 2021-03-02 op event_loopbreak();
482 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
484 5e11c00c 2021-03-02 op struct imsgbuf *ibuf = d;
485 5e11c00c 2021-03-02 op struct imsg imsg;
487 5e11c00c 2021-03-02 op size_t datalen;
489 5e11c00c 2021-03-02 op if ((n = imsg_read(ibuf)) == -1) {
490 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
499 5e11c00c 2021-03-02 op if ((n = imsg_get(ibuf, &imsg)) == -1)
503 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
504 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
505 5e11c00c 2021-03-02 op imsg_free(&imsg);
510 5e11c00c 2021-03-02 op client_main(struct imsgbuf *b)
514 5e11c00c 2021-03-02 op TAILQ_INIT(&reqhead);
516 5e11c00c 2021-03-02 op if ((tlsconf = tls_config_new()) == NULL)
518 5e11c00c 2021-03-02 op tls_config_insecure_noverifycert(tlsconf);
520 5e11c00c 2021-03-02 op event_init();
522 5e11c00c 2021-03-02 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
523 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
525 5e11c00c 2021-03-02 op event_dispatch();