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 c8dba1e6 2021-07-24 op static void close_with_errf(struct req*, const char*, ...) __attribute__((format(printf, 2, 3)));
59 c8dba1e6 2021-07-24 op static struct req *req_by_id(uint32_t);
60 c8dba1e6 2021-07-24 op static struct req *req_by_id_try(uint32_t);
62 c8dba1e6 2021-07-24 op static void setup_tls(struct req*);
64 5b762f01 2021-07-24 op static void net_tls_handshake(int, short, void *);
65 5b762f01 2021-07-24 op static void net_tls_readcb(int, short, void *);
66 5b762f01 2021-07-24 op static void net_tls_writecb(int, short, void *);
68 5b762f01 2021-07-24 op static int gemini_parse_reply(struct req *, const char *, size_t);
70 5b762f01 2021-07-24 op static void net_ready(struct req *req);
71 5b762f01 2021-07-24 op static void net_read(struct bufferevent *, void *);
72 5b762f01 2021-07-24 op static void net_write(struct bufferevent *, void *);
73 5b762f01 2021-07-24 op static void net_error(struct bufferevent *, short, void *);
75 c8dba1e6 2021-07-24 op static void handle_get_raw(struct imsg *, size_t);
76 c8dba1e6 2021-07-24 op static void handle_cert_status(struct imsg*, size_t);
77 c8dba1e6 2021-07-24 op static void handle_proceed(struct imsg*, size_t);
78 c8dba1e6 2021-07-24 op static void handle_stop(struct imsg*, size_t);
79 c8dba1e6 2021-07-24 op static void handle_quit(struct imsg*, size_t);
80 c8dba1e6 2021-07-24 op static void handle_dispatch_imsg(int, short, void*);
82 c8dba1e6 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
84 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
85 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
87 c8dba1e6 2021-07-24 op static imsg_handlerfn *handlers[] = {
88 c8dba1e6 2021-07-24 op [IMSG_GET_RAW] = handle_get_raw,
89 c8dba1e6 2021-07-24 op [IMSG_CERT_STATUS] = handle_cert_status,
90 c8dba1e6 2021-07-24 op [IMSG_PROCEED] = handle_proceed,
91 c8dba1e6 2021-07-24 op [IMSG_STOP] = handle_stop,
92 c8dba1e6 2021-07-24 op [IMSG_QUIT] = handle_quit,
95 c8dba1e6 2021-07-24 op typedef void (*statefn)(int, short, void*);
97 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
98 c8dba1e6 2021-07-24 op /* a pending request */
100 c8dba1e6 2021-07-24 op struct phos_uri url;
103 c8dba1e6 2021-07-24 op struct tls *ctx;
104 5b762f01 2021-07-24 op char req[1024];
106 5b762f01 2021-07-24 op int done_header;
107 5b762f01 2021-07-24 op struct bufferevent *bev;
109 c8dba1e6 2021-07-24 op struct addrinfo *servinfo, *p;
110 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
111 c8dba1e6 2021-07-24 op struct addrinfo hints;
112 c8dba1e6 2021-07-24 op struct event_asr *asrev;
115 c8dba1e6 2021-07-24 op TAILQ_ENTRY(req) reqs;
118 c8dba1e6 2021-07-24 op static inline void
119 c8dba1e6 2021-07-24 op yield_r(struct req *req, statefn fn, struct timeval *tv)
121 c8dba1e6 2021-07-24 op event_once(req->fd, EV_READ, fn, req, tv);
124 c8dba1e6 2021-07-24 op static inline void
125 c8dba1e6 2021-07-24 op yield_w(struct req *req, statefn fn, struct timeval *tv)
127 c8dba1e6 2021-07-24 op event_once(req->fd, EV_WRITE, fn, req, tv);
130 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
133 c8dba1e6 2021-07-24 op abort(); /* TODO */
137 c8dba1e6 2021-07-24 op try_to_connect(int fd, short ev, void *d)
139 c8dba1e6 2021-07-24 op struct req *req = d;
140 c8dba1e6 2021-07-24 op int error = 0;
141 c8dba1e6 2021-07-24 op socklen_t len = sizeof(error);
144 c8dba1e6 2021-07-24 op if (req->p == NULL)
147 c8dba1e6 2021-07-24 op if (req->fd != -1) {
148 c8dba1e6 2021-07-24 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error, &len) == -1)
150 c8dba1e6 2021-07-24 op if (error != 0) {
151 c8dba1e6 2021-07-24 op errno = error;
157 c8dba1e6 2021-07-24 op req->fd = socket(req->p->ai_family, req->p->ai_socktype, req->p->ai_protocol);
158 c8dba1e6 2021-07-24 op if (req->fd == -1) {
159 c8dba1e6 2021-07-24 op req->p = req->p->ai_next;
162 c8dba1e6 2021-07-24 op mark_nonblock(req->fd);
163 c8dba1e6 2021-07-24 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
165 c8dba1e6 2021-07-24 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);
177 c8dba1e6 2021-07-24 op setup_tls(req);
180 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
182 c8dba1e6 2021-07-24 op query_done(struct asr_result *res, void *d)
184 c8dba1e6 2021-07-24 op struct req *req = d;
186 c8dba1e6 2021-07-24 op req->asrev = NULL;
187 c8dba1e6 2021-07-24 op if (res->ar_gai_errno != 0) {
188 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
189 c8dba1e6 2021-07-24 op req->url.host, gai_strerror(res->ar_gai_errno));
193 c8dba1e6 2021-07-24 op req->fd = -1;
194 c8dba1e6 2021-07-24 op req->servinfo = res->ar_addrinfo;
195 c8dba1e6 2021-07-24 op req->p = res->ar_addrinfo;
196 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
200 c8dba1e6 2021-07-24 op async_conn_towards(struct req *req)
202 c8dba1e6 2021-07-24 op struct asr_query *q;
203 c8dba1e6 2021-07-24 op const char *proto = "1965";
205 c8dba1e6 2021-07-24 op if (*req->url.port != '\0')
206 c8dba1e6 2021-07-24 op proto = req->url.port;
208 c8dba1e6 2021-07-24 op req->hints.ai_family = AF_UNSPEC;
209 c8dba1e6 2021-07-24 op req->hints.ai_socktype = SOCK_STREAM;
210 c8dba1e6 2021-07-24 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
211 c8dba1e6 2021-07-24 op req->asrev = event_asr_run(q, query_done, req);
215 c8dba1e6 2021-07-24 op blocking_conn_towards(struct req *req)
217 c8dba1e6 2021-07-24 op struct addrinfo hints;
218 c8dba1e6 2021-07-24 op struct phos_uri *url = &req->url;
220 c8dba1e6 2021-07-24 op const char *proto = "1965";
222 c8dba1e6 2021-07-24 op if (*url->port != '\0')
223 c8dba1e6 2021-07-24 op proto = url->port;
225 c8dba1e6 2021-07-24 op memset(&hints, 0, sizeof(hints));
226 c8dba1e6 2021-07-24 op hints.ai_family = AF_UNSPEC;
227 c8dba1e6 2021-07-24 op hints.ai_socktype = SOCK_STREAM;
229 c8dba1e6 2021-07-24 op if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
230 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
231 c8dba1e6 2021-07-24 op url->host, gai_strerror(status));
235 c8dba1e6 2021-07-24 op req->fd = -1;
236 c8dba1e6 2021-07-24 op req->p = req->servinfo;
237 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
241 c8dba1e6 2021-07-24 op static struct req *
242 c8dba1e6 2021-07-24 op req_by_id(uint32_t id)
244 c8dba1e6 2021-07-24 op struct req *r;
246 c8dba1e6 2021-07-24 op if ((r = req_by_id_try(id)) == NULL)
251 c8dba1e6 2021-07-24 op static struct req *
252 c8dba1e6 2021-07-24 op req_by_id_try(uint32_t id)
254 c8dba1e6 2021-07-24 op struct req *r;
256 c8dba1e6 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
257 c8dba1e6 2021-07-24 op if (r->id == id)
265 c8dba1e6 2021-07-24 op close_conn(int fd, short ev, void *d)
267 c8dba1e6 2021-07-24 op struct req *req = d;
269 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
270 c8dba1e6 2021-07-24 op if (req->asrev != NULL)
271 c8dba1e6 2021-07-24 op event_asr_abort(req->asrev);
274 5b762f01 2021-07-24 op if (req->bev != NULL) {
275 5b762f01 2021-07-24 op bufferevent_free(req->bev);
276 5b762f01 2021-07-24 op req->bev = NULL;
279 c8dba1e6 2021-07-24 op if (req->ctx != NULL) {
280 c8dba1e6 2021-07-24 op switch (tls_close(req->ctx)) {
281 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
282 c8dba1e6 2021-07-24 op yield_r(req, close_conn, NULL);
284 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
285 c8dba1e6 2021-07-24 op yield_w(req, close_conn, NULL);
289 c8dba1e6 2021-07-24 op tls_free(req->ctx);
290 5b762f01 2021-07-24 op req->ctx = NULL;
293 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
294 c8dba1e6 2021-07-24 op if (req->fd != -1)
295 c8dba1e6 2021-07-24 op close(req->fd);
300 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
302 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
303 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
307 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
312 c8dba1e6 2021-07-24 op va_start(ap, fmt);
313 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
317 c8dba1e6 2021-07-24 op close_with_err(req, s);
322 c8dba1e6 2021-07-24 op setup_tls(struct req *req)
324 c8dba1e6 2021-07-24 op if ((req->ctx = tls_client()) == NULL) {
325 c8dba1e6 2021-07-24 op close_with_errf(req, "tls_client: %s", strerror(errno));
328 c8dba1e6 2021-07-24 op if (tls_configure(req->ctx, tlsconf) == -1) {
329 c8dba1e6 2021-07-24 op close_with_errf(req, "tls_configure: %s", tls_error(req->ctx));
332 c8dba1e6 2021-07-24 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
333 c8dba1e6 2021-07-24 op close_with_errf(req, "tls_connect_socket: %s", tls_error(req->ctx));
336 5b762f01 2021-07-24 op yield_w(req, net_tls_handshake, &timeout_for_handshake);
340 5b762f01 2021-07-24 op net_tls_handshake(int fd, short event, void *d)
342 c8dba1e6 2021-07-24 op struct req *req = d;
343 c8dba1e6 2021-07-24 op const char *hash;
345 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
346 c8dba1e6 2021-07-24 op close_with_err(req, "Timeout loading page");
350 c8dba1e6 2021-07-24 op switch (tls_handshake(req->ctx)) {
351 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
352 5b762f01 2021-07-24 op yield_r(req, net_tls_handshake, NULL);
354 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
355 5b762f01 2021-07-24 op yield_w(req, net_tls_handshake, NULL);
359 c8dba1e6 2021-07-24 op hash = tls_peer_cert_hash(req->ctx);
360 c8dba1e6 2021-07-24 op if (hash == NULL) {
361 c8dba1e6 2021-07-24 op close_with_errf(req, "handshake failed: %s", tls_error(req->ctx));
364 c8dba1e6 2021-07-24 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
368 5b762f01 2021-07-24 op net_tls_readcb(int fd, short event, void *d)
370 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
371 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
372 5b762f01 2021-07-24 op char buf[BUFSIZ];
373 5b762f01 2021-07-24 op int what = EVBUFFER_READ;
374 5b762f01 2021-07-24 op int howmuch = IBUF_READ_SIZE;
378 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
379 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
383 5b762f01 2021-07-24 op if (bufev->wm_read.high != 0)
384 5b762f01 2021-07-24 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
386 5b762f01 2021-07-24 op switch (ret = tls_read(req->ctx, buf, howmuch)) {
387 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
388 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
391 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
396 5b762f01 2021-07-24 op if (len == 0) {
397 5b762f01 2021-07-24 op what |= EVBUFFER_EOF;
401 5b762f01 2021-07-24 op if (evbuffer_add(bufev->input, buf, len) == -1) {
402 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
406 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
408 5b762f01 2021-07-24 op len = EVBUFFER_LENGTH(bufev->input);
409 5b762f01 2021-07-24 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
412 5b762f01 2021-07-24 op if (bufev->readcb != NULL)
413 5b762f01 2021-07-24 op (*bufev->readcb)(bufev, bufev->cbarg);
417 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
421 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
425 5b762f01 2021-07-24 op net_tls_writecb(int fd, short event, void *d)
427 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
428 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
431 5b762f01 2021-07-24 op short what = EVBUFFER_WRITE;
433 5b762f01 2021-07-24 op if (event & EV_TIMEOUT) {
434 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
438 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
439 5b762f01 2021-07-24 op ret = tls_write(req->ctx, EVBUFFER_DATA(bufev->output),
440 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output));
441 5b762f01 2021-07-24 op switch (ret) {
442 5b762f01 2021-07-24 op case TLS_WANT_POLLIN:
443 5b762f01 2021-07-24 op case TLS_WANT_POLLOUT:
446 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
450 5b762f01 2021-07-24 op evbuffer_drain(bufev->output, len);
453 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0)
454 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
456 5b762f01 2021-07-24 op if (bufev->writecb != NULL &&
457 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
458 5b762f01 2021-07-24 op (*bufev->writecb)(bufev, bufev->cbarg);
462 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
466 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
470 5b762f01 2021-07-24 op gemini_parse_reply(struct req *req, const char *header, size_t len)
473 5b762f01 2021-07-24 op const char *t;
478 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
481 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
482 5b762f01 2021-07-24 op if (header[2] != ' ')
485 5b762f01 2021-07-24 op t = header + 3;
487 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
488 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_META, req->id, t, strlen(t)+1);
490 5b762f01 2021-07-24 op bufferevent_disable(req->bev, EV_READ|EV_WRITE);
492 5b762f01 2021-07-24 op if (code < 20 || code >= 30)
493 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
497 5b762f01 2021-07-24 op /* called when we're ready to read/write */
499 5b762f01 2021-07-24 op net_ready(struct req *req)
501 5b762f01 2021-07-24 op req->bev = bufferevent_new(req->fd, net_read, net_write, net_error,
503 5b762f01 2021-07-24 op if (req->bev == NULL)
506 5b762f01 2021-07-24 op /* setup tls i/o layer */
507 5b762f01 2021-07-24 op if (req->ctx != NULL) {
508 5b762f01 2021-07-24 op event_set(&req->bev->ev_read, req->fd, EV_READ,
509 5b762f01 2021-07-24 op net_tls_readcb, req->bev);
510 5b762f01 2021-07-24 op event_set(&req->bev->ev_write, req->fd, EV_WRITE,
511 5b762f01 2021-07-24 op net_tls_writecb, req->bev);
514 5b762f01 2021-07-24 op /* TODO: adjust watermarks */
515 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_WRITE, 1, 0);
516 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_READ, 1, 0);
518 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ|EV_WRITE);
520 5b762f01 2021-07-24 op bufferevent_write(req->bev, req->req, req->len);
523 5b762f01 2021-07-24 op /* called after a read has been done */
525 5b762f01 2021-07-24 op net_read(struct bufferevent *bev, void *d)
527 5b762f01 2021-07-24 op struct req *req = d;
528 5b762f01 2021-07-24 op struct evbuffer *src = EVBUFFER_INPUT(bev);
532 5b762f01 2021-07-24 op char *header;
534 5b762f01 2021-07-24 op if (!req->done_header) {
535 5b762f01 2021-07-24 op header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
536 5b762f01 2021-07-24 op if (header == NULL && EVBUFFER_LENGTH(src) >= 1024)
538 5b762f01 2021-07-24 op if (header == NULL)
540 5b762f01 2021-07-24 op r = gemini_parse_reply(req, header, len);
541 5b762f01 2021-07-24 op free(header);
544 5b762f01 2021-07-24 op req->done_header = 1;
548 5b762f01 2021-07-24 op if ((len = EVBUFFER_LENGTH(src)) == 0)
550 5b762f01 2021-07-24 op data = EVBUFFER_DATA(src);
551 5b762f01 2021-07-24 op net_send_ui(IMSG_BUF, req->id, data, len);
552 5b762f01 2021-07-24 op evbuffer_drain(src, len);
556 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
559 5b762f01 2021-07-24 op /* called after a write has been done */
561 5b762f01 2021-07-24 op net_write(struct bufferevent *bev, void *d)
563 5b762f01 2021-07-24 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
565 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(dst) == 0)
566 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg);
570 5b762f01 2021-07-24 op net_error(struct bufferevent *bev, short error, void *d)
572 c8dba1e6 2021-07-24 op struct req *req = d;
574 5b762f01 2021-07-24 op if (error & EVBUFFER_TIMEOUT) {
575 5b762f01 2021-07-24 op close_with_err(req, "Timeout loading page");
579 5b762f01 2021-07-24 op if (error & EVBUFFER_ERROR) {
580 5b762f01 2021-07-24 op close_with_err(req, "buffer event error");
584 5b762f01 2021-07-24 op if (error & EVBUFFER_EOF) {
585 5b762f01 2021-07-24 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
586 5b762f01 2021-07-24 op close_conn(0, 0, req);
590 5b762f01 2021-07-24 op if (error & EVBUFFER_WRITE) {
591 5b762f01 2021-07-24 op /* finished sending request */
592 5b762f01 2021-07-24 op bufferevent_disable(bev, EV_WRITE);
596 5b762f01 2021-07-24 op if (error & EVBUFFER_READ) {
597 5b762f01 2021-07-24 op close_with_err(req, "protocol error");
601 5b762f01 2021-07-24 op close_with_errf(req, "unknown event error %x", error);
605 c8dba1e6 2021-07-24 op handle_get_raw(struct imsg *imsg, size_t datalen)
607 c8dba1e6 2021-07-24 op struct req *req;
608 c8dba1e6 2021-07-24 op struct get_req *r;
610 c8dba1e6 2021-07-24 op r = imsg->data;
612 c8dba1e6 2021-07-24 op if (datalen != sizeof(*r))
615 c8dba1e6 2021-07-24 op if ((req = calloc(1, sizeof(*req))) == NULL)
618 c8dba1e6 2021-07-24 op req->id = imsg->hdr.peerid;
619 c8dba1e6 2021-07-24 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
621 c8dba1e6 2021-07-24 op strlcpy(req->url.host, r->host, sizeof(req->url.host));
622 c8dba1e6 2021-07-24 op strlcpy(req->url.port, r->port, sizeof(req->url.port));
624 5b762f01 2021-07-24 op strlcpy(req->req, r->req, sizeof(req->req));
625 5b762f01 2021-07-24 op req->len = strlen(r->req);
627 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
628 c8dba1e6 2021-07-24 op async_conn_towards(req);
630 c8dba1e6 2021-07-24 op blocking_conn_towards(req);
635 c8dba1e6 2021-07-24 op handle_cert_status(struct imsg *imsg, size_t datalen)
637 c8dba1e6 2021-07-24 op struct req *req;
640 c8dba1e6 2021-07-24 op req = req_by_id(imsg->hdr.peerid);
642 c8dba1e6 2021-07-24 op if (datalen < sizeof(is_ok))
644 c8dba1e6 2021-07-24 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
647 5b762f01 2021-07-24 op net_ready(req);
649 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
653 c8dba1e6 2021-07-24 op handle_proceed(struct imsg *imsg, size_t datalen)
655 5b762f01 2021-07-24 op struct req *req;
657 5b762f01 2021-07-24 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
660 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ);
664 c8dba1e6 2021-07-24 op handle_stop(struct imsg *imsg, size_t datalen)
666 c8dba1e6 2021-07-24 op struct req *req;
668 c8dba1e6 2021-07-24 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
670 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
674 c8dba1e6 2021-07-24 op handle_quit(struct imsg *imsg, size_t datalen)
676 c8dba1e6 2021-07-24 op event_loopbreak();
680 c8dba1e6 2021-07-24 op handle_dispatch_imsg(int fd, short ev, void *d)
682 c8dba1e6 2021-07-24 op struct imsgev *iev = d;
684 c8dba1e6 2021-07-24 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
685 c8dba1e6 2021-07-24 op err(1, "connection closed");
689 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
690 c8dba1e6 2021-07-24 op uint16_t datalen)
692 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
693 c8dba1e6 2021-07-24 op data, datalen);
697 f45bd2e3 2021-07-24 op net_main(void)
699 c8dba1e6 2021-07-24 op setproctitle("net");
701 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
703 c8dba1e6 2021-07-24 op if ((tlsconf = tls_config_new()) == NULL)
705 c8dba1e6 2021-07-24 op tls_config_insecure_noverifycert(tlsconf);
706 c8dba1e6 2021-07-24 op tls_config_insecure_noverifyname(tlsconf);
708 c8dba1e6 2021-07-24 op event_init();
710 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
711 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
713 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
714 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
715 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
716 c8dba1e6 2021-07-24 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
717 c8dba1e6 2021-07-24 op iev_ui->handler, iev_ui);
718 c8dba1e6 2021-07-24 op event_add(&iev_ui->ev, NULL);
720 c8dba1e6 2021-07-24 op sandbox_net_process();
722 c8dba1e6 2021-07-24 op event_dispatch();
724 74c0f6ba 2021-07-24 op tls_config_free(tlsconf);
725 74c0f6ba 2021-07-24 op msgbuf_clear(&iev_ui->ibuf.w);
726 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
727 74c0f6ba 2021-07-24 op free(iev_ui);