2 c8dba1e6 2021-07-24 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 c8dba1e6 2021-07-24 op * Permission to use, copy, modify, and distribute this software for any
5 c8dba1e6 2021-07-24 op * purpose with or without fee is hereby granted, provided that the above
6 c8dba1e6 2021-07-24 op * copyright notice and this permission notice appear in all copies.
8 c8dba1e6 2021-07-24 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c8dba1e6 2021-07-24 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c8dba1e6 2021-07-24 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c8dba1e6 2021-07-24 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c8dba1e6 2021-07-24 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c8dba1e6 2021-07-24 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c8dba1e6 2021-07-24 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 c8dba1e6 2021-07-24 op #include "compat.h"
19 c8dba1e6 2021-07-24 op #include <sys/types.h>
20 c8dba1e6 2021-07-24 op #include <sys/socket.h>
22 c8dba1e6 2021-07-24 op #include <netinet/in.h>
24 c8dba1e6 2021-07-24 op #include <assert.h>
25 c8dba1e6 2021-07-24 op #include <ctype.h>
26 c8dba1e6 2021-07-24 op #include <errno.h>
27 c8dba1e6 2021-07-24 op #include <netdb.h>
28 c8dba1e6 2021-07-24 op #include <stdarg.h>
29 c8dba1e6 2021-07-24 op #include <stdio.h>
30 c8dba1e6 2021-07-24 op #include <stdlib.h>
31 c8dba1e6 2021-07-24 op #include <string.h>
32 c8dba1e6 2021-07-24 op #include <tls.h>
33 c8dba1e6 2021-07-24 op #include <unistd.h>
35 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
36 c8dba1e6 2021-07-24 op # include <asr.h>
39 35a31606 2021-09-22 op #define DNS_RDATACLASS_IN 1
40 35a31606 2021-09-22 op #define DNS_RDATATYPE_TXT 16
42 c8dba1e6 2021-07-24 op #include "telescope.h"
44 c8dba1e6 2021-07-24 op static struct imsgev *iev_ui;
45 c8dba1e6 2021-07-24 op static struct tls_config *tlsconf;
47 edfe87be 2021-07-24 op /* a pending request */
49 edfe87be 2021-07-24 op struct phos_uri url;
53 edfe87be 2021-07-24 op struct tls *ctx;
54 edfe87be 2021-07-24 op char req[1024];
56 edfe87be 2021-07-24 op int done_header;
57 edfe87be 2021-07-24 op struct bufferevent *bev;
59 edfe87be 2021-07-24 op struct addrinfo *servinfo, *p;
60 edfe87be 2021-07-24 op #if HAVE_ASR_RUN
61 edfe87be 2021-07-24 op struct addrinfo hints;
62 edfe87be 2021-07-24 op struct event_asr *asrev;
65 edfe87be 2021-07-24 op TAILQ_ENTRY(req) reqs;
68 e2a349f7 2021-07-24 op static struct req *req_by_id(uint32_t);
70 77ae5e91 2021-07-24 op static void die(void) __attribute__((__noreturn__));
72 77ae5e91 2021-07-24 op static void try_to_connect(int, short, void*);
74 35a31606 2021-09-22 op static void offline_dns_done(struct rrsetinfo *, struct req *);
75 35a31606 2021-09-22 op static void offline_dns_query(struct req *);
77 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
78 77ae5e91 2021-07-24 op static void query_done(struct asr_result*, void*);
79 77ae5e91 2021-07-24 op static void async_conn_towards(struct req*);
80 35a31606 2021-09-22 op static void offline_query_done(struct asr_result *, void *);
82 77ae5e91 2021-07-24 op static void blocking_conn_towards(struct req*);
85 77ae5e91 2021-07-24 op static void close_with_err(struct req*, const char*);
86 77ae5e91 2021-07-24 op static void close_with_errf(struct req*, const char*, ...)
87 bfb9acb0 2021-07-24 op __attribute__((format(printf, 2, 3)));
89 77ae5e91 2021-07-24 op static void net_tls_handshake(int, short, void *);
90 77ae5e91 2021-07-24 op static void net_tls_readcb(int, short, void *);
91 77ae5e91 2021-07-24 op static void net_tls_writecb(int, short, void *);
93 77ae5e91 2021-07-24 op static int gemini_parse_reply(struct req *, const char *, size_t);
95 77ae5e91 2021-07-24 op static void net_ready(struct req *req);
96 77ae5e91 2021-07-24 op static void net_read(struct bufferevent *, void *);
97 77ae5e91 2021-07-24 op static void net_write(struct bufferevent *, void *);
98 77ae5e91 2021-07-24 op static void net_error(struct bufferevent *, short, void *);
100 77ae5e91 2021-07-24 op static void handle_get_raw(struct imsg *, size_t);
101 77ae5e91 2021-07-24 op static void handle_cert_status(struct imsg*, size_t);
102 77ae5e91 2021-07-24 op static void handle_proceed(struct imsg*, size_t);
103 77ae5e91 2021-07-24 op static void handle_stop(struct imsg*, size_t);
104 77ae5e91 2021-07-24 op static void handle_quit(struct imsg*, size_t);
105 77ae5e91 2021-07-24 op static void handle_dispatch_imsg(int, short, void*);
107 77ae5e91 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
109 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
110 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
112 c8dba1e6 2021-07-24 op static imsg_handlerfn *handlers[] = {
113 c8dba1e6 2021-07-24 op [IMSG_GET_RAW] = handle_get_raw,
114 c8dba1e6 2021-07-24 op [IMSG_CERT_STATUS] = handle_cert_status,
115 c8dba1e6 2021-07-24 op [IMSG_PROCEED] = handle_proceed,
116 c8dba1e6 2021-07-24 op [IMSG_STOP] = handle_stop,
117 c8dba1e6 2021-07-24 op [IMSG_QUIT] = handle_quit,
120 c8dba1e6 2021-07-24 op typedef void (*statefn)(int, short, void*);
122 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
124 c8dba1e6 2021-07-24 op static inline void
125 c8dba1e6 2021-07-24 op yield_r(struct req *req, statefn fn, struct timeval *tv)
127 c8dba1e6 2021-07-24 op event_once(req->fd, EV_READ, fn, req, tv);
130 c8dba1e6 2021-07-24 op static inline void
131 c8dba1e6 2021-07-24 op yield_w(struct req *req, statefn fn, struct timeval *tv)
133 c8dba1e6 2021-07-24 op event_once(req->fd, EV_WRITE, fn, req, tv);
136 e2a349f7 2021-07-24 op static struct req *
137 e2a349f7 2021-07-24 op req_by_id(uint32_t id)
139 e2a349f7 2021-07-24 op struct req *r;
141 e2a349f7 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
142 e2a349f7 2021-07-24 op if (r->id == id)
149 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
152 c8dba1e6 2021-07-24 op abort(); /* TODO */
156 c8dba1e6 2021-07-24 op try_to_connect(int fd, short ev, void *d)
158 c8dba1e6 2021-07-24 op struct req *req = d;
159 c8dba1e6 2021-07-24 op int error = 0;
160 c8dba1e6 2021-07-24 op socklen_t len = sizeof(error);
163 c8dba1e6 2021-07-24 op if (req->p == NULL)
166 c8dba1e6 2021-07-24 op if (req->fd != -1) {
167 bfb9acb0 2021-07-24 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
170 c8dba1e6 2021-07-24 op if (error != 0) {
171 95a8c791 2021-08-26 op errno = error;
177 bfb9acb0 2021-07-24 op req->fd = socket(req->p->ai_family, req->p->ai_socktype,
178 bfb9acb0 2021-07-24 op req->p->ai_protocol);
179 c8dba1e6 2021-07-24 op if (req->fd == -1) {
180 c8dba1e6 2021-07-24 op req->p = req->p->ai_next;
183 c8dba1e6 2021-07-24 op mark_nonblock(req->fd);
184 c8dba1e6 2021-07-24 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
186 c8dba1e6 2021-07-24 op yield_w(req, try_to_connect, NULL);
191 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
192 35a31606 2021-09-22 op offline_dns_query(req);
196 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
198 5fb52fc0 2021-07-25 op switch (req->proto) {
199 5fb52fc0 2021-07-25 op case PROTO_FINGER:
200 1663bf1e 2021-07-25 op case PROTO_GOPHER:
201 1663bf1e 2021-07-25 op /* finger and gopher don't have a header nor TLS */
202 5fb52fc0 2021-07-25 op req->done_header = 1;
203 5fb52fc0 2021-07-25 op net_ready(req);
206 5fb52fc0 2021-07-25 op case PROTO_GEMINI:
207 5fb52fc0 2021-07-25 op /* prepare tls */
208 5fb52fc0 2021-07-25 op if ((req->ctx = tls_client()) == NULL) {
209 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_client: %s",
210 5fb52fc0 2021-07-25 op strerror(errno));
213 5fb52fc0 2021-07-25 op if (tls_configure(req->ctx, tlsconf) == -1) {
214 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_configure: %s",
215 5fb52fc0 2021-07-25 op tls_error(req->ctx));
218 5fb52fc0 2021-07-25 op if (tls_connect_socket(req->ctx, req->fd, req->url.host)
220 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_connect_socket: %s",
221 5fb52fc0 2021-07-25 op tls_error(req->ctx));
224 5fb52fc0 2021-07-25 op yield_w(req, net_tls_handshake, &timeout_for_handshake);
233 35a31606 2021-09-22 op offline_dns_done(struct rrsetinfo *res, struct req *req)
235 35a31606 2021-09-22 op size_t i, len;
236 35a31606 2021-09-22 op const char *reason = NULL;
238 35a31606 2021-09-22 op for (i = 0; i < res->rri_nrdatas; ++i) {
239 35a31606 2021-09-22 op if (res->rri_rdatas[i].rdi_length <= 1)
242 35a31606 2021-09-22 op len = *(uint8_t*)res->rri_rdatas[i].rdi_data;
243 35a31606 2021-09-22 op reason = res->rri_rdatas[i].rdi_data+1;
247 35a31606 2021-09-22 op if (reason == NULL)
248 35a31606 2021-09-22 op close_with_errf(req, "failed to connect to %s", req->url.host);
250 35a31606 2021-09-22 op close_with_errf(req,
251 35a31606 2021-09-22 op "failed to connect to %s\n\nThe site says: %*s\n",
252 35a31606 2021-09-22 op req->url.host, (int)len, reason);
254 35a31606 2021-09-22 op freerrset(res);
258 35a31606 2021-09-22 op offline_dns_query(struct req *req)
260 35a31606 2021-09-22 op char hostname[254];
262 35a31606 2021-09-22 op strlcpy(hostname, "_offline.", sizeof(hostname));
263 35a31606 2021-09-22 op strlcat(hostname, req->url.host, sizeof(hostname));
265 35a31606 2021-09-22 op #if HAVE_ASR_RUN
267 35a31606 2021-09-22 op struct asr_query *q;
269 35a31606 2021-09-22 op q = getrrsetbyname_async(hostname, DNS_RDATACLASS_IN,
270 35a31606 2021-09-22 op DNS_RDATATYPE_TXT, 0, NULL);
271 35a31606 2021-09-22 op req->asrev = event_asr_run(q, offline_query_done, req);
275 35a31606 2021-09-22 op struct rrsetinfo *res;
277 35a31606 2021-09-22 op if (getrrsetbyname(hostname, DNS_RDATACLASS_IN,
278 35a31606 2021-09-22 op DNS_RDATATYPE_TXT, 0, &res))
279 35a31606 2021-09-22 op close_with_errf(req, "failed to connect to %s",
280 35a31606 2021-09-22 op req->url.host);
282 35a31606 2021-09-22 op offline_dns_done(res, req);
287 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
289 c8dba1e6 2021-07-24 op query_done(struct asr_result *res, void *d)
291 c8dba1e6 2021-07-24 op struct req *req = d;
293 c8dba1e6 2021-07-24 op req->asrev = NULL;
294 c8dba1e6 2021-07-24 op if (res->ar_gai_errno != 0) {
295 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
296 c8dba1e6 2021-07-24 op req->url.host, gai_strerror(res->ar_gai_errno));
300 c8dba1e6 2021-07-24 op req->fd = -1;
301 c8dba1e6 2021-07-24 op req->servinfo = res->ar_addrinfo;
302 c8dba1e6 2021-07-24 op req->p = res->ar_addrinfo;
303 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
307 c8dba1e6 2021-07-24 op async_conn_towards(struct req *req)
309 c8dba1e6 2021-07-24 op struct asr_query *q;
310 c8dba1e6 2021-07-24 op const char *proto = "1965";
312 c8dba1e6 2021-07-24 op if (*req->url.port != '\0')
313 c8dba1e6 2021-07-24 op proto = req->url.port;
315 c8dba1e6 2021-07-24 op req->hints.ai_family = AF_UNSPEC;
316 c8dba1e6 2021-07-24 op req->hints.ai_socktype = SOCK_STREAM;
317 c8dba1e6 2021-07-24 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
318 c8dba1e6 2021-07-24 op req->asrev = event_asr_run(q, query_done, req);
322 35a31606 2021-09-22 op offline_query_done(struct asr_result *res, void *d)
324 35a31606 2021-09-22 op struct req *req = d;
326 35a31606 2021-09-22 op req->asrev = NULL;
327 35a31606 2021-09-22 op if (res->ar_rrset_errno != 0) {
328 35a31606 2021-09-22 op close_with_errf(req, "failed to resolve %s", req->url.host);
332 35a31606 2021-09-22 op offline_dns_done(res->ar_rrsetinfo, req);
337 c8dba1e6 2021-07-24 op blocking_conn_towards(struct req *req)
339 c8dba1e6 2021-07-24 op struct addrinfo hints;
340 c8dba1e6 2021-07-24 op struct phos_uri *url = &req->url;
342 c8dba1e6 2021-07-24 op const char *proto = "1965";
344 c8dba1e6 2021-07-24 op if (*url->port != '\0')
345 c8dba1e6 2021-07-24 op proto = url->port;
347 c8dba1e6 2021-07-24 op memset(&hints, 0, sizeof(hints));
348 c8dba1e6 2021-07-24 op hints.ai_family = AF_UNSPEC;
349 c8dba1e6 2021-07-24 op hints.ai_socktype = SOCK_STREAM;
351 c8dba1e6 2021-07-24 op if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
352 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
353 c8dba1e6 2021-07-24 op url->host, gai_strerror(status));
357 c8dba1e6 2021-07-24 op req->fd = -1;
358 c8dba1e6 2021-07-24 op req->p = req->servinfo;
359 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
364 c8dba1e6 2021-07-24 op close_conn(int fd, short ev, void *d)
366 c8dba1e6 2021-07-24 op struct req *req = d;
368 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
369 c8dba1e6 2021-07-24 op if (req->asrev != NULL)
370 c8dba1e6 2021-07-24 op event_asr_abort(req->asrev);
373 5b762f01 2021-07-24 op if (req->bev != NULL) {
374 5b762f01 2021-07-24 op bufferevent_free(req->bev);
375 5b762f01 2021-07-24 op req->bev = NULL;
378 c8dba1e6 2021-07-24 op if (req->ctx != NULL) {
379 c8dba1e6 2021-07-24 op switch (tls_close(req->ctx)) {
380 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
381 c8dba1e6 2021-07-24 op yield_r(req, close_conn, NULL);
383 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
384 c8dba1e6 2021-07-24 op yield_w(req, close_conn, NULL);
388 c8dba1e6 2021-07-24 op tls_free(req->ctx);
389 5b762f01 2021-07-24 op req->ctx = NULL;
392 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
393 c8dba1e6 2021-07-24 op if (req->fd != -1)
394 c8dba1e6 2021-07-24 op close(req->fd);
399 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
401 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
402 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
406 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
411 c8dba1e6 2021-07-24 op va_start(ap, fmt);
412 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
416 c8dba1e6 2021-07-24 op close_with_err(req, s);
421 5b762f01 2021-07-24 op net_tls_handshake(int fd, short event, void *d)
423 c8dba1e6 2021-07-24 op struct req *req = d;
424 c8dba1e6 2021-07-24 op const char *hash;
426 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
427 c8dba1e6 2021-07-24 op close_with_err(req, "Timeout loading page");
431 c8dba1e6 2021-07-24 op switch (tls_handshake(req->ctx)) {
432 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
433 5b762f01 2021-07-24 op yield_r(req, net_tls_handshake, NULL);
435 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
436 5b762f01 2021-07-24 op yield_w(req, net_tls_handshake, NULL);
440 c8dba1e6 2021-07-24 op hash = tls_peer_cert_hash(req->ctx);
441 c8dba1e6 2021-07-24 op if (hash == NULL) {
442 bfb9acb0 2021-07-24 op close_with_errf(req, "handshake failed: %s",
443 bfb9acb0 2021-07-24 op tls_error(req->ctx));
446 c8dba1e6 2021-07-24 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
450 5b762f01 2021-07-24 op net_tls_readcb(int fd, short event, void *d)
452 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
453 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
454 69f631d9 2021-07-25 op char buf[IBUF_READ_SIZE];
455 5b762f01 2021-07-24 op int what = EVBUFFER_READ;
456 5b762f01 2021-07-24 op int howmuch = IBUF_READ_SIZE;
461 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
462 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
466 5b762f01 2021-07-24 op if (bufev->wm_read.high != 0)
467 5b762f01 2021-07-24 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
469 5b762f01 2021-07-24 op switch (ret = tls_read(req->ctx, buf, howmuch)) {
470 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
471 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
474 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
479 5b762f01 2021-07-24 op if (len == 0) {
480 5b762f01 2021-07-24 op what |= EVBUFFER_EOF;
484 1470eab3 2021-07-27 op res = evbuffer_add(bufev->input, buf, len);
485 1470eab3 2021-07-27 op if (res == -1) {
486 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
490 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
492 5b762f01 2021-07-24 op len = EVBUFFER_LENGTH(bufev->input);
493 5b762f01 2021-07-24 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
496 5b762f01 2021-07-24 op if (bufev->readcb != NULL)
497 5b762f01 2021-07-24 op (*bufev->readcb)(bufev, bufev->cbarg);
501 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
505 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
509 5b762f01 2021-07-24 op net_tls_writecb(int fd, short event, void *d)
511 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
512 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
515 5b762f01 2021-07-24 op short what = EVBUFFER_WRITE;
517 5b762f01 2021-07-24 op if (event & EV_TIMEOUT) {
518 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
522 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
523 5b762f01 2021-07-24 op ret = tls_write(req->ctx, EVBUFFER_DATA(bufev->output),
524 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output));
525 5b762f01 2021-07-24 op switch (ret) {
526 5b762f01 2021-07-24 op case TLS_WANT_POLLIN:
527 5b762f01 2021-07-24 op case TLS_WANT_POLLOUT:
530 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
535 5b762f01 2021-07-24 op evbuffer_drain(bufev->output, len);
538 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0)
539 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
541 5b762f01 2021-07-24 op if (bufev->writecb != NULL &&
542 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
543 5b762f01 2021-07-24 op (*bufev->writecb)(bufev, bufev->cbarg);
547 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
551 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
555 5b762f01 2021-07-24 op gemini_parse_reply(struct req *req, const char *header, size_t len)
558 5b762f01 2021-07-24 op const char *t;
563 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
566 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
567 5b762f01 2021-07-24 op if (header[2] != ' ')
570 5b762f01 2021-07-24 op t = header + 3;
572 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
573 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_META, req->id, t, strlen(t)+1);
575 5b762f01 2021-07-24 op bufferevent_disable(req->bev, EV_READ|EV_WRITE);
577 5b762f01 2021-07-24 op if (code < 20 || code >= 30)
578 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
582 5b762f01 2021-07-24 op /* called when we're ready to read/write */
584 5b762f01 2021-07-24 op net_ready(struct req *req)
586 5b762f01 2021-07-24 op req->bev = bufferevent_new(req->fd, net_read, net_write, net_error,
588 5b762f01 2021-07-24 op if (req->bev == NULL)
591 b5c5e56d 2021-07-28 op #if HAVE_EVENT2
592 73bb7969 2021-07-27 op evbuffer_unfreeze(req->bev->input, 0);
593 73bb7969 2021-07-27 op evbuffer_unfreeze(req->bev->output, 1);
596 5b762f01 2021-07-24 op /* setup tls i/o layer */
597 5b762f01 2021-07-24 op if (req->ctx != NULL) {
598 5b762f01 2021-07-24 op event_set(&req->bev->ev_read, req->fd, EV_READ,
599 5b762f01 2021-07-24 op net_tls_readcb, req->bev);
600 5b762f01 2021-07-24 op event_set(&req->bev->ev_write, req->fd, EV_WRITE,
601 5b762f01 2021-07-24 op net_tls_writecb, req->bev);
604 5b762f01 2021-07-24 op /* TODO: adjust watermarks */
605 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_WRITE, 1, 0);
606 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_READ, 1, 0);
608 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ|EV_WRITE);
610 5b762f01 2021-07-24 op bufferevent_write(req->bev, req->req, req->len);
613 5b762f01 2021-07-24 op /* called after a read has been done */
615 5b762f01 2021-07-24 op net_read(struct bufferevent *bev, void *d)
617 5b762f01 2021-07-24 op struct req *req = d;
618 5b762f01 2021-07-24 op struct evbuffer *src = EVBUFFER_INPUT(bev);
620 0e8f22ed 2021-09-12 op size_t len, chunk;
622 5b762f01 2021-07-24 op char *header;
624 5b762f01 2021-07-24 op if (!req->done_header) {
625 5b762f01 2021-07-24 op header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
626 5b762f01 2021-07-24 op if (header == NULL && EVBUFFER_LENGTH(src) >= 1024)
628 5b762f01 2021-07-24 op if (header == NULL)
630 5b762f01 2021-07-24 op r = gemini_parse_reply(req, header, len);
631 5b762f01 2021-07-24 op free(header);
634 5b762f01 2021-07-24 op req->done_header = 1;
638 5b762f01 2021-07-24 op if ((len = EVBUFFER_LENGTH(src)) == 0)
640 5b762f01 2021-07-24 op data = EVBUFFER_DATA(src);
643 0e8f22ed 2021-09-12 op * Split data into chunks before sending. imsg can't handle
644 0e8f22ed 2021-09-12 op * message that are "too big".
646 0e8f22ed 2021-09-12 op while (len != 0) {
647 0e8f22ed 2021-09-12 op chunk = MIN(len, 4096);
648 0e8f22ed 2021-09-12 op net_send_ui(IMSG_BUF, req->id, data, chunk);
649 0e8f22ed 2021-09-12 op data += chunk;
650 0e8f22ed 2021-09-12 op len -= chunk;
653 0e8f22ed 2021-09-12 op evbuffer_drain(src, EVBUFFER_LENGTH(src));
657 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
660 5b762f01 2021-07-24 op /* called after a write has been done */
662 5b762f01 2021-07-24 op net_write(struct bufferevent *bev, void *d)
664 5b762f01 2021-07-24 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
666 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(dst) == 0)
667 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg);
671 5b762f01 2021-07-24 op net_error(struct bufferevent *bev, short error, void *d)
673 c8dba1e6 2021-07-24 op struct req *req = d;
674 590c00b7 2021-07-25 op struct evbuffer *src;
676 5b762f01 2021-07-24 op if (error & EVBUFFER_TIMEOUT) {
677 5b762f01 2021-07-24 op close_with_err(req, "Timeout loading page");
681 5b762f01 2021-07-24 op if (error & EVBUFFER_ERROR) {
682 5b762f01 2021-07-24 op close_with_err(req, "buffer event error");
686 5b762f01 2021-07-24 op if (error & EVBUFFER_EOF) {
687 95a8c791 2021-08-26 op src = EVBUFFER_INPUT(req->bev);
688 590c00b7 2021-07-25 op if (EVBUFFER_LENGTH(src) != 0)
689 590c00b7 2021-07-25 op net_send_ui(IMSG_BUF, req->id, EVBUFFER_DATA(src),
690 590c00b7 2021-07-25 op EVBUFFER_LENGTH(src));
691 5b762f01 2021-07-24 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
692 5b762f01 2021-07-24 op close_conn(0, 0, req);
696 5b762f01 2021-07-24 op if (error & EVBUFFER_WRITE) {
697 5b762f01 2021-07-24 op /* finished sending request */
698 5b762f01 2021-07-24 op bufferevent_disable(bev, EV_WRITE);
702 5b762f01 2021-07-24 op if (error & EVBUFFER_READ) {
703 5b762f01 2021-07-24 op close_with_err(req, "protocol error");
707 5b762f01 2021-07-24 op close_with_errf(req, "unknown event error %x", error);
711 c8dba1e6 2021-07-24 op handle_get_raw(struct imsg *imsg, size_t datalen)
713 c8dba1e6 2021-07-24 op struct req *req;
714 c8dba1e6 2021-07-24 op struct get_req *r;
716 c8dba1e6 2021-07-24 op r = imsg->data;
718 c8dba1e6 2021-07-24 op if (datalen != sizeof(*r))
721 c8dba1e6 2021-07-24 op if ((req = calloc(1, sizeof(*req))) == NULL)
724 c8dba1e6 2021-07-24 op req->id = imsg->hdr.peerid;
725 c8dba1e6 2021-07-24 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
727 c8dba1e6 2021-07-24 op strlcpy(req->url.host, r->host, sizeof(req->url.host));
728 c8dba1e6 2021-07-24 op strlcpy(req->url.port, r->port, sizeof(req->url.port));
730 5b762f01 2021-07-24 op strlcpy(req->req, r->req, sizeof(req->req));
731 5b762f01 2021-07-24 op req->len = strlen(r->req);
733 5fb52fc0 2021-07-25 op req->proto = r->proto;
735 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
736 95a8c791 2021-08-26 op async_conn_towards(req);
738 c8dba1e6 2021-07-24 op blocking_conn_towards(req);
743 c8dba1e6 2021-07-24 op handle_cert_status(struct imsg *imsg, size_t datalen)
745 c8dba1e6 2021-07-24 op struct req *req;
748 c8dba1e6 2021-07-24 op req = req_by_id(imsg->hdr.peerid);
750 c8dba1e6 2021-07-24 op if (datalen < sizeof(is_ok))
752 c8dba1e6 2021-07-24 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
755 5b762f01 2021-07-24 op net_ready(req);
757 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
761 c8dba1e6 2021-07-24 op handle_proceed(struct imsg *imsg, size_t datalen)
763 5b762f01 2021-07-24 op struct req *req;
765 318a6c1a 2021-07-24 op if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
768 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ);
772 c8dba1e6 2021-07-24 op handle_stop(struct imsg *imsg, size_t datalen)
774 c8dba1e6 2021-07-24 op struct req *req;
776 318a6c1a 2021-07-24 op if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
778 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
782 c8dba1e6 2021-07-24 op handle_quit(struct imsg *imsg, size_t datalen)
784 c8dba1e6 2021-07-24 op event_loopbreak();
788 c8dba1e6 2021-07-24 op handle_dispatch_imsg(int fd, short ev, void *d)
790 c8dba1e6 2021-07-24 op struct imsgev *iev = d;
792 c8dba1e6 2021-07-24 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
793 c8dba1e6 2021-07-24 op err(1, "connection closed");
797 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
798 c8dba1e6 2021-07-24 op uint16_t datalen)
800 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
801 c8dba1e6 2021-07-24 op data, datalen);
805 f45bd2e3 2021-07-24 op net_main(void)
807 c8dba1e6 2021-07-24 op setproctitle("net");
809 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
811 c8dba1e6 2021-07-24 op if ((tlsconf = tls_config_new()) == NULL)
813 c8dba1e6 2021-07-24 op tls_config_insecure_noverifycert(tlsconf);
814 c8dba1e6 2021-07-24 op tls_config_insecure_noverifyname(tlsconf);
816 c8dba1e6 2021-07-24 op event_init();
818 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
819 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
821 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
822 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
823 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
824 c8dba1e6 2021-07-24 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
825 c8dba1e6 2021-07-24 op iev_ui->handler, iev_ui);
826 c8dba1e6 2021-07-24 op event_add(&iev_ui->ev, NULL);
828 c8dba1e6 2021-07-24 op sandbox_net_process();
830 c8dba1e6 2021-07-24 op event_dispatch();
832 74c0f6ba 2021-07-24 op tls_config_free(tlsconf);
833 74c0f6ba 2021-07-24 op msgbuf_clear(&iev_ui->ibuf.w);
834 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
835 74c0f6ba 2021-07-24 op free(iev_ui);