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 /* socket(), bind(), accept(), listen() */
23 #include <sys/types.h>
24 #include <sys/socket.h>
28 #include <arpa/inet.h>
37 /* Maximum line of the request line. Longer request lines are aborted with an
38 * error. The standard doesn't specify a maximum line length but this should
39 * be a good limit to make processing simpler. */
40 #define MAX_REQUEST_LINE 4096
43 static void handle_connection(int socket);
44 static int read_http_request(FILE *client_fd, char *request, size_t length);
45 static void send_close_bad_request(FILE *client_fd);
46 static void send_close_forwarding_failure(FILE *client_fd);
48 static void transfer_data(int client, int server);
49 static int read_from_write_to(int from, int to);
51 static int connect_to_host(const char *hostname, const char *port);
53 static int parse_request(const char *buffer, char *host, char *port,
57 int main(int argc, char **argv) {
59 int client_socket, server_socket;
60 struct sockaddr_in6 server_in;
63 printf("Usage: %s port\n", argv[0]);
68 if (0 >= port || 0xffff < port) {
69 printf("Usage: %s port\n", argv[0]);
71 printf("Invalid port: %s!\n", argv[1]);
75 server_socket = socket(PF_INET6, SOCK_STREAM, 0);
76 if (-1 == server_socket) {
82 /* Fast rebinding for debug mode, could cause invalid packets. */
84 int socket_option = 1;
85 setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
86 &socket_option, sizeof(socket_option));
90 /* Bind to the listen socket. */
91 memset(&server_in, 0, sizeof(server_in));
92 server_in.sin6_family = AF_INET6; /* IPv6 (and IPv4) */
93 server_in.sin6_addr = in6addr_any; /* bind to any address */
94 server_in.sin6_port = htons((uint16_t)port); /* port to bind to */
95 if (-1 == bind(server_socket, (struct sockaddr *)&server_in,
100 /* And accept connections. */
101 if (-1 == listen(server_socket, 5)) {
107 /* Accept new connection. */
108 client_socket = accept(server_socket, NULL, NULL);
109 if (-1 == client_socket) {
114 handle_connection(client_socket);
120 static void handle_connection(int client_socket) {
122 FILE *client_fd, *server_fd;
124 char buffer[MAX_REQUEST_LINE];
125 char host[MAX_REQUEST_LINE];
131 client_fd = fdopen(client_socket, "a+");
132 if (NULL == client_fd) {
134 close(client_socket);
139 printf("New connection:\n");
142 /* Read request line (CONNECT ..) and headers (they are discarded). */
143 result = read_http_request(client_fd, buffer, sizeof(buffer));
147 } else if (result == -2) {
149 send_close_bad_request(client_fd);
154 printf(" request: %s", buffer);
157 if (0 != parse_request(buffer, host, port, &version_minor)) {
158 send_close_bad_request(client_fd);
160 printf(" bad request\n");
166 printf(" %s:%s (HTTP 1.%d)\n", host, port, version_minor);
169 server_socket = connect_to_host(host, port);
170 if (-1 == server_socket) {
171 send_close_forwarding_failure(client_fd);
174 server_fd = fdopen(server_socket, "a+");
175 if (NULL == server_fd) {
176 send_close_forwarding_failure(client_fd);
180 /* We've established a connection, tell the client. */
181 fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
182 fprintf(client_fd, "\r\n");
185 /* And transfer all data between client and server transparently. */
186 transfer_data(client_socket, server_socket);
192 /* Read HTTP request line and headers (ignored).
194 * On success 0 is returned, -1 on client error (we close client descriptor in
195 * this case), -2 on unexpected EOF.
197 static int read_http_request(FILE *client_fd, char *request, size_t length) {
198 char buffer[MAX_REQUEST_LINE];
200 if (NULL == fgets(request, (int)length, client_fd)) {
201 if (ferror(client_fd)) {
202 perror("fgets(), request");
210 while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
212 if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
216 if (ferror(client_fd)) {
217 perror("fgets(), header");
225 static void send_close_bad_request(FILE *client_fd) {
226 fprintf(client_fd, "HTTP/1.0 400 Bad Request\r\n");
227 fprintf(client_fd, "\r\n");
230 static void send_close_forwarding_failure(FILE *client_fd) {
231 fprintf(client_fd, "HTTP/1.0 503 Forwarding failure\r\n");
232 fprintf(client_fd, "\r\n");
237 /* Transfer data between client and server sockets until one closes the
239 static void transfer_data(int client, int server) {
240 struct pollfd fds[2];
242 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
245 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
249 int result = poll(fds, 2, 0);
255 /* Data available from client. */
256 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
257 if (0 != read_from_write_to(client, server)) {
258 /* EOF (or other error) */
262 /* Data available from server. */
263 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
264 if (0 != read_from_write_to(server, client)) {
265 /* EOF (or other error) */
270 /* Client closed connection. */
271 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
274 /* Server closed connection. */
275 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
281 /* Read available data from socket from and write it to socket to. At maximum
282 * 4096 bytes are read/written. */
283 static int read_from_write_to(int from, int to) {
285 ssize_t size_written;
288 size_read = read(from, buffer, sizeof(buffer));
294 if (0 == size_read) {
298 size_written = write(to, buffer, (size_t)size_read);
299 if (0 > size_written) {
303 if (size_read != size_written) {
304 printf("only written %ld of %ld bytes!\n", size_read, size_written);
312 static int connect_to_host(const char *hostname, const char *port) {
313 struct addrinfo gai_hints;
314 struct addrinfo *gai_result;
318 struct addrinfo *server;
320 if (NULL == hostname || NULL == port) {
324 /* Get IP of hostname server. */
325 memset(&gai_hints, 0, sizeof(gai_hints));
326 gai_hints.ai_family = AF_UNSPEC;
327 gai_hints.ai_socktype = SOCK_STREAM;
328 gai_hints.ai_protocol = 0;
329 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
330 | AI_ADDRCONFIG /* supported by this computer */
331 | AI_V4MAPPED; /* support IPv4 through IPv6 */
332 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
333 if (0 != gai_return) {
334 perror("getaddrinfo()");
338 /* Now try to connect to each server returned by getaddrinfo(), use the
339 * first successful connect. */
340 for (server = gai_result; NULL != server; server = server->ai_next) {
341 server_socket = socket(server->ai_family,
343 server->ai_protocol);
344 if (-1 == server_socket) {
345 perror("socket(), trying next");
349 if (-1 != connect(server_socket, server->ai_addr,
350 server->ai_addrlen)) {
353 perror("connect(), trying next");
355 close(server_socket);
357 /* Make sure we free the result from getaddrinfo(). */
358 freeaddrinfo(gai_result);
360 if (NULL == server) {
361 fprintf(stderr, "no server found, aborting\n");
365 return server_socket;
369 /* Parse HTTP CONNECT request string and save its parameters.
371 * The following format is expected: "CONNECT host:port HTTP/1.y".
373 * request and host must have the same size! port must be at least 6 bytes
376 static int parse_request(const char *request, char *host, char *port,
377 int *version_minor) {
378 int port_unused; /* just used to verify the port is numeric */
381 /* scanf() doesn't check spaces. */
382 if (0 != strncmp(request, "CONNECT ", 8)) {
385 /* Check request and extract data, "host:port" is not yet separated. */
386 if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
387 host, version_minor)) {
390 /* Make sure ":port" is there. */
391 if (NULL == (position = strchr(host, ':'))) {
394 /* Make sure port is numeric. */
395 if (1 != sscanf(position + 1, "%d", &port_unused)) {
398 /* Store it in *port. */
399 strncpy(port, position + 1, 5);
401 /* And remove port from host. */