]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blob - src/tlsproxy.c
src/*: Split src/tlsproxy.c in several files.
[tlsproxy/tlsproxy.git] / src / tlsproxy.c
1 /*
2  * tlsproxy is a transparent TLS proxy for HTTPS connections.
3  *
4  * Copyright (C) 2011  Simon Ruderich
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 /* socket(), bind(), accept(), listen() */
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 /* close() */
29 #include <unistd.h>
30 /* htons() */
31 #include <arpa/inet.h>
32 /* sigaction() */
33 #include <signal.h>
34 /* errno */
35 #include <errno.h>
36 /* pthread_*() */
37 #include <pthread.h>
38
39 #include "tlsproxy.h"
40 #include "sem.h"
41 #include "connection.h"
42
43 /* Size of ringbuffer. */
44 #define RINGBUFFER_SIZE 10
45
46
47 /* Server should shut down. Set by SIGINT handler. */
48 static volatile int done;
49
50 /* Number of threads. */
51 static size_t thread_count;
52 /* Synchronized ring buffer storing accept()ed client sockets. */
53 static int ringbuffer[RINGBUFFER_SIZE];
54 static int ringbuffer_read;
55 static int ringbuffer_write;
56 static SEM *ringbuffer_full; /* At least one element in the buffer? */
57 static SEM *ringbuffer_free; /* Space for another element in the buffer? */
58 static SEM *ringbuffer_lock; /* Read lock. */
59
60
61 static void sigint_handler(int signal);
62
63 static void parse_arguments(int argc, char **argv);
64 static void print_usage(const char *argv);
65
66 static void worker_thread(void);
67
68
69 int main(int argc, char **argv) {
70     int port;
71     int client_socket, server_socket;
72     struct sockaddr_in6 server_in;
73
74     size_t i;
75     pthread_t *threads;
76
77     struct sigaction action;
78
79     parse_arguments(argc, argv);
80
81     port = atoi(argv[argc - 1]);
82     if (0 >= port || 0xffff < port) {
83         print_usage(argv[0]);
84         fprintf(stderr, "\ninvalid port");
85         return EXIT_FAILURE;
86     }
87
88     /* Setup our SIGINT signal handler which allows a "normal" termination of
89      * the server. */
90     sigemptyset(&action.sa_mask);
91     action.sa_handler = sigint_handler;
92     action.sa_flags   = 0;
93     sigaction(SIGINT, &action, NULL);
94
95     /* Initialize ring buffer. */
96     ringbuffer_read  = 0;
97     ringbuffer_write = 0;
98     ringbuffer_full  = sem_init(0);
99     ringbuffer_free  = sem_init(RINGBUFFER_SIZE);
100     ringbuffer_lock  = sem_init(1);
101     if (NULL == ringbuffer_full
102             || NULL == ringbuffer_free
103             || NULL == ringbuffer_lock) {
104         perror("sem_init()");
105         return EXIT_FAILURE;
106     }
107
108     /* Spawn worker threads to handle requests. */
109     threads = (pthread_t *)malloc(thread_count * sizeof(pthread_t));
110     if (NULL == threads) {
111         perror("thread malloc failed");
112         return EXIT_FAILURE;
113     }
114     for (i = 0; i < thread_count; i++) {
115         int result;
116         pthread_t thread;
117
118         result = pthread_create(&thread, NULL,
119                                 (void * (*)(void *))&worker_thread,
120                                 NULL);
121         if (0 != result) {
122             printf("failed to create worker thread: %s\n", strerror(result));
123             return EXIT_FAILURE;
124         }
125
126         threads[i] = thread;
127     }
128
129     server_socket = socket(PF_INET6, SOCK_STREAM, 0);
130     if (-1 == server_socket) {
131         perror("socket()");
132         return EXIT_FAILURE;
133     }
134
135 #ifdef DEBUG
136     /* Fast rebinding for debug mode, could cause invalid packets. */
137     {
138         int socket_option = 1;
139         setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
140                    &socket_option, sizeof(socket_option));
141     }
142 #endif
143
144     /* Bind to the listen socket. */
145     memset(&server_in, 0, sizeof(server_in));
146     server_in.sin6_family = AF_INET6;              /* IPv6 (and IPv4) */
147     server_in.sin6_addr   = in6addr_any;           /* bind to any address */
148     server_in.sin6_port   = htons((uint16_t)port); /* port to bind to */
149     if (-1 == bind(server_socket, (struct sockaddr *)&server_in,
150                                   sizeof(server_in))) {
151         perror("bind()");
152         return EXIT_FAILURE;
153     }
154     /* And accept connections. */
155     if (-1 == listen(server_socket, 5)) {
156         perror("listen()");
157         return EXIT_FAILURE;
158     }
159
160 #ifdef DEBUG
161     printf("Listening for connections on port %d.\n", port);
162
163     if (NULL != use_proxy_host && NULL != use_proxy_port) {
164         printf("Using proxy: %s:%s.\n", use_proxy_host, use_proxy_port);
165     }
166 #endif
167
168     while (!done) {
169         /* Accept new connection. */
170         client_socket = accept(server_socket, NULL, NULL);
171         if (-1 == client_socket) {
172             perror("accept()");
173             break;
174         }
175
176         /* No lock, we only have one producer! */
177         P(ringbuffer_free);
178         ringbuffer[ringbuffer_write] = client_socket;
179         ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
180         V(ringbuffer_full);
181     }
182
183     close(server_socket);
184
185     /* Poison all threads and shut them down. */
186     for (i = 0; i < thread_count; i++) {
187         P(ringbuffer_free);
188         ringbuffer[ringbuffer_write] = -1; /* poison */
189         ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
190         V(ringbuffer_full);
191     }
192     for (i = 0; i < thread_count; i++) {
193         errno = pthread_join(threads[i], NULL);
194         if (0 != errno) {
195             perror("pthread_join()");
196             continue;
197         }
198     }
199
200     free(ringbuffer_full);
201     free(ringbuffer_free);
202     free(ringbuffer_lock);
203
204     free(threads);
205
206     free(use_proxy_host);
207     free(use_proxy_port);
208
209     return EXIT_FAILURE;
210 }
211
212 static void sigint_handler(int signal_number) {
213     (void)signal_number;
214
215     done = 1;
216 }
217
218 static void parse_arguments(int argc, char **argv) {
219     int option;
220
221     /* Default values. */
222     thread_count = 10;
223
224     while (-1 != (option = getopt(argc, argv, "p:t:h?"))) {
225         switch (option) {
226             case 'p': {
227                 char *position;
228
229                 /* -p must have the format host:port. */
230                 if (NULL == (position = strchr(optarg, ':'))
231                         || position == optarg
232                         || 0 == strlen(position + 1)
233                         || 0 >= atoi(position + 1)
234                         || 0xffff < atoi(position + 1)) {
235                     fprintf(stderr, "-p host:port\n");
236                     exit(EXIT_FAILURE);
237                 }
238
239                 use_proxy_host = malloc((size_t)(position - optarg) + 1);
240                 if (NULL == use_proxy_host) {
241                     perror("malloc()");
242                     exit(EXIT_FAILURE);
243                 }
244                 memcpy(use_proxy_host, optarg, (size_t)(position - optarg));
245                 use_proxy_host[position - optarg] = '\0';
246
247                 use_proxy_port = malloc(strlen(position + 1) + 1);
248                 if (NULL == use_proxy_port) {
249                     perror("malloc()");
250                     exit(EXIT_FAILURE);
251                 }
252                 strcpy(use_proxy_port, position + 1);
253
254                 break;
255             }
256             case 't': {
257                 if (0 >= atoi(optarg)) {
258                     fprintf(stderr, "-t positive number required\n");
259                     exit(EXIT_FAILURE);
260                 }
261                 thread_count = (size_t)atoi(optarg);
262                 break;
263             }
264             case 'h':
265             default: /* '?' */
266                 print_usage(argv[0]);
267                 exit(EXIT_FAILURE);
268         }
269     }
270
271     if (optind >= argc) {
272         print_usage(argv[0]);
273         exit(EXIT_FAILURE);
274     }
275 }
276 static void print_usage(const char *argv) {
277     fprintf(stderr, "Usage: %s [-p host:port] port\n", argv);
278     fprintf(stderr, "\n");
279     fprintf(stderr, "-p proxy hostname and port\n");
280     fprintf(stderr, "-t number of threads [default: 10]\n");
281 }
282
283 static void worker_thread(void) {
284     int client_socket;
285
286     for (;;) {
287         /* Get next element from ring buffer. */
288         P(ringbuffer_full);
289         P(ringbuffer_lock);
290         client_socket = ringbuffer[ringbuffer_read];
291         ringbuffer_read = (ringbuffer_read + 1) % RINGBUFFER_SIZE;
292         V(ringbuffer_lock);
293         V(ringbuffer_free);
294
295         /* Negative value indicates we should shut down our thread. */
296         if (client_socket < 0) {
297             break;
298         }
299
300         handle_connection(client_socket);
301     }
302 }