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 c8dba1e6 2021-07-24 op #include "telescope.h"
40 9d65b1d9 2022-01-11 op #include "utils.h"
42 c8dba1e6 2021-07-24 op static struct imsgev *iev_ui;
44 edfe87be 2021-07-24 op /* a pending request */
46 edfe87be 2021-07-24 op struct phos_uri url;
50 edfe87be 2021-07-24 op struct tls *ctx;
51 612d2546 2022-05-27 op char req[1027];
53 edfe87be 2021-07-24 op int done_header;
54 edfe87be 2021-07-24 op struct bufferevent *bev;
56 edfe87be 2021-07-24 op struct addrinfo *servinfo, *p;
57 edfe87be 2021-07-24 op #if HAVE_ASR_RUN
58 edfe87be 2021-07-24 op struct addrinfo hints;
59 edfe87be 2021-07-24 op struct event_asr *asrev;
62 edfe87be 2021-07-24 op TAILQ_ENTRY(req) reqs;
65 e2a349f7 2021-07-24 op static struct req *req_by_id(uint32_t);
67 77ae5e91 2021-07-24 op static void die(void) __attribute__((__noreturn__));
69 77ae5e91 2021-07-24 op static void try_to_connect(int, short, void*);
71 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
72 77ae5e91 2021-07-24 op static void query_done(struct asr_result*, void*);
74 4e28261d 2022-05-27 op static void conn_towards(struct req*);
76 77ae5e91 2021-07-24 op static void close_with_err(struct req*, const char*);
77 77ae5e91 2021-07-24 op static void close_with_errf(struct req*, const char*, ...)
78 bfb9acb0 2021-07-24 op __attribute__((format(printf, 2, 3)));
80 77ae5e91 2021-07-24 op static void net_tls_handshake(int, short, void *);
81 77ae5e91 2021-07-24 op static void net_tls_readcb(int, short, void *);
82 77ae5e91 2021-07-24 op static void net_tls_writecb(int, short, void *);
84 77ae5e91 2021-07-24 op static int gemini_parse_reply(struct req *, const char *, size_t);
86 77ae5e91 2021-07-24 op static void net_ready(struct req *req);
87 77ae5e91 2021-07-24 op static void net_read(struct bufferevent *, void *);
88 77ae5e91 2021-07-24 op static void net_write(struct bufferevent *, void *);
89 77ae5e91 2021-07-24 op static void net_error(struct bufferevent *, short, void *);
91 77ae5e91 2021-07-24 op static void handle_dispatch_imsg(int, short, void*);
93 77ae5e91 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
95 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
96 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
98 c8dba1e6 2021-07-24 op typedef void (*statefn)(int, short, void*);
100 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
102 c8dba1e6 2021-07-24 op static inline void
103 c8dba1e6 2021-07-24 op yield_r(struct req *req, statefn fn, struct timeval *tv)
105 c8dba1e6 2021-07-24 op event_once(req->fd, EV_READ, fn, req, tv);
108 c8dba1e6 2021-07-24 op static inline void
109 c8dba1e6 2021-07-24 op yield_w(struct req *req, statefn fn, struct timeval *tv)
111 c8dba1e6 2021-07-24 op event_once(req->fd, EV_WRITE, fn, req, tv);
114 e2a349f7 2021-07-24 op static struct req *
115 e2a349f7 2021-07-24 op req_by_id(uint32_t id)
117 e2a349f7 2021-07-24 op struct req *r;
119 e2a349f7 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
120 e2a349f7 2021-07-24 op if (r->id == id)
127 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
130 c8dba1e6 2021-07-24 op abort(); /* TODO */
134 c8dba1e6 2021-07-24 op try_to_connect(int fd, short ev, void *d)
136 c8dba1e6 2021-07-24 op struct req *req = d;
137 c8dba1e6 2021-07-24 op int error = 0;
138 c8dba1e6 2021-07-24 op socklen_t len = sizeof(error);
141 c8dba1e6 2021-07-24 op if (req->p == NULL)
144 c8dba1e6 2021-07-24 op if (req->fd != -1) {
145 bfb9acb0 2021-07-24 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
148 c8dba1e6 2021-07-24 op if (error != 0) {
149 95a8c791 2021-08-26 op errno = error;
155 bfb9acb0 2021-07-24 op req->fd = socket(req->p->ai_family, req->p->ai_socktype,
156 bfb9acb0 2021-07-24 op req->p->ai_protocol);
157 c8dba1e6 2021-07-24 op if (req->fd == -1) {
158 c8dba1e6 2021-07-24 op req->p = req->p->ai_next;
162 12b80284 2022-05-27 op if (!mark_nonblock_cloexec(req->fd))
164 12b80284 2022-05-27 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
166 12b80284 2022-05-27 op yield_w(req, try_to_connect, NULL);
170 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
171 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to connect to %s",
172 c8dba1e6 2021-07-24 op req->url.host);
176 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
178 5fb52fc0 2021-07-25 op switch (req->proto) {
179 5fb52fc0 2021-07-25 op case PROTO_FINGER:
180 1663bf1e 2021-07-25 op case PROTO_GOPHER:
181 1663bf1e 2021-07-25 op /* finger and gopher don't have a header nor TLS */
182 5fb52fc0 2021-07-25 op req->done_header = 1;
183 5fb52fc0 2021-07-25 op net_ready(req);
186 b38d86fc 2022-01-04 op case PROTO_GEMINI: {
187 b38d86fc 2022-01-04 op struct tls_config *conf;
189 b38d86fc 2022-01-04 op if ((conf = tls_config_new()) == NULL)
192 b38d86fc 2022-01-04 op tls_config_insecure_noverifycert(conf);
193 b38d86fc 2022-01-04 op tls_config_insecure_noverifyname(conf);
195 5fb52fc0 2021-07-25 op /* prepare tls */
196 5fb52fc0 2021-07-25 op if ((req->ctx = tls_client()) == NULL) {
197 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_client: %s",
198 5fb52fc0 2021-07-25 op strerror(errno));
202 b38d86fc 2022-01-04 op if (tls_configure(req->ctx, conf) == -1) {
203 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_configure: %s",
204 5fb52fc0 2021-07-25 op tls_error(req->ctx));
207 b38d86fc 2022-01-04 op tls_config_free(conf);
209 5fb52fc0 2021-07-25 op if (tls_connect_socket(req->ctx, req->fd, req->url.host)
211 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_connect_socket: %s",
212 5fb52fc0 2021-07-25 op tls_error(req->ctx));
215 5fb52fc0 2021-07-25 op yield_w(req, net_tls_handshake, &timeout_for_handshake);
224 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
226 c8dba1e6 2021-07-24 op query_done(struct asr_result *res, void *d)
228 c8dba1e6 2021-07-24 op struct req *req = d;
230 c8dba1e6 2021-07-24 op req->asrev = NULL;
231 c8dba1e6 2021-07-24 op if (res->ar_gai_errno != 0) {
232 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
233 c8dba1e6 2021-07-24 op req->url.host, gai_strerror(res->ar_gai_errno));
237 c8dba1e6 2021-07-24 op req->fd = -1;
238 c8dba1e6 2021-07-24 op req->servinfo = res->ar_addrinfo;
239 c8dba1e6 2021-07-24 op req->p = res->ar_addrinfo;
240 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
244 4e28261d 2022-05-27 op conn_towards(struct req *req)
246 c8dba1e6 2021-07-24 op struct asr_query *q;
247 c8dba1e6 2021-07-24 op const char *proto = "1965";
249 c8dba1e6 2021-07-24 op if (*req->url.port != '\0')
250 c8dba1e6 2021-07-24 op proto = req->url.port;
252 c8dba1e6 2021-07-24 op req->hints.ai_family = AF_UNSPEC;
253 c8dba1e6 2021-07-24 op req->hints.ai_socktype = SOCK_STREAM;
254 c8dba1e6 2021-07-24 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
255 c8dba1e6 2021-07-24 op req->asrev = event_asr_run(q, query_done, req);
259 4e28261d 2022-05-27 op conn_towards(struct req *req)
261 c8dba1e6 2021-07-24 op struct addrinfo hints;
262 c8dba1e6 2021-07-24 op struct phos_uri *url = &req->url;
264 c8dba1e6 2021-07-24 op const char *proto = "1965";
266 c8dba1e6 2021-07-24 op if (*url->port != '\0')
267 c8dba1e6 2021-07-24 op proto = url->port;
269 c8dba1e6 2021-07-24 op memset(&hints, 0, sizeof(hints));
270 c8dba1e6 2021-07-24 op hints.ai_family = AF_UNSPEC;
271 c8dba1e6 2021-07-24 op hints.ai_socktype = SOCK_STREAM;
273 c8dba1e6 2021-07-24 op if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
274 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
275 c8dba1e6 2021-07-24 op url->host, gai_strerror(status));
279 c8dba1e6 2021-07-24 op req->fd = -1;
280 c8dba1e6 2021-07-24 op req->p = req->servinfo;
281 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
286 c8dba1e6 2021-07-24 op close_conn(int fd, short ev, void *d)
288 c8dba1e6 2021-07-24 op struct req *req = d;
290 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
291 c8dba1e6 2021-07-24 op if (req->asrev != NULL)
292 c8dba1e6 2021-07-24 op event_asr_abort(req->asrev);
295 5b762f01 2021-07-24 op if (req->bev != NULL) {
296 5b762f01 2021-07-24 op bufferevent_free(req->bev);
297 5b762f01 2021-07-24 op req->bev = NULL;
300 c8dba1e6 2021-07-24 op if (req->ctx != NULL) {
301 c8dba1e6 2021-07-24 op switch (tls_close(req->ctx)) {
302 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
303 c8dba1e6 2021-07-24 op yield_r(req, close_conn, NULL);
305 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
306 c8dba1e6 2021-07-24 op yield_w(req, close_conn, NULL);
310 c8dba1e6 2021-07-24 op tls_free(req->ctx);
311 5b762f01 2021-07-24 op req->ctx = NULL;
314 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
315 c8dba1e6 2021-07-24 op if (req->fd != -1)
316 c8dba1e6 2021-07-24 op close(req->fd);
321 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
323 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
324 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
328 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
333 c8dba1e6 2021-07-24 op va_start(ap, fmt);
334 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
338 c8dba1e6 2021-07-24 op close_with_err(req, s);
343 5b762f01 2021-07-24 op net_tls_handshake(int fd, short event, void *d)
345 c8dba1e6 2021-07-24 op struct req *req = d;
346 c8dba1e6 2021-07-24 op const char *hash;
348 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
349 c8dba1e6 2021-07-24 op close_with_err(req, "Timeout loading page");
353 c8dba1e6 2021-07-24 op switch (tls_handshake(req->ctx)) {
354 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
355 5b762f01 2021-07-24 op yield_r(req, net_tls_handshake, NULL);
357 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
358 5b762f01 2021-07-24 op yield_w(req, net_tls_handshake, NULL);
362 c8dba1e6 2021-07-24 op hash = tls_peer_cert_hash(req->ctx);
363 c8dba1e6 2021-07-24 op if (hash == NULL) {
364 bfb9acb0 2021-07-24 op close_with_errf(req, "handshake failed: %s",
365 bfb9acb0 2021-07-24 op tls_error(req->ctx));
368 c8dba1e6 2021-07-24 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
372 5b762f01 2021-07-24 op net_tls_readcb(int fd, short event, void *d)
374 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
375 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
376 69f631d9 2021-07-25 op char buf[IBUF_READ_SIZE];
377 5b762f01 2021-07-24 op int what = EVBUFFER_READ;
378 5b762f01 2021-07-24 op int howmuch = IBUF_READ_SIZE;
383 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
384 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
388 5b762f01 2021-07-24 op if (bufev->wm_read.high != 0)
389 5b762f01 2021-07-24 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
391 5b762f01 2021-07-24 op switch (ret = tls_read(req->ctx, buf, howmuch)) {
392 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
393 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
396 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
401 5b762f01 2021-07-24 op if (len == 0) {
402 5b762f01 2021-07-24 op what |= EVBUFFER_EOF;
406 1470eab3 2021-07-27 op res = evbuffer_add(bufev->input, buf, len);
407 1470eab3 2021-07-27 op if (res == -1) {
408 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
412 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
414 5b762f01 2021-07-24 op len = EVBUFFER_LENGTH(bufev->input);
415 5b762f01 2021-07-24 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
418 5b762f01 2021-07-24 op if (bufev->readcb != NULL)
419 5b762f01 2021-07-24 op (*bufev->readcb)(bufev, bufev->cbarg);
423 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
427 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
431 5b762f01 2021-07-24 op net_tls_writecb(int fd, short event, void *d)
433 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
434 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
437 5b762f01 2021-07-24 op short what = EVBUFFER_WRITE;
439 5b762f01 2021-07-24 op if (event & EV_TIMEOUT) {
440 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
444 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
445 5b762f01 2021-07-24 op ret = tls_write(req->ctx, EVBUFFER_DATA(bufev->output),
446 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output));
447 5b762f01 2021-07-24 op switch (ret) {
448 5b762f01 2021-07-24 op case TLS_WANT_POLLIN:
449 5b762f01 2021-07-24 op case TLS_WANT_POLLOUT:
452 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
457 5b762f01 2021-07-24 op evbuffer_drain(bufev->output, len);
460 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0)
461 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
463 5b762f01 2021-07-24 op if (bufev->writecb != NULL &&
464 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
465 5b762f01 2021-07-24 op (*bufev->writecb)(bufev, bufev->cbarg);
469 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
473 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
477 5b762f01 2021-07-24 op gemini_parse_reply(struct req *req, const char *header, size_t len)
480 5b762f01 2021-07-24 op const char *t;
485 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
488 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
489 5b762f01 2021-07-24 op if (header[2] != ' ')
492 5b762f01 2021-07-24 op t = header + 3;
494 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
495 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_META, req->id, t, strlen(t)+1);
497 5b762f01 2021-07-24 op bufferevent_disable(req->bev, EV_READ|EV_WRITE);
502 5b762f01 2021-07-24 op /* called when we're ready to read/write */
504 5b762f01 2021-07-24 op net_ready(struct req *req)
506 5b762f01 2021-07-24 op req->bev = bufferevent_new(req->fd, net_read, net_write, net_error,
508 5b762f01 2021-07-24 op if (req->bev == NULL)
511 b5c5e56d 2021-07-28 op #if HAVE_EVENT2
512 73bb7969 2021-07-27 op evbuffer_unfreeze(req->bev->input, 0);
513 73bb7969 2021-07-27 op evbuffer_unfreeze(req->bev->output, 1);
516 5b762f01 2021-07-24 op /* setup tls i/o layer */
517 5b762f01 2021-07-24 op if (req->ctx != NULL) {
518 5b762f01 2021-07-24 op event_set(&req->bev->ev_read, req->fd, EV_READ,
519 5b762f01 2021-07-24 op net_tls_readcb, req->bev);
520 5b762f01 2021-07-24 op event_set(&req->bev->ev_write, req->fd, EV_WRITE,
521 5b762f01 2021-07-24 op net_tls_writecb, req->bev);
524 5b762f01 2021-07-24 op /* TODO: adjust watermarks */
525 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_WRITE, 1, 0);
526 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_READ, 1, 0);
528 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ|EV_WRITE);
530 5b762f01 2021-07-24 op bufferevent_write(req->bev, req->req, req->len);
533 5b762f01 2021-07-24 op /* called after a read has been done */
535 5b762f01 2021-07-24 op net_read(struct bufferevent *bev, void *d)
537 5b762f01 2021-07-24 op struct req *req = d;
538 5b762f01 2021-07-24 op struct evbuffer *src = EVBUFFER_INPUT(bev);
539 3591e6db 2022-01-20 op uint8_t *data;
540 0e8f22ed 2021-09-12 op size_t len, chunk;
542 5b762f01 2021-07-24 op char *header;
544 5b762f01 2021-07-24 op if (!req->done_header) {
545 5b762f01 2021-07-24 op header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
546 5b762f01 2021-07-24 op if (header == NULL && EVBUFFER_LENGTH(src) >= 1024)
548 5b762f01 2021-07-24 op if (header == NULL)
550 5b762f01 2021-07-24 op r = gemini_parse_reply(req, header, len);
551 5b762f01 2021-07-24 op free(header);
552 0e49c9bb 2022-01-21 op req->done_header = 1;
555 0e49c9bb 2022-01-21 op else if (r < 20 || r >= 30)
556 0e49c9bb 2022-01-21 op close_conn(0, 0, req);
560 5b762f01 2021-07-24 op if ((len = EVBUFFER_LENGTH(src)) == 0)
562 5b762f01 2021-07-24 op data = EVBUFFER_DATA(src);
565 0e8f22ed 2021-09-12 op * Split data into chunks before sending. imsg can't handle
566 0e8f22ed 2021-09-12 op * message that are "too big".
568 0e8f22ed 2021-09-12 op while (len != 0) {
569 0e8f22ed 2021-09-12 op chunk = MIN(len, 4096);
570 0e8f22ed 2021-09-12 op net_send_ui(IMSG_BUF, req->id, data, chunk);
571 0e8f22ed 2021-09-12 op data += chunk;
572 0e8f22ed 2021-09-12 op len -= chunk;
575 0e8f22ed 2021-09-12 op evbuffer_drain(src, EVBUFFER_LENGTH(src));
579 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
582 5b762f01 2021-07-24 op /* called after a write has been done */
584 5b762f01 2021-07-24 op net_write(struct bufferevent *bev, void *d)
586 5b762f01 2021-07-24 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
588 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(dst) == 0)
589 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg);
593 5b762f01 2021-07-24 op net_error(struct bufferevent *bev, short error, void *d)
595 c8dba1e6 2021-07-24 op struct req *req = d;
596 590c00b7 2021-07-25 op struct evbuffer *src;
598 5b762f01 2021-07-24 op if (error & EVBUFFER_TIMEOUT) {
599 5b762f01 2021-07-24 op close_with_err(req, "Timeout loading page");
603 5b762f01 2021-07-24 op if (error & EVBUFFER_ERROR) {
604 5b762f01 2021-07-24 op close_with_err(req, "buffer event error");
608 5b762f01 2021-07-24 op if (error & EVBUFFER_EOF) {
609 98cba69e 2021-11-10 op /* EOF and no header */
610 98cba69e 2021-11-10 op if (!req->done_header) {
611 98cba69e 2021-11-10 op close_with_err(req, "protocol error");
615 95a8c791 2021-08-26 op src = EVBUFFER_INPUT(req->bev);
616 590c00b7 2021-07-25 op if (EVBUFFER_LENGTH(src) != 0)
617 590c00b7 2021-07-25 op net_send_ui(IMSG_BUF, req->id, EVBUFFER_DATA(src),
618 590c00b7 2021-07-25 op EVBUFFER_LENGTH(src));
619 5b762f01 2021-07-24 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
620 5b762f01 2021-07-24 op close_conn(0, 0, req);
624 5b762f01 2021-07-24 op if (error & EVBUFFER_WRITE) {
625 5b762f01 2021-07-24 op /* finished sending request */
626 5b762f01 2021-07-24 op bufferevent_disable(bev, EV_WRITE);
630 5b762f01 2021-07-24 op if (error & EVBUFFER_READ) {
631 5b762f01 2021-07-24 op close_with_err(req, "protocol error");
635 5b762f01 2021-07-24 op close_with_errf(req, "unknown event error %x", error);
639 0640b555 2022-05-27 op handle_dispatch_imsg(int fd, short event, void *d)
641 0640b555 2022-05-27 op struct imsgev *iev = d;
642 0640b555 2022-05-27 op struct imsgbuf *ibuf = &iev->ibuf;
643 0640b555 2022-05-27 op struct imsg imsg;
644 c8dba1e6 2021-07-24 op struct req *req;
645 c8dba1e6 2021-07-24 op struct get_req *r;
646 0640b555 2022-05-27 op size_t datalen;
650 0640b555 2022-05-27 op if (event & EV_READ) {
651 0640b555 2022-05-27 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
652 0640b555 2022-05-27 op err(1, "imsg_read");
654 0640b555 2022-05-27 op err(1, "connection closed");
656 0640b555 2022-05-27 op if (event & EV_WRITE) {
657 0640b555 2022-05-27 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
658 0640b555 2022-05-27 op err(1, "msgbuf_write");
660 0640b555 2022-05-27 op err(1, "connection closed");
664 0640b555 2022-05-27 op if ((n = imsg_get(ibuf, &imsg)) == -1)
665 0640b555 2022-05-27 op err(1, "imsg_get");
668 0640b555 2022-05-27 op datalen = IMSG_DATA_SIZE(imsg);
669 0640b555 2022-05-27 op switch (imsg.hdr.type) {
670 c6e37ffd 2022-05-27 op case IMSG_GET:
671 0640b555 2022-05-27 op r = imsg.data;
672 0640b555 2022-05-27 op if (datalen != sizeof(*r) ||
673 0640b555 2022-05-27 op r->host[sizeof(r->host) - 1] != '\0' ||
674 0640b555 2022-05-27 op r->port[sizeof(r->port) - 1] != '\0' ||
675 0640b555 2022-05-27 op r->req[sizeof(r->req) - 1] != '\0')
677 0640b555 2022-05-27 op if (r->proto != PROTO_FINGER &&
678 0640b555 2022-05-27 op r->proto != PROTO_GEMINI &&
679 0640b555 2022-05-27 op r->proto != PROTO_GOPHER)
682 0640b555 2022-05-27 op if ((req = calloc(1, sizeof(*req))) == NULL)
685 0640b555 2022-05-27 op req->id = imsg.hdr.peerid;
686 0640b555 2022-05-27 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
688 0640b555 2022-05-27 op strlcpy(req->url.host, r->host, sizeof(req->url.host));
689 0640b555 2022-05-27 op strlcpy(req->url.port, r->port, sizeof(req->url.port));
690 0640b555 2022-05-27 op req->len = strlcpy(req->req, r->req, sizeof(req->req));
691 0640b555 2022-05-27 op req->proto = r->proto;
692 0640b555 2022-05-27 op conn_towards(req);
695 0640b555 2022-05-27 op case IMSG_CERT_STATUS:
696 0640b555 2022-05-27 op if ((req = req_by_id(imsg.hdr.peerid)) == NULL)
699 0640b555 2022-05-27 op if (datalen != sizeof(certok))
701 0640b555 2022-05-27 op memcpy(&certok, imsg.data, sizeof(certok));
703 0640b555 2022-05-27 op net_ready(req);
705 0640b555 2022-05-27 op close_conn(0, 0, req);
708 0640b555 2022-05-27 op case IMSG_PROCEED:
709 0640b555 2022-05-27 op if ((req = req_by_id(imsg.hdr.peerid)) == NULL)
711 0640b555 2022-05-27 op bufferevent_enable(req->bev, EV_READ);
714 0640b555 2022-05-27 op case IMSG_STOP:
715 0640b555 2022-05-27 op if ((req = req_by_id(imsg.hdr.peerid)) == NULL)
717 0640b555 2022-05-27 op close_conn(0, 0, req);
720 0640b555 2022-05-27 op case IMSG_QUIT:
721 0640b555 2022-05-27 op event_loopbreak();
722 0640b555 2022-05-27 op imsg_free(&imsg);
726 0640b555 2022-05-27 op errx(1, "got unknown imsg %d", imsg.hdr.type);
729 0640b555 2022-05-27 op imsg_free(&imsg);
732 0640b555 2022-05-27 op imsg_event_add(iev);
736 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
737 c8dba1e6 2021-07-24 op uint16_t datalen)
739 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
740 c8dba1e6 2021-07-24 op data, datalen);
744 f45bd2e3 2021-07-24 op net_main(void)
746 c8dba1e6 2021-07-24 op setproctitle("net");
748 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
750 c8dba1e6 2021-07-24 op event_init();
752 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
753 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
755 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
756 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
757 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
758 c8dba1e6 2021-07-24 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
759 c8dba1e6 2021-07-24 op iev_ui->handler, iev_ui);
760 c8dba1e6 2021-07-24 op event_add(&iev_ui->ev, NULL);
762 c8dba1e6 2021-07-24 op sandbox_net_process();
764 c8dba1e6 2021-07-24 op event_dispatch();
766 74c0f6ba 2021-07-24 op msgbuf_clear(&iev_ui->ibuf.w);
767 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
768 74c0f6ba 2021-07-24 op free(iev_ui);