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/>.
21 #include "connection.h"
31 /* Maximum line of a HTTP request line. Longer request lines are aborted with
32 * an error. The standard doesn't specify a maximum line length but this
33 * should be a good limit to make processing simpler. */
34 #define MAX_REQUEST_LINE 4096
37 static int read_http_request(FILE *client_fd, char *request, size_t length);
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 void handle_connection(int client_socket) {
52 FILE *client_fd, *server_fd;
54 char buffer[MAX_REQUEST_LINE];
55 char host[MAX_REQUEST_LINE];
61 client_fd = fdopen(client_socket, "a+");
62 if (NULL == client_fd) {
69 printf("New connection:\n");
72 /* Read request line (CONNECT ..) and headers (they are discarded). */
73 result = read_http_request(client_fd, buffer, sizeof(buffer));
75 /* Read error, client_fd already closed. */
77 } else if (-2 == result) {
79 send_close_bad_request(client_fd);
84 printf(" request: %s", buffer);
87 if (0 != parse_request(buffer, host, port, &version_minor)) {
88 send_close_bad_request(client_fd);
90 printf(" bad request\n");
96 printf(" %s:%s (HTTP 1.%d)\n", host, port, version_minor);
99 /* Connect to proxy server or directly to server. */
100 if (NULL != global_proxy_host && NULL != global_proxy_port) {
101 server_socket = connect_to_host(global_proxy_host, global_proxy_port);
103 server_socket = connect_to_host(host, port);
106 if (-1 == server_socket) {
107 send_close_forwarding_failure(client_fd);
110 server_fd = fdopen(server_socket, "a+");
111 if (NULL == server_fd) {
112 send_close_forwarding_failure(client_fd);
113 close(server_socket);
117 /* Connect to proxy if requested (command line option). */
118 if (NULL != global_proxy_host && NULL != global_proxy_port) {
119 fprintf(server_fd, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
120 fprintf(server_fd, "\r\n");
122 /* Read response line from proxy server. */
123 result = read_http_request(server_fd, buffer, sizeof(buffer));
125 /* Read error, server_fd already closed. */
126 send_close_forwarding_failure(client_fd);
128 } else if (-2 == result) {
131 send_close_forwarding_failure(client_fd);
135 /* Check response of proxy server. */
136 if (0 != strncmp(buffer, "HTTP/1.0 200", 12)) {
138 printf(" bad proxy response\n");
141 send_close_forwarding_failure(client_fd);
147 printf(" connection to server established\n");
150 /* We've established a connection, tell the client. */
151 fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
152 fprintf(client_fd, "\r\n");
155 /* And transfer all data between client and server transparently. */
156 transfer_data(client_socket, server_socket);
162 /* Read HTTP request line and headers (ignored).
164 * On success 0 is returned, -1 on client error (we close client descriptor in
165 * this case), -2 on unexpected EOF.
167 static int read_http_request(FILE *client_fd, char *request, size_t length) {
168 char buffer[MAX_REQUEST_LINE];
170 if (NULL == fgets(request, (int)length, client_fd)) {
171 if (ferror(client_fd)) {
172 perror("fgets(), request");
180 while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
182 if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
186 if (ferror(client_fd)) {
187 perror("fgets(), header");
195 static void send_close_bad_request(FILE *client_fd) {
196 fprintf(client_fd, "HTTP/1.0 400 Bad Request\r\n");
197 fprintf(client_fd, "\r\n");
200 static void send_close_forwarding_failure(FILE *client_fd) {
201 fprintf(client_fd, "HTTP/1.0 503 Forwarding failure\r\n");
202 fprintf(client_fd, "\r\n");
207 /* Transfer data between client and server sockets until one closes the
209 static void transfer_data(int client, int server) {
210 struct pollfd fds[2];
212 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
215 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
219 int result = poll(fds, 2, -1 /* no timeout */);
225 /* Data available from client. */
226 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
227 if (0 != read_from_write_to(client, server)) {
228 /* EOF (or other error) */
232 /* Data available from server. */
233 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
234 if (0 != read_from_write_to(server, client)) {
235 /* EOF (or other error) */
240 /* Client closed connection. */
241 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
244 /* Server closed connection. */
245 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
251 /* Read available data from socket from and write it to socket to. At maximum
252 * 4096 bytes are read/written. */
253 static int read_from_write_to(int from, int to) {
255 ssize_t size_written;
258 size_read = read(from, buffer, sizeof(buffer));
264 if (0 == size_read) {
268 size_written = write(to, buffer, (size_t)size_read);
269 if (0 > size_written) {
273 if (size_read != size_written) {
274 printf("only written %ld of %ld bytes!\n", (long int)size_written,
275 (long int)size_read);
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.x".
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. */