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;
44 edfe87be 2021-07-24 op /* a pending request */
46 edfe87be 2021-07-24 op struct phos_uri url;
49 edfe87be 2021-07-24 op struct tls *ctx;
50 edfe87be 2021-07-24 op char req[1024];
52 edfe87be 2021-07-24 op int done_header;
53 edfe87be 2021-07-24 op struct bufferevent *bev;
55 edfe87be 2021-07-24 op struct addrinfo *servinfo, *p;
56 edfe87be 2021-07-24 op #if HAVE_ASR_RUN
57 edfe87be 2021-07-24 op struct addrinfo hints;
58 edfe87be 2021-07-24 op struct event_asr *asrev;
61 edfe87be 2021-07-24 op TAILQ_ENTRY(req) reqs;
64 e2a349f7 2021-07-24 op static struct req *req_by_id(uint32_t);
66 77ae5e91 2021-07-24 op static void die(void) __attribute__((__noreturn__));
68 77ae5e91 2021-07-24 op static void try_to_connect(int, short, void*);
70 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
71 77ae5e91 2021-07-24 op static void query_done(struct asr_result*, void*);
72 77ae5e91 2021-07-24 op static void async_conn_towards(struct req*);
74 77ae5e91 2021-07-24 op static void blocking_conn_towards(struct req*);
77 77ae5e91 2021-07-24 op static void close_with_err(struct req*, const char*);
78 77ae5e91 2021-07-24 op static void close_with_errf(struct req*, const char*, ...)
79 bfb9acb0 2021-07-24 op __attribute__((format(printf, 2, 3)));
81 77ae5e91 2021-07-24 op static void net_tls_handshake(int, short, void *);
82 77ae5e91 2021-07-24 op static void net_tls_readcb(int, short, void *);
83 77ae5e91 2021-07-24 op static void net_tls_writecb(int, short, void *);
85 77ae5e91 2021-07-24 op static int gemini_parse_reply(struct req *, const char *, size_t);
87 77ae5e91 2021-07-24 op static void net_ready(struct req *req);
88 77ae5e91 2021-07-24 op static void net_read(struct bufferevent *, void *);
89 77ae5e91 2021-07-24 op static void net_write(struct bufferevent *, void *);
90 77ae5e91 2021-07-24 op static void net_error(struct bufferevent *, short, void *);
92 77ae5e91 2021-07-24 op static void handle_get_raw(struct imsg *, size_t);
93 77ae5e91 2021-07-24 op static void handle_cert_status(struct imsg*, size_t);
94 77ae5e91 2021-07-24 op static void handle_proceed(struct imsg*, size_t);
95 77ae5e91 2021-07-24 op static void handle_stop(struct imsg*, size_t);
96 77ae5e91 2021-07-24 op static void handle_quit(struct imsg*, size_t);
97 77ae5e91 2021-07-24 op static void handle_dispatch_imsg(int, short, void*);
99 77ae5e91 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
101 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
102 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
104 c8dba1e6 2021-07-24 op static imsg_handlerfn *handlers[] = {
105 c8dba1e6 2021-07-24 op [IMSG_GET_RAW] = handle_get_raw,
106 c8dba1e6 2021-07-24 op [IMSG_CERT_STATUS] = handle_cert_status,
107 c8dba1e6 2021-07-24 op [IMSG_PROCEED] = handle_proceed,
108 c8dba1e6 2021-07-24 op [IMSG_STOP] = handle_stop,
109 c8dba1e6 2021-07-24 op [IMSG_QUIT] = handle_quit,
112 c8dba1e6 2021-07-24 op typedef void (*statefn)(int, short, void*);
114 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
116 c8dba1e6 2021-07-24 op static inline void
117 c8dba1e6 2021-07-24 op yield_r(struct req *req, statefn fn, struct timeval *tv)
119 c8dba1e6 2021-07-24 op event_once(req->fd, EV_READ, fn, req, tv);
122 c8dba1e6 2021-07-24 op static inline void
123 c8dba1e6 2021-07-24 op yield_w(struct req *req, statefn fn, struct timeval *tv)
125 c8dba1e6 2021-07-24 op event_once(req->fd, EV_WRITE, fn, req, tv);
128 e2a349f7 2021-07-24 op static struct req *
129 e2a349f7 2021-07-24 op req_by_id(uint32_t id)
131 e2a349f7 2021-07-24 op struct req *r;
133 e2a349f7 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
134 e2a349f7 2021-07-24 op if (r->id == id)
141 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
144 c8dba1e6 2021-07-24 op abort(); /* TODO */
148 c8dba1e6 2021-07-24 op try_to_connect(int fd, short ev, void *d)
150 c8dba1e6 2021-07-24 op struct req *req = d;
151 c8dba1e6 2021-07-24 op int error = 0;
152 c8dba1e6 2021-07-24 op socklen_t len = sizeof(error);
155 c8dba1e6 2021-07-24 op if (req->p == NULL)
158 c8dba1e6 2021-07-24 op if (req->fd != -1) {
159 bfb9acb0 2021-07-24 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
162 c8dba1e6 2021-07-24 op if (error != 0) {
163 c8dba1e6 2021-07-24 op errno = error;
169 bfb9acb0 2021-07-24 op req->fd = socket(req->p->ai_family, req->p->ai_socktype,
170 bfb9acb0 2021-07-24 op req->p->ai_protocol);
171 c8dba1e6 2021-07-24 op if (req->fd == -1) {
172 c8dba1e6 2021-07-24 op req->p = req->p->ai_next;
175 c8dba1e6 2021-07-24 op mark_nonblock(req->fd);
176 c8dba1e6 2021-07-24 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
178 c8dba1e6 2021-07-24 op yield_w(req, try_to_connect, NULL);
183 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
184 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to connect to %s",
185 c8dba1e6 2021-07-24 op req->url.host);
189 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
191 0d40acb1 2021-07-24 op /* prepare tls */
192 0d40acb1 2021-07-24 op if ((req->ctx = tls_client()) == NULL) {
193 0d40acb1 2021-07-24 op close_with_errf(req, "tls_client: %s", strerror(errno));
196 0d40acb1 2021-07-24 op if (tls_configure(req->ctx, tlsconf) == -1) {
197 0d40acb1 2021-07-24 op close_with_errf(req, "tls_configure: %s", tls_error(req->ctx));
200 0d40acb1 2021-07-24 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
201 0d40acb1 2021-07-24 op close_with_errf(req, "tls_connect_socket: %s",
202 0d40acb1 2021-07-24 op tls_error(req->ctx));
205 0d40acb1 2021-07-24 op yield_w(req, net_tls_handshake, &timeout_for_handshake);
208 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
210 c8dba1e6 2021-07-24 op query_done(struct asr_result *res, void *d)
212 c8dba1e6 2021-07-24 op struct req *req = d;
214 c8dba1e6 2021-07-24 op req->asrev = NULL;
215 c8dba1e6 2021-07-24 op if (res->ar_gai_errno != 0) {
216 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
217 c8dba1e6 2021-07-24 op req->url.host, gai_strerror(res->ar_gai_errno));
221 c8dba1e6 2021-07-24 op req->fd = -1;
222 c8dba1e6 2021-07-24 op req->servinfo = res->ar_addrinfo;
223 c8dba1e6 2021-07-24 op req->p = res->ar_addrinfo;
224 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
228 c8dba1e6 2021-07-24 op async_conn_towards(struct req *req)
230 c8dba1e6 2021-07-24 op struct asr_query *q;
231 c8dba1e6 2021-07-24 op const char *proto = "1965";
233 c8dba1e6 2021-07-24 op if (*req->url.port != '\0')
234 c8dba1e6 2021-07-24 op proto = req->url.port;
236 c8dba1e6 2021-07-24 op req->hints.ai_family = AF_UNSPEC;
237 c8dba1e6 2021-07-24 op req->hints.ai_socktype = SOCK_STREAM;
238 c8dba1e6 2021-07-24 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
239 c8dba1e6 2021-07-24 op req->asrev = event_asr_run(q, query_done, req);
243 c8dba1e6 2021-07-24 op blocking_conn_towards(struct req *req)
245 c8dba1e6 2021-07-24 op struct addrinfo hints;
246 c8dba1e6 2021-07-24 op struct phos_uri *url = &req->url;
248 c8dba1e6 2021-07-24 op const char *proto = "1965";
250 c8dba1e6 2021-07-24 op if (*url->port != '\0')
251 c8dba1e6 2021-07-24 op proto = url->port;
253 c8dba1e6 2021-07-24 op memset(&hints, 0, sizeof(hints));
254 c8dba1e6 2021-07-24 op hints.ai_family = AF_UNSPEC;
255 c8dba1e6 2021-07-24 op hints.ai_socktype = SOCK_STREAM;
257 c8dba1e6 2021-07-24 op if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
258 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
259 c8dba1e6 2021-07-24 op url->host, gai_strerror(status));
263 c8dba1e6 2021-07-24 op req->fd = -1;
264 c8dba1e6 2021-07-24 op req->p = req->servinfo;
265 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
270 c8dba1e6 2021-07-24 op close_conn(int fd, short ev, void *d)
272 c8dba1e6 2021-07-24 op struct req *req = d;
274 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
275 c8dba1e6 2021-07-24 op if (req->asrev != NULL)
276 c8dba1e6 2021-07-24 op event_asr_abort(req->asrev);
279 5b762f01 2021-07-24 op if (req->bev != NULL) {
280 5b762f01 2021-07-24 op bufferevent_free(req->bev);
281 5b762f01 2021-07-24 op req->bev = NULL;
284 c8dba1e6 2021-07-24 op if (req->ctx != NULL) {
285 c8dba1e6 2021-07-24 op switch (tls_close(req->ctx)) {
286 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
287 c8dba1e6 2021-07-24 op yield_r(req, close_conn, NULL);
289 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
290 c8dba1e6 2021-07-24 op yield_w(req, close_conn, NULL);
294 c8dba1e6 2021-07-24 op tls_free(req->ctx);
295 5b762f01 2021-07-24 op req->ctx = NULL;
298 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
299 c8dba1e6 2021-07-24 op if (req->fd != -1)
300 c8dba1e6 2021-07-24 op close(req->fd);
305 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
307 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
308 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
312 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
317 c8dba1e6 2021-07-24 op va_start(ap, fmt);
318 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
322 c8dba1e6 2021-07-24 op close_with_err(req, s);
327 5b762f01 2021-07-24 op net_tls_handshake(int fd, short event, void *d)
329 c8dba1e6 2021-07-24 op struct req *req = d;
330 c8dba1e6 2021-07-24 op const char *hash;
332 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
333 c8dba1e6 2021-07-24 op close_with_err(req, "Timeout loading page");
337 c8dba1e6 2021-07-24 op switch (tls_handshake(req->ctx)) {
338 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
339 5b762f01 2021-07-24 op yield_r(req, net_tls_handshake, NULL);
341 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
342 5b762f01 2021-07-24 op yield_w(req, net_tls_handshake, NULL);
346 c8dba1e6 2021-07-24 op hash = tls_peer_cert_hash(req->ctx);
347 c8dba1e6 2021-07-24 op if (hash == NULL) {
348 bfb9acb0 2021-07-24 op close_with_errf(req, "handshake failed: %s",
349 bfb9acb0 2021-07-24 op tls_error(req->ctx));
352 c8dba1e6 2021-07-24 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
356 5b762f01 2021-07-24 op net_tls_readcb(int fd, short event, void *d)
358 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
359 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
360 5b762f01 2021-07-24 op char buf[BUFSIZ];
361 5b762f01 2021-07-24 op int what = EVBUFFER_READ;
362 5b762f01 2021-07-24 op int howmuch = IBUF_READ_SIZE;
366 5b762f01 2021-07-24 op if (event == EV_TIMEOUT) {
367 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
371 5b762f01 2021-07-24 op if (bufev->wm_read.high != 0)
372 5b762f01 2021-07-24 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
374 5b762f01 2021-07-24 op switch (ret = tls_read(req->ctx, buf, howmuch)) {
375 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
376 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
379 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
384 5b762f01 2021-07-24 op if (len == 0) {
385 5b762f01 2021-07-24 op what |= EVBUFFER_EOF;
389 5b762f01 2021-07-24 op if (evbuffer_add(bufev->input, buf, len) == -1) {
390 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
394 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
396 5b762f01 2021-07-24 op len = EVBUFFER_LENGTH(bufev->input);
397 5b762f01 2021-07-24 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
400 5b762f01 2021-07-24 op if (bufev->readcb != NULL)
401 5b762f01 2021-07-24 op (*bufev->readcb)(bufev, bufev->cbarg);
405 5b762f01 2021-07-24 op event_add(&bufev->ev_read, NULL);
409 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
413 5b762f01 2021-07-24 op net_tls_writecb(int fd, short event, void *d)
415 5b762f01 2021-07-24 op struct bufferevent *bufev = d;
416 5b762f01 2021-07-24 op struct req *req = bufev->cbarg;
419 5b762f01 2021-07-24 op short what = EVBUFFER_WRITE;
421 5b762f01 2021-07-24 op if (event & EV_TIMEOUT) {
422 5b762f01 2021-07-24 op what |= EVBUFFER_TIMEOUT;
426 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
427 5b762f01 2021-07-24 op ret = tls_write(req->ctx, EVBUFFER_DATA(bufev->output),
428 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output));
429 5b762f01 2021-07-24 op switch (ret) {
430 5b762f01 2021-07-24 op case TLS_WANT_POLLIN:
431 5b762f01 2021-07-24 op case TLS_WANT_POLLOUT:
434 5b762f01 2021-07-24 op what |= EVBUFFER_ERROR;
438 5b762f01 2021-07-24 op evbuffer_drain(bufev->output, len);
441 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(bufev->output) != 0)
442 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
444 5b762f01 2021-07-24 op if (bufev->writecb != NULL &&
445 5b762f01 2021-07-24 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
446 5b762f01 2021-07-24 op (*bufev->writecb)(bufev, bufev->cbarg);
450 5b762f01 2021-07-24 op event_add(&bufev->ev_write, NULL);
454 5b762f01 2021-07-24 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
458 5b762f01 2021-07-24 op gemini_parse_reply(struct req *req, const char *header, size_t len)
461 5b762f01 2021-07-24 op const char *t;
466 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
469 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
470 5b762f01 2021-07-24 op if (header[2] != ' ')
473 5b762f01 2021-07-24 op t = header + 3;
475 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
476 5b762f01 2021-07-24 op net_send_ui(IMSG_GOT_META, req->id, t, strlen(t)+1);
478 5b762f01 2021-07-24 op bufferevent_disable(req->bev, EV_READ|EV_WRITE);
480 5b762f01 2021-07-24 op if (code < 20 || code >= 30)
481 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
485 5b762f01 2021-07-24 op /* called when we're ready to read/write */
487 5b762f01 2021-07-24 op net_ready(struct req *req)
489 5b762f01 2021-07-24 op req->bev = bufferevent_new(req->fd, net_read, net_write, net_error,
491 5b762f01 2021-07-24 op if (req->bev == NULL)
494 5b762f01 2021-07-24 op /* setup tls i/o layer */
495 5b762f01 2021-07-24 op if (req->ctx != NULL) {
496 5b762f01 2021-07-24 op event_set(&req->bev->ev_read, req->fd, EV_READ,
497 5b762f01 2021-07-24 op net_tls_readcb, req->bev);
498 5b762f01 2021-07-24 op event_set(&req->bev->ev_write, req->fd, EV_WRITE,
499 5b762f01 2021-07-24 op net_tls_writecb, req->bev);
502 5b762f01 2021-07-24 op /* TODO: adjust watermarks */
503 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_WRITE, 1, 0);
504 5b762f01 2021-07-24 op bufferevent_setwatermark(req->bev, EV_READ, 1, 0);
506 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ|EV_WRITE);
508 5b762f01 2021-07-24 op bufferevent_write(req->bev, req->req, req->len);
511 5b762f01 2021-07-24 op /* called after a read has been done */
513 5b762f01 2021-07-24 op net_read(struct bufferevent *bev, void *d)
515 5b762f01 2021-07-24 op struct req *req = d;
516 5b762f01 2021-07-24 op struct evbuffer *src = EVBUFFER_INPUT(bev);
520 5b762f01 2021-07-24 op char *header;
522 5b762f01 2021-07-24 op if (!req->done_header) {
523 5b762f01 2021-07-24 op header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
524 5b762f01 2021-07-24 op if (header == NULL && EVBUFFER_LENGTH(src) >= 1024)
526 5b762f01 2021-07-24 op if (header == NULL)
528 5b762f01 2021-07-24 op r = gemini_parse_reply(req, header, len);
529 5b762f01 2021-07-24 op free(header);
532 5b762f01 2021-07-24 op req->done_header = 1;
536 5b762f01 2021-07-24 op if ((len = EVBUFFER_LENGTH(src)) == 0)
538 5b762f01 2021-07-24 op data = EVBUFFER_DATA(src);
539 5b762f01 2021-07-24 op net_send_ui(IMSG_BUF, req->id, data, len);
540 5b762f01 2021-07-24 op evbuffer_drain(src, len);
544 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
547 5b762f01 2021-07-24 op /* called after a write has been done */
549 5b762f01 2021-07-24 op net_write(struct bufferevent *bev, void *d)
551 5b762f01 2021-07-24 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
553 5b762f01 2021-07-24 op if (EVBUFFER_LENGTH(dst) == 0)
554 5b762f01 2021-07-24 op (*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg);
558 5b762f01 2021-07-24 op net_error(struct bufferevent *bev, short error, void *d)
560 c8dba1e6 2021-07-24 op struct req *req = d;
562 5b762f01 2021-07-24 op if (error & EVBUFFER_TIMEOUT) {
563 5b762f01 2021-07-24 op close_with_err(req, "Timeout loading page");
567 5b762f01 2021-07-24 op if (error & EVBUFFER_ERROR) {
568 5b762f01 2021-07-24 op close_with_err(req, "buffer event error");
572 5b762f01 2021-07-24 op if (error & EVBUFFER_EOF) {
573 5b762f01 2021-07-24 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
574 5b762f01 2021-07-24 op close_conn(0, 0, req);
578 5b762f01 2021-07-24 op if (error & EVBUFFER_WRITE) {
579 5b762f01 2021-07-24 op /* finished sending request */
580 5b762f01 2021-07-24 op bufferevent_disable(bev, EV_WRITE);
584 5b762f01 2021-07-24 op if (error & EVBUFFER_READ) {
585 5b762f01 2021-07-24 op close_with_err(req, "protocol error");
589 5b762f01 2021-07-24 op close_with_errf(req, "unknown event error %x", error);
593 c8dba1e6 2021-07-24 op handle_get_raw(struct imsg *imsg, size_t datalen)
595 c8dba1e6 2021-07-24 op struct req *req;
596 c8dba1e6 2021-07-24 op struct get_req *r;
598 c8dba1e6 2021-07-24 op r = imsg->data;
600 c8dba1e6 2021-07-24 op if (datalen != sizeof(*r))
603 c8dba1e6 2021-07-24 op if ((req = calloc(1, sizeof(*req))) == NULL)
606 c8dba1e6 2021-07-24 op req->id = imsg->hdr.peerid;
607 c8dba1e6 2021-07-24 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
609 c8dba1e6 2021-07-24 op strlcpy(req->url.host, r->host, sizeof(req->url.host));
610 c8dba1e6 2021-07-24 op strlcpy(req->url.port, r->port, sizeof(req->url.port));
612 5b762f01 2021-07-24 op strlcpy(req->req, r->req, sizeof(req->req));
613 5b762f01 2021-07-24 op req->len = strlen(r->req);
615 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
616 c8dba1e6 2021-07-24 op async_conn_towards(req);
618 c8dba1e6 2021-07-24 op blocking_conn_towards(req);
623 c8dba1e6 2021-07-24 op handle_cert_status(struct imsg *imsg, size_t datalen)
625 c8dba1e6 2021-07-24 op struct req *req;
628 c8dba1e6 2021-07-24 op req = req_by_id(imsg->hdr.peerid);
630 c8dba1e6 2021-07-24 op if (datalen < sizeof(is_ok))
632 c8dba1e6 2021-07-24 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
635 5b762f01 2021-07-24 op net_ready(req);
637 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
641 c8dba1e6 2021-07-24 op handle_proceed(struct imsg *imsg, size_t datalen)
643 5b762f01 2021-07-24 op struct req *req;
645 318a6c1a 2021-07-24 op if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
648 5b762f01 2021-07-24 op bufferevent_enable(req->bev, EV_READ);
652 c8dba1e6 2021-07-24 op handle_stop(struct imsg *imsg, size_t datalen)
654 c8dba1e6 2021-07-24 op struct req *req;
656 318a6c1a 2021-07-24 op if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
658 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
662 c8dba1e6 2021-07-24 op handle_quit(struct imsg *imsg, size_t datalen)
664 c8dba1e6 2021-07-24 op event_loopbreak();
668 c8dba1e6 2021-07-24 op handle_dispatch_imsg(int fd, short ev, void *d)
670 c8dba1e6 2021-07-24 op struct imsgev *iev = d;
672 c8dba1e6 2021-07-24 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
673 c8dba1e6 2021-07-24 op err(1, "connection closed");
677 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
678 c8dba1e6 2021-07-24 op uint16_t datalen)
680 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
681 c8dba1e6 2021-07-24 op data, datalen);
685 f45bd2e3 2021-07-24 op net_main(void)
687 c8dba1e6 2021-07-24 op setproctitle("net");
689 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
691 c8dba1e6 2021-07-24 op if ((tlsconf = tls_config_new()) == NULL)
693 c8dba1e6 2021-07-24 op tls_config_insecure_noverifycert(tlsconf);
694 c8dba1e6 2021-07-24 op tls_config_insecure_noverifyname(tlsconf);
696 c8dba1e6 2021-07-24 op event_init();
698 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
699 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
701 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
702 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
703 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
704 c8dba1e6 2021-07-24 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
705 c8dba1e6 2021-07-24 op iev_ui->handler, iev_ui);
706 c8dba1e6 2021-07-24 op event_add(&iev_ui->ev, NULL);
708 c8dba1e6 2021-07-24 op sandbox_net_process();
710 c8dba1e6 2021-07-24 op event_dispatch();
712 74c0f6ba 2021-07-24 op tls_config_free(tlsconf);
713 74c0f6ba 2021-07-24 op msgbuf_clear(&iev_ui->ibuf.w);
714 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
715 74c0f6ba 2021-07-24 op free(iev_ui);