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"
41 c8dba1e6 2021-07-24 op static struct imsgev *iev_ui;
42 c8dba1e6 2021-07-24 op static struct tls_config *tlsconf;
46 c8dba1e6 2021-07-24 op static void die(void) __attribute__((__noreturn__));
48 c8dba1e6 2021-07-24 op static void try_to_connect(int, short, void*);
50 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
51 c8dba1e6 2021-07-24 op static void query_done(struct asr_result*, void*);
52 c8dba1e6 2021-07-24 op static void async_conn_towards(struct req*);
54 c8dba1e6 2021-07-24 op static void blocking_conn_towards(struct req*);
57 c8dba1e6 2021-07-24 op static void close_with_err(struct req*, const char*);
58 bfb9acb0 2021-07-24 op static void close_with_errf(struct req*, const char*, ...)
59 bfb9acb0 2021-07-24 op __attribute__((format(printf, 2, 3)));
60 c8dba1e6 2021-07-24 op static struct req *req_by_id(uint32_t);
61 c8dba1e6 2021-07-24 op static struct req *req_by_id_try(uint32_t);
63 5b762f01 2021-07-24 op static void net_tls_handshake(int, short, void *);
64 5b762f01 2021-07-24 op static void net_tls_readcb(int, short, void *);
65 5b762f01 2021-07-24 op static void net_tls_writecb(int, short, void *);
67 5b762f01 2021-07-24 op static int gemini_parse_reply(struct req *, const char *, size_t);
69 5b762f01 2021-07-24 op static void net_ready(struct req *req);
70 5b762f01 2021-07-24 op static void net_read(struct bufferevent *, void *);
71 5b762f01 2021-07-24 op static void net_write(struct bufferevent *, void *);
72 5b762f01 2021-07-24 op static void net_error(struct bufferevent *, short, void *);
74 c8dba1e6 2021-07-24 op static void handle_get_raw(struct imsg *, size_t);
75 c8dba1e6 2021-07-24 op static void handle_cert_status(struct imsg*, size_t);
76 c8dba1e6 2021-07-24 op static void handle_proceed(struct imsg*, size_t);
77 c8dba1e6 2021-07-24 op static void handle_stop(struct imsg*, size_t);
78 c8dba1e6 2021-07-24 op static void handle_quit(struct imsg*, size_t);
79 c8dba1e6 2021-07-24 op static void handle_dispatch_imsg(int, short, void*);
81 c8dba1e6 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
83 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
84 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
86 c8dba1e6 2021-07-24 op static imsg_handlerfn *handlers[] = {
87 c8dba1e6 2021-07-24 op [IMSG_GET_RAW] = handle_get_raw,
88 c8dba1e6 2021-07-24 op [IMSG_CERT_STATUS] = handle_cert_status,
89 c8dba1e6 2021-07-24 op [IMSG_PROCEED] = handle_proceed,
90 c8dba1e6 2021-07-24 op [IMSG_STOP] = handle_stop,
91 c8dba1e6 2021-07-24 op [IMSG_QUIT] = handle_quit,
94 c8dba1e6 2021-07-24 op typedef void (*statefn)(int, short, void*);
96 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
97 c8dba1e6 2021-07-24 op /* a pending request */
99 c8dba1e6 2021-07-24 op struct phos_uri url;
102 c8dba1e6 2021-07-24 op struct tls *ctx;
103 5b762f01 2021-07-24 op char req[1024];
105 5b762f01 2021-07-24 op int done_header;
106 5b762f01 2021-07-24 op struct bufferevent *bev;
108 c8dba1e6 2021-07-24 op struct addrinfo *servinfo, *p;
109 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
110 c8dba1e6 2021-07-24 op struct addrinfo hints;
111 c8dba1e6 2021-07-24 op struct event_asr *asrev;
114 c8dba1e6 2021-07-24 op TAILQ_ENTRY(req) reqs;
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 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
132 c8dba1e6 2021-07-24 op abort(); /* TODO */
136 c8dba1e6 2021-07-24 op try_to_connect(int fd, short ev, void *d)
138 c8dba1e6 2021-07-24 op struct req *req = d;
139 c8dba1e6 2021-07-24 op int error = 0;
140 c8dba1e6 2021-07-24 op socklen_t len = sizeof(error);
143 c8dba1e6 2021-07-24 op if (req->p == NULL)
146 c8dba1e6 2021-07-24 op if (req->fd != -1) {
147 bfb9acb0 2021-07-24 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
150 c8dba1e6 2021-07-24 op if (error != 0) {
151 c8dba1e6 2021-07-24 op errno = error;
157 bfb9acb0 2021-07-24 op req->fd = socket(req->p->ai_family, req->p->ai_socktype,
158 bfb9acb0 2021-07-24 op req->p->ai_protocol);
159 c8dba1e6 2021-07-24 op if (req->fd == -1) {
160 c8dba1e6 2021-07-24 op req->p = req->p->ai_next;
163 c8dba1e6 2021-07-24 op mark_nonblock(req->fd);
164 c8dba1e6 2021-07-24 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
166 c8dba1e6 2021-07-24 op yield_w(req, try_to_connect, NULL);
171 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
172 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to connect to %s",
173 c8dba1e6 2021-07-24 op req->url.host);
177 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
179 0d40acb1 2021-07-24 op /* prepare tls */
180 0d40acb1 2021-07-24 op if ((req->ctx = tls_client()) == NULL) {
181 0d40acb1 2021-07-24 op close_with_errf(req, "tls_client: %s", strerror(errno));
184 0d40acb1 2021-07-24 op if (tls_configure(req->ctx, tlsconf) == -1) {
185 0d40acb1 2021-07-24 op close_with_errf(req, "tls_configure: %s", tls_error(req->ctx));
188 0d40acb1 2021-07-24 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
189 0d40acb1 2021-07-24 op close_with_errf(req, "tls_connect_socket: %s",
190 0d40acb1 2021-07-24 op tls_error(req->ctx));
193 0d40acb1 2021-07-24 op yield_w(req, net_tls_handshake, &timeout_for_handshake);
196 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
198 c8dba1e6 2021-07-24 op query_done(struct asr_result *res, void *d)
200 c8dba1e6 2021-07-24 op struct req *req = d;
202 c8dba1e6 2021-07-24 op req->asrev = NULL;
203 c8dba1e6 2021-07-24 op if (res->ar_gai_errno != 0) {
204 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
205 c8dba1e6 2021-07-24 op req->url.host, gai_strerror(res->ar_gai_errno));
209 c8dba1e6 2021-07-24 op req->fd = -1;
210 c8dba1e6 2021-07-24 op req->servinfo = res->ar_addrinfo;
211 c8dba1e6 2021-07-24 op req->p = res->ar_addrinfo;
212 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
216 c8dba1e6 2021-07-24 op async_conn_towards(struct req *req)
218 c8dba1e6 2021-07-24 op struct asr_query *q;
219 c8dba1e6 2021-07-24 op const char *proto = "1965";
221 c8dba1e6 2021-07-24 op if (*req->url.port != '\0')
222 c8dba1e6 2021-07-24 op proto = req->url.port;
224 c8dba1e6 2021-07-24 op req->hints.ai_family = AF_UNSPEC;
225 c8dba1e6 2021-07-24 op req->hints.ai_socktype = SOCK_STREAM;
226 c8dba1e6 2021-07-24 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
227 c8dba1e6 2021-07-24 op req->asrev = event_asr_run(q, query_done, req);
231 c8dba1e6 2021-07-24 op blocking_conn_towards(struct req *req)
233 c8dba1e6 2021-07-24 op struct addrinfo hints;
234 c8dba1e6 2021-07-24 op struct phos_uri *url = &req->url;
236 c8dba1e6 2021-07-24 op const char *proto = "1965";
238 c8dba1e6 2021-07-24 op if (*url->port != '\0')
239 c8dba1e6 2021-07-24 op proto = url->port;
241 c8dba1e6 2021-07-24 op memset(&hints, 0, sizeof(hints));
242 c8dba1e6 2021-07-24 op hints.ai_family = AF_UNSPEC;
243 c8dba1e6 2021-07-24 op hints.ai_socktype = SOCK_STREAM;
245 c8dba1e6 2021-07-24 op if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
246 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
247 c8dba1e6 2021-07-24 op url->host, gai_strerror(status));
251 c8dba1e6 2021-07-24 op req->fd = -1;
252 c8dba1e6 2021-07-24 op req->p = req->servinfo;
253 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
257 c8dba1e6 2021-07-24 op static struct req *
258 c8dba1e6 2021-07-24 op req_by_id(uint32_t id)
260 c8dba1e6 2021-07-24 op struct req *r;
262 c8dba1e6 2021-07-24 op if ((r = req_by_id_try(id)) == NULL)
267 c8dba1e6 2021-07-24 op static struct req *
268 c8dba1e6 2021-07-24 op req_by_id_try(uint32_t id)
270 c8dba1e6 2021-07-24 op struct req *r;
272 c8dba1e6 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
273 c8dba1e6 2021-07-24 op if (r->id == id)
281 c8dba1e6 2021-07-24 op close_conn(int fd, short ev, void *d)
283 c8dba1e6 2021-07-24 op struct req *req = d;
285 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
286 c8dba1e6 2021-07-24 op if (req->asrev != NULL)
287 c8dba1e6 2021-07-24 op event_asr_abort(req->asrev);
290 5b762f01 2021-07-24 op if (req->bev != NULL) {
291 5b762f01 2021-07-24 op bufferevent_free(req->bev);
292 5b762f01 2021-07-24 op req->bev = NULL;
295 c8dba1e6 2021-07-24 op if (req->ctx != NULL) {
296 c8dba1e6 2021-07-24 op switch (tls_close(req->ctx)) {
297 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
298 c8dba1e6 2021-07-24 op yield_r(req, close_conn, NULL);
300 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
301 c8dba1e6 2021-07-24 op yield_w(req, close_conn, NULL);
305 c8dba1e6 2021-07-24 op tls_free(req->ctx);
306 5b762f01 2021-07-24 op req->ctx = NULL;
309 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
310 c8dba1e6 2021-07-24 op if (req->fd != -1)
311 c8dba1e6 2021-07-24 op close(req->fd);
316 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
318 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
319 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
323 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
328 c8dba1e6 2021-07-24 op va_start(ap, fmt);
329 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
333 c8dba1e6 2021-07-24 op close_with_err(req, s);
338 5b762f01 2021-07-24 op net_tls_handshake(int fd, short event, void *d)
340 c8dba1e6 2021-07-24 op struct req *req = d;
341 c8dba1e6 2021-07-24 op const char *hash;
343 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
344 c8dba1e6 2021-07-24 op close_with_err(req, "Timeout loading page");
348 c8dba1e6 2021-07-24 op switch (tls_handshake(req->ctx)) {
349 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
350 5b762f01 2021-07-24 op yield_r(req, net_tls_handshake, NULL);
352 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
353 5b762f01 2021-07-24 op yield_w(req, net_tls_handshake, NULL);
357 c8dba1e6 2021-07-24 op hash = tls_peer_cert_hash(req->ctx);
358 c8dba1e6 2021-07-24 op if (hash == NULL) {
359 bfb9acb0 2021-07-24 op close_with_errf(req, "handshake failed: %s",
360 bfb9acb0 2021-07-24 op tls_error(req->ctx));
363 c8dba1e6 2021-07-24 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
367 5b762f01 2021-07-24 op net_tls_readcb(int fd, short event, void *d)
369 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
370 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
371 5b762f01 2021-07-24 op char buf[BUFSIZ];
372 5b762f01 2021-07-24 op int what = EVBUFFER_READ;
373 5b762f01 2021-07-24 op int howmuch = IBUF_READ_SIZE;
377 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
378 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
382 5b762f01 2021-07-24 op if (bufev->wm_read.high != 0)
383 5b762f01 2021-07-24 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
385 5b762f01 2021-07-24 op switch (ret = tls_read(req->ctx, buf, howmuch)) {
386 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
387 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
390 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
395 5b762f01 2021-07-24 op if (len == 0) {
396 5b762f01 2021-07-24 op what |= EVBUFFER_EOF;
400 5b762f01 2021-07-24 op if (evbuffer_add(bufev->input, buf, len) == -1) {
401 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
405 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
407 5b762f01 2021-07-24 op len = EVBUFFER_LENGTH(bufev->input);
408 5b762f01 2021-07-24 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
411 5b762f01 2021-07-24 op if (bufev->readcb != NULL)
412 5b762f01 2021-07-24 op (*bufev->readcb)(bufev, bufev->cbarg);
416 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
420 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
424 5b762f01 2021-07-24 op net_tls_writecb(int fd, short event, void *d)
426 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
427 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
430 5b762f01 2021-07-24 op short what = EVBUFFER_WRITE;
432 5b762f01 2021-07-24 op if (event & EV_TIMEOUT) {
433 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
437 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
438 5b762f01 2021-07-24 op ret = tls_write(req->ctx, EVBUFFER_DATA(bufev->output),
439 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output));
440 5b762f01 2021-07-24 op switch (ret) {
441 5b762f01 2021-07-24 op case TLS_WANT_POLLIN:
442 5b762f01 2021-07-24 op case TLS_WANT_POLLOUT:
445 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
449 5b762f01 2021-07-24 op evbuffer_drain(bufev->output, len);
452 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0)
453 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
455 5b762f01 2021-07-24 op if (bufev->writecb != NULL &&
456 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
457 5b762f01 2021-07-24 op (*bufev->writecb)(bufev, bufev->cbarg);
461 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
465 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
469 5b762f01 2021-07-24 op gemini_parse_reply(struct req *req, const char *header, size_t len)
472 5b762f01 2021-07-24 op const char *t;
477 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
480 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
481 5b762f01 2021-07-24 op if (header[2] != ' ')
484 5b762f01 2021-07-24 op t = header + 3;
486 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
487 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_META, req->id, t, strlen(t)+1);
489 5b762f01 2021-07-24 op bufferevent_disable(req->bev, EV_READ|EV_WRITE);
491 5b762f01 2021-07-24 op if (code < 20 || code >= 30)
492 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
496 5b762f01 2021-07-24 op /* called when we're ready to read/write */
498 5b762f01 2021-07-24 op net_ready(struct req *req)
500 5b762f01 2021-07-24 op req->bev = bufferevent_new(req->fd, net_read, net_write, net_error,
502 5b762f01 2021-07-24 op if (req->bev == NULL)
505 5b762f01 2021-07-24 op /* setup tls i/o layer */
506 5b762f01 2021-07-24 op if (req->ctx != NULL) {
507 5b762f01 2021-07-24 op event_set(&req->bev->ev_read, req->fd, EV_READ,
508 5b762f01 2021-07-24 op net_tls_readcb, req->bev);
509 5b762f01 2021-07-24 op event_set(&req->bev->ev_write, req->fd, EV_WRITE,
510 5b762f01 2021-07-24 op net_tls_writecb, req->bev);
513 5b762f01 2021-07-24 op /* TODO: adjust watermarks */
514 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_WRITE, 1, 0);
515 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_READ, 1, 0);
517 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ|EV_WRITE);
519 5b762f01 2021-07-24 op bufferevent_write(req->bev, req->req, req->len);
522 5b762f01 2021-07-24 op /* called after a read has been done */
524 5b762f01 2021-07-24 op net_read(struct bufferevent *bev, void *d)
526 5b762f01 2021-07-24 op struct req *req = d;
527 5b762f01 2021-07-24 op struct evbuffer *src = EVBUFFER_INPUT(bev);
531 5b762f01 2021-07-24 op char *header;
533 5b762f01 2021-07-24 op if (!req->done_header) {
534 5b762f01 2021-07-24 op header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
535 5b762f01 2021-07-24 op if (header == NULL && EVBUFFER_LENGTH(src) >= 1024)
537 5b762f01 2021-07-24 op if (header == NULL)
539 5b762f01 2021-07-24 op r = gemini_parse_reply(req, header, len);
540 5b762f01 2021-07-24 op free(header);
543 5b762f01 2021-07-24 op req->done_header = 1;
547 5b762f01 2021-07-24 op if ((len = EVBUFFER_LENGTH(src)) == 0)
549 5b762f01 2021-07-24 op data = EVBUFFER_DATA(src);
550 5b762f01 2021-07-24 op net_send_ui(IMSG_BUF, req->id, data, len);
551 5b762f01 2021-07-24 op evbuffer_drain(src, len);
555 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
558 5b762f01 2021-07-24 op /* called after a write has been done */
560 5b762f01 2021-07-24 op net_write(struct bufferevent *bev, void *d)
562 5b762f01 2021-07-24 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
564 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(dst) == 0)
565 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg);
569 5b762f01 2021-07-24 op net_error(struct bufferevent *bev, short error, void *d)
571 c8dba1e6 2021-07-24 op struct req *req = d;
573 5b762f01 2021-07-24 op if (error & EVBUFFER_TIMEOUT) {
574 5b762f01 2021-07-24 op close_with_err(req, "Timeout loading page");
578 5b762f01 2021-07-24 op if (error & EVBUFFER_ERROR) {
579 5b762f01 2021-07-24 op close_with_err(req, "buffer event error");
583 5b762f01 2021-07-24 op if (error & EVBUFFER_EOF) {
584 5b762f01 2021-07-24 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
585 5b762f01 2021-07-24 op close_conn(0, 0, req);
589 5b762f01 2021-07-24 op if (error & EVBUFFER_WRITE) {
590 5b762f01 2021-07-24 op /* finished sending request */
591 5b762f01 2021-07-24 op bufferevent_disable(bev, EV_WRITE);
595 5b762f01 2021-07-24 op if (error & EVBUFFER_READ) {
596 5b762f01 2021-07-24 op close_with_err(req, "protocol error");
600 5b762f01 2021-07-24 op close_with_errf(req, "unknown event error %x", error);
604 c8dba1e6 2021-07-24 op handle_get_raw(struct imsg *imsg, size_t datalen)
606 c8dba1e6 2021-07-24 op struct req *req;
607 c8dba1e6 2021-07-24 op struct get_req *r;
609 c8dba1e6 2021-07-24 op r = imsg->data;
611 c8dba1e6 2021-07-24 op if (datalen != sizeof(*r))
614 c8dba1e6 2021-07-24 op if ((req = calloc(1, sizeof(*req))) == NULL)
617 c8dba1e6 2021-07-24 op req->id = imsg->hdr.peerid;
618 c8dba1e6 2021-07-24 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
620 c8dba1e6 2021-07-24 op strlcpy(req->url.host, r->host, sizeof(req->url.host));
621 c8dba1e6 2021-07-24 op strlcpy(req->url.port, r->port, sizeof(req->url.port));
623 5b762f01 2021-07-24 op strlcpy(req->req, r->req, sizeof(req->req));
624 5b762f01 2021-07-24 op req->len = strlen(r->req);
626 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
627 c8dba1e6 2021-07-24 op async_conn_towards(req);
629 c8dba1e6 2021-07-24 op blocking_conn_towards(req);
634 c8dba1e6 2021-07-24 op handle_cert_status(struct imsg *imsg, size_t datalen)
636 c8dba1e6 2021-07-24 op struct req *req;
639 c8dba1e6 2021-07-24 op req = req_by_id(imsg->hdr.peerid);
641 c8dba1e6 2021-07-24 op if (datalen < sizeof(is_ok))
643 c8dba1e6 2021-07-24 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
646 5b762f01 2021-07-24 op net_ready(req);
648 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
652 c8dba1e6 2021-07-24 op handle_proceed(struct imsg *imsg, size_t datalen)
654 5b762f01 2021-07-24 op struct req *req;
656 5b762f01 2021-07-24 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
659 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ);
663 c8dba1e6 2021-07-24 op handle_stop(struct imsg *imsg, size_t datalen)
665 c8dba1e6 2021-07-24 op struct req *req;
667 c8dba1e6 2021-07-24 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
669 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
673 c8dba1e6 2021-07-24 op handle_quit(struct imsg *imsg, size_t datalen)
675 c8dba1e6 2021-07-24 op event_loopbreak();
679 c8dba1e6 2021-07-24 op handle_dispatch_imsg(int fd, short ev, void *d)
681 c8dba1e6 2021-07-24 op struct imsgev *iev = d;
683 c8dba1e6 2021-07-24 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
684 c8dba1e6 2021-07-24 op err(1, "connection closed");
688 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
689 c8dba1e6 2021-07-24 op uint16_t datalen)
691 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
692 c8dba1e6 2021-07-24 op data, datalen);
696 f45bd2e3 2021-07-24 op net_main(void)
698 c8dba1e6 2021-07-24 op setproctitle("net");
700 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
702 c8dba1e6 2021-07-24 op if ((tlsconf = tls_config_new()) == NULL)
704 c8dba1e6 2021-07-24 op tls_config_insecure_noverifycert(tlsconf);
705 c8dba1e6 2021-07-24 op tls_config_insecure_noverifyname(tlsconf);
707 c8dba1e6 2021-07-24 op event_init();
709 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
710 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
712 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
713 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
714 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
715 c8dba1e6 2021-07-24 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
716 c8dba1e6 2021-07-24 op iev_ui->handler, iev_ui);
717 c8dba1e6 2021-07-24 op event_add(&iev_ui->ev, NULL);
719 c8dba1e6 2021-07-24 op sandbox_net_process();
721 c8dba1e6 2021-07-24 op event_dispatch();
723 74c0f6ba 2021-07-24 op tls_config_free(tlsconf);
724 74c0f6ba 2021-07-24 op msgbuf_clear(&iev_ui->ibuf.w);
725 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
726 74c0f6ba 2021-07-24 op free(iev_ui);