1*e985b929SDavid van Moolenbroek /* $NetBSD: le-proxy.c,v 1.1.1.1 2013/04/11 16:43:31 christos Exp $ */
2*e985b929SDavid van Moolenbroek /*
3*e985b929SDavid van Moolenbroek This example code shows how to write an (optionally encrypting) SSL proxy
4*e985b929SDavid van Moolenbroek with Libevent's bufferevent layer.
5*e985b929SDavid van Moolenbroek
6*e985b929SDavid van Moolenbroek XXX It's a little ugly and should probably be cleaned up.
7*e985b929SDavid van Moolenbroek */
8*e985b929SDavid van Moolenbroek
9*e985b929SDavid van Moolenbroek #include <stdio.h>
10*e985b929SDavid van Moolenbroek #include <assert.h>
11*e985b929SDavid van Moolenbroek #include <stdlib.h>
12*e985b929SDavid van Moolenbroek #include <string.h>
13*e985b929SDavid van Moolenbroek #include <errno.h>
14*e985b929SDavid van Moolenbroek
15*e985b929SDavid van Moolenbroek #ifdef WIN32
16*e985b929SDavid van Moolenbroek #include <winsock2.h>
17*e985b929SDavid van Moolenbroek #include <ws2tcpip.h>
18*e985b929SDavid van Moolenbroek #else
19*e985b929SDavid van Moolenbroek #include <sys/socket.h>
20*e985b929SDavid van Moolenbroek #include <netinet/in.h>
21*e985b929SDavid van Moolenbroek #endif
22*e985b929SDavid van Moolenbroek
23*e985b929SDavid van Moolenbroek #include <event2/bufferevent_ssl.h>
24*e985b929SDavid van Moolenbroek #include <event2/bufferevent.h>
25*e985b929SDavid van Moolenbroek #include <event2/buffer.h>
26*e985b929SDavid van Moolenbroek #include <event2/listener.h>
27*e985b929SDavid van Moolenbroek #include <event2/util.h>
28*e985b929SDavid van Moolenbroek
29*e985b929SDavid van Moolenbroek #include <openssl/ssl.h>
30*e985b929SDavid van Moolenbroek #include <openssl/err.h>
31*e985b929SDavid van Moolenbroek #include <openssl/rand.h>
32*e985b929SDavid van Moolenbroek
33*e985b929SDavid van Moolenbroek static struct event_base *base;
34*e985b929SDavid van Moolenbroek static struct sockaddr_storage listen_on_addr;
35*e985b929SDavid van Moolenbroek static struct sockaddr_storage connect_to_addr;
36*e985b929SDavid van Moolenbroek static int connect_to_addrlen;
37*e985b929SDavid van Moolenbroek static int use_wrapper = 1;
38*e985b929SDavid van Moolenbroek
39*e985b929SDavid van Moolenbroek static SSL_CTX *ssl_ctx = NULL;
40*e985b929SDavid van Moolenbroek
41*e985b929SDavid van Moolenbroek #define MAX_OUTPUT (512*1024)
42*e985b929SDavid van Moolenbroek
43*e985b929SDavid van Moolenbroek static void drained_writecb(struct bufferevent *bev, void *ctx);
44*e985b929SDavid van Moolenbroek static void eventcb(struct bufferevent *bev, short what, void *ctx);
45*e985b929SDavid van Moolenbroek
46*e985b929SDavid van Moolenbroek static void
readcb(struct bufferevent * bev,void * ctx)47*e985b929SDavid van Moolenbroek readcb(struct bufferevent *bev, void *ctx)
48*e985b929SDavid van Moolenbroek {
49*e985b929SDavid van Moolenbroek struct bufferevent *partner = ctx;
50*e985b929SDavid van Moolenbroek struct evbuffer *src, *dst;
51*e985b929SDavid van Moolenbroek size_t len;
52*e985b929SDavid van Moolenbroek src = bufferevent_get_input(bev);
53*e985b929SDavid van Moolenbroek len = evbuffer_get_length(src);
54*e985b929SDavid van Moolenbroek if (!partner) {
55*e985b929SDavid van Moolenbroek evbuffer_drain(src, len);
56*e985b929SDavid van Moolenbroek return;
57*e985b929SDavid van Moolenbroek }
58*e985b929SDavid van Moolenbroek dst = bufferevent_get_output(partner);
59*e985b929SDavid van Moolenbroek evbuffer_add_buffer(dst, src);
60*e985b929SDavid van Moolenbroek
61*e985b929SDavid van Moolenbroek if (evbuffer_get_length(dst) >= MAX_OUTPUT) {
62*e985b929SDavid van Moolenbroek /* We're giving the other side data faster than it can
63*e985b929SDavid van Moolenbroek * pass it on. Stop reading here until we have drained the
64*e985b929SDavid van Moolenbroek * other side to MAX_OUTPUT/2 bytes. */
65*e985b929SDavid van Moolenbroek bufferevent_setcb(partner, readcb, drained_writecb,
66*e985b929SDavid van Moolenbroek eventcb, bev);
67*e985b929SDavid van Moolenbroek bufferevent_setwatermark(partner, EV_WRITE, MAX_OUTPUT/2,
68*e985b929SDavid van Moolenbroek MAX_OUTPUT);
69*e985b929SDavid van Moolenbroek bufferevent_disable(bev, EV_READ);
70*e985b929SDavid van Moolenbroek }
71*e985b929SDavid van Moolenbroek }
72*e985b929SDavid van Moolenbroek
73*e985b929SDavid van Moolenbroek static void
drained_writecb(struct bufferevent * bev,void * ctx)74*e985b929SDavid van Moolenbroek drained_writecb(struct bufferevent *bev, void *ctx)
75*e985b929SDavid van Moolenbroek {
76*e985b929SDavid van Moolenbroek struct bufferevent *partner = ctx;
77*e985b929SDavid van Moolenbroek
78*e985b929SDavid van Moolenbroek /* We were choking the other side until we drained our outbuf a bit.
79*e985b929SDavid van Moolenbroek * Now it seems drained. */
80*e985b929SDavid van Moolenbroek bufferevent_setcb(bev, readcb, NULL, eventcb, partner);
81*e985b929SDavid van Moolenbroek bufferevent_setwatermark(bev, EV_WRITE, 0, 0);
82*e985b929SDavid van Moolenbroek if (partner)
83*e985b929SDavid van Moolenbroek bufferevent_enable(partner, EV_READ);
84*e985b929SDavid van Moolenbroek }
85*e985b929SDavid van Moolenbroek
86*e985b929SDavid van Moolenbroek static void
close_on_finished_writecb(struct bufferevent * bev,void * ctx)87*e985b929SDavid van Moolenbroek close_on_finished_writecb(struct bufferevent *bev, void *ctx)
88*e985b929SDavid van Moolenbroek {
89*e985b929SDavid van Moolenbroek struct evbuffer *b = bufferevent_get_output(bev);
90*e985b929SDavid van Moolenbroek
91*e985b929SDavid van Moolenbroek if (evbuffer_get_length(b) == 0) {
92*e985b929SDavid van Moolenbroek bufferevent_free(bev);
93*e985b929SDavid van Moolenbroek }
94*e985b929SDavid van Moolenbroek }
95*e985b929SDavid van Moolenbroek
96*e985b929SDavid van Moolenbroek static void
eventcb(struct bufferevent * bev,short what,void * ctx)97*e985b929SDavid van Moolenbroek eventcb(struct bufferevent *bev, short what, void *ctx)
98*e985b929SDavid van Moolenbroek {
99*e985b929SDavid van Moolenbroek struct bufferevent *partner = ctx;
100*e985b929SDavid van Moolenbroek
101*e985b929SDavid van Moolenbroek if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
102*e985b929SDavid van Moolenbroek if (what & BEV_EVENT_ERROR) {
103*e985b929SDavid van Moolenbroek unsigned long err;
104*e985b929SDavid van Moolenbroek while ((err = (bufferevent_get_openssl_error(bev)))) {
105*e985b929SDavid van Moolenbroek const char *msg = (const char*)
106*e985b929SDavid van Moolenbroek ERR_reason_error_string(err);
107*e985b929SDavid van Moolenbroek const char *lib = (const char*)
108*e985b929SDavid van Moolenbroek ERR_lib_error_string(err);
109*e985b929SDavid van Moolenbroek const char *func = (const char*)
110*e985b929SDavid van Moolenbroek ERR_func_error_string(err);
111*e985b929SDavid van Moolenbroek fprintf(stderr,
112*e985b929SDavid van Moolenbroek "%s in %s %s\n", msg, lib, func);
113*e985b929SDavid van Moolenbroek }
114*e985b929SDavid van Moolenbroek if (errno)
115*e985b929SDavid van Moolenbroek perror("connection error");
116*e985b929SDavid van Moolenbroek }
117*e985b929SDavid van Moolenbroek
118*e985b929SDavid van Moolenbroek if (partner) {
119*e985b929SDavid van Moolenbroek /* Flush all pending data */
120*e985b929SDavid van Moolenbroek readcb(bev, ctx);
121*e985b929SDavid van Moolenbroek
122*e985b929SDavid van Moolenbroek if (evbuffer_get_length(
123*e985b929SDavid van Moolenbroek bufferevent_get_output(partner))) {
124*e985b929SDavid van Moolenbroek /* We still have to flush data from the other
125*e985b929SDavid van Moolenbroek * side, but when that's done, close the other
126*e985b929SDavid van Moolenbroek * side. */
127*e985b929SDavid van Moolenbroek bufferevent_setcb(partner,
128*e985b929SDavid van Moolenbroek NULL, close_on_finished_writecb,
129*e985b929SDavid van Moolenbroek eventcb, NULL);
130*e985b929SDavid van Moolenbroek bufferevent_disable(partner, EV_READ);
131*e985b929SDavid van Moolenbroek } else {
132*e985b929SDavid van Moolenbroek /* We have nothing left to say to the other
133*e985b929SDavid van Moolenbroek * side; close it. */
134*e985b929SDavid van Moolenbroek bufferevent_free(partner);
135*e985b929SDavid van Moolenbroek }
136*e985b929SDavid van Moolenbroek }
137*e985b929SDavid van Moolenbroek bufferevent_free(bev);
138*e985b929SDavid van Moolenbroek }
139*e985b929SDavid van Moolenbroek }
140*e985b929SDavid van Moolenbroek
141*e985b929SDavid van Moolenbroek static void
syntax(void)142*e985b929SDavid van Moolenbroek syntax(void)
143*e985b929SDavid van Moolenbroek {
144*e985b929SDavid van Moolenbroek fputs("Syntax:\n", stderr);
145*e985b929SDavid van Moolenbroek fputs(" le-proxy [-s] [-W] <listen-on-addr> <connect-to-addr>\n", stderr);
146*e985b929SDavid van Moolenbroek fputs("Example:\n", stderr);
147*e985b929SDavid van Moolenbroek fputs(" le-proxy 127.0.0.1:8888 1.2.3.4:80\n", stderr);
148*e985b929SDavid van Moolenbroek
149*e985b929SDavid van Moolenbroek exit(1);
150*e985b929SDavid van Moolenbroek }
151*e985b929SDavid van Moolenbroek
152*e985b929SDavid van Moolenbroek static void
accept_cb(struct evconnlistener * listener,evutil_socket_t fd,struct sockaddr * a,int slen,void * p)153*e985b929SDavid van Moolenbroek accept_cb(struct evconnlistener *listener, evutil_socket_t fd,
154*e985b929SDavid van Moolenbroek struct sockaddr *a, int slen, void *p)
155*e985b929SDavid van Moolenbroek {
156*e985b929SDavid van Moolenbroek struct bufferevent *b_out, *b_in;
157*e985b929SDavid van Moolenbroek /* Create two linked bufferevent objects: one to connect, one for the
158*e985b929SDavid van Moolenbroek * new connection */
159*e985b929SDavid van Moolenbroek b_in = bufferevent_socket_new(base, fd,
160*e985b929SDavid van Moolenbroek BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
161*e985b929SDavid van Moolenbroek
162*e985b929SDavid van Moolenbroek if (!ssl_ctx || use_wrapper)
163*e985b929SDavid van Moolenbroek b_out = bufferevent_socket_new(base, -1,
164*e985b929SDavid van Moolenbroek BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
165*e985b929SDavid van Moolenbroek else {
166*e985b929SDavid van Moolenbroek SSL *ssl = SSL_new(ssl_ctx);
167*e985b929SDavid van Moolenbroek b_out = bufferevent_openssl_socket_new(base, -1, ssl,
168*e985b929SDavid van Moolenbroek BUFFEREVENT_SSL_CONNECTING,
169*e985b929SDavid van Moolenbroek BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
170*e985b929SDavid van Moolenbroek }
171*e985b929SDavid van Moolenbroek
172*e985b929SDavid van Moolenbroek assert(b_in && b_out);
173*e985b929SDavid van Moolenbroek
174*e985b929SDavid van Moolenbroek if (bufferevent_socket_connect(b_out,
175*e985b929SDavid van Moolenbroek (struct sockaddr*)&connect_to_addr, connect_to_addrlen)<0) {
176*e985b929SDavid van Moolenbroek perror("bufferevent_socket_connect");
177*e985b929SDavid van Moolenbroek bufferevent_free(b_out);
178*e985b929SDavid van Moolenbroek bufferevent_free(b_in);
179*e985b929SDavid van Moolenbroek return;
180*e985b929SDavid van Moolenbroek }
181*e985b929SDavid van Moolenbroek
182*e985b929SDavid van Moolenbroek if (ssl_ctx && use_wrapper) {
183*e985b929SDavid van Moolenbroek struct bufferevent *b_ssl;
184*e985b929SDavid van Moolenbroek SSL *ssl = SSL_new(ssl_ctx);
185*e985b929SDavid van Moolenbroek b_ssl = bufferevent_openssl_filter_new(base,
186*e985b929SDavid van Moolenbroek b_out, ssl, BUFFEREVENT_SSL_CONNECTING,
187*e985b929SDavid van Moolenbroek BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
188*e985b929SDavid van Moolenbroek if (!b_ssl) {
189*e985b929SDavid van Moolenbroek perror("Bufferevent_openssl_new");
190*e985b929SDavid van Moolenbroek bufferevent_free(b_out);
191*e985b929SDavid van Moolenbroek bufferevent_free(b_in);
192*e985b929SDavid van Moolenbroek }
193*e985b929SDavid van Moolenbroek b_out = b_ssl;
194*e985b929SDavid van Moolenbroek }
195*e985b929SDavid van Moolenbroek
196*e985b929SDavid van Moolenbroek bufferevent_setcb(b_in, readcb, NULL, eventcb, b_out);
197*e985b929SDavid van Moolenbroek bufferevent_setcb(b_out, readcb, NULL, eventcb, b_in);
198*e985b929SDavid van Moolenbroek
199*e985b929SDavid van Moolenbroek bufferevent_enable(b_in, EV_READ|EV_WRITE);
200*e985b929SDavid van Moolenbroek bufferevent_enable(b_out, EV_READ|EV_WRITE);
201*e985b929SDavid van Moolenbroek }
202*e985b929SDavid van Moolenbroek
203*e985b929SDavid van Moolenbroek int
main(int argc,char ** argv)204*e985b929SDavid van Moolenbroek main(int argc, char **argv)
205*e985b929SDavid van Moolenbroek {
206*e985b929SDavid van Moolenbroek int i;
207*e985b929SDavid van Moolenbroek int socklen;
208*e985b929SDavid van Moolenbroek
209*e985b929SDavid van Moolenbroek int use_ssl = 0;
210*e985b929SDavid van Moolenbroek struct evconnlistener *listener;
211*e985b929SDavid van Moolenbroek
212*e985b929SDavid van Moolenbroek if (argc < 3)
213*e985b929SDavid van Moolenbroek syntax();
214*e985b929SDavid van Moolenbroek
215*e985b929SDavid van Moolenbroek for (i=1; i < argc; ++i) {
216*e985b929SDavid van Moolenbroek if (!strcmp(argv[i], "-s")) {
217*e985b929SDavid van Moolenbroek use_ssl = 1;
218*e985b929SDavid van Moolenbroek } else if (!strcmp(argv[i], "-W")) {
219*e985b929SDavid van Moolenbroek use_wrapper = 0;
220*e985b929SDavid van Moolenbroek } else if (argv[i][0] == '-') {
221*e985b929SDavid van Moolenbroek syntax();
222*e985b929SDavid van Moolenbroek } else
223*e985b929SDavid van Moolenbroek break;
224*e985b929SDavid van Moolenbroek }
225*e985b929SDavid van Moolenbroek
226*e985b929SDavid van Moolenbroek if (i+2 != argc)
227*e985b929SDavid van Moolenbroek syntax();
228*e985b929SDavid van Moolenbroek
229*e985b929SDavid van Moolenbroek memset(&listen_on_addr, 0, sizeof(listen_on_addr));
230*e985b929SDavid van Moolenbroek socklen = sizeof(listen_on_addr);
231*e985b929SDavid van Moolenbroek if (evutil_parse_sockaddr_port(argv[i],
232*e985b929SDavid van Moolenbroek (struct sockaddr*)&listen_on_addr, &socklen)<0) {
233*e985b929SDavid van Moolenbroek int p = atoi(argv[i]);
234*e985b929SDavid van Moolenbroek struct sockaddr_in *sin = (struct sockaddr_in*)&listen_on_addr;
235*e985b929SDavid van Moolenbroek if (p < 1 || p > 65535)
236*e985b929SDavid van Moolenbroek syntax();
237*e985b929SDavid van Moolenbroek sin->sin_port = htons(p);
238*e985b929SDavid van Moolenbroek sin->sin_addr.s_addr = htonl(0x7f000001);
239*e985b929SDavid van Moolenbroek sin->sin_family = AF_INET;
240*e985b929SDavid van Moolenbroek socklen = sizeof(struct sockaddr_in);
241*e985b929SDavid van Moolenbroek }
242*e985b929SDavid van Moolenbroek
243*e985b929SDavid van Moolenbroek memset(&connect_to_addr, 0, sizeof(connect_to_addr));
244*e985b929SDavid van Moolenbroek connect_to_addrlen = sizeof(connect_to_addr);
245*e985b929SDavid van Moolenbroek if (evutil_parse_sockaddr_port(argv[i+1],
246*e985b929SDavid van Moolenbroek (struct sockaddr*)&connect_to_addr, &connect_to_addrlen)<0)
247*e985b929SDavid van Moolenbroek syntax();
248*e985b929SDavid van Moolenbroek
249*e985b929SDavid van Moolenbroek base = event_base_new();
250*e985b929SDavid van Moolenbroek if (!base) {
251*e985b929SDavid van Moolenbroek perror("event_base_new()");
252*e985b929SDavid van Moolenbroek return 1;
253*e985b929SDavid van Moolenbroek }
254*e985b929SDavid van Moolenbroek
255*e985b929SDavid van Moolenbroek if (use_ssl) {
256*e985b929SDavid van Moolenbroek int r;
257*e985b929SDavid van Moolenbroek SSL_library_init();
258*e985b929SDavid van Moolenbroek ERR_load_crypto_strings();
259*e985b929SDavid van Moolenbroek SSL_load_error_strings();
260*e985b929SDavid van Moolenbroek OpenSSL_add_all_algorithms();
261*e985b929SDavid van Moolenbroek r = RAND_poll();
262*e985b929SDavid van Moolenbroek if (r == 0) {
263*e985b929SDavid van Moolenbroek fprintf(stderr, "RAND_poll() failed.\n");
264*e985b929SDavid van Moolenbroek return 1;
265*e985b929SDavid van Moolenbroek }
266*e985b929SDavid van Moolenbroek ssl_ctx = SSL_CTX_new(SSLv23_method());
267*e985b929SDavid van Moolenbroek }
268*e985b929SDavid van Moolenbroek
269*e985b929SDavid van Moolenbroek listener = evconnlistener_new_bind(base, accept_cb, NULL,
270*e985b929SDavid van Moolenbroek LEV_OPT_CLOSE_ON_FREE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_REUSEABLE,
271*e985b929SDavid van Moolenbroek -1, (struct sockaddr*)&listen_on_addr, socklen);
272*e985b929SDavid van Moolenbroek
273*e985b929SDavid van Moolenbroek event_base_dispatch(base);
274*e985b929SDavid van Moolenbroek
275*e985b929SDavid van Moolenbroek evconnlistener_free(listener);
276*e985b929SDavid van Moolenbroek event_base_free(base);
277*e985b929SDavid van Moolenbroek
278*e985b929SDavid van Moolenbroek return 0;
279*e985b929SDavid van Moolenbroek }
280