002
2021-07-24
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
004
2021-07-24
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-07-24
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-07-24
op
* copyright notice and this permission notice appear in all copies.
008
2021-07-24
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-07-24
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-07-24
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-07-24
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-07-24
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-07-24
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-07-24
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2021-07-24
op
#include "compat.h"
019
2021-07-24
op
#include <sys/types.h>
020
2021-07-24
op
#include <sys/socket.h>
022
2021-07-24
op
#include <netinet/in.h>
024
2021-07-24
op
#include <assert.h>
025
2021-07-24
op
#include <ctype.h>
026
2021-07-24
op
#include <errno.h>
027
2021-07-24
op
#include <netdb.h>
028
2021-07-24
op
#include <stdarg.h>
029
2021-07-24
op
#include <stdio.h>
030
2021-07-24
op
#include <stdlib.h>
031
2021-07-24
op
#include <string.h>
032
2021-07-24
op
#include <tls.h>
033
2021-07-24
op
#include <unistd.h>
035
2021-07-24
op
#if HAVE_ASR_RUN
036
2021-07-24
op
# include <asr.h>
039
2021-07-24
op
#include "telescope.h"
040
2022-01-11
op
#include "utils.h"
042
2021-07-24
op
static struct imsgev *iev_ui;
044
2021-07-24
op
/* a pending request */
045
2021-07-24
op
struct req {
046
2021-07-24
op
struct phos_uri url;
047
2021-07-24
op
uint32_t id;
048
2021-07-25
op
int proto;
050
2021-07-24
op
struct tls *ctx;
051
2021-07-24
op
char req[1024];
052
2021-07-24
op
size_t len;
053
2021-07-24
op
int done_header;
054
2021-07-24
op
struct bufferevent *bev;
056
2021-07-24
op
struct addrinfo *servinfo, *p;
057
2021-07-24
op
#if HAVE_ASR_RUN
058
2021-07-24
op
struct addrinfo hints;
059
2021-07-24
op
struct event_asr *asrev;
062
2021-07-24
op
TAILQ_ENTRY(req) reqs;
065
2021-07-24
op
static struct req *req_by_id(uint32_t);
067
2021-07-24
op
static void die(void) __attribute__((__noreturn__));
069
2021-07-24
op
static void try_to_connect(int, short, void*);
071
2021-07-24
op
#if HAVE_ASR_RUN
072
2021-07-24
op
static void query_done(struct asr_result*, void*);
073
2021-07-24
op
static void async_conn_towards(struct req*);
075
2021-07-24
op
static void blocking_conn_towards(struct req*);
078
2021-07-24
op
static void close_with_err(struct req*, const char*);
079
2021-07-24
op
static void close_with_errf(struct req*, const char*, ...)
080
2021-07-24
op
__attribute__((format(printf, 2, 3)));
082
2021-07-24
op
static void net_tls_handshake(int, short, void *);
083
2021-07-24
op
static void net_tls_readcb(int, short, void *);
084
2021-07-24
op
static void net_tls_writecb(int, short, void *);
086
2021-07-24
op
static int gemini_parse_reply(struct req *, const char *, size_t);
088
2021-07-24
op
static void net_ready(struct req *req);
089
2021-07-24
op
static void net_read(struct bufferevent *, void *);
090
2021-07-24
op
static void net_write(struct bufferevent *, void *);
091
2021-07-24
op
static void net_error(struct bufferevent *, short, void *);
093
2021-07-24
op
static void handle_get_raw(struct imsg *, size_t);
094
2021-07-24
op
static void handle_cert_status(struct imsg*, size_t);
095
2021-07-24
op
static void handle_proceed(struct imsg*, size_t);
096
2021-07-24
op
static void handle_stop(struct imsg*, size_t);
097
2021-07-24
op
static void handle_quit(struct imsg*, size_t);
098
2021-07-24
op
static void handle_dispatch_imsg(int, short, void*);
100
2021-07-24
op
static int net_send_ui(int, uint32_t, const void *, uint16_t);
102
2021-07-24
op
/* TODO: making this customizable */
103
2021-07-24
op
struct timeval timeout_for_handshake = { 5, 0 };
105
2021-07-24
op
static imsg_handlerfn *handlers[] = {
106
2021-07-24
op
[IMSG_GET_RAW] = handle_get_raw,
107
2021-07-24
op
[IMSG_CERT_STATUS] = handle_cert_status,
108
2021-07-24
op
[IMSG_PROCEED] = handle_proceed,
109
2021-07-24
op
[IMSG_STOP] = handle_stop,
110
2021-07-24
op
[IMSG_QUIT] = handle_quit,
113
2021-07-24
op
typedef void (*statefn)(int, short, void*);
115
2021-07-24
op
TAILQ_HEAD(, req) reqhead;
117
2021-07-24
op
static inline void
118
2021-07-24
op
yield_r(struct req *req, statefn fn, struct timeval *tv)
120
2021-07-24
op
event_once(req->fd, EV_READ, fn, req, tv);
123
2021-07-24
op
static inline void
124
2021-07-24
op
yield_w(struct req *req, statefn fn, struct timeval *tv)
126
2021-07-24
op
event_once(req->fd, EV_WRITE, fn, req, tv);
129
2021-07-24
op
static struct req *
130
2021-07-24
op
req_by_id(uint32_t id)
132
2021-07-24
op
struct req *r;
134
2021-07-24
op
TAILQ_FOREACH(r, &reqhead, reqs) {
135
2021-07-24
op
if (r->id == id)
136
2021-07-24
op
return r;
139
2021-07-24
op
return NULL;
142
2021-07-24
op
static void __attribute__((__noreturn__))
145
2021-07-24
op
abort(); /* TODO */
148
2021-07-24
op
static void
149
2021-07-24
op
try_to_connect(int fd, short ev, void *d)
151
2021-07-24
op
struct req *req = d;
152
2021-07-24
op
int error = 0;
153
2021-07-24
op
socklen_t len = sizeof(error);
156
2021-07-24
op
if (req->p == NULL)
157
2021-07-24
op
goto err;
159
2021-07-24
op
if (req->fd != -1) {
160
2021-07-24
op
if (getsockopt(req->fd, SOL_SOCKET, SO_ERROR, &error,
161
2021-07-24
op
&len) == -1)
162
2021-07-24
op
goto err;
163
2021-07-24
op
if (error != 0) {
164
2021-08-26
op
errno = error;
165
2021-08-26
op
goto err;
167
2021-07-24
op
goto done;
170
2021-07-24
op
req->fd = socket(req->p->ai_family, req->p->ai_socktype,
171
2021-07-24
op
req->p->ai_protocol);
172
2021-07-24
op
if (req->fd == -1) {
173
2021-07-24
op
req->p = req->p->ai_next;
174
2021-07-24
op
goto again;
176
2021-07-24
op
mark_nonblock(req->fd);
177
2021-07-24
op
if (connect(req->fd, req->p->ai_addr, req->p->ai_addrlen) == 0)
178
2021-07-24
op
goto done;
179
2021-07-24
op
yield_w(req, try_to_connect, NULL);
184
2021-07-24
op
freeaddrinfo(req->servinfo);
185
2021-07-24
op
close_with_errf(req, "failed to connect to %s",
186
2021-07-24
op
req->url.host);
190
2021-07-24
op
freeaddrinfo(req->servinfo);
192
2021-07-25
op
switch (req->proto) {
193
2021-07-25
op
case PROTO_FINGER:
194
2021-07-25
op
case PROTO_GOPHER:
195
2021-07-25
op
/* finger and gopher don't have a header nor TLS */
196
2021-07-25
op
req->done_header = 1;
197
2021-07-25
op
net_ready(req);
200
2022-01-04
op
case PROTO_GEMINI: {
201
2022-01-04
op
struct tls_config *conf;
203
2022-01-04
op
if ((conf = tls_config_new()) == NULL)
206
2022-01-04
op
tls_config_insecure_noverifycert(conf);
207
2022-01-04
op
tls_config_insecure_noverifyname(conf);
209
2021-07-25
op
/* prepare tls */
210
2021-07-25
op
if ((req->ctx = tls_client()) == NULL) {
211
2021-07-25
op
close_with_errf(req, "tls_client: %s",
212
2021-07-25
op
strerror(errno));
216
2022-01-04
op
if (tls_configure(req->ctx, conf) == -1) {
217
2021-07-25
op
close_with_errf(req, "tls_configure: %s",
218
2021-07-25
op
tls_error(req->ctx));
221
2022-01-04
op
tls_config_free(conf);
223
2021-07-25
op
if (tls_connect_socket(req->ctx, req->fd, req->url.host)
225
2021-07-25
op
close_with_errf(req, "tls_connect_socket: %s",
226
2021-07-25
op
tls_error(req->ctx));
229
2021-07-25
op
yield_w(req, net_tls_handshake, &timeout_for_handshake);
238
2021-07-24
op
#if HAVE_ASR_RUN
239
2021-07-24
op
static void
240
2021-07-24
op
query_done(struct asr_result *res, void *d)
242
2021-07-24
op
struct req *req = d;
244
2021-07-24
op
req->asrev = NULL;
245
2021-07-24
op
if (res->ar_gai_errno != 0) {
246
2021-07-24
op
close_with_errf(req, "failed to resolve %s: %s",
247
2021-07-24
op
req->url.host, gai_strerror(res->ar_gai_errno));
251
2021-07-24
op
req->fd = -1;
252
2021-07-24
op
req->servinfo = res->ar_addrinfo;
253
2021-07-24
op
req->p = res->ar_addrinfo;
254
2021-07-24
op
try_to_connect(0, 0, req);
257
2021-07-24
op
static void
258
2021-07-24
op
async_conn_towards(struct req *req)
260
2021-07-24
op
struct asr_query *q;
261
2021-07-24
op
const char *proto = "1965";
263
2021-07-24
op
if (*req->url.port != '\0')
264
2021-07-24
op
proto = req->url.port;
266
2021-07-24
op
req->hints.ai_family = AF_UNSPEC;
267
2021-07-24
op
req->hints.ai_socktype = SOCK_STREAM;
268
2021-07-24
op
q = getaddrinfo_async(req->url.host, proto, &req->hints, NULL);
269
2021-07-24
op
req->asrev = event_asr_run(q, query_done, req);
272
2021-07-24
op
static void
273
2021-07-24
op
blocking_conn_towards(struct req *req)
275
2021-07-24
op
struct addrinfo hints;
276
2021-07-24
op
struct phos_uri *url = &req->url;
277
2021-07-24
op
int status;
278
2021-07-24
op
const char *proto = "1965";
280
2021-07-24
op
if (*url->port != '\0')
281
2021-07-24
op
proto = url->port;
283
2021-07-24
op
memset(&hints, 0, sizeof(hints));
284
2021-07-24
op
hints.ai_family = AF_UNSPEC;
285
2021-07-24
op
hints.ai_socktype = SOCK_STREAM;
287
2021-07-24
op
if ((status = getaddrinfo(url->host, proto, &hints, &req->servinfo))) {
288
2021-07-24
op
close_with_errf(req, "failed to resolve %s: %s",
289
2021-07-24
op
url->host, gai_strerror(status));
293
2021-07-24
op
req->fd = -1;
294
2021-07-24
op
req->p = req->servinfo;
295
2021-07-24
op
try_to_connect(0, 0, req);
299
2021-07-24
op
static void
300
2021-07-24
op
close_conn(int fd, short ev, void *d)
302
2021-07-24
op
struct req *req = d;
304
2021-07-24
op
#if HAVE_ASR_RUN
305
2021-07-24
op
if (req->asrev != NULL)
306
2021-07-24
op
event_asr_abort(req->asrev);
309
2021-07-24
op
if (req->bev != NULL) {
310
2021-07-24
op
bufferevent_free(req->bev);
311
2021-07-24
op
req->bev = NULL;
314
2021-07-24
op
if (req->ctx != NULL) {
315
2021-07-24
op
switch (tls_close(req->ctx)) {
316
2021-07-24
op
case TLS_WANT_POLLIN:
317
2021-07-24
op
yield_r(req, close_conn, NULL);
319
2021-07-24
op
case TLS_WANT_POLLOUT:
320
2021-07-24
op
yield_w(req, close_conn, NULL);
324
2021-07-24
op
tls_free(req->ctx);
325
2021-07-24
op
req->ctx = NULL;
328
2021-07-24
op
TAILQ_REMOVE(&reqhead, req, reqs);
329
2021-07-24
op
if (req->fd != -1)
330
2021-07-24
op
close(req->fd);
331
2021-07-24
op
free(req);
334
2021-07-24
op
static void
335
2021-07-24
op
close_with_err(struct req *req, const char *err)
337
2021-07-24
op
net_send_ui(IMSG_ERR, req->id, err, strlen(err)+1);
338
2021-07-24
op
close_conn(0, 0, req);
341
2021-07-24
op
static void
342
2021-07-24
op
close_with_errf(struct req *req, const char *fmt, ...)
344
2021-07-24
op
va_list ap;
347
2021-07-24
op
va_start(ap, fmt);
348
2021-07-24
op
if (vasprintf(&s, fmt, ap) == -1)
350
2021-07-24
op
va_end(ap);
352
2021-07-24
op
close_with_err(req, s);
356
2021-07-24
op
static void
357
2021-07-24
op
net_tls_handshake(int fd, short event, void *d)
359
2021-07-24
op
struct req *req = d;
360
2021-07-24
op
const char *hash;
362
2021-07-24
op
if (event == EV_TIMEOUT) {
363
2021-07-24
op
close_with_err(req, "Timeout loading page");
367
2021-07-24
op
switch (tls_handshake(req->ctx)) {
368
2021-07-24
op
case TLS_WANT_POLLIN:
369
2021-07-24
op
yield_r(req, net_tls_handshake, NULL);
371
2021-07-24
op
case TLS_WANT_POLLOUT:
372
2021-07-24
op
yield_w(req, net_tls_handshake, NULL);
376
2021-07-24
op
hash = tls_peer_cert_hash(req->ctx);
377
2021-07-24
op
if (hash == NULL) {
378
2021-07-24
op
close_with_errf(req, "handshake failed: %s",
379
2021-07-24
op
tls_error(req->ctx));
382
2021-07-24
op
net_send_ui(IMSG_CHECK_CERT, req->id, hash, strlen(hash)+1);
385
2021-07-24
op
static void
386
2021-07-24
op
net_tls_readcb(int fd, short event, void *d)
388
2021-07-24
op
struct bufferevent *bufev = d;
389
2021-07-24
op
struct req *req = bufev->cbarg;
390
2021-07-25
op
char buf[IBUF_READ_SIZE];
391
2021-07-24
op
int what = EVBUFFER_READ;
392
2021-07-24
op
int howmuch = IBUF_READ_SIZE;
394
2021-07-24
op
ssize_t ret;
395
2021-07-24
op
size_t len;
397
2021-07-24
op
if (event == EV_TIMEOUT) {
398
2021-07-24
op
what |= EVBUFFER_TIMEOUT;
399
2021-07-24
op
goto err;
402
2021-07-24
op
if (bufev->wm_read.high != 0)
403
2021-07-24
op
howmuch = MIN(sizeof(buf), bufev->wm_read.high);
405
2021-07-24
op
switch (ret = tls_read(req->ctx, buf, howmuch)) {
406
2021-07-24
op
case TLS_WANT_POLLIN:
407
2021-07-24
op
case TLS_WANT_POLLOUT:
408
2021-07-24
op
goto retry;
410
2021-07-24
op
what |= EVBUFFER_ERROR;
411
2021-07-24
op
goto err;
413
2021-07-24
op
len = ret;
415
2021-07-24
op
if (len == 0) {
416
2021-07-24
op
what |= EVBUFFER_EOF;
417
2021-07-24
op
goto err;
420
2021-07-27
op
res = evbuffer_add(bufev->input, buf, len);
421
2021-07-27
op
if (res == -1) {
422
2021-07-24
op
what |= EVBUFFER_ERROR;
423
2021-07-24
op
goto err;
426
2021-07-24
op
event_add(&bufev->ev_read, NULL);
428
2021-07-24
op
len = EVBUFFER_LENGTH(bufev->input);
429
2021-07-24
op
if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
432
2021-07-24
op
if (bufev->readcb != NULL)
433
2021-07-24
op
(*bufev->readcb)(bufev, bufev->cbarg);
437
2021-07-24
op
event_add(&bufev->ev_read, NULL);
441
2021-07-24
op
(*bufev->errorcb)(bufev, what, bufev->cbarg);
444
2021-07-24
op
static void
445
2021-07-24
op
net_tls_writecb(int fd, short event, void *d)
447
2021-07-24
op
struct bufferevent *bufev = d;
448
2021-07-24
op
struct req *req = bufev->cbarg;
449
2021-07-24
op
ssize_t ret;
450
2021-07-24
op
size_t len;
451
2021-07-24
op
short what = EVBUFFER_WRITE;
453
2021-07-24
op
if (event & EV_TIMEOUT) {
454
2021-07-24
op
what |= EVBUFFER_TIMEOUT;
455
2021-07-24
op
goto err;
458
2021-07-24
op
if (EVBUFFER_LENGTH(bufev->output) != 0) {
459
2021-07-24
op
ret = tls_write(req->ctx, EVBUFFER_DATA(bufev->output),
460
2021-07-24
op
EVBUFFER_LENGTH(bufev->output));
461
2021-07-24
op
switch (ret) {
462
2021-07-24
op
case TLS_WANT_POLLIN:
463
2021-07-24
op
case TLS_WANT_POLLOUT:
464
2021-07-24
op
goto retry;
466
2021-07-24
op
what |= EVBUFFER_ERROR;
467
2021-07-24
op
goto err;
469
2021-07-24
op
len = ret;
471
2021-07-24
op
evbuffer_drain(bufev->output, len);
474
2021-07-24
op
if (EVBUFFER_LENGTH(bufev->output) != 0)
475
2021-07-24
op
event_add(&bufev->ev_write, NULL);
477
2021-07-24
op
if (bufev->writecb != NULL &&
478
2021-07-24
op
EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
479
2021-07-24
op
(*bufev->writecb)(bufev, bufev->cbarg);
483
2021-07-24
op
event_add(&bufev->ev_write, NULL);
487
2021-07-24
op
(*bufev->errorcb)(bufev, what, bufev->cbarg);
490
2021-07-24
op
static int
491
2021-07-24
op
gemini_parse_reply(struct req *req, const char *header, size_t len)
493
2021-07-24
op
int code;
494
2021-07-24
op
const char *t;
496
2021-07-24
op
if (len < 4)
497
2021-07-24
op
return 0;
499
2021-07-24
op
if (!isdigit(header[0]) || !isdigit(header[1]))
500
2021-07-24
op
return 0;
502
2021-07-24
op
code = (header[0] - '0')*10 + (header[1] - '0');
503
2021-07-24
op
if (header[2] != ' ')
504
2021-07-24
op
return 0;
506
2021-07-24
op
t = header + 3;
508
2021-07-24
op
net_send_ui(IMSG_GOT_CODE, req->id, &code, sizeof(code));
509
2021-07-24
op
net_send_ui(IMSG_GOT_META, req->id, t, strlen(t)+1);
511
2021-07-24
op
bufferevent_disable(req->bev, EV_READ|EV_WRITE);
513
2022-01-21
op
return code;
516
2021-07-24
op
/* called when we're ready to read/write */
517
2021-07-24
op
static void
518
2021-07-24
op
net_ready(struct req *req)
520
2021-07-24
op
req->bev = bufferevent_new(req->fd, net_read, net_write, net_error,
522
2021-07-24
op
if (req->bev == NULL)
525
2021-07-28
op
#if HAVE_EVENT2
526
2021-07-27
op
evbuffer_unfreeze(req->bev->input, 0);
527
2021-07-27
op
evbuffer_unfreeze(req->bev->output, 1);
530
2021-07-24
op
/* setup tls i/o layer */
531
2021-07-24
op
if (req->ctx != NULL) {
532
2021-07-24
op
event_set(&req->bev->ev_read, req->fd, EV_READ,
533
2021-07-24
op
net_tls_readcb, req->bev);
534
2021-07-24
op
event_set(&req->bev->ev_write, req->fd, EV_WRITE,
535
2021-07-24
op
net_tls_writecb, req->bev);
538
2021-07-24
op
/* TODO: adjust watermarks */
539
2021-07-24
op
bufferevent_setwatermark(req->bev, EV_WRITE, 1, 0);
540
2021-07-24
op
bufferevent_setwatermark(req->bev, EV_READ, 1, 0);
542
2021-07-24
op
bufferevent_enable(req->bev, EV_READ|EV_WRITE);
544
2021-07-24
op
bufferevent_write(req->bev, req->req, req->len);
547
2021-07-24
op
/* called after a read has been done */
548
2021-07-24
op
static void
549
2021-07-24
op
net_read(struct bufferevent *bev, void *d)
551
2021-07-24
op
struct req *req = d;
552
2021-07-24
op
struct evbuffer *src = EVBUFFER_INPUT(bev);
553
2022-01-20
op
uint8_t *data;
554
2021-09-12
op
size_t len, chunk;
556
2021-07-24
op
char *header;
558
2021-07-24
op
if (!req->done_header) {
559
2021-07-24
op
header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
560
2021-07-24
op
if (header == NULL && EVBUFFER_LENGTH(src) >= 1024)
561
2021-07-24
op
goto err;
562
2021-07-24
op
if (header == NULL)
564
2021-07-24
op
r = gemini_parse_reply(req, header, len);
565
2021-07-24
op
free(header);
566
2022-01-21
op
req->done_header = 1;
567
2022-01-21
op
if (r == 0)
568
2021-07-24
op
goto err;
569
2022-01-21
op
else if (r < 20 || r >= 30)
570
2022-01-21
op
close_conn(0, 0, req);
574
2021-07-24
op
if ((len = EVBUFFER_LENGTH(src)) == 0)
576
2021-07-24
op
data = EVBUFFER_DATA(src);
579
2021-09-12
op
* Split data into chunks before sending. imsg can't handle
580
2021-09-12
op
* message that are "too big".
582
2021-09-12
op
while (len != 0) {
583
2021-09-12
op
chunk = MIN(len, 4096);
584
2021-09-12
op
net_send_ui(IMSG_BUF, req->id, data, chunk);
585
2021-09-12
op
data += chunk;
586
2021-09-12
op
len -= chunk;
589
2021-09-12
op
evbuffer_drain(src, EVBUFFER_LENGTH(src));
593
2021-07-24
op
(*bev->errorcb)(bev, EVBUFFER_READ, bev->cbarg);
596
2021-07-24
op
/* called after a write has been done */
597
2021-07-24
op
static void
598
2021-07-24
op
net_write(struct bufferevent *bev, void *d)
600
2021-07-24
op
struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
602
2021-07-24
op
if (EVBUFFER_LENGTH(dst) == 0)
603
2021-07-24
op
(*bev->errorcb)(bev, EVBUFFER_WRITE, bev->cbarg);
606
2021-07-24
op
static void
607
2021-07-24
op
net_error(struct bufferevent *bev, short error, void *d)
609
2021-07-24
op
struct req *req = d;
610
2021-07-25
op
struct evbuffer *src;
612
2021-07-24
op
if (error & EVBUFFER_TIMEOUT) {
613
2021-07-24
op
close_with_err(req, "Timeout loading page");
617
2021-07-24
op
if (error & EVBUFFER_ERROR) {
618
2021-07-24
op
close_with_err(req, "buffer event error");
622
2021-07-24
op
if (error & EVBUFFER_EOF) {
623
2021-11-10
op
/* EOF and no header */
624
2021-11-10
op
if (!req->done_header) {
625
2021-11-10
op
close_with_err(req, "protocol error");
629
2021-08-26
op
src = EVBUFFER_INPUT(req->bev);
630
2021-07-25
op
if (EVBUFFER_LENGTH(src) != 0)
631
2021-07-25
op
net_send_ui(IMSG_BUF, req->id, EVBUFFER_DATA(src),
632
2021-07-25
op
EVBUFFER_LENGTH(src));
633
2021-07-24
op
net_send_ui(IMSG_EOF, req->id, NULL, 0);
634
2021-07-24
op
close_conn(0, 0, req);
638
2021-07-24
op
if (error & EVBUFFER_WRITE) {
639
2021-07-24
op
/* finished sending request */
640
2021-07-24
op
bufferevent_disable(bev, EV_WRITE);
644
2021-07-24
op
if (error & EVBUFFER_READ) {
645
2021-07-24
op
close_with_err(req, "protocol error");
649
2021-07-24
op
close_with_errf(req, "unknown event error %x", error);
652
2021-07-24
op
static void
653
2021-07-24
op
handle_get_raw(struct imsg *imsg, size_t datalen)
655
2021-07-24
op
struct req *req;
656
2021-07-24
op
struct get_req *r;
658
2021-07-24
op
r = imsg->data;
660
2021-07-24
op
if (datalen != sizeof(*r))
663
2021-07-24
op
if ((req = calloc(1, sizeof(*req))) == NULL)
666
2021-07-24
op
req->id = imsg->hdr.peerid;
667
2021-07-24
op
TAILQ_INSERT_HEAD(&reqhead, req, reqs);
669
2021-07-24
op
strlcpy(req->url.host, r->host, sizeof(req->url.host));
670
2021-07-24
op
strlcpy(req->url.port, r->port, sizeof(req->url.port));
672
2021-07-24
op
strlcpy(req->req, r->req, sizeof(req->req));
673
2021-07-24
op
req->len = strlen(r->req);
675
2021-07-25
op
req->proto = r->proto;
677
2021-07-24
op
#if HAVE_ASR_RUN
678
2021-08-26
op
async_conn_towards(req);
680
2021-07-24
op
blocking_conn_towards(req);
684
2021-07-24
op
static void
685
2021-07-24
op
handle_cert_status(struct imsg *imsg, size_t datalen)
687
2021-07-24
op
struct req *req;
688
2021-07-24
op
int is_ok;
690
2021-07-24
op
req = req_by_id(imsg->hdr.peerid);
692
2021-07-24
op
if (datalen < sizeof(is_ok))
694
2021-07-24
op
memcpy(&is_ok, imsg->data, sizeof(is_ok));
696
2021-07-24
op
if (is_ok)
697
2021-07-24
op
net_ready(req);
699
2021-07-24
op
close_conn(0, 0, req);
702
2021-07-24
op
static void
703
2021-07-24
op
handle_proceed(struct imsg *imsg, size_t datalen)
705
2021-07-24
op
struct req *req;
707
2021-07-24
op
if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
710
2021-07-24
op
bufferevent_enable(req->bev, EV_READ);
713
2021-07-24
op
static void
714
2021-07-24
op
handle_stop(struct imsg *imsg, size_t datalen)
716
2021-07-24
op
struct req *req;
718
2021-07-24
op
if ((req = req_by_id(imsg->hdr.peerid)) == NULL)
720
2021-07-24
op
close_conn(0, 0, req);
723
2021-07-24
op
static void
724
2021-07-24
op
handle_quit(struct imsg *imsg, size_t datalen)
726
2021-07-24
op
event_loopbreak();
729
2021-07-24
op
static void
730
2021-07-24
op
handle_dispatch_imsg(int fd, short ev, void *d)
732
2021-07-24
op
struct imsgev *iev = d;
734
2021-07-24
op
if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
735
2021-07-24
op
err(1, "connection closed");
738
2021-07-24
op
static int
739
2021-07-24
op
net_send_ui(int type, uint32_t peerid, const void *data,
740
2021-07-24
op
uint16_t datalen)
742
2021-07-24
op
return imsg_compose_event(iev_ui, type, peerid, 0, -1,
743
2021-07-24
op
data, datalen);
747
2021-07-24
op
net_main(void)
749
2021-07-24
op
setproctitle("net");
751
2021-07-24
op
TAILQ_INIT(&reqhead);
753
2021-07-24
op
event_init();
755
2021-07-24
op
/* Setup pipe and event handler to the main process */
756
2021-07-24
op
if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
758
2021-07-24
op
imsg_init(&iev_ui->ibuf, 3);
759
2021-07-24
op
iev_ui->handler = handle_dispatch_imsg;
760
2021-07-24
op
iev_ui->events = EV_READ;
761
2021-07-24
op
event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
762
2021-07-24
op
iev_ui->handler, iev_ui);
763
2021-07-24
op
event_add(&iev_ui->ev, NULL);
765
2021-07-24
op
sandbox_net_process();
767
2021-07-24
op
event_dispatch();
769
2021-07-24
op
msgbuf_clear(&iev_ui->ibuf.w);
770
2021-07-24
op
close(iev_ui->ibuf.fd);
771
2021-07-24
op
free(iev_ui);
773
2021-07-24
op
return 0;