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*);
63 c8dba1e6 2021-07-24 op static void do_handshake(int, short, void*);
64 c8dba1e6 2021-07-24 op static void write_request(int, short, void*);
65 c8dba1e6 2021-07-24 op static void read_reply(int, short, void*);
66 c8dba1e6 2021-07-24 op static void parse_reply(struct req*);
67 c8dba1e6 2021-07-24 op static void copy_body(int, short, void*);
69 c8dba1e6 2021-07-24 op static void handle_get_raw(struct imsg *, size_t);
70 c8dba1e6 2021-07-24 op static void handle_cert_status(struct imsg*, size_t);
71 c8dba1e6 2021-07-24 op static void handle_proceed(struct imsg*, size_t);
72 c8dba1e6 2021-07-24 op static void handle_stop(struct imsg*, size_t);
73 c8dba1e6 2021-07-24 op static void handle_quit(struct imsg*, size_t);
74 c8dba1e6 2021-07-24 op static void handle_dispatch_imsg(int, short, void*);
76 c8dba1e6 2021-07-24 op static int net_send_ui(int, uint32_t, const void *, uint16_t);
78 c8dba1e6 2021-07-24 op /* TODO: making this customizable */
79 c8dba1e6 2021-07-24 op struct timeval timeout_for_handshake = { 5, 0 };
81 c8dba1e6 2021-07-24 op static imsg_handlerfn *handlers[] = {
82 c8dba1e6 2021-07-24 op [IMSG_GET_RAW] = handle_get_raw,
83 c8dba1e6 2021-07-24 op [IMSG_CERT_STATUS] = handle_cert_status,
84 c8dba1e6 2021-07-24 op [IMSG_PROCEED] = handle_proceed,
85 c8dba1e6 2021-07-24 op [IMSG_STOP] = handle_stop,
86 c8dba1e6 2021-07-24 op [IMSG_QUIT] = handle_quit,
89 c8dba1e6 2021-07-24 op typedef void (*statefn)(int, short, void*);
91 c8dba1e6 2021-07-24 op TAILQ_HEAD(, req) reqhead;
92 c8dba1e6 2021-07-24 op /* a pending request */
94 c8dba1e6 2021-07-24 op struct event ev;
95 c8dba1e6 2021-07-24 op struct phos_uri url;
98 c8dba1e6 2021-07-24 op struct tls *ctx;
99 c8dba1e6 2021-07-24 op char buf[1024];
102 c8dba1e6 2021-07-24 op struct addrinfo *servinfo, *p;
103 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
104 c8dba1e6 2021-07-24 op struct addrinfo hints;
105 c8dba1e6 2021-07-24 op struct event_asr *asrev;
108 c8dba1e6 2021-07-24 op TAILQ_ENTRY(req) reqs;
111 c8dba1e6 2021-07-24 op static inline void
112 c8dba1e6 2021-07-24 op yield_r(struct req *req, statefn fn, struct timeval *tv)
114 c8dba1e6 2021-07-24 op event_once(req->fd, EV_READ, fn, req, tv);
117 c8dba1e6 2021-07-24 op static inline void
118 c8dba1e6 2021-07-24 op yield_w(struct req *req, statefn fn, struct timeval *tv)
120 c8dba1e6 2021-07-24 op event_once(req->fd, EV_WRITE, fn, req, tv);
123 c8dba1e6 2021-07-24 op static inline void
124 c8dba1e6 2021-07-24 op advance_buf(struct req *req, size_t len)
126 c8dba1e6 2021-07-24 op assert(len <= req->off);
128 c8dba1e6 2021-07-24 op req->off -= len;
129 c8dba1e6 2021-07-24 op memmove(req->buf, req->buf + len, req->off);
132 c8dba1e6 2021-07-24 op static void __attribute__((__noreturn__))
135 c8dba1e6 2021-07-24 op abort(); /* TODO */
139 c8dba1e6 2021-07-24 op try_to_connect(int fd, short ev, void *d)
141 c8dba1e6 2021-07-24 op struct req *req = d;
142 c8dba1e6 2021-07-24 op int error = 0;
143 c8dba1e6 2021-07-24 op socklen_t len = sizeof(error);
146 c8dba1e6 2021-07-24 op if (req->p == NULL)
149 c8dba1e6 2021-07-24 op if (req->fd != -1) {
150 c8dba1e6 2021-07-24 op if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error, &len) == -1)
152 c8dba1e6 2021-07-24 op if (error != 0) {
153 c8dba1e6 2021-07-24 op errno = error;
159 c8dba1e6 2021-07-24 op req->fd = socket(req->p->ai_family, req->p->ai_socktype, req->p->ai_protocol);
160 c8dba1e6 2021-07-24 op if (req->fd == -1) {
161 c8dba1e6 2021-07-24 op req->p = req->p->ai_next;
164 c8dba1e6 2021-07-24 op mark_nonblock(req->fd);
165 c8dba1e6 2021-07-24 op if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
167 c8dba1e6 2021-07-24 op yield_w(req, try_to_connect, NULL);
172 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
173 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to connect to %s",
174 c8dba1e6 2021-07-24 op req->url.host);
178 c8dba1e6 2021-07-24 op freeaddrinfo(req->servinfo);
179 c8dba1e6 2021-07-24 op setup_tls(req);
182 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
184 c8dba1e6 2021-07-24 op query_done(struct asr_result *res, void *d)
186 c8dba1e6 2021-07-24 op struct req *req = d;
188 c8dba1e6 2021-07-24 op req->asrev = NULL;
189 c8dba1e6 2021-07-24 op if (res->ar_gai_errno != 0) {
190 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
191 c8dba1e6 2021-07-24 op req->url.host, gai_strerror(res->ar_gai_errno));
195 c8dba1e6 2021-07-24 op req->fd = -1;
196 c8dba1e6 2021-07-24 op req->servinfo = res->ar_addrinfo;
197 c8dba1e6 2021-07-24 op req->p = res->ar_addrinfo;
198 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
202 c8dba1e6 2021-07-24 op async_conn_towards(struct req *req)
204 c8dba1e6 2021-07-24 op struct asr_query *q;
205 c8dba1e6 2021-07-24 op const char *proto = "1965";
207 c8dba1e6 2021-07-24 op if (*req->url.port != '\0')
208 c8dba1e6 2021-07-24 op proto = req->url.port;
210 c8dba1e6 2021-07-24 op req->hints.ai_family = AF_UNSPEC;
211 c8dba1e6 2021-07-24 op req->hints.ai_socktype = SOCK_STREAM;
212 c8dba1e6 2021-07-24 op q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
213 c8dba1e6 2021-07-24 op req->asrev = event_asr_run(q, query_done, req);
217 c8dba1e6 2021-07-24 op blocking_conn_towards(struct req *req)
219 c8dba1e6 2021-07-24 op struct addrinfo hints;
220 c8dba1e6 2021-07-24 op struct phos_uri *url = &req->url;
222 c8dba1e6 2021-07-24 op const char *proto = "1965";
224 c8dba1e6 2021-07-24 op if (*url->port != '\0')
225 c8dba1e6 2021-07-24 op proto = url->port;
227 c8dba1e6 2021-07-24 op memset(&hints, 0, sizeof(hints));
228 c8dba1e6 2021-07-24 op hints.ai_family = AF_UNSPEC;
229 c8dba1e6 2021-07-24 op hints.ai_socktype = SOCK_STREAM;
231 c8dba1e6 2021-07-24 op if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
232 c8dba1e6 2021-07-24 op close_with_errf(req, "failed to resolve %s: %s",
233 c8dba1e6 2021-07-24 op url->host, gai_strerror(status));
237 c8dba1e6 2021-07-24 op req->fd = -1;
238 c8dba1e6 2021-07-24 op req->p = req->servinfo;
239 c8dba1e6 2021-07-24 op try_to_connect(0, 0, req);
243 c8dba1e6 2021-07-24 op static struct req *
244 c8dba1e6 2021-07-24 op req_by_id(uint32_t id)
246 c8dba1e6 2021-07-24 op struct req *r;
248 c8dba1e6 2021-07-24 op if ((r = req_by_id_try(id)) == NULL)
253 c8dba1e6 2021-07-24 op static struct req *
254 c8dba1e6 2021-07-24 op req_by_id_try(uint32_t id)
256 c8dba1e6 2021-07-24 op struct req *r;
258 c8dba1e6 2021-07-24 op TAILQ_FOREACH(r, &reqhead, reqs) {
259 c8dba1e6 2021-07-24 op if (r->id == id)
267 c8dba1e6 2021-07-24 op close_conn(int fd, short ev, void *d)
269 c8dba1e6 2021-07-24 op struct req *req = d;
271 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
272 c8dba1e6 2021-07-24 op if (req->asrev != NULL)
273 c8dba1e6 2021-07-24 op event_asr_abort(req->asrev);
276 c8dba1e6 2021-07-24 op if (req->ctx != NULL) {
277 c8dba1e6 2021-07-24 op switch (tls_close(req->ctx)) {
278 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
279 c8dba1e6 2021-07-24 op yield_r(req, close_conn, NULL);
281 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
282 c8dba1e6 2021-07-24 op yield_w(req, close_conn, NULL);
286 c8dba1e6 2021-07-24 op tls_free(req->ctx);
289 c8dba1e6 2021-07-24 op TAILQ_REMOVE(&reqhead, req, reqs);
290 c8dba1e6 2021-07-24 op if (req->fd != -1)
291 c8dba1e6 2021-07-24 op close(req->fd);
296 c8dba1e6 2021-07-24 op close_with_err(struct req *req, const char *err)
298 c8dba1e6 2021-07-24 op net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
299 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
303 c8dba1e6 2021-07-24 op close_with_errf(struct req *req, const char *fmt, ...)
308 c8dba1e6 2021-07-24 op va_start(ap, fmt);
309 c8dba1e6 2021-07-24 op if (vasprintf(&s, fmt, ap) == -1)
313 c8dba1e6 2021-07-24 op close_with_err(req, s);
318 c8dba1e6 2021-07-24 op setup_tls(struct req *req)
320 c8dba1e6 2021-07-24 op if ((req->ctx = tls_client()) == NULL) {
321 c8dba1e6 2021-07-24 op close_with_errf(req, "tls_client: %s", strerror(errno));
324 c8dba1e6 2021-07-24 op if (tls_configure(req->ctx, tlsconf) == -1) {
325 c8dba1e6 2021-07-24 op close_with_errf(req, "tls_configure: %s", tls_error(req->ctx));
328 c8dba1e6 2021-07-24 op if (tls_connect_socket(req->ctx, req->fd, req->url.host) == -1) {
329 c8dba1e6 2021-07-24 op close_with_errf(req, "tls_connect_socket: %s", tls_error(req->ctx));
332 c8dba1e6 2021-07-24 op yield_w(req, do_handshake, &timeout_for_handshake);
336 c8dba1e6 2021-07-24 op do_handshake(int fd, short ev, void *d)
338 c8dba1e6 2021-07-24 op struct req *req = d;
339 c8dba1e6 2021-07-24 op const char *hash;
341 c8dba1e6 2021-07-24 op if (ev == EV_TIMEOUT) {
342 c8dba1e6 2021-07-24 op close_with_err(req, "Timeout loading page");
346 c8dba1e6 2021-07-24 op switch (tls_handshake(req->ctx)) {
347 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
348 c8dba1e6 2021-07-24 op yield_r(req, do_handshake, NULL);
350 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
351 c8dba1e6 2021-07-24 op yield_w(req, do_handshake, NULL);
355 c8dba1e6 2021-07-24 op hash = tls_peer_cert_hash(req->ctx);
356 c8dba1e6 2021-07-24 op if (hash == NULL) {
357 c8dba1e6 2021-07-24 op close_with_errf(req, "handshake failed: %s", tls_error(req->ctx));
360 c8dba1e6 2021-07-24 op net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
364 c8dba1e6 2021-07-24 op write_request(int fd, short ev, void *d)
366 c8dba1e6 2021-07-24 op struct req *req = d;
370 c8dba1e6 2021-07-24 op len = strlen(req->buf);
372 c8dba1e6 2021-07-24 op switch (r = tls_write(req->ctx, req->buf, len)) {
374 c8dba1e6 2021-07-24 op close_with_errf(req, "tls_write: %s", tls_error(req->ctx));
376 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
377 c8dba1e6 2021-07-24 op yield_r(req, write_request, NULL);
379 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
380 c8dba1e6 2021-07-24 op yield_w(req, write_request, NULL);
383 c8dba1e6 2021-07-24 op /* assume r == len */
385 c8dba1e6 2021-07-24 op yield_r(req, read_reply, NULL);
391 c8dba1e6 2021-07-24 op read_reply(int fd, short ev, void *d)
393 c8dba1e6 2021-07-24 op struct req *req = d;
398 c8dba1e6 2021-07-24 op buf = req->buf + req->off;
399 c8dba1e6 2021-07-24 op len = sizeof(req->buf) - req->off;
401 c8dba1e6 2021-07-24 op switch (r = tls_read(req->ctx, buf, len)) {
403 c8dba1e6 2021-07-24 op close_with_errf(req, "tls_read: %s", tls_error(req->ctx));
405 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
406 c8dba1e6 2021-07-24 op yield_r(req, read_reply, NULL);
408 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
409 c8dba1e6 2021-07-24 op yield_w(req, read_reply, NULL);
412 c8dba1e6 2021-07-24 op req->off += r;
414 c8dba1e6 2021-07-24 op if (memmem(req->buf, req->off, "\r\n", 2) != NULL)
415 c8dba1e6 2021-07-24 op parse_reply(req);
416 c8dba1e6 2021-07-24 op else if (req->off == sizeof(req->buf))
417 c8dba1e6 2021-07-24 op close_with_err(req, "invalid response");
419 c8dba1e6 2021-07-24 op yield_r(req, read_reply, NULL);
425 c8dba1e6 2021-07-24 op parse_reply(struct req *req)
431 c8dba1e6 2021-07-24 op if (req->off < 4)
434 c8dba1e6 2021-07-24 op if (!isdigit(req->buf[0]) || !isdigit(req->buf[1]))
437 c8dba1e6 2021-07-24 op code = (req->buf[0] - '0')*10 + (req->buf[1] - '0');
439 c8dba1e6 2021-07-24 op if (!isspace(req->buf[2]))
442 c8dba1e6 2021-07-24 op advance_buf(req, 3);
443 c8dba1e6 2021-07-24 op if ((e = memmem(req->buf, req->off, "\r\n", 2)) == NULL)
448 c8dba1e6 2021-07-24 op len = e - req->buf;
449 c8dba1e6 2021-07-24 op net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
450 c8dba1e6 2021-07-24 op net_send_ui(IMSG_GOT_META, req->id, req->buf, len);
452 c8dba1e6 2021-07-24 op if (20 <= code && code < 30)
453 c8dba1e6 2021-07-24 op advance_buf(req, len+1); /* skip \n too */
455 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
460 c8dba1e6 2021-07-24 op close_with_err(req, "malformed request");
464 c8dba1e6 2021-07-24 op copy_body(int fd, short ev, void *d)
466 c8dba1e6 2021-07-24 op struct req *req = d;
470 c8dba1e6 2021-07-24 op if (req->off != 0) {
471 c8dba1e6 2021-07-24 op net_send_ui(IMSG_BUF, req->id,
472 c8dba1e6 2021-07-24 op req->buf, req->off);
473 c8dba1e6 2021-07-24 op req->off = 0;
476 c8dba1e6 2021-07-24 op switch (r = tls_read(req->ctx, req->buf, sizeof(req->buf))) {
477 c8dba1e6 2021-07-24 op case TLS_WANT_POLLIN:
478 c8dba1e6 2021-07-24 op yield_r(req, copy_body, NULL);
480 c8dba1e6 2021-07-24 op case TLS_WANT_POLLOUT:
481 c8dba1e6 2021-07-24 op yield_w(req, copy_body, NULL);
485 c8dba1e6 2021-07-24 op * XXX: should notify the user that the
486 c8dba1e6 2021-07-24 op * download was aborted.
488 c8dba1e6 2021-07-24 op /* fallthrough */
490 c8dba1e6 2021-07-24 op net_send_ui(IMSG_EOF, req->id, NULL, 0);
491 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
494 c8dba1e6 2021-07-24 op req->off = r;
500 c8dba1e6 2021-07-24 op handle_get_raw(struct imsg *imsg, size_t datalen)
502 c8dba1e6 2021-07-24 op struct req *req;
503 c8dba1e6 2021-07-24 op struct get_req *r;
505 c8dba1e6 2021-07-24 op r = imsg->data;
507 c8dba1e6 2021-07-24 op if (datalen != sizeof(*r))
510 c8dba1e6 2021-07-24 op if ((req = calloc(1, sizeof(*req))) == NULL)
513 c8dba1e6 2021-07-24 op req->id = imsg->hdr.peerid;
514 c8dba1e6 2021-07-24 op TAILQ_INSERT_HEAD(&reqhead, req, reqs);
516 c8dba1e6 2021-07-24 op strlcpy(req->url.host, r->host, sizeof(req->url.host));
517 c8dba1e6 2021-07-24 op strlcpy(req->url.port, r->port, sizeof(req->url.port));
518 c8dba1e6 2021-07-24 op strlcpy(req->buf, r->req, sizeof(req->buf));
520 c8dba1e6 2021-07-24 op #if HAVE_ASR_RUN
521 c8dba1e6 2021-07-24 op async_conn_towards(req);
523 c8dba1e6 2021-07-24 op blocking_conn_towards(req);
528 c8dba1e6 2021-07-24 op handle_cert_status(struct imsg *imsg, size_t datalen)
530 c8dba1e6 2021-07-24 op struct req *req;
533 c8dba1e6 2021-07-24 op req = req_by_id(imsg->hdr.peerid);
535 c8dba1e6 2021-07-24 op if (datalen < sizeof(is_ok))
537 c8dba1e6 2021-07-24 op memcpy(&is_ok, imsg->data, sizeof(is_ok));
540 c8dba1e6 2021-07-24 op yield_w(req, write_request, NULL);
542 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
546 c8dba1e6 2021-07-24 op handle_proceed(struct imsg *imsg, size_t datalen)
548 c8dba1e6 2021-07-24 op yield_r(req_by_id(imsg->hdr.peerid),
549 c8dba1e6 2021-07-24 op copy_body, NULL);
553 c8dba1e6 2021-07-24 op handle_stop(struct imsg *imsg, size_t datalen)
555 c8dba1e6 2021-07-24 op struct req *req;
557 c8dba1e6 2021-07-24 op if ((req = req_by_id_try(imsg->hdr.peerid)) == NULL)
559 c8dba1e6 2021-07-24 op close_conn(0, 0, req);
563 c8dba1e6 2021-07-24 op handle_quit(struct imsg *imsg, size_t datalen)
565 c8dba1e6 2021-07-24 op event_loopbreak();
569 c8dba1e6 2021-07-24 op handle_dispatch_imsg(int fd, short ev, void *d)
571 c8dba1e6 2021-07-24 op struct imsgev *iev = d;
573 c8dba1e6 2021-07-24 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
574 c8dba1e6 2021-07-24 op err(1, "connection closed");
578 c8dba1e6 2021-07-24 op net_send_ui(int type, uint32_t peerid, const void *data,
579 c8dba1e6 2021-07-24 op uint16_t datalen)
581 c8dba1e6 2021-07-24 op return imsg_compose_event(iev_ui, type, peerid, 0, -1,
582 c8dba1e6 2021-07-24 op data, datalen);
586 c8dba1e6 2021-07-24 op client_main(void)
588 c8dba1e6 2021-07-24 op setproctitle("net");
590 c8dba1e6 2021-07-24 op TAILQ_INIT(&reqhead);
592 c8dba1e6 2021-07-24 op if ((tlsconf = tls_config_new()) == NULL)
594 c8dba1e6 2021-07-24 op tls_config_insecure_noverifycert(tlsconf);
595 c8dba1e6 2021-07-24 op tls_config_insecure_noverifyname(tlsconf);
597 c8dba1e6 2021-07-24 op event_init();
599 c8dba1e6 2021-07-24 op /* Setup pipe and event handler to the main process */
600 c8dba1e6 2021-07-24 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
602 c8dba1e6 2021-07-24 op imsg_init(&iev_ui->ibuf, 3);
603 c8dba1e6 2021-07-24 op iev_ui->handler = handle_dispatch_imsg;
604 c8dba1e6 2021-07-24 op iev_ui->events = EV_READ;
605 c8dba1e6 2021-07-24 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
606 c8dba1e6 2021-07-24 op iev_ui->handler, iev_ui);
607 c8dba1e6 2021-07-24 op event_add(&iev_ui->ev, NULL);
609 c8dba1e6 2021-07-24 op sandbox_net_process();
611 c8dba1e6 2021-07-24 op event_dispatch();