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 static void handle_connection(int socket);
38 static void send_close_bad_request(FILE *client_fd);
39 static void send_close_forwarding_failure(FILE *client_fd);
41 static void transfer_data(int client, int server);
42 static int read_from_write_to(int from, int to);
44 static int connect_to_host(const char *hostname, const char *port);
46 static int parse_request(const char *buffer, char *host, char *port,
50 int main(int argc, char **argv) {
52 int client_socket, server_socket;
53 struct sockaddr_in6 server_in;
56 printf("Usage: %s port\n", argv[0]);
61 if (0 >= port || 0xffff < port) {
62 printf("Usage: %s port\n", argv[0]);
64 printf("Invalid port: %s!\n", argv[1]);
68 server_socket = socket(PF_INET6, SOCK_STREAM, 0);
69 if (-1 == server_socket) {
75 /* Fast rebinding for debug mode, could cause invalid packets. */
77 int socket_option = 1;
78 setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
79 &socket_option, sizeof(socket_option));
83 /* Bind to the listen socket. */
84 memset(&server_in, 0, sizeof(server_in));
85 server_in.sin6_family = AF_INET6; /* IPv6 (and IPv4) */
86 server_in.sin6_addr = in6addr_any; /* bind to any address */
87 server_in.sin6_port = htons((uint16_t)port); /* port to bind to */
88 if (-1 == bind(server_socket, (struct sockaddr *)&server_in,
93 /* And accept connections. */
94 if (-1 == listen(server_socket, 5)) {
100 /* Accept new connection. */
101 client_socket = accept(server_socket, NULL, NULL);
102 if (-1 == client_socket) {
107 handle_connection(client_socket);
113 static void handle_connection(int client_socket) {
115 FILE *client_fd, *server_fd;
123 client_fd = fdopen(client_socket, "a+");
124 if (NULL == client_fd) {
126 close(client_socket);
131 printf("New connection:\n");
134 if (NULL == fgets(buffer, sizeof(buffer), client_fd)) {
135 if (ferror(client_fd)) {
136 perror("fgets(), request");
141 send_close_bad_request(client_fd);
146 printf(" request: %s", buffer);
149 if (0 != parse_request(buffer, host, port, &version_minor)) {
150 send_close_bad_request(client_fd);
152 printf(" bad request\n");
157 while (NULL != fgets(buffer, sizeof(buffer), client_fd)) {
159 if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
163 if (ferror(client_fd)) {
164 perror("fgets(), header");
170 printf(" %s:%s (HTTP 1.%d)\n", host, port, version_minor);
173 server_socket = connect_to_host(host, port);
174 if (-1 == server_socket) {
175 send_close_forwarding_failure(client_fd);
178 server_fd = fdopen(server_socket, "a+");
179 if (NULL == server_fd) {
180 send_close_forwarding_failure(client_fd);
184 /* We've established a connection, tell the client. */
185 fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
186 fprintf(client_fd, "\r\n");
189 /* And transfer all data between client and server transparently. */
190 transfer_data(client_socket, server_socket);
196 static void send_close_bad_request(FILE *client_fd) {
197 fprintf(client_fd, "HTTP/1.0 400 Bad Request\r\n");
198 fprintf(client_fd, "\r\n");
201 static void send_close_forwarding_failure(FILE *client_fd) {
202 fprintf(client_fd, "HTTP/1.0 503 Forwarding failure\r\n");
203 fprintf(client_fd, "\r\n");
208 /* Transfer data between client and server sockets until one closes the
210 static void transfer_data(int client, int server) {
211 struct pollfd fds[2];
213 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
216 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
220 int result = poll(fds, 2, 0);
226 /* Data available from client. */
227 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
228 if (0 != read_from_write_to(client, server)) {
229 /* EOF (or other error) */
233 /* Data available from server. */
234 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
235 if (0 != read_from_write_to(server, client)) {
236 /* EOF (or other error) */
241 /* Client closed connection. */
242 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
245 /* Server closed connection. */
246 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
252 /* Read available data from socket from and write it to socket to. At maximum
253 * 4096 bytes are read/written. */
254 static int read_from_write_to(int from, int to) {
256 ssize_t size_written;
259 size_read = read(from, buffer, sizeof(buffer));
265 if (0 == size_read) {
269 size_written = write(to, buffer, (size_t)size_read);
270 if (0 > size_written) {
274 if (size_read != size_written) {
275 printf("only written %ld of %ld bytes!\n", size_read, size_written);
283 static int connect_to_host(const char *hostname, const char *port) {
284 struct addrinfo gai_hints;
285 struct addrinfo *gai_result;
289 struct addrinfo *server;
291 if (NULL == hostname || NULL == port) {
295 /* Get IP of hostname server. */
296 memset(&gai_hints, 0, sizeof(gai_hints));
297 gai_hints.ai_family = AF_UNSPEC;
298 gai_hints.ai_socktype = SOCK_STREAM;
299 gai_hints.ai_protocol = 0;
300 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
301 | AI_ADDRCONFIG /* supported by this computer */
302 | AI_V4MAPPED; /* support IPv4 through IPv6 */
303 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
304 if (0 != gai_return) {
305 perror("getaddrinfo()");
309 /* Now try to connect to each server returned by getaddrinfo(), use the
310 * first successful connect. */
311 for (server = gai_result; NULL != server; server = server->ai_next) {
312 server_socket = socket(server->ai_family,
314 server->ai_protocol);
315 if (-1 == server_socket) {
316 perror("socket(), trying next");
320 if (-1 != connect(server_socket, server->ai_addr,
321 server->ai_addrlen)) {
324 perror("connect(), trying next");
326 close(server_socket);
328 /* Make sure we free the result from getaddrinfo(). */
329 freeaddrinfo(gai_result);
331 if (NULL == server) {
332 fprintf(stderr, "no server found, aborting\n");
336 return server_socket;
340 /* Parse HTTP CONNECT request string and save its parameters.
342 * The following format is expected: "CONNECT host:port HTTP/1.y".
344 * request and host must have the same size! port must be at least 6 bytes
347 static int parse_request(const char *request, char *host, char *port,
348 int *version_minor) {
349 int port_unused; /* just used to verify the port is numeric */
352 /* scanf() doesn't check spaces. */
353 if (0 != strncmp(request, "CONNECT ", 8)) {
356 /* Check request and extract data, "host:port" is not yet separated. */
357 if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
358 host, version_minor)) {
361 /* Make sure ":port" is there. */
362 if (NULL == (position = strchr(host, ':'))) {
365 /* Make sure port is numeric. */
366 if (1 != sscanf(position + 1, "%d", &port_unused)) {
369 /* Store it in *port. */
370 strncpy(port, position + 1, 5);
372 /* And remove port from host. */