2 5657662f 2024-01-16 op * Copyright (c) 2021, 2024 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 d35e18b3 2024-02-04 op #include <sys/mman.h>
20 c8dba1e6 2021-07-24 op #include <sys/types.h>
21 c8dba1e6 2021-07-24 op #include <sys/socket.h>
22 d35e18b3 2024-02-04 op #include <sys/stat.h>
24 c8dba1e6 2021-07-24 op #include <netinet/in.h>
26 c8dba1e6 2021-07-24 op #include <ctype.h>
27 c8dba1e6 2021-07-24 op #include <errno.h>
28 c8dba1e6 2021-07-24 op #include <netdb.h>
29 c8dba1e6 2021-07-24 op #include <stdarg.h>
30 c8dba1e6 2021-07-24 op #include <stdio.h>
31 c8dba1e6 2021-07-24 op #include <stdlib.h>
32 c8dba1e6 2021-07-24 op #include <string.h>
33 c8dba1e6 2021-07-24 op #include <tls.h>
34 c8dba1e6 2021-07-24 op #include <unistd.h>
36 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
37 c8dba1e6 2021-07-24 op # include <asr.h>
40 98d3e6c1 2024-02-18 op #include "bufio.h"
41 98d3e6c1 2024-02-18 op #include "ev.h"
42 87d297d1 2024-02-22 op #include "imsgev.h"
43 c8dba1e6 2021-07-24 op #include "telescope.h"
44 9d65b1d9 2022-01-11 op #include "utils.h"
45 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
47 c8dba1e6 2021-07-24 op static struct imsgev *iev_ui;
49 98d3e6c1 2024-02-18 op enum conn_state {
50 98d3e6c1 2024-02-18 op CONN_CONNECTING,
51 98d3e6c1 2024-02-18 op CONN_HANDSHAKE,
58 edfe87be 2021-07-24 op /* a pending request */
61 98d3e6c1 2024-02-18 op enum conn_state state;
69 d35e18b3 2024-02-04 op size_t ccert_len;
73 62d3cd29 2024-02-22 op unsigned int timer;
74 98d3e6c1 2024-02-18 op struct bufio bio;
76 c6287992 2024-02-13 op int conn_error;
77 c6287992 2024-02-13 op const char *cause;
79 edfe87be 2021-07-24 op struct addrinfo *servinfo, *p;
80 edfe87be 2021-07-24 op #if HAVE_ASR_RUN
81 10884685 2024-02-22 op struct asr_query *q;
85 edfe87be 2021-07-24 op TAILQ_ENTRY(req) reqs;
88 e2a349f7 2021-07-24 op static struct req *req_by_id(uint32_t);
90 77ae5e91 2021-07-24 op static void die(void) __attribute__((__noreturn__));
92 77ae5e91 2021-07-24 op static void close_with_err(struct req*, const char*);
93 77ae5e91 2021-07-24 op static void close_with_errf(struct req*, const char*, ...)
94 bfb9acb0 2021-07-24 op __attribute__((format(printf, 2, 3)));
96 98d3e6c1 2024-02-18 op static int try_to_connect(struct req *);
97 5f285272 2024-03-25 op static int gemini_parse_reply(struct req *, const char *);
98 98d3e6c1 2024-02-18 op static void net_ev(int, int, void *);
99 98d3e6c1 2024-02-18 op static void handle_dispatch_imsg(int, int, void*);
101 77ae5e91 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
103 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
104 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
106 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
108 e2a349f7 2021-07-24 op static struct req *
109 e2a349f7 2021-07-24 op req_by_id(uint32_t id)
111 e2a349f7 2021-07-24 op struct req *r;
113 e2a349f7 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
114 e2a349f7 2021-07-24 op if (r->id == id)
121 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
124 c8dba1e6 2021-07-24 op abort(); /* TODO */
127 ce023433 2024-02-22 op static inline int
128 ce023433 2024-02-22 op req_bio_ev(struct req *req)
133 ce023433 2024-02-22 op ev = bufio_ev(&req->bio);
134 ce023433 2024-02-22 op if (ev & BUFIO_WANT_READ)
135 ce023433 2024-02-22 op ret |= EV_READ;
136 ce023433 2024-02-22 op if (ev & BUFIO_WANT_WRITE)
137 ce023433 2024-02-22 op ret |= EV_WRITE;
138 ce023433 2024-02-22 op return (ret);
142 98d3e6c1 2024-02-18 op close_conn(int fd, int ev, void *d)
144 c8dba1e6 2021-07-24 op struct req *req = d;
146 98d3e6c1 2024-02-18 op if (req->state != CONN_ERROR)
147 98d3e6c1 2024-02-18 op req->state = CONN_CLOSE;
149 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
150 10884685 2024-02-22 op if (req->q) {
151 10884685 2024-02-22 op asr_abort(req->q);
152 10884685 2024-02-22 op ev_del(req->ar_fd);
156 62d3cd29 2024-02-22 op if (req->timer != 0) {
157 62d3cd29 2024-02-22 op ev_timer_cancel(req->timer);
158 62d3cd29 2024-02-22 op req->timer = 0;
161 98d3e6c1 2024-02-18 op if (req->state == CONN_CLOSE &&
162 99ebdacb 2024-02-18 op req->fd != -1 &&
163 98d3e6c1 2024-02-18 op bufio_close(&req->bio) == -1 &&
164 98d3e6c1 2024-02-18 op errno == EAGAIN) {
165 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), close_conn, req);
169 98d3e6c1 2024-02-18 op if (req->servinfo)
170 98d3e6c1 2024-02-18 op freeaddrinfo(req->servinfo);
172 98d3e6c1 2024-02-18 op bufio_free(&req->bio);
174 d35e18b3 2024-02-04 op if (req->ccert != NULL) {
175 d35e18b3 2024-02-04 op munmap(req->ccert, req->ccert_len);
176 d35e18b3 2024-02-04 op close(req->ccert_fd);
179 5657662f 2024-01-16 op free(req->host);
180 5657662f 2024-01-16 op free(req->port);
181 5657662f 2024-01-16 op free(req->req);
183 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
184 98d3e6c1 2024-02-18 op if (req->fd != -1) {
185 98d3e6c1 2024-02-18 op ev_del(req->fd);
186 c8dba1e6 2021-07-24 op close(req->fd);
192 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
194 98d3e6c1 2024-02-18 op req->state = CONN_ERROR;
195 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
196 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
200 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
205 c8dba1e6 2021-07-24 op va_start(ap, fmt);
206 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
210 c8dba1e6 2021-07-24 op close_with_err(req, s);
214 10884685 2024-02-22 op #if HAVE_ASR_RUN
216 10884685 2024-02-22 op req_resolve(int fd, int ev, void *d)
218 10884685 2024-02-22 op struct req *req = d;
219 10884685 2024-02-22 op struct addrinfo hints;
220 10884685 2024-02-22 op struct asr_result ar;
221 10884685 2024-02-22 op struct timeval tv;
223 10884685 2024-02-22 op if (req->q == NULL) {
224 10884685 2024-02-22 op memset(&hints, 0, sizeof(hints));
225 10884685 2024-02-22 op hints.ai_family = AF_UNSPEC;
226 10884685 2024-02-22 op hints.ai_socktype = SOCK_STREAM;
228 10884685 2024-02-22 op req->q = getaddrinfo_async(req->host, req->port, &hints, NULL);
229 10884685 2024-02-22 op if (req->q == NULL) {
230 10884685 2024-02-22 op close_with_errf(req, "getaddrinfo_async: %s",
231 10884685 2024-02-22 op strerror(errno));
236 10884685 2024-02-22 op if (fd != -1)
238 62d3cd29 2024-02-22 op if (req->timer) {
239 62d3cd29 2024-02-22 op ev_timer_cancel(req->timer);
240 62d3cd29 2024-02-22 op req->timer = 0;
243 10884685 2024-02-22 op if (asr_run(req->q, &ar) == 0) {
245 10884685 2024-02-22 op if (ar.ar_cond & ASR_WANT_READ)
246 10884685 2024-02-22 op ev |= EV_READ;
247 10884685 2024-02-22 op if (ar.ar_cond & ASR_WANT_WRITE)
248 10884685 2024-02-22 op ev |= EV_WRITE;
250 10884685 2024-02-22 op req->ar_fd = ar.ar_fd;
251 10884685 2024-02-22 op if (ev_add(req->ar_fd, ev, req_resolve, req) == -1) {
252 10884685 2024-02-22 op close_with_errf(req, "ev_add failure: %s",
253 10884685 2024-02-22 op strerror(errno));
257 10884685 2024-02-22 op tv.tv_sec = ar.ar_timeout / 1000;
258 10884685 2024-02-22 op tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
259 62d3cd29 2024-02-22 op req->timer = ev_timer(&tv, req_resolve, req);
260 62d3cd29 2024-02-22 op if (req->timer == 0)
261 10884685 2024-02-22 op close_with_errf(req, "ev_timer failure: %s",
262 10884685 2024-02-22 op strerror(errno));
266 10884685 2024-02-22 op req->ar_fd = -1;
267 10884685 2024-02-22 op req->q = NULL;
269 10884685 2024-02-22 op if (ar.ar_gai_errno) {
270 10884685 2024-02-22 op close_with_errf(req, "failed to resolve %s: %s",
271 10884685 2024-02-22 op req->host, gai_strerror(ar.ar_gai_errno));
275 10884685 2024-02-22 op req->servinfo = ar.ar_addrinfo;
277 10884685 2024-02-22 op req->p = req->servinfo;
278 10884685 2024-02-22 op net_ev(-1, EV_READ, req);
282 10884685 2024-02-22 op req_resolve(int fd, int ev, struct req *req)
284 10884685 2024-02-22 op struct addrinfo hints;
287 10884685 2024-02-22 op memset(&hints, 0, sizeof(hints));
288 10884685 2024-02-22 op hints.ai_family = AF_UNSPEC;
289 10884685 2024-02-22 op hints.ai_socktype = SOCK_STREAM;
291 10884685 2024-02-22 op s = getaddrinfo(req->host, req->port, &hints, &req->servinfo);
292 10884685 2024-02-22 op if (s != 0) {
293 10884685 2024-02-22 op close_with_errf(req, "failed to resolve %s: %s",
294 10884685 2024-02-22 op req->host, gai_strerror(s));
298 10884685 2024-02-22 op req->fd = -1;
299 10884685 2024-02-22 op req->p = req->servinfo;
300 10884685 2024-02-22 op net_ev(-1, EV_READ, req);
305 98d3e6c1 2024-02-18 op try_to_connect(struct req *req)
308 98d3e6c1 2024-02-18 op socklen_t len = sizeof(error);
311 98d3e6c1 2024-02-18 op if (req->p == NULL)
314 98d3e6c1 2024-02-18 op if (req->fd != -1) {
315 98d3e6c1 2024-02-18 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
316 98d3e6c1 2024-02-18 op &len) == -1) {
317 98d3e6c1 2024-02-18 op req->conn_error = errno;
318 98d3e6c1 2024-02-18 op req->cause = "getsockopt";
322 98d3e6c1 2024-02-18 op if (error == 0) /* connected */
325 98d3e6c1 2024-02-18 op req->conn_error = error;
326 98d3e6c1 2024-02-18 op req->cause = "connect";
327 98d3e6c1 2024-02-18 op close(req->fd);
328 98d3e6c1 2024-02-18 op req->fd = -1;
329 98d3e6c1 2024-02-18 op req->p = req->p->ai_next;
333 98d3e6c1 2024-02-18 op req->fd = socket(req->p->ai_family, req->p->ai_socktype,
334 98d3e6c1 2024-02-18 op req->p->ai_protocol);
335 98d3e6c1 2024-02-18 op if (req->fd == -1) {
336 98d3e6c1 2024-02-18 op req->conn_error = errno;
337 98d3e6c1 2024-02-18 op req->cause = "socket";
338 98d3e6c1 2024-02-18 op req->p = req->p->ai_next;
342 98d3e6c1 2024-02-18 op if (!mark_nonblock_cloexec(req->fd)) {
343 98d3e6c1 2024-02-18 op req->conn_error = errno;
344 98d3e6c1 2024-02-18 op req->cause = "setsockopt";
348 98d3e6c1 2024-02-18 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
350 98d3e6c1 2024-02-18 op errno = EAGAIN;
355 5f285272 2024-03-25 op gemini_parse_reply(struct req *req, const char *header)
357 54764e41 2024-02-02 op struct ibuf *ibuf;
361 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
364 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
365 5b762f01 2021-07-24 op if (header[2] != ' ')
369 5f285272 2024-03-25 op len = strlen(header) + 1;
371 54764e41 2024-02-02 op if ((ibuf = imsg_create(&iev_ui->ibuf, IMSG_REPLY, req->id, 0,
372 54764e41 2024-02-02 op sizeof(code) + len)) == NULL)
374 54764e41 2024-02-02 op if (imsg_add(ibuf, &code, sizeof(code)) == -1 ||
375 5f285272 2024-03-25 op imsg_add(ibuf, header, len) == -1)
377 54764e41 2024-02-02 op imsg_close(&iev_ui->ibuf, ibuf);
378 54764e41 2024-02-02 op imsg_event_add(iev_ui);
382 98d3e6c1 2024-02-18 op static inline int
383 98d3e6c1 2024-02-18 op net_send_req(struct req *req)
385 98d3e6c1 2024-02-18 op return (bufio_compose(&req->bio, req->req, req->len));
389 98d3e6c1 2024-02-18 op net_ev(int fd, int ev, void *d)
391 b07d2757 2024-01-31 op static char buf[4096];
392 5b762f01 2021-07-24 op struct req *req = d;
393 98d3e6c1 2024-02-18 op const char *hash;
394 98d3e6c1 2024-02-18 op ssize_t read;
396 f2a80e94 2024-03-22 op char *header;
399 98d3e6c1 2024-02-18 op if (ev == EV_TIMEOUT) {
400 98d3e6c1 2024-02-18 op close_with_err(req, "Timeout loading page");
404 98d3e6c1 2024-02-18 op if (req->state == CONN_CONNECTING) {
405 98d3e6c1 2024-02-18 op ev_del(req->fd);
406 98d3e6c1 2024-02-18 op if (try_to_connect(req) == -1) {
407 98d3e6c1 2024-02-18 op if (req->fd != -1 && errno == EAGAIN) {
408 98d3e6c1 2024-02-18 op ev_add(req->fd, EV_WRITE, net_ev, req);
411 98d3e6c1 2024-02-18 op close_with_errf(req, "failed to connect to %s"
412 98d3e6c1 2024-02-18 op " (%s: %s)", req->host, req->cause,
413 98d3e6c1 2024-02-18 op strerror(req->conn_error));
417 98d3e6c1 2024-02-18 op bufio_set_fd(&req->bio, req->fd);
419 98d3e6c1 2024-02-18 op switch (req->proto) {
420 98d3e6c1 2024-02-18 op case PROTO_FINGER:
421 98d3e6c1 2024-02-18 op case PROTO_GOPHER:
422 98d3e6c1 2024-02-18 op /* finger and gopher don't have a header nor TLS */
423 98d3e6c1 2024-02-18 op req->state = CONN_BODY;
424 98d3e6c1 2024-02-18 op if (net_send_req(req) == -1) {
425 98d3e6c1 2024-02-18 op close_with_err(req, "failed to send request");
429 98d3e6c1 2024-02-18 op case PROTO_GEMINI:
430 98d3e6c1 2024-02-18 op req->state = CONN_HANDSHAKE;
431 98d3e6c1 2024-02-18 op if (bufio_starttls(&req->bio, req->host, 1,
432 98d3e6c1 2024-02-18 op req->ccert, req->ccert_len,
433 98d3e6c1 2024-02-18 op req->ccert, req->ccert_len) == -1) {
434 98d3e6c1 2024-02-18 op close_with_err(req, "failed to setup TLS");
437 62d3cd29 2024-02-22 op req->timer = ev_timer(&timeout_for_handshake,
438 98d3e6c1 2024-02-18 op net_ev, req);
439 62d3cd29 2024-02-22 op if (req->timer == 0) {
440 98d3e6c1 2024-02-18 op close_with_err(req, "failed to setup"
441 98d3e6c1 2024-02-18 op " handshake timer");
448 98d3e6c1 2024-02-18 op if (req->state == CONN_HANDSHAKE) {
449 98d3e6c1 2024-02-18 op if (bufio_handshake(&req->bio) == -1 && errno == EAGAIN) {
450 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
454 62d3cd29 2024-02-22 op ev_timer_cancel(req->timer);
455 62d3cd29 2024-02-22 op req->timer = 0;
457 98d3e6c1 2024-02-18 op req->state = CONN_HEADER;
459 98d3e6c1 2024-02-18 op /* pause until we've told the certificate is OK */
460 98d3e6c1 2024-02-18 op ev_del(req->fd);
462 98d3e6c1 2024-02-18 op hash = tls_peer_cert_hash(req->bio.ctx);
463 98d3e6c1 2024-02-18 op if (hash == NULL) {
464 98d3e6c1 2024-02-18 op close_with_errf(req, "handshake failed: %s",
465 98d3e6c1 2024-02-18 op tls_error(req->bio.ctx));
469 98d3e6c1 2024-02-18 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
473 98d3e6c1 2024-02-18 op if (ev & EV_READ) {
474 98d3e6c1 2024-02-18 op read = bufio_read(&req->bio);
475 a3e4d56b 2024-05-25 thomas.ad if (read == -1 && errno != EAGAIN) {
476 a3e4d56b 2024-05-25 thomas.ad req->eof = 1;
477 a3e4d56b 2024-05-25 thomas.ad net_send_ui(IMSG_FAULTY_GEMSERVER, req->id, NULL, 0);
479 98d3e6c1 2024-02-18 op if (read == 0)
480 98d3e6c1 2024-02-18 op req->eof = 1;
483 98d3e6c1 2024-02-18 op if ((ev & EV_WRITE) && bufio_write(&req->bio) == -1 &&
484 98d3e6c1 2024-02-18 op errno != EAGAIN) {
485 98d3e6c1 2024-02-18 op close_with_errf(req, "bufio_write: %s", strerror(errno));
489 98d3e6c1 2024-02-18 op if (req->state == CONN_HEADER) {
490 f2a80e94 2024-03-22 op header = buf_getdelim(&req->bio.rbuf, "\r\n", &len);
491 f2a80e94 2024-03-22 op if (header == NULL && req->bio.rbuf.len >= 1024) {
492 98d3e6c1 2024-02-18 op close_with_err(req, "Invalid gemini reply (too long)");
495 f2a80e94 2024-03-22 op if (header == NULL && req->eof) {
496 98d3e6c1 2024-02-18 op close_with_err(req, "Invalid gemini reply.");
499 f2a80e94 2024-03-22 op if (header == NULL) {
500 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
503 98d3e6c1 2024-02-18 op req->state = CONN_BODY;
504 140a1abd 2024-03-25 op if ((code = gemini_parse_reply(req, header)) == -1) {
505 98d3e6c1 2024-02-18 op close_with_err(req, "Malformed gemini reply");
508 140a1abd 2024-03-25 op if (code < 20 || code >= 30) {
509 98d3e6c1 2024-02-18 op close_conn(0, 0, req);
513 140a1abd 2024-03-25 op buf_drain(&req->bio.rbuf, len);
515 9659786c 2024-03-25 op /* pause until we've been told to go ahead */
516 81df72e1 2024-03-25 op ev_del(req->fd);
521 98d3e6c1 2024-02-18 op * Split data into chunks before sending. imsg can't handle
522 98d3e6c1 2024-02-18 op * message that are "too big".
525 98d3e6c1 2024-02-18 op if ((len = bufio_drain(&req->bio, buf, sizeof(buf))) == 0)
527 98d3e6c1 2024-02-18 op net_send_ui(IMSG_BUF, req->id, buf, len);
530 98d3e6c1 2024-02-18 op if (req->eof) {
531 98d3e6c1 2024-02-18 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
532 98d3e6c1 2024-02-18 op close_conn(0, 0, req);
536 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
540 d35e18b3 2024-02-04 op load_cert(struct imsg *imsg, struct req *req)
542 d35e18b3 2024-02-04 op struct stat sb;
545 d35e18b3 2024-02-04 op if ((fd = imsg_get_fd(imsg)) == -1)
548 d35e18b3 2024-02-04 op if (fstat(fd, &sb) == -1)
552 d35e18b3 2024-02-04 op if (sb.st_size >= (off_t)SIZE_MAX) {
558 d35e18b3 2024-02-04 op req->ccert = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
559 d35e18b3 2024-02-04 op if (req->ccert == MAP_FAILED) {
560 d35e18b3 2024-02-04 op req->ccert = NULL;
565 d35e18b3 2024-02-04 op req->ccert_len = sb.st_size;
566 d35e18b3 2024-02-04 op req->ccert_fd = fd;
572 98d3e6c1 2024-02-18 op handle_dispatch_imsg(int fd, int event, void *d)
574 0640b555 2022-05-27 op struct imsgev *iev = d;
575 0640b555 2022-05-27 op struct imsgbuf *ibuf = &iev->ibuf;
576 0640b555 2022-05-27 op struct imsg imsg;
577 c8dba1e6 2021-07-24 op struct req *req;
578 0388b56b 2024-01-31 op struct get_req r;
582 0640b555 2022-05-27 op if (event & EV_READ) {
583 c8b8dc5c 2025-03-28 op if (imsgbuf_read(ibuf) == -1) {
584 c8b8dc5c 2025-03-28 op if (errno == EPIPE)
585 c8b8dc5c 2025-03-28 op errx(1, "connection closed");
587 c8b8dc5c 2025-03-28 op err(1, "imsg_read failure");
590 0640b555 2022-05-27 op if (event & EV_WRITE) {
591 c8b8dc5c 2025-03-28 op if (imsgbuf_write(ibuf) == -1 && errno != EAGAIN)
592 0640b555 2022-05-27 op err(1, "msgbuf_write");
596 0640b555 2022-05-27 op if ((n = imsg_get(ibuf, &imsg)) == -1)
597 0640b555 2022-05-27 op err(1, "imsg_get");
600 0388b56b 2024-01-31 op switch (imsg_get_type(&imsg)) {
601 c6e37ffd 2022-05-27 op case IMSG_GET:
602 0388b56b 2024-01-31 op if (imsg_get_data(&imsg, &r, sizeof(r)) == -1 ||
603 0388b56b 2024-01-31 op r.host[sizeof(r.host) - 1] != '\0' ||
604 0388b56b 2024-01-31 op r.port[sizeof(r.port) - 1] != '\0' ||
605 0388b56b 2024-01-31 op r.req[sizeof(r.req) - 1] != '\0')
607 0388b56b 2024-01-31 op if (r.proto != PROTO_FINGER &&
608 0388b56b 2024-01-31 op r.proto != PROTO_GEMINI &&
609 0388b56b 2024-01-31 op r.proto != PROTO_GOPHER)
612 3d89457c 2024-06-18 thomas.ad req = xcalloc(1, sizeof(*req));
614 51ea137b 2024-02-22 op req->fd = -1;
615 51ea137b 2024-02-22 op #if HAVE_ASR_RUN
616 51ea137b 2024-02-22 op req->ar_fd = -1;
618 d35e18b3 2024-02-04 op req->ccert_fd = -1;
619 0388b56b 2024-01-31 op req->id = imsg_get_id(&imsg);
620 0640b555 2022-05-27 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
622 3d89457c 2024-06-18 thomas.ad req->host = xstrdup(r.host);
623 3d89457c 2024-06-18 thomas.ad req->port = xstrdup(r.port);
624 3d89457c 2024-06-18 thomas.ad req->req = xstrdup(r.req);
625 d35e18b3 2024-02-04 op if (load_cert(&imsg, req) == -1)
627 98d3e6c1 2024-02-18 op if (bufio_init(&req->bio) == -1)
630 5657662f 2024-01-16 op req->len = strlen(req->req);
631 0388b56b 2024-01-31 op req->proto = r.proto;
632 10884685 2024-02-22 op req_resolve(-1, 0, req);
635 0640b555 2022-05-27 op case IMSG_CERT_STATUS:
636 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
639 c24b14ad 2024-01-22 op if (imsg_get_data(&imsg, &certok, sizeof(certok)) ==
642 98d3e6c1 2024-02-18 op if (!certok) {
643 0640b555 2022-05-27 op close_conn(0, 0, req);
647 98d3e6c1 2024-02-18 op if (net_send_req(req) == -1) {
648 98d3e6c1 2024-02-18 op close_with_err(req, "failed to send request");
652 98d3e6c1 2024-02-18 op if (ev_add(req->fd, EV_WRITE, net_ev, req) == -1) {
653 98d3e6c1 2024-02-18 op close_with_err(req,
654 98d3e6c1 2024-02-18 op "failed to register event.");
659 0640b555 2022-05-27 op case IMSG_PROCEED:
660 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
662 98d3e6c1 2024-02-18 op ev_add(req->fd, EV_READ, net_ev, req);
663 98d3e6c1 2024-02-18 op net_ev(req->fd, 0, req);
666 0640b555 2022-05-27 op case IMSG_STOP:
667 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
669 0640b555 2022-05-27 op close_conn(0, 0, req);
672 0640b555 2022-05-27 op case IMSG_QUIT:
674 0640b555 2022-05-27 op imsg_free(&imsg);
678 0388b56b 2024-01-31 op errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
681 0640b555 2022-05-27 op imsg_free(&imsg);
684 0640b555 2022-05-27 op imsg_event_add(iev);
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 98d3e6c1 2024-02-18 op if (ev_init() == -1)
705 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
706 3d89457c 2024-06-18 thomas.ad iev_ui = xmalloc(sizeof(*iev_ui));
707 c8b8dc5c 2025-03-28 op if (imsgbuf_init(&iev_ui->ibuf, 3) == -1)
708 c8b8dc5c 2025-03-28 op err(1, "imsgbuf_init failed");
709 0082f426 2025-04-01 op imsgbuf_allow_fdpass(&iev_ui->ibuf);
710 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
711 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
712 98d3e6c1 2024-02-18 op ev_add(iev_ui->ibuf.fd, iev_ui->events, iev_ui->handler, iev_ui);
714 c8dba1e6 2021-07-24 op sandbox_net_process();
718 c8b8dc5c 2025-03-28 op imsgbuf_clear(&iev_ui->ibuf);
719 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
720 74c0f6ba 2021-07-24 op free(iev_ui);