2 * tlsproxy is a transparent TLS proxy for HTTPS connections.
4 * Copyright (C) 2011 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "connection.h"
24 /* socket(), bind(), accept(), listen() */
25 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include <arpa/inet.h>
41 GCRY_THREAD_OPTION_PTHREAD_IMPL;
44 /* Size of ringbuffer. */
45 #define RINGBUFFER_SIZE 10
47 /* Bit size of Diffie-Hellman key exchange parameters. */
51 /* Server should shut down. Set by SIGINT handler. */
52 static volatile int done;
54 /* Number of threads. */
55 static size_t thread_count;
57 /* Synchronized ring buffer storing accept()ed client sockets. */
58 static int ringbuffer[RINGBUFFER_SIZE];
59 static int ringbuffer_read;
60 static int ringbuffer_write;
61 static SEM *ringbuffer_full; /* At least one element in the buffer? */
62 static SEM *ringbuffer_free; /* Space for another element in the buffer? */
63 static SEM *ringbuffer_lock; /* Read lock. */
66 static void sigint_handler(int signal);
68 static void parse_arguments(int argc, char **argv);
69 static void print_usage(const char *argv);
71 static void initialize_gnutls(void);
72 static void deinitialize_gnutls(void);
74 static void worker_thread(void);
77 int main(int argc, char **argv) {
79 int client_socket, server_socket;
80 struct sockaddr_in6 server_in;
85 struct sigaction action;
87 parse_arguments(argc, argv);
89 port = atoi(argv[argc - 1]);
90 if (0 >= port || 0xffff < port) {
92 fprintf(stderr, "\ninvalid port\n");
96 /* Setup our SIGINT signal handler which allows a "normal" termination of
98 sigemptyset(&action.sa_mask);
99 action.sa_handler = sigint_handler;
101 sigaction(SIGINT, &action, NULL);
102 /* Ignore SIGPIPEs. */
103 action.sa_handler = SIG_IGN;
104 sigaction(SIGPIPE, &action, NULL);
106 /* Initialize ring buffer. */
108 ringbuffer_write = 0;
109 ringbuffer_full = sem_init(0);
110 ringbuffer_free = sem_init(RINGBUFFER_SIZE);
111 ringbuffer_lock = sem_init(1);
112 if (NULL == ringbuffer_full
113 || NULL == ringbuffer_free
114 || NULL == ringbuffer_lock) {
115 perror("sem_init()");
121 /* Spawn worker threads to handle requests. */
122 threads = (pthread_t *)malloc(thread_count * sizeof(pthread_t));
123 if (NULL == threads) {
124 perror("thread malloc failed");
127 for (i = 0; i < thread_count; i++) {
131 result = pthread_create(&thread, NULL,
132 (void * (*)(void *))&worker_thread,
135 printf("failed to create worker thread: %s\n", strerror(result));
142 server_socket = socket(PF_INET6, SOCK_STREAM, 0);
143 if (-1 == server_socket) {
149 /* Fast rebinding for debug mode, could cause invalid packets. */
151 int socket_option = 1;
152 setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
153 &socket_option, sizeof(socket_option));
157 /* Bind to the listen socket. */
158 memset(&server_in, 0, sizeof(server_in));
159 server_in.sin6_family = AF_INET6; /* IPv6 (and IPv4) */
160 server_in.sin6_addr = in6addr_any; /* bind to any address */
161 server_in.sin6_port = htons((uint16_t)port); /* port to bind to */
162 if (-1 == bind(server_socket, (struct sockaddr *)&server_in,
163 sizeof(server_in))) {
167 /* And accept connections. */
168 if (-1 == listen(server_socket, 5)) {
173 if (LOG_DEBUG <= global_log_level) {
174 printf("Listening for connections on port %d.\n", port);
176 if (NULL != global_proxy_host && NULL != global_proxy_port) {
177 printf("Using proxy: %s:%s.\n", global_proxy_host,
183 /* Accept new connection. */
184 client_socket = accept(server_socket, NULL, NULL);
185 if (-1 == client_socket) {
190 /* No lock, we only have one producer! */
192 ringbuffer[ringbuffer_write] = client_socket;
193 ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
197 close(server_socket);
199 /* Poison all threads and shut them down. */
200 for (i = 0; i < thread_count; i++) {
202 ringbuffer[ringbuffer_write] = -1; /* poison */
203 ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
206 for (i = 0; i < thread_count; i++) {
207 errno = pthread_join(threads[i], NULL);
209 perror("pthread_join()");
214 free(ringbuffer_full);
215 free(ringbuffer_free);
216 free(ringbuffer_lock);
220 deinitialize_gnutls();
222 free(global_proxy_host);
223 free(global_proxy_port);
228 static void sigint_handler(int signal_number) {
234 static void parse_arguments(int argc, char **argv) {
237 /* Default values. */
240 global_log_level = LOG_DEBUG;
242 global_log_level = LOG_WARNING;
245 while (-1 != (option = getopt(argc, argv, "d:p:t:h?"))) {
248 if (0 > atoi(optarg)) {
249 print_usage(argv[0]);
250 fprintf(stderr, "\n-d positive number required\n");
253 global_log_level = atoi(optarg);
259 /* -p must have the format host:port. */
260 if (NULL == (position = strchr(optarg, ':'))
261 || position == optarg
262 || 0 == strlen(position + 1)
263 || 0 >= atoi(position + 1)
264 || 0xffff < atoi(position + 1)) {
265 print_usage(argv[0]);
266 fprintf(stderr, "\ninvalid -p, format host:port\n");
270 global_proxy_host = malloc((size_t)(position - optarg) + 1);
271 if (NULL == global_proxy_host) {
275 memcpy(global_proxy_host, optarg, (size_t)(position - optarg));
276 global_proxy_host[position - optarg] = '\0';
278 global_proxy_port = malloc(strlen(position + 1) + 1);
279 if (NULL == global_proxy_port) {
283 strcpy(global_proxy_port, position + 1);
288 if (0 >= atoi(optarg)) {
289 print_usage(argv[0]);
290 fprintf(stderr, "\n-t positive number required\n");
293 thread_count = (size_t)atoi(optarg);
298 print_usage(argv[0]);
303 if (optind >= argc) {
304 print_usage(argv[0]);
305 fprintf(stderr, "\nport missing\n");
309 static void print_usage(const char *argv) {
310 fprintf(stderr, "Usage: %s [-d level] [-p host:port] [-t count] port\n",
312 fprintf(stderr, "\n");
313 fprintf(stderr, "-d debug level: 0=errors only, 2=debug [default: 1]\n");
314 fprintf(stderr, "-p proxy hostname and port\n");
315 fprintf(stderr, "-t number of threads [default: 10]\n");
318 static void initialize_gnutls(void) {
320 gcry_error_t error = 0;
322 /* Thread safe setup. Must be called before gnutls_global_init(). */
323 error = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
325 fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
326 gcry_strerror(error));
329 /* Prevent usage of blocking /dev/random. */
330 error = gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
332 fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
333 gcry_strerror(error));
337 /* Initialize GnuTLS. */
338 result = gnutls_global_init();
339 GNUTLS_ERROR_EXIT(result, "gnutls_global_init()");
341 /* Setup GnuTLS cipher suites. */
342 result = gnutls_priority_init(&tls_priority_cache, "NORMAL", NULL);
343 GNUTLS_ERROR_EXIT(result, "gnutls_priority_init()");
345 /* Generate Diffie-Hellman parameters. */
346 result = gnutls_dh_params_init(&tls_dh_params);
347 GNUTLS_ERROR_EXIT(result, "gnutls_dh_params_init()");
348 result = gnutls_dh_params_generate2(tls_dh_params, DH_SIZE);
349 GNUTLS_ERROR_EXIT(result, "gnutls_dh_params_generate2()");
351 static void deinitialize_gnutls(void) {
352 gnutls_dh_params_deinit(tls_dh_params);
353 gnutls_priority_deinit(tls_priority_cache);
355 gnutls_global_deinit();
358 static void worker_thread(void) {
362 /* Get next element from ring buffer. */
365 client_socket = ringbuffer[ringbuffer_read];
366 ringbuffer_read = (ringbuffer_read + 1) % RINGBUFFER_SIZE;
370 /* Negative value indicates we should shut down our thread. */
371 if (0 > client_socket) {
375 handle_connection(client_socket);