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 <assert.h>
27 c8dba1e6 2021-07-24 op #include <ctype.h>
28 c8dba1e6 2021-07-24 op #include <errno.h>
29 c8dba1e6 2021-07-24 op #include <netdb.h>
30 c8dba1e6 2021-07-24 op #include <stdarg.h>
31 c8dba1e6 2021-07-24 op #include <stdio.h>
32 c8dba1e6 2021-07-24 op #include <stdlib.h>
33 c8dba1e6 2021-07-24 op #include <string.h>
34 c8dba1e6 2021-07-24 op #include <tls.h>
35 c8dba1e6 2021-07-24 op #include <unistd.h>
37 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
38 c8dba1e6 2021-07-24 op # include <asr.h>
41 98d3e6c1 2024-02-18 op #include "bufio.h"
42 98d3e6c1 2024-02-18 op #include "ev.h"
43 87d297d1 2024-02-22 op #include "imsgev.h"
44 c8dba1e6 2021-07-24 op #include "telescope.h"
45 9d65b1d9 2022-01-11 op #include "utils.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 77ae5e91 2021-07-24 op static int gemini_parse_reply(struct req *, const char *, size_t);
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 5b762f01 2021-07-24 op gemini_parse_reply(struct req *req, const char *header, size_t len)
357 54764e41 2024-02-02 op struct ibuf *ibuf;
359 5b762f01 2021-07-24 op const char *t;
364 5b762f01 2021-07-24 op if (!isdigit(header[0]) || !isdigit(header[1]))
367 5b762f01 2021-07-24 op code = (header[0] - '0')*10 + (header[1] - '0');
368 5b762f01 2021-07-24 op if (header[2] != ' ')
371 5b762f01 2021-07-24 op t = header + 3;
372 54764e41 2024-02-02 op len = strlen(t) + 1;
374 54764e41 2024-02-02 op if ((ibuf = imsg_create(&iev_ui->ibuf, IMSG_REPLY, req->id, 0,
375 54764e41 2024-02-02 op sizeof(code) + len)) == NULL)
377 54764e41 2024-02-02 op if (imsg_add(ibuf, &code, sizeof(code)) == -1 ||
378 54764e41 2024-02-02 op imsg_add(ibuf, t, len) == -1)
380 54764e41 2024-02-02 op imsg_close(&iev_ui->ibuf, ibuf);
381 54764e41 2024-02-02 op imsg_event_add(iev_ui);
383 98d3e6c1 2024-02-18 op /* pause until we've told go go ahead */
384 98d3e6c1 2024-02-18 op ev_del(req->fd);
389 98d3e6c1 2024-02-18 op static inline int
390 98d3e6c1 2024-02-18 op net_send_req(struct req *req)
392 98d3e6c1 2024-02-18 op return (bufio_compose(&req->bio, req->req, req->len));
396 98d3e6c1 2024-02-18 op net_ev(int fd, int ev, void *d)
398 b07d2757 2024-01-31 op static char buf[4096];
399 5b762f01 2021-07-24 op struct req *req = d;
400 98d3e6c1 2024-02-18 op const char *hash;
401 98d3e6c1 2024-02-18 op ssize_t read;
403 f2a80e94 2024-03-22 op char *header;
406 98d3e6c1 2024-02-18 op if (ev == EV_TIMEOUT) {
407 98d3e6c1 2024-02-18 op close_with_err(req, "Timeout loading page");
411 98d3e6c1 2024-02-18 op if (req->state == CONN_CONNECTING) {
412 98d3e6c1 2024-02-18 op ev_del(req->fd);
413 98d3e6c1 2024-02-18 op if (try_to_connect(req) == -1) {
414 98d3e6c1 2024-02-18 op if (req->fd != -1 && errno == EAGAIN) {
415 98d3e6c1 2024-02-18 op ev_add(req->fd, EV_WRITE, net_ev, req);
418 98d3e6c1 2024-02-18 op close_with_errf(req, "failed to connect to %s"
419 98d3e6c1 2024-02-18 op " (%s: %s)", req->host, req->cause,
420 98d3e6c1 2024-02-18 op strerror(req->conn_error));
424 98d3e6c1 2024-02-18 op bufio_set_fd(&req->bio, req->fd);
426 98d3e6c1 2024-02-18 op switch (req->proto) {
427 98d3e6c1 2024-02-18 op case PROTO_FINGER:
428 98d3e6c1 2024-02-18 op case PROTO_GOPHER:
429 98d3e6c1 2024-02-18 op /* finger and gopher don't have a header nor TLS */
430 98d3e6c1 2024-02-18 op req->state = CONN_BODY;
431 98d3e6c1 2024-02-18 op if (net_send_req(req) == -1) {
432 98d3e6c1 2024-02-18 op close_with_err(req, "failed to send request");
436 98d3e6c1 2024-02-18 op case PROTO_GEMINI:
437 98d3e6c1 2024-02-18 op req->state = CONN_HANDSHAKE;
438 98d3e6c1 2024-02-18 op if (bufio_starttls(&req->bio, req->host, 1,
439 98d3e6c1 2024-02-18 op req->ccert, req->ccert_len,
440 98d3e6c1 2024-02-18 op req->ccert, req->ccert_len) == -1) {
441 98d3e6c1 2024-02-18 op close_with_err(req, "failed to setup TLS");
444 62d3cd29 2024-02-22 op req->timer = ev_timer(&timeout_for_handshake,
445 98d3e6c1 2024-02-18 op net_ev, req);
446 62d3cd29 2024-02-22 op if (req->timer == 0) {
447 98d3e6c1 2024-02-18 op close_with_err(req, "failed to setup"
448 98d3e6c1 2024-02-18 op " handshake timer");
455 98d3e6c1 2024-02-18 op if (req->state == CONN_HANDSHAKE) {
456 98d3e6c1 2024-02-18 op if (bufio_handshake(&req->bio) == -1 && errno == EAGAIN) {
457 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
461 62d3cd29 2024-02-22 op ev_timer_cancel(req->timer);
462 62d3cd29 2024-02-22 op req->timer = 0;
464 98d3e6c1 2024-02-18 op req->state = CONN_HEADER;
466 98d3e6c1 2024-02-18 op /* pause until we've told the certificate is OK */
467 98d3e6c1 2024-02-18 op ev_del(req->fd);
469 98d3e6c1 2024-02-18 op hash = tls_peer_cert_hash(req->bio.ctx);
470 98d3e6c1 2024-02-18 op if (hash == NULL) {
471 98d3e6c1 2024-02-18 op close_with_errf(req, "handshake failed: %s",
472 98d3e6c1 2024-02-18 op tls_error(req->bio.ctx));
476 98d3e6c1 2024-02-18 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
480 98d3e6c1 2024-02-18 op if (ev & EV_READ) {
481 98d3e6c1 2024-02-18 op read = bufio_read(&req->bio);
482 98d3e6c1 2024-02-18 op if (read == -1 && errno != EAGAIN) {
483 98d3e6c1 2024-02-18 op close_with_errf(req, "Read error");
486 98d3e6c1 2024-02-18 op if (read == 0)
487 98d3e6c1 2024-02-18 op req->eof = 1;
490 98d3e6c1 2024-02-18 op if ((ev & EV_WRITE) && bufio_write(&req->bio) == -1 &&
491 98d3e6c1 2024-02-18 op errno != EAGAIN) {
492 98d3e6c1 2024-02-18 op close_with_errf(req, "bufio_write: %s", strerror(errno));
496 98d3e6c1 2024-02-18 op if (req->state == CONN_HEADER) {
497 f2a80e94 2024-03-22 op header = buf_getdelim(&req->bio.rbuf, "\r\n", &len);
498 f2a80e94 2024-03-22 op if (header == NULL && req->bio.rbuf.len >= 1024) {
499 98d3e6c1 2024-02-18 op close_with_err(req, "Invalid gemini reply (too long)");
502 f2a80e94 2024-03-22 op if (header == NULL && req->eof) {
503 98d3e6c1 2024-02-18 op close_with_err(req, "Invalid gemini reply.");
506 f2a80e94 2024-03-22 op if (header == NULL) {
507 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
510 98d3e6c1 2024-02-18 op req->state = CONN_BODY;
511 f2a80e94 2024-03-22 op r = gemini_parse_reply(req, header, len);
512 f2a80e94 2024-03-22 op buf_drain(&req->bio.rbuf, len);
513 98d3e6c1 2024-02-18 op if (r == 0) {
514 98d3e6c1 2024-02-18 op close_with_err(req, "Malformed gemini reply");
517 98d3e6c1 2024-02-18 op if (r < 20 || r >= 30)
518 98d3e6c1 2024-02-18 op close_conn(0, 0, req);
523 98d3e6c1 2024-02-18 op * Split data into chunks before sending. imsg can't handle
524 98d3e6c1 2024-02-18 op * message that are "too big".
527 98d3e6c1 2024-02-18 op if ((len = bufio_drain(&req->bio, buf, sizeof(buf))) == 0)
529 98d3e6c1 2024-02-18 op net_send_ui(IMSG_BUF, req->id, buf, len);
532 98d3e6c1 2024-02-18 op if (req->eof) {
533 98d3e6c1 2024-02-18 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
534 98d3e6c1 2024-02-18 op close_conn(0, 0, req);
538 ce023433 2024-02-22 op ev_add(req->fd, req_bio_ev(req), net_ev, req);
542 d35e18b3 2024-02-04 op load_cert(struct imsg *imsg, struct req *req)
544 d35e18b3 2024-02-04 op struct stat sb;
547 d35e18b3 2024-02-04 op if ((fd = imsg_get_fd(imsg)) == -1)
550 d35e18b3 2024-02-04 op if (fstat(fd, &sb) == -1)
554 d35e18b3 2024-02-04 op if (sb.st_size >= (off_t)SIZE_MAX) {
560 d35e18b3 2024-02-04 op req->ccert = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
561 d35e18b3 2024-02-04 op if (req->ccert == MAP_FAILED) {
562 d35e18b3 2024-02-04 op req->ccert = NULL;
567 d35e18b3 2024-02-04 op req->ccert_len = sb.st_size;
568 d35e18b3 2024-02-04 op req->ccert_fd = fd;
574 98d3e6c1 2024-02-18 op handle_dispatch_imsg(int fd, int event, void *d)
576 0640b555 2022-05-27 op struct imsgev *iev = d;
577 0640b555 2022-05-27 op struct imsgbuf *ibuf = &iev->ibuf;
578 0640b555 2022-05-27 op struct imsg imsg;
579 c8dba1e6 2021-07-24 op struct req *req;
580 0388b56b 2024-01-31 op struct get_req r;
584 0640b555 2022-05-27 op if (event & EV_READ) {
585 0640b555 2022-05-27 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
586 0640b555 2022-05-27 op err(1, "imsg_read");
588 0640b555 2022-05-27 op err(1, "connection closed");
590 0640b555 2022-05-27 op if (event & EV_WRITE) {
591 0640b555 2022-05-27 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
592 0640b555 2022-05-27 op err(1, "msgbuf_write");
594 0640b555 2022-05-27 op err(1, "connection closed");
598 0640b555 2022-05-27 op if ((n = imsg_get(ibuf, &imsg)) == -1)
599 0640b555 2022-05-27 op err(1, "imsg_get");
602 0388b56b 2024-01-31 op switch (imsg_get_type(&imsg)) {
603 c6e37ffd 2022-05-27 op case IMSG_GET:
604 0388b56b 2024-01-31 op if (imsg_get_data(&imsg, &r, sizeof(r)) == -1 ||
605 0388b56b 2024-01-31 op r.host[sizeof(r.host) - 1] != '\0' ||
606 0388b56b 2024-01-31 op r.port[sizeof(r.port) - 1] != '\0' ||
607 0388b56b 2024-01-31 op r.req[sizeof(r.req) - 1] != '\0')
609 0388b56b 2024-01-31 op if (r.proto != PROTO_FINGER &&
610 0388b56b 2024-01-31 op r.proto != PROTO_GEMINI &&
611 0388b56b 2024-01-31 op r.proto != PROTO_GOPHER)
614 0640b555 2022-05-27 op if ((req = calloc(1, sizeof(*req))) == NULL)
617 51ea137b 2024-02-22 op req->fd = -1;
618 51ea137b 2024-02-22 op #if HAVE_ASR_RUN
619 51ea137b 2024-02-22 op req->ar_fd = -1;
621 d35e18b3 2024-02-04 op req->ccert_fd = -1;
622 0388b56b 2024-01-31 op req->id = imsg_get_id(&imsg);
623 0640b555 2022-05-27 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
625 0388b56b 2024-01-31 op if ((req->host = strdup(r.host)) == NULL)
627 0388b56b 2024-01-31 op if ((req->port = strdup(r.port)) == NULL)
629 0388b56b 2024-01-31 op if ((req->req = strdup(r.req)) == NULL)
631 d35e18b3 2024-02-04 op if (load_cert(&imsg, req) == -1)
633 98d3e6c1 2024-02-18 op if (bufio_init(&req->bio) == -1)
636 5657662f 2024-01-16 op req->len = strlen(req->req);
637 0388b56b 2024-01-31 op req->proto = r.proto;
638 10884685 2024-02-22 op req_resolve(-1, 0, req);
641 0640b555 2022-05-27 op case IMSG_CERT_STATUS:
642 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
645 c24b14ad 2024-01-22 op if (imsg_get_data(&imsg, &certok, sizeof(certok)) ==
648 98d3e6c1 2024-02-18 op if (!certok) {
649 0640b555 2022-05-27 op close_conn(0, 0, req);
653 98d3e6c1 2024-02-18 op if (net_send_req(req) == -1) {
654 98d3e6c1 2024-02-18 op close_with_err(req, "failed to send request");
658 98d3e6c1 2024-02-18 op if (ev_add(req->fd, EV_WRITE, net_ev, req) == -1) {
659 98d3e6c1 2024-02-18 op close_with_err(req,
660 98d3e6c1 2024-02-18 op "failed to register event.");
665 0640b555 2022-05-27 op case IMSG_PROCEED:
666 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
668 98d3e6c1 2024-02-18 op ev_add(req->fd, EV_READ, net_ev, req);
669 98d3e6c1 2024-02-18 op net_ev(req->fd, 0, req);
672 0640b555 2022-05-27 op case IMSG_STOP:
673 0388b56b 2024-01-31 op if ((req = req_by_id(imsg_get_id(&imsg))) == NULL)
675 0640b555 2022-05-27 op close_conn(0, 0, req);
678 0640b555 2022-05-27 op case IMSG_QUIT:
680 0640b555 2022-05-27 op imsg_free(&imsg);
684 0388b56b 2024-01-31 op errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
687 0640b555 2022-05-27 op imsg_free(&imsg);
690 0640b555 2022-05-27 op imsg_event_add(iev);
694 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
695 c8dba1e6 2021-07-24 op uint16_t datalen)
697 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
698 c8dba1e6 2021-07-24 op data, datalen);
702 f45bd2e3 2021-07-24 op net_main(void)
704 c8dba1e6 2021-07-24 op setproctitle("net");
706 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
708 98d3e6c1 2024-02-18 op if (ev_init() == -1)
711 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
712 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
714 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
715 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
716 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
717 98d3e6c1 2024-02-18 op ev_add(iev_ui->ibuf.fd, iev_ui->events, iev_ui->handler, iev_ui);
719 c8dba1e6 2021-07-24 op sandbox_net_process();
723 74c0f6ba 2021-07-24 op msgbuf_clear(&iev_ui->ibuf.w);
724 74c0f6ba 2021-07-24 op close(iev_ui->ibuf.fd);
725 74c0f6ba 2021-07-24 op free(iev_ui);