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/>.
25 /* socket(), bind(), accept(), listen() */
26 #include <sys/types.h>
27 #include <sys/socket.h>
31 #include <arpa/inet.h>
41 #include "connection.h"
43 /* Size of ringbuffer. */
44 #define RINGBUFFER_SIZE 10
47 /* Server should shut down. Set by SIGINT handler. */
48 static volatile int done;
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. */
61 static void sigint_handler(int signal);
63 static void parse_arguments(int argc, char **argv);
64 static void print_usage(const char *argv);
66 static void worker_thread(void);
69 int main(int argc, char **argv) {
71 int client_socket, server_socket;
72 struct sockaddr_in6 server_in;
77 struct sigaction action;
79 parse_arguments(argc, argv);
81 port = atoi(argv[argc - 1]);
82 if (0 >= port || 0xffff < port) {
84 fprintf(stderr, "\ninvalid port");
88 /* Setup our SIGINT signal handler which allows a "normal" termination of
90 sigemptyset(&action.sa_mask);
91 action.sa_handler = sigint_handler;
93 sigaction(SIGINT, &action, NULL);
95 /* Initialize ring buffer. */
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()");
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");
114 for (i = 0; i < thread_count; i++) {
118 result = pthread_create(&thread, NULL,
119 (void * (*)(void *))&worker_thread,
122 printf("failed to create worker thread: %s\n", strerror(result));
129 server_socket = socket(PF_INET6, SOCK_STREAM, 0);
130 if (-1 == server_socket) {
136 /* Fast rebinding for debug mode, could cause invalid packets. */
138 int socket_option = 1;
139 setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
140 &socket_option, sizeof(socket_option));
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))) {
154 /* And accept connections. */
155 if (-1 == listen(server_socket, 5)) {
161 printf("Listening for connections on port %d.\n", port);
163 if (NULL != use_proxy_host && NULL != use_proxy_port) {
164 printf("Using proxy: %s:%s.\n", use_proxy_host, use_proxy_port);
169 /* Accept new connection. */
170 client_socket = accept(server_socket, NULL, NULL);
171 if (-1 == client_socket) {
176 /* No lock, we only have one producer! */
178 ringbuffer[ringbuffer_write] = client_socket;
179 ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
183 close(server_socket);
185 /* Poison all threads and shut them down. */
186 for (i = 0; i < thread_count; i++) {
188 ringbuffer[ringbuffer_write] = -1; /* poison */
189 ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
192 for (i = 0; i < thread_count; i++) {
193 errno = pthread_join(threads[i], NULL);
195 perror("pthread_join()");
200 free(ringbuffer_full);
201 free(ringbuffer_free);
202 free(ringbuffer_lock);
206 free(use_proxy_host);
207 free(use_proxy_port);
212 static void sigint_handler(int signal_number) {
218 static void parse_arguments(int argc, char **argv) {
221 /* Default values. */
224 while (-1 != (option = getopt(argc, argv, "p:t:h?"))) {
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");
239 use_proxy_host = malloc((size_t)(position - optarg) + 1);
240 if (NULL == use_proxy_host) {
244 memcpy(use_proxy_host, optarg, (size_t)(position - optarg));
245 use_proxy_host[position - optarg] = '\0';
247 use_proxy_port = malloc(strlen(position + 1) + 1);
248 if (NULL == use_proxy_port) {
252 strcpy(use_proxy_port, position + 1);
257 if (0 >= atoi(optarg)) {
258 fprintf(stderr, "-t positive number required\n");
261 thread_count = (size_t)atoi(optarg);
266 print_usage(argv[0]);
271 if (optind >= argc) {
272 print_usage(argv[0]);
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");
283 static void worker_thread(void) {
287 /* Get next element from ring buffer. */
290 client_socket = ringbuffer[ringbuffer_read];
291 ringbuffer_read = (ringbuffer_read + 1) % RINGBUFFER_SIZE;
295 /* Negative value indicates we should shut down our thread. */
296 if (client_socket < 0) {
300 handle_connection(client_socket);