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 edfe87be 2021-07-24 op char req[1024];
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*);
73 77ae5e91 2021-07-24 op static void async_conn_towards(struct req*);
75 77ae5e91 2021-07-24 op static void blocking_conn_towards(struct req*);
78 77ae5e91 2021-07-24 op static void close_with_err(struct req*, const char*);
79 77ae5e91 2021-07-24 op static void close_with_errf(struct req*, const char*, ...)
80 bfb9acb0 2021-07-24 op __attribute__((format(printf, 2, 3)));
82 77ae5e91 2021-07-24 op static void net_tls_handshake(int, short, void *);
83 77ae5e91 2021-07-24 op static void net_tls_readcb(int, short, void *);
84 77ae5e91 2021-07-24 op static void net_tls_writecb(int, short, void *);
86 77ae5e91 2021-07-24 op static int gemini_parse_reply(struct req *, const char *, size_t);
88 77ae5e91 2021-07-24 op static void net_ready(struct req *req);
89 77ae5e91 2021-07-24 op static void net_read(struct bufferevent *, void *);
90 77ae5e91 2021-07-24 op static void net_write(struct bufferevent *, void *);
91 77ae5e91 2021-07-24 op static void net_error(struct bufferevent *, short, void *);
93 77ae5e91 2021-07-24 op static void handle_get_raw(struct imsg *, size_t);
94 77ae5e91 2021-07-24 op static void handle_cert_status(struct imsg*, size_t);
95 77ae5e91 2021-07-24 op static void handle_proceed(struct imsg*, size_t);
96 77ae5e91 2021-07-24 op static void handle_stop(struct imsg*, size_t);
97 77ae5e91 2021-07-24 op static void handle_quit(struct imsg*, size_t);
98 77ae5e91 2021-07-24 op static void handle_dispatch_imsg(int, short, void*);
100 77ae5e91 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
102 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
103 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
105 c8dba1e6 2021-07-24 op static imsg_handlerfn *handlers[] = {
106 c8dba1e6 2021-07-24 op [IMSG_GET_RAW] = handle_get_raw,
107 c8dba1e6 2021-07-24 op [IMSG_CERT_STATUS] = handle_cert_status,
108 c8dba1e6 2021-07-24 op [IMSG_PROCEED] = handle_proceed,
109 c8dba1e6 2021-07-24 op [IMSG_STOP] = handle_stop,
110 c8dba1e6 2021-07-24 op [IMSG_QUIT] = handle_quit,
113 c8dba1e6 2021-07-24 op typedef void (*statefn)(int, short, void*);
115 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
117 c8dba1e6 2021-07-24 op static inline void
118 c8dba1e6 2021-07-24 op yield_r(struct req *req, statefn fn, struct timeval *tv)
120 c8dba1e6 2021-07-24 op event_once(req->fd, EV_READ, fn, req, tv);
123 c8dba1e6 2021-07-24 op static inline void
124 c8dba1e6 2021-07-24 op yield_w(struct req *req, statefn fn, struct timeval *tv)
126 c8dba1e6 2021-07-24 op event_once(req->fd, EV_WRITE, fn, req, tv);
129 e2a349f7 2021-07-24 op static struct req *
130 e2a349f7 2021-07-24 op req_by_id(uint32_t id)
132 e2a349f7 2021-07-24 op struct req *r;
134 e2a349f7 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
135 e2a349f7 2021-07-24 op if (r->id == id)
142 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
145 c8dba1e6 2021-07-24 op abort(); /* TODO */
149 c8dba1e6 2021-07-24 op try_to_connect(int fd, short ev, void *d)
151 c8dba1e6 2021-07-24 op struct req *req = d;
152 c8dba1e6 2021-07-24 op int error = 0;
153 c8dba1e6 2021-07-24 op socklen_t len = sizeof(error);
156 c8dba1e6 2021-07-24 op if (req->p == NULL)
159 c8dba1e6 2021-07-24 op if (req->fd != -1) {
160 bfb9acb0 2021-07-24 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
163 c8dba1e6 2021-07-24 op if (error != 0) {
164 95a8c791 2021-08-26 op errno = error;
170 bfb9acb0 2021-07-24 op req->fd = socket(req->p->ai_family, req->p->ai_socktype,
171 bfb9acb0 2021-07-24 op req->p->ai_protocol);
172 c8dba1e6 2021-07-24 op if (req->fd == -1) {
173 c8dba1e6 2021-07-24 op req->p = req->p->ai_next;
176 27551633 2022-05-25 op if (!mark_nonblock_cloexec(req->fd))
178 c8dba1e6 2021-07-24 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
180 c8dba1e6 2021-07-24 op yield_w(req, try_to_connect, NULL);
185 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
186 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to connect to %s",
187 c8dba1e6 2021-07-24 op req->url.host);
191 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
193 5fb52fc0 2021-07-25 op switch (req->proto) {
194 5fb52fc0 2021-07-25 op case PROTO_FINGER:
195 1663bf1e 2021-07-25 op case PROTO_GOPHER:
196 1663bf1e 2021-07-25 op /* finger and gopher don't have a header nor TLS */
197 5fb52fc0 2021-07-25 op req->done_header = 1;
198 5fb52fc0 2021-07-25 op net_ready(req);
201 b38d86fc 2022-01-04 op case PROTO_GEMINI: {
202 b38d86fc 2022-01-04 op struct tls_config *conf;
204 b38d86fc 2022-01-04 op if ((conf = tls_config_new()) == NULL)
207 b38d86fc 2022-01-04 op tls_config_insecure_noverifycert(conf);
208 b38d86fc 2022-01-04 op tls_config_insecure_noverifyname(conf);
210 5fb52fc0 2021-07-25 op /* prepare tls */
211 5fb52fc0 2021-07-25 op if ((req->ctx = tls_client()) == NULL) {
212 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_client: %s",
213 5fb52fc0 2021-07-25 op strerror(errno));
217 b38d86fc 2022-01-04 op if (tls_configure(req->ctx, conf) == -1) {
218 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_configure: %s",
219 5fb52fc0 2021-07-25 op tls_error(req->ctx));
222 b38d86fc 2022-01-04 op tls_config_free(conf);
224 5fb52fc0 2021-07-25 op if (tls_connect_socket(req->ctx, req->fd, req->url.host)
226 5fb52fc0 2021-07-25 op close_with_errf(req, "tls_connect_socket: %s",
227 5fb52fc0 2021-07-25 op tls_error(req->ctx));
230 5fb52fc0 2021-07-25 op yield_w(req, net_tls_handshake, &timeout_for_handshake);
239 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
241 c8dba1e6 2021-07-24 op query_done(struct asr_result *res, void *d)
243 c8dba1e6 2021-07-24 op struct req *req = d;
245 c8dba1e6 2021-07-24 op req->asrev = NULL;
246 c8dba1e6 2021-07-24 op if (res->ar_gai_errno != 0) {
247 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
248 c8dba1e6 2021-07-24 op req->url.host, gai_strerror(res->ar_gai_errno));
252 c8dba1e6 2021-07-24 op req->fd = -1;
253 c8dba1e6 2021-07-24 op req->servinfo = res->ar_addrinfo;
254 c8dba1e6 2021-07-24 op req->p = res->ar_addrinfo;
255 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
259 c8dba1e6 2021-07-24 op async_conn_towards(struct req *req)
261 c8dba1e6 2021-07-24 op struct asr_query *q;
262 c8dba1e6 2021-07-24 op const char *proto = "1965";
264 c8dba1e6 2021-07-24 op if (*req->url.port != '\0')
265 c8dba1e6 2021-07-24 op proto = req->url.port;
267 c8dba1e6 2021-07-24 op req->hints.ai_family = AF_UNSPEC;
268 c8dba1e6 2021-07-24 op req->hints.ai_socktype = SOCK_STREAM;
269 c8dba1e6 2021-07-24 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
270 c8dba1e6 2021-07-24 op req->asrev = event_asr_run(q, query_done, req);
274 c8dba1e6 2021-07-24 op blocking_conn_towards(struct req *req)
276 c8dba1e6 2021-07-24 op struct addrinfo hints;
277 c8dba1e6 2021-07-24 op struct phos_uri *url = &req->url;
279 c8dba1e6 2021-07-24 op const char *proto = "1965";
281 c8dba1e6 2021-07-24 op if (*url->port != '\0')
282 c8dba1e6 2021-07-24 op proto = url->port;
284 c8dba1e6 2021-07-24 op memset(&hints, 0, sizeof(hints));
285 c8dba1e6 2021-07-24 op hints.ai_family = AF_UNSPEC;
286 c8dba1e6 2021-07-24 op hints.ai_socktype = SOCK_STREAM;
288 c8dba1e6 2021-07-24 op if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
289 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
290 c8dba1e6 2021-07-24 op url->host, gai_strerror(status));
294 c8dba1e6 2021-07-24 op req->fd = -1;
295 c8dba1e6 2021-07-24 op req->p = req->servinfo;
296 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
301 c8dba1e6 2021-07-24 op close_conn(int fd, short ev, void *d)
303 c8dba1e6 2021-07-24 op struct req *req = d;
305 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
306 c8dba1e6 2021-07-24 op if (req->asrev != NULL)
307 c8dba1e6 2021-07-24 op event_asr_abort(req->asrev);
310 5b762f01 2021-07-24 op if (req->bev != NULL) {
311 5b762f01 2021-07-24 op bufferevent_free(req->bev);
312 5b762f01 2021-07-24 op req->bev = NULL;
315 c8dba1e6 2021-07-24 op if (req->ctx != NULL) {
316 c8dba1e6 2021-07-24 op switch (tls_close(req->ctx)) {
317 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
318 c8dba1e6 2021-07-24 op yield_r(req, close_conn, NULL);
320 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
321 c8dba1e6 2021-07-24 op yield_w(req, close_conn, NULL);
325 c8dba1e6 2021-07-24 op tls_free(req->ctx);
326 5b762f01 2021-07-24 op req->ctx = NULL;
329 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
330 c8dba1e6 2021-07-24 op if (req->fd != -1)
331 c8dba1e6 2021-07-24 op close(req->fd);
336 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
338 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
339 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
343 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
348 c8dba1e6 2021-07-24 op va_start(ap, fmt);
349 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
353 c8dba1e6 2021-07-24 op close_with_err(req, s);
358 5b762f01 2021-07-24 op net_tls_handshake(int fd, short event, void *d)
360 c8dba1e6 2021-07-24 op struct req *req = d;
361 c8dba1e6 2021-07-24 op const char *hash;
363 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
364 c8dba1e6 2021-07-24 op close_with_err(req, "Timeout loading page");
368 c8dba1e6 2021-07-24 op switch (tls_handshake(req->ctx)) {
369 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
370 5b762f01 2021-07-24 op yield_r(req, net_tls_handshake, NULL);
372 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
373 5b762f01 2021-07-24 op yield_w(req, net_tls_handshake, NULL);
377 c8dba1e6 2021-07-24 op hash = tls_peer_cert_hash(req->ctx);
378 c8dba1e6 2021-07-24 op if (hash == NULL) {
379 bfb9acb0 2021-07-24 op close_with_errf(req, "handshake failed: %s",
380 bfb9acb0 2021-07-24 op tls_error(req->ctx));
383 c8dba1e6 2021-07-24 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
387 5b762f01 2021-07-24 op net_tls_readcb(int fd, short event, void *d)
389 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
390 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
391 69f631d9 2021-07-25 op char buf[IBUF_READ_SIZE];
392 5b762f01 2021-07-24 op int what = EVBUFFER_READ;
393 5b762f01 2021-07-24 op int howmuch = IBUF_READ_SIZE;
398 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
399 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
403 5b762f01 2021-07-24 op if (bufev->wm_read.high != 0)
404 5b762f01 2021-07-24 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
406 5b762f01 2021-07-24 op switch (ret = tls_read(req->ctx, buf, howmuch)) {
407 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
408 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
411 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
416 5b762f01 2021-07-24 op if (len == 0) {
417 5b762f01 2021-07-24 op what |= EVBUFFER_EOF;
421 1470eab3 2021-07-27 op res = evbuffer_add(bufev->input, buf, len);
422 1470eab3 2021-07-27 op if (res == -1) {
423 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
427 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
429 5b762f01 2021-07-24 op len = EVBUFFER_LENGTH(bufev->input);
430 5b762f01 2021-07-24 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
433 5b762f01 2021-07-24 op if (bufev->readcb != NULL)
434 5b762f01 2021-07-24 op (*bufev->readcb)(bufev, bufev->cbarg);
438 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
442 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
446 5b762f01 2021-07-24 op net_tls_writecb(int fd, short event, void *d)
448 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
449 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
452 5b762f01 2021-07-24 op short what = EVBUFFER_WRITE;
454 5b762f01 2021-07-24 op if (event & EV_TIMEOUT) {
455 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
459 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
460 5b762f01 2021-07-24 op ret = tls_write(req->ctx, EVBUFFER_DATA(bufev->output),
461 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output));
462 5b762f01 2021-07-24 op switch (ret) {
463 5b762f01 2021-07-24 op case TLS_WANT_POLLIN:
464 5b762f01 2021-07-24 op case TLS_WANT_POLLOUT:
467 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
472 5b762f01 2021-07-24 op evbuffer_drain(bufev->output, len);
475 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0)
476 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
478 5b762f01 2021-07-24 op if (bufev->writecb != NULL &&
479 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
480 5b762f01 2021-07-24 op (*bufev->writecb)(bufev, bufev->cbarg);
484 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
488 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
492 5b762f01 2021-07-24 op gemini_parse_reply(struct req *req, const char *header, size_t len)
495 5b762f01 2021-07-24 op const char *t;
500 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
503 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
504 5b762f01 2021-07-24 op if (header[2] != ' ')
507 5b762f01 2021-07-24 op t = header + 3;
509 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
510 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_META, req->id, t, strlen(t)+1);
512 5b762f01 2021-07-24 op bufferevent_disable(req->bev, EV_READ|EV_WRITE);
517 5b762f01 2021-07-24 op /* called when we're ready to read/write */
519 5b762f01 2021-07-24 op net_ready(struct req *req)
521 5b762f01 2021-07-24 op req->bev = bufferevent_new(req->fd, net_read, net_write, net_error,
523 5b762f01 2021-07-24 op if (req->bev == NULL)
526 b5c5e56d 2021-07-28 op #if HAVE_EVENT2
527 73bb7969 2021-07-27 op evbuffer_unfreeze(req->bev->input, 0);
528 73bb7969 2021-07-27 op evbuffer_unfreeze(req->bev->output, 1);
531 5b762f01 2021-07-24 op /* setup tls i/o layer */
532 5b762f01 2021-07-24 op if (req->ctx != NULL) {
533 5b762f01 2021-07-24 op event_set(&req->bev->ev_read, req->fd, EV_READ,
534 5b762f01 2021-07-24 op net_tls_readcb, req->bev);
535 5b762f01 2021-07-24 op event_set(&req->bev->ev_write, req->fd, EV_WRITE,
536 5b762f01 2021-07-24 op net_tls_writecb, req->bev);
539 5b762f01 2021-07-24 op /* TODO: adjust watermarks */
540 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_WRITE, 1, 0);
541 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_READ, 1, 0);
543 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ|EV_WRITE);
545 5b762f01 2021-07-24 op bufferevent_write(req->bev, req->req, req->len);
548 5b762f01 2021-07-24 op /* called after a read has been done */
550 5b762f01 2021-07-24 op net_read(struct bufferevent *bev, void *d)
552 5b762f01 2021-07-24 op struct req *req = d;
553 5b762f01 2021-07-24 op struct evbuffer *src = EVBUFFER_INPUT(bev);
554 3591e6db 2022-01-20 op uint8_t *data;
555 0e8f22ed 2021-09-12 op size_t len, chunk;
557 5b762f01 2021-07-24 op char *header;
559 5b762f01 2021-07-24 op if (!req->done_header) {
560 5b762f01 2021-07-24 op header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
561 5b762f01 2021-07-24 op if (header == NULL && EVBUFFER_LENGTH(src) >= 1024)
563 5b762f01 2021-07-24 op if (header == NULL)
565 5b762f01 2021-07-24 op r = gemini_parse_reply(req, header, len);
566 5b762f01 2021-07-24 op free(header);
567 0e49c9bb 2022-01-21 op req->done_header = 1;
570 0e49c9bb 2022-01-21 op else if (r < 20 || r >= 30)
571 0e49c9bb 2022-01-21 op close_conn(0, 0, req);
575 5b762f01 2021-07-24 op if ((len = EVBUFFER_LENGTH(src)) == 0)
577 5b762f01 2021-07-24 op data = EVBUFFER_DATA(src);
580 0e8f22ed 2021-09-12 op * Split data into chunks before sending. imsg can't handle
581 0e8f22ed 2021-09-12 op * message that are "too big".
583 0e8f22ed 2021-09-12 op while (len != 0) {
584 0e8f22ed 2021-09-12 op chunk = MIN(len, 4096);
585 0e8f22ed 2021-09-12 op net_send_ui(IMSG_BUF, req->id, data, chunk);
586 0e8f22ed 2021-09-12 op data += chunk;
587 0e8f22ed 2021-09-12 op len -= chunk;
590 0e8f22ed 2021-09-12 op evbuffer_drain(src, EVBUFFER_LENGTH(src));
594 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
597 5b762f01 2021-07-24 op /* called after a write has been done */
599 5b762f01 2021-07-24 op net_write(struct bufferevent *bev, void *d)
601 5b762f01 2021-07-24 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
603 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(dst) == 0)
604 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg);
608 5b762f01 2021-07-24 op net_error(struct bufferevent *bev, short error, void *d)
610 c8dba1e6 2021-07-24 op struct req *req = d;
611 590c00b7 2021-07-25 op struct evbuffer *src;
613 5b762f01 2021-07-24 op if (error & EVBUFFER_TIMEOUT) {
614 5b762f01 2021-07-24 op close_with_err(req, "Timeout loading page");
618 5b762f01 2021-07-24 op if (error & EVBUFFER_ERROR) {
619 5b762f01 2021-07-24 op close_with_err(req, "buffer event error");
623 5b762f01 2021-07-24 op if (error & EVBUFFER_EOF) {
624 98cba69e 2021-11-10 op /* EOF and no header */
625 98cba69e 2021-11-10 op if (!req->done_header) {
626 98cba69e 2021-11-10 op close_with_err(req, "protocol error");
630 95a8c791 2021-08-26 op src = EVBUFFER_INPUT(req->bev);
631 590c00b7 2021-07-25 op if (EVBUFFER_LENGTH(src) != 0)
632 590c00b7 2021-07-25 op net_send_ui(IMSG_BUF, req->id, EVBUFFER_DATA(src),
633 590c00b7 2021-07-25 op EVBUFFER_LENGTH(src));
634 5b762f01 2021-07-24 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
635 5b762f01 2021-07-24 op close_conn(0, 0, req);
639 5b762f01 2021-07-24 op if (error & EVBUFFER_WRITE) {
640 5b762f01 2021-07-24 op /* finished sending request */
641 5b762f01 2021-07-24 op bufferevent_disable(bev, EV_WRITE);
645 5b762f01 2021-07-24 op if (error & EVBUFFER_READ) {
646 5b762f01 2021-07-24 op close_with_err(req, "protocol error");
650 5b762f01 2021-07-24 op close_with_errf(req, "unknown event error %x", error);
654 c8dba1e6 2021-07-24 op handle_get_raw(struct imsg *imsg, size_t datalen)
656 c8dba1e6 2021-07-24 op struct req *req;
657 c8dba1e6 2021-07-24 op struct get_req *r;
659 c8dba1e6 2021-07-24 op r = imsg->data;
661 c8dba1e6 2021-07-24 op if (datalen != sizeof(*r))
664 c8dba1e6 2021-07-24 op if ((req = calloc(1, sizeof(*req))) == NULL)
667 c8dba1e6 2021-07-24 op req->id = imsg->hdr.peerid;
668 c8dba1e6 2021-07-24 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
670 c8dba1e6 2021-07-24 op strlcpy(req->url.host, r->host, sizeof(req->url.host));
671 c8dba1e6 2021-07-24 op strlcpy(req->url.port, r->port, sizeof(req->url.port));
673 5b762f01 2021-07-24 op strlcpy(req->req, r->req, sizeof(req->req));
674 5b762f01 2021-07-24 op req->len = strlen(r->req);
676 5fb52fc0 2021-07-25 op req->proto = r->proto;
678 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
679 95a8c791 2021-08-26 op async_conn_towards(req);
681 c8dba1e6 2021-07-24 op blocking_conn_towards(req);
686 c8dba1e6 2021-07-24 op handle_cert_status(struct imsg *imsg, size_t datalen)
688 c8dba1e6 2021-07-24 op struct req *req;
691 c8dba1e6 2021-07-24 op req = req_by_id(imsg->hdr.peerid);
693 c8dba1e6 2021-07-24 op if (datalen < sizeof(is_ok))
695 c8dba1e6 2021-07-24 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
698 5b762f01 2021-07-24 op net_ready(req);
700 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
704 c8dba1e6 2021-07-24 op handle_proceed(struct imsg *imsg, size_t datalen)
706 5b762f01 2021-07-24 op struct req *req;
708 318a6c1a 2021-07-24 op if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
711 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ);
715 c8dba1e6 2021-07-24 op handle_stop(struct imsg *imsg, size_t datalen)
717 c8dba1e6 2021-07-24 op struct req *req;
719 318a6c1a 2021-07-24 op if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
721 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
725 c8dba1e6 2021-07-24 op handle_quit(struct imsg *imsg, size_t datalen)
727 c8dba1e6 2021-07-24 op event_loopbreak();
731 c8dba1e6 2021-07-24 op handle_dispatch_imsg(int fd, short ev, void *d)
733 c8dba1e6 2021-07-24 op struct imsgev *iev = d;
735 c8dba1e6 2021-07-24 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
736 c8dba1e6 2021-07-24 op err(1, "connection closed");
740 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
741 c8dba1e6 2021-07-24 op uint16_t datalen)
743 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
744 c8dba1e6 2021-07-24 op data, datalen);
748 f45bd2e3 2021-07-24 op net_main(void)
750 c8dba1e6 2021-07-24 op setproctitle("net");
752 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
754 c8dba1e6 2021-07-24 op event_init();
756 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
757 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
759 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
760 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
761 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
762 c8dba1e6 2021-07-24 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
763 c8dba1e6 2021-07-24 op iev_ui->handler, iev_ui);
764 c8dba1e6 2021-07-24 op event_add(&iev_ui->ev, NULL);
766 c8dba1e6 2021-07-24 op sandbox_net_process();
768 c8dba1e6 2021-07-24 op event_dispatch();
770 74c0f6ba 2021-07-24 op msgbuf_clear(&iev_ui->ibuf.w);
771 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
772 74c0f6ba 2021-07-24 op free(iev_ui);