xref: /libevent-2.1.12/sample/le-proxy.c (revision 2a1e1530)
1d60a1bd5SNick Mathewson /*
2d60a1bd5SNick Mathewson   This example code shows how to write an (optionally encrypting) SSL proxy
3d60a1bd5SNick Mathewson   with Libevent's bufferevent layer.
4d60a1bd5SNick Mathewson 
5d60a1bd5SNick Mathewson   XXX It's a little ugly and should probably be cleaned up.
6d60a1bd5SNick Mathewson  */
7709c21c4SNick Mathewson 
869c3516bSJoakim Söderberg // Get rid of OSX 10.7 and greater deprecation warnings.
98b40a5b0SJoakim Söderberg #if defined(__APPLE__) && defined(__clang__)
1069c3516bSJoakim Söderberg #pragma clang diagnostic ignored "-Wdeprecated-declarations"
110ef1d04eSJoakim Soderberg #endif
1269c3516bSJoakim Söderberg 
13709c21c4SNick Mathewson #include <stdio.h>
14709c21c4SNick Mathewson #include <assert.h>
15709c21c4SNick Mathewson #include <stdlib.h>
16709c21c4SNick Mathewson #include <string.h>
17709c21c4SNick Mathewson #include <errno.h>
18709c21c4SNick Mathewson 
199f560bfaSNick Mathewson #ifdef _WIN32
2013b912e4STrond Norbye #include <winsock2.h>
2113b912e4STrond Norbye #include <ws2tcpip.h>
2213b912e4STrond Norbye #else
23709c21c4SNick Mathewson #include <sys/socket.h>
24709c21c4SNick Mathewson #include <netinet/in.h>
2513b912e4STrond Norbye #endif
26709c21c4SNick Mathewson 
27709c21c4SNick Mathewson #include <event2/bufferevent_ssl.h>
28709c21c4SNick Mathewson #include <event2/bufferevent.h>
29709c21c4SNick Mathewson #include <event2/buffer.h>
30709c21c4SNick Mathewson #include <event2/listener.h>
31709c21c4SNick Mathewson #include <event2/util.h>
32709c21c4SNick Mathewson 
33449f5a57SAzat Khuzhin #include "util-internal.h"
34709c21c4SNick Mathewson #include <openssl/ssl.h>
35709c21c4SNick Mathewson #include <openssl/err.h>
36709c21c4SNick Mathewson #include <openssl/rand.h>
37fa58cb1dSAzat Khuzhin #include "openssl-compat.h"
38709c21c4SNick Mathewson 
39709c21c4SNick Mathewson static struct event_base *base;
40709c21c4SNick Mathewson static struct sockaddr_storage listen_on_addr;
41709c21c4SNick Mathewson static struct sockaddr_storage connect_to_addr;
42709c21c4SNick Mathewson static int connect_to_addrlen;
43709c21c4SNick Mathewson static int use_wrapper = 1;
44709c21c4SNick Mathewson 
45709c21c4SNick Mathewson static SSL_CTX *ssl_ctx = NULL;
46709c21c4SNick Mathewson 
47c119e4a1SNick Mathewson #define MAX_OUTPUT (512*1024)
48c119e4a1SNick Mathewson 
49c119e4a1SNick Mathewson static void drained_writecb(struct bufferevent *bev, void *ctx);
50c119e4a1SNick Mathewson static void eventcb(struct bufferevent *bev, short what, void *ctx);
51c119e4a1SNick Mathewson 
52709c21c4SNick Mathewson static void
readcb(struct bufferevent * bev,void * ctx)53709c21c4SNick Mathewson readcb(struct bufferevent *bev, void *ctx)
54709c21c4SNick Mathewson {
55709c21c4SNick Mathewson 	struct bufferevent *partner = ctx;
56709c21c4SNick Mathewson 	struct evbuffer *src, *dst;
57c119e4a1SNick Mathewson 	size_t len;
58709c21c4SNick Mathewson 	src = bufferevent_get_input(bev);
59c119e4a1SNick Mathewson 	len = evbuffer_get_length(src);
60709c21c4SNick Mathewson 	if (!partner) {
61c119e4a1SNick Mathewson 		evbuffer_drain(src, len);
62709c21c4SNick Mathewson 		return;
63709c21c4SNick Mathewson 	}
64709c21c4SNick Mathewson 	dst = bufferevent_get_output(partner);
65709c21c4SNick Mathewson 	evbuffer_add_buffer(dst, src);
66c119e4a1SNick Mathewson 
67c119e4a1SNick Mathewson 	if (evbuffer_get_length(dst) >= MAX_OUTPUT) {
68c119e4a1SNick Mathewson 		/* We're giving the other side data faster than it can
69c119e4a1SNick Mathewson 		 * pass it on.  Stop reading here until we have drained the
70c119e4a1SNick Mathewson 		 * other side to MAX_OUTPUT/2 bytes. */
71c119e4a1SNick Mathewson 		bufferevent_setcb(partner, readcb, drained_writecb,
72c119e4a1SNick Mathewson 		    eventcb, bev);
73c119e4a1SNick Mathewson 		bufferevent_setwatermark(partner, EV_WRITE, MAX_OUTPUT/2,
74c119e4a1SNick Mathewson 		    MAX_OUTPUT);
75c119e4a1SNick Mathewson 		bufferevent_disable(bev, EV_READ);
76c119e4a1SNick Mathewson 	}
77c119e4a1SNick Mathewson }
78c119e4a1SNick Mathewson 
79c119e4a1SNick Mathewson static void
drained_writecb(struct bufferevent * bev,void * ctx)80c119e4a1SNick Mathewson drained_writecb(struct bufferevent *bev, void *ctx)
81c119e4a1SNick Mathewson {
82c119e4a1SNick Mathewson 	struct bufferevent *partner = ctx;
83c119e4a1SNick Mathewson 
84c119e4a1SNick Mathewson 	/* We were choking the other side until we drained our outbuf a bit.
85c119e4a1SNick Mathewson 	 * Now it seems drained. */
86c119e4a1SNick Mathewson 	bufferevent_setcb(bev, readcb, NULL, eventcb, partner);
87c119e4a1SNick Mathewson 	bufferevent_setwatermark(bev, EV_WRITE, 0, 0);
88c119e4a1SNick Mathewson 	if (partner)
89c119e4a1SNick Mathewson 		bufferevent_enable(partner, EV_READ);
90709c21c4SNick Mathewson }
91709c21c4SNick Mathewson 
92709c21c4SNick Mathewson static void
close_on_finished_writecb(struct bufferevent * bev,void * ctx)93709c21c4SNick Mathewson close_on_finished_writecb(struct bufferevent *bev, void *ctx)
94709c21c4SNick Mathewson {
95709c21c4SNick Mathewson 	struct evbuffer *b = bufferevent_get_output(bev);
96c119e4a1SNick Mathewson 
97709c21c4SNick Mathewson 	if (evbuffer_get_length(b) == 0) {
98709c21c4SNick Mathewson 		bufferevent_free(bev);
99709c21c4SNick Mathewson 	}
100709c21c4SNick Mathewson }
101709c21c4SNick Mathewson 
102709c21c4SNick Mathewson static void
eventcb(struct bufferevent * bev,short what,void * ctx)103709c21c4SNick Mathewson eventcb(struct bufferevent *bev, short what, void *ctx)
104709c21c4SNick Mathewson {
105709c21c4SNick Mathewson 	struct bufferevent *partner = ctx;
106709c21c4SNick Mathewson 
107709c21c4SNick Mathewson 	if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
108516452b7SNick Mathewson 		if (what & BEV_EVENT_ERROR) {
109516452b7SNick Mathewson 			unsigned long err;
110516452b7SNick Mathewson 			while ((err = (bufferevent_get_openssl_error(bev)))) {
111516452b7SNick Mathewson 				const char *msg = (const char*)
112516452b7SNick Mathewson 				    ERR_reason_error_string(err);
113516452b7SNick Mathewson 				const char *lib = (const char*)
114516452b7SNick Mathewson 				    ERR_lib_error_string(err);
115516452b7SNick Mathewson 				const char *func = (const char*)
116516452b7SNick Mathewson 				    ERR_func_error_string(err);
117516452b7SNick Mathewson 				fprintf(stderr,
118516452b7SNick Mathewson 				    "%s in %s %s\n", msg, lib, func);
119516452b7SNick Mathewson 			}
120516452b7SNick Mathewson 			if (errno)
121516452b7SNick Mathewson 				perror("connection error");
122516452b7SNick Mathewson 		}
123709c21c4SNick Mathewson 
124709c21c4SNick Mathewson 		if (partner) {
125709c21c4SNick Mathewson 			/* Flush all pending data */
126709c21c4SNick Mathewson 			readcb(bev, ctx);
127709c21c4SNick Mathewson 
128c119e4a1SNick Mathewson 			if (evbuffer_get_length(
129c119e4a1SNick Mathewson 				    bufferevent_get_output(partner))) {
130c119e4a1SNick Mathewson 				/* We still have to flush data from the other
131c119e4a1SNick Mathewson 				 * side, but when that's done, close the other
132c119e4a1SNick Mathewson 				 * side. */
133709c21c4SNick Mathewson 				bufferevent_setcb(partner,
134709c21c4SNick Mathewson 				    NULL, close_on_finished_writecb,
135709c21c4SNick Mathewson 				    eventcb, NULL);
136c119e4a1SNick Mathewson 				bufferevent_disable(partner, EV_READ);
137c119e4a1SNick Mathewson 			} else {
138c119e4a1SNick Mathewson 				/* We have nothing left to say to the other
139c119e4a1SNick Mathewson 				 * side; close it. */
140c119e4a1SNick Mathewson 				bufferevent_free(partner);
141c119e4a1SNick Mathewson 			}
142709c21c4SNick Mathewson 		}
143709c21c4SNick Mathewson 		bufferevent_free(bev);
144709c21c4SNick Mathewson 	}
145709c21c4SNick Mathewson }
146709c21c4SNick Mathewson 
147709c21c4SNick Mathewson static void
syntax(void)148709c21c4SNick Mathewson syntax(void)
149709c21c4SNick Mathewson {
150709c21c4SNick Mathewson 	fputs("Syntax:\n", stderr);
151709c21c4SNick Mathewson 	fputs("   le-proxy [-s] [-W] <listen-on-addr> <connect-to-addr>\n", stderr);
152709c21c4SNick Mathewson 	fputs("Example:\n", stderr);
153709c21c4SNick Mathewson 	fputs("   le-proxy 127.0.0.1:8888 1.2.3.4:80\n", stderr);
154709c21c4SNick Mathewson 
155709c21c4SNick Mathewson 	exit(1);
156709c21c4SNick Mathewson }
157709c21c4SNick Mathewson 
158709c21c4SNick Mathewson static void
accept_cb(struct evconnlistener * listener,evutil_socket_t fd,struct sockaddr * a,int slen,void * p)159709c21c4SNick Mathewson accept_cb(struct evconnlistener *listener, evutil_socket_t fd,
160709c21c4SNick Mathewson     struct sockaddr *a, int slen, void *p)
161709c21c4SNick Mathewson {
162709c21c4SNick Mathewson 	struct bufferevent *b_out, *b_in;
163709c21c4SNick Mathewson 	/* Create two linked bufferevent objects: one to connect, one for the
164709c21c4SNick Mathewson 	 * new connection */
165709c21c4SNick Mathewson 	b_in = bufferevent_socket_new(base, fd,
166709c21c4SNick Mathewson 	    BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
167709c21c4SNick Mathewson 
168709c21c4SNick Mathewson 	if (!ssl_ctx || use_wrapper)
169709c21c4SNick Mathewson 		b_out = bufferevent_socket_new(base, -1,
170709c21c4SNick Mathewson 		    BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
171709c21c4SNick Mathewson 	else {
172709c21c4SNick Mathewson 		SSL *ssl = SSL_new(ssl_ctx);
173709c21c4SNick Mathewson 		b_out = bufferevent_openssl_socket_new(base, -1, ssl,
174709c21c4SNick Mathewson 		    BUFFEREVENT_SSL_CONNECTING,
175709c21c4SNick Mathewson 		    BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
176709c21c4SNick Mathewson 	}
177709c21c4SNick Mathewson 
178709c21c4SNick Mathewson 	assert(b_in && b_out);
179709c21c4SNick Mathewson 
180709c21c4SNick Mathewson 	if (bufferevent_socket_connect(b_out,
181709c21c4SNick Mathewson 		(struct sockaddr*)&connect_to_addr, connect_to_addrlen)<0) {
182709c21c4SNick Mathewson 		perror("bufferevent_socket_connect");
183709c21c4SNick Mathewson 		bufferevent_free(b_out);
184709c21c4SNick Mathewson 		bufferevent_free(b_in);
185709c21c4SNick Mathewson 		return;
186709c21c4SNick Mathewson 	}
187709c21c4SNick Mathewson 
188709c21c4SNick Mathewson 	if (ssl_ctx && use_wrapper) {
189709c21c4SNick Mathewson 		struct bufferevent *b_ssl;
190709c21c4SNick Mathewson 		SSL *ssl = SSL_new(ssl_ctx);
191709c21c4SNick Mathewson 		b_ssl = bufferevent_openssl_filter_new(base,
192709c21c4SNick Mathewson 		    b_out, ssl, BUFFEREVENT_SSL_CONNECTING,
193709c21c4SNick Mathewson 		    BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
194709c21c4SNick Mathewson 		if (!b_ssl) {
195709c21c4SNick Mathewson 			perror("Bufferevent_openssl_new");
196709c21c4SNick Mathewson 			bufferevent_free(b_out);
197709c21c4SNick Mathewson 			bufferevent_free(b_in);
19856a52003SMaximilian Brunner 			return;
199709c21c4SNick Mathewson 		}
200709c21c4SNick Mathewson 		b_out = b_ssl;
201709c21c4SNick Mathewson 	}
202709c21c4SNick Mathewson 
203709c21c4SNick Mathewson 	bufferevent_setcb(b_in, readcb, NULL, eventcb, b_out);
204709c21c4SNick Mathewson 	bufferevent_setcb(b_out, readcb, NULL, eventcb, b_in);
205709c21c4SNick Mathewson 
206709c21c4SNick Mathewson 	bufferevent_enable(b_in, EV_READ|EV_WRITE);
207709c21c4SNick Mathewson 	bufferevent_enable(b_out, EV_READ|EV_WRITE);
208709c21c4SNick Mathewson }
209709c21c4SNick Mathewson 
210709c21c4SNick Mathewson int
main(int argc,char ** argv)211709c21c4SNick Mathewson main(int argc, char **argv)
212709c21c4SNick Mathewson {
213709c21c4SNick Mathewson 	int i;
214709c21c4SNick Mathewson 	int socklen;
215709c21c4SNick Mathewson 
216709c21c4SNick Mathewson 	int use_ssl = 0;
217709c21c4SNick Mathewson 	struct evconnlistener *listener;
218709c21c4SNick Mathewson 
219*2a1e1530Slinxiaohui #ifdef _WIN32
220*2a1e1530Slinxiaohui 	WORD wVersionRequested;
221*2a1e1530Slinxiaohui 	WSADATA wsaData;
222*2a1e1530Slinxiaohui 	wVersionRequested = MAKEWORD(2, 2);
223*2a1e1530Slinxiaohui 	(void) WSAStartup(wVersionRequested, &wsaData);
224*2a1e1530Slinxiaohui #endif
225*2a1e1530Slinxiaohui 
226709c21c4SNick Mathewson 	if (argc < 3)
227709c21c4SNick Mathewson 		syntax();
228709c21c4SNick Mathewson 
229709c21c4SNick Mathewson 	for (i=1; i < argc; ++i) {
230709c21c4SNick Mathewson 		if (!strcmp(argv[i], "-s")) {
231709c21c4SNick Mathewson 			use_ssl = 1;
232709c21c4SNick Mathewson 		} else if (!strcmp(argv[i], "-W")) {
233709c21c4SNick Mathewson 			use_wrapper = 0;
234709c21c4SNick Mathewson 		} else if (argv[i][0] == '-') {
235709c21c4SNick Mathewson 			syntax();
236709c21c4SNick Mathewson 		} else
237709c21c4SNick Mathewson 			break;
238709c21c4SNick Mathewson 	}
239709c21c4SNick Mathewson 
240709c21c4SNick Mathewson 	if (i+2 != argc)
241709c21c4SNick Mathewson 		syntax();
242709c21c4SNick Mathewson 
243709c21c4SNick Mathewson 	memset(&listen_on_addr, 0, sizeof(listen_on_addr));
244709c21c4SNick Mathewson 	socklen = sizeof(listen_on_addr);
245709c21c4SNick Mathewson 	if (evutil_parse_sockaddr_port(argv[i],
246709c21c4SNick Mathewson 		(struct sockaddr*)&listen_on_addr, &socklen)<0) {
247709c21c4SNick Mathewson 		int p = atoi(argv[i]);
248709c21c4SNick Mathewson 		struct sockaddr_in *sin = (struct sockaddr_in*)&listen_on_addr;
249709c21c4SNick Mathewson 		if (p < 1 || p > 65535)
250709c21c4SNick Mathewson 			syntax();
251709c21c4SNick Mathewson 		sin->sin_port = htons(p);
252709c21c4SNick Mathewson 		sin->sin_addr.s_addr = htonl(0x7f000001);
253709c21c4SNick Mathewson 		sin->sin_family = AF_INET;
254709c21c4SNick Mathewson 		socklen = sizeof(struct sockaddr_in);
255709c21c4SNick Mathewson 	}
256709c21c4SNick Mathewson 
257709c21c4SNick Mathewson 	memset(&connect_to_addr, 0, sizeof(connect_to_addr));
258709c21c4SNick Mathewson 	connect_to_addrlen = sizeof(connect_to_addr);
259709c21c4SNick Mathewson 	if (evutil_parse_sockaddr_port(argv[i+1],
260709c21c4SNick Mathewson 		(struct sockaddr*)&connect_to_addr, &connect_to_addrlen)<0)
261709c21c4SNick Mathewson 		syntax();
262709c21c4SNick Mathewson 
263709c21c4SNick Mathewson 	base = event_base_new();
264709c21c4SNick Mathewson 	if (!base) {
265709c21c4SNick Mathewson 		perror("event_base_new()");
266709c21c4SNick Mathewson 		return 1;
267709c21c4SNick Mathewson 	}
268709c21c4SNick Mathewson 
269709c21c4SNick Mathewson 	if (use_ssl) {
270709c21c4SNick Mathewson 		int r;
271894ca48aSBernard Spil #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
272894ca48aSBernard Spil 	(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
273709c21c4SNick Mathewson 		SSL_library_init();
274709c21c4SNick Mathewson 		ERR_load_crypto_strings();
275709c21c4SNick Mathewson 		SSL_load_error_strings();
276709c21c4SNick Mathewson 		OpenSSL_add_all_algorithms();
277122bf144SAzat Khuzhin #endif
278709c21c4SNick Mathewson 		r = RAND_poll();
279709c21c4SNick Mathewson 		if (r == 0) {
280709c21c4SNick Mathewson 			fprintf(stderr, "RAND_poll() failed.\n");
281709c21c4SNick Mathewson 			return 1;
282709c21c4SNick Mathewson 		}
283fa58cb1dSAzat Khuzhin 		ssl_ctx = SSL_CTX_new(TLS_method());
284709c21c4SNick Mathewson 	}
285709c21c4SNick Mathewson 
286709c21c4SNick Mathewson 	listener = evconnlistener_new_bind(base, accept_cb, NULL,
287709c21c4SNick Mathewson 	    LEV_OPT_CLOSE_ON_FREE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_REUSEABLE,
288709c21c4SNick Mathewson 	    -1, (struct sockaddr*)&listen_on_addr, socklen);
289709c21c4SNick Mathewson 
29044b2491bSNick Mathewson 	if (! listener) {
29144b2491bSNick Mathewson 		fprintf(stderr, "Couldn't open listener.\n");
29244b2491bSNick Mathewson 		event_base_free(base);
29344b2491bSNick Mathewson 		return 1;
29444b2491bSNick Mathewson 	}
295709c21c4SNick Mathewson 	event_base_dispatch(base);
296709c21c4SNick Mathewson 
2977bcace2dSNick Mathewson 	evconnlistener_free(listener);
2987bcace2dSNick Mathewson 	event_base_free(base);
2997bcace2dSNick Mathewson 
300*2a1e1530Slinxiaohui #ifdef _WIN32
301*2a1e1530Slinxiaohui 	WSACleanup();
302*2a1e1530Slinxiaohui #endif
303*2a1e1530Slinxiaohui 
304709c21c4SNick Mathewson 	return 0;
305709c21c4SNick Mathewson }
306