Blame


1 cfe57149 2022-07-30 op /*
2 cfe57149 2022-07-30 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 cfe57149 2022-07-30 op *
4 cfe57149 2022-07-30 op * Permission to use, copy, modify, and distribute this software for any
5 cfe57149 2022-07-30 op * purpose with or without fee is hereby granted, provided that the above
6 cfe57149 2022-07-30 op * copyright notice and this permission notice appear in all copies.
7 cfe57149 2022-07-30 op *
8 cfe57149 2022-07-30 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 cfe57149 2022-07-30 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 cfe57149 2022-07-30 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 cfe57149 2022-07-30 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 cfe57149 2022-07-30 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 cfe57149 2022-07-30 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 cfe57149 2022-07-30 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 cfe57149 2022-07-30 op */
16 cfe57149 2022-07-30 op
17 cfe57149 2022-07-30 op #include "config.h"
18 cfe57149 2022-07-30 op
19 cfe57149 2022-07-30 op #if !HAVE_SO_SPLICE
20 cfe57149 2022-07-30 op
21 cfe57149 2022-07-30 op #include <sys/socket.h>
22 cfe57149 2022-07-30 op
23 cfe57149 2022-07-30 op #include "log.h"
24 cfe57149 2022-07-30 op #include "lstun.h"
25 cfe57149 2022-07-30 op
26 cfe57149 2022-07-30 op static void
27 cfe57149 2022-07-30 op nopcb(struct bufferevent *bev, void *d)
28 cfe57149 2022-07-30 op {
29 cfe57149 2022-07-30 op return;
30 cfe57149 2022-07-30 op }
31 cfe57149 2022-07-30 op
32 cfe57149 2022-07-30 op static void
33 cfe57149 2022-07-30 op sreadcb(struct bufferevent *bev, void *d)
34 cfe57149 2022-07-30 op {
35 cfe57149 2022-07-30 op struct conn *c = d;
36 cfe57149 2022-07-30 op
37 cfe57149 2022-07-30 op bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
38 cfe57149 2022-07-30 op }
39 cfe57149 2022-07-30 op
40 cfe57149 2022-07-30 op static void
41 cfe57149 2022-07-30 op treadcb(struct bufferevent *bev, void *d)
42 cfe57149 2022-07-30 op {
43 cfe57149 2022-07-30 op struct conn *c = d;
44 cfe57149 2022-07-30 op
45 cfe57149 2022-07-30 op bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
46 cfe57149 2022-07-30 op }
47 cfe57149 2022-07-30 op
48 cfe57149 2022-07-30 op static void
49 cfe57149 2022-07-30 op errcb(struct bufferevent *bev, short event, void *d)
50 cfe57149 2022-07-30 op {
51 cfe57149 2022-07-30 op struct conn *c = d;
52 cfe57149 2022-07-30 op
53 cfe57149 2022-07-30 op log_info("closing connection (event=%x)", event);
54 cfe57149 2022-07-30 op conn_free(c);
55 cfe57149 2022-07-30 op }
56 cfe57149 2022-07-30 op
57 cfe57149 2022-07-30 op int
58 cfe57149 2022-07-30 op conn_splice(struct conn *c)
59 cfe57149 2022-07-30 op {
60 cfe57149 2022-07-30 op c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
61 cfe57149 2022-07-30 op c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
62 cfe57149 2022-07-30 op
63 cfe57149 2022-07-30 op if (c->sourcebev == NULL || c->tobev == NULL) {
64 cfe57149 2022-07-30 op log_warn("bufferevent_new");
65 cfe57149 2022-07-30 op return -1;
66 cfe57149 2022-07-30 op }
67 cfe57149 2022-07-30 op
68 cfe57149 2022-07-30 op bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
69 cfe57149 2022-07-30 op bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
70 cfe57149 2022-07-30 op return 0;
71 cfe57149 2022-07-30 op }
72 cfe57149 2022-07-30 op
73 cfe57149 2022-07-30 op #endif /* !HAVE_SO_SPLICE */