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/>.
32 #include "connection.h"
36 /* Maximum line of the request line. Longer request lines are aborted with an
37 * error. The standard doesn't specify a maximum line length but this should
38 * be a good limit to make processing simpler. */
39 #define MAX_REQUEST_LINE 4096
42 static int read_http_request(FILE *client_fd, char *request, size_t length);
43 static void send_close_bad_request(FILE *client_fd);
44 static void send_close_forwarding_failure(FILE *client_fd);
46 static void transfer_data(int client, int server);
47 static int read_from_write_to(int from, int to);
49 static int connect_to_host(const char *hostname, const char *port);
51 static int parse_request(const char *buffer, char *host, char *port,
55 void handle_connection(int client_socket) {
57 FILE *client_fd, *server_fd;
59 char buffer[MAX_REQUEST_LINE];
60 char host[MAX_REQUEST_LINE];
66 client_fd = fdopen(client_socket, "a+");
67 if (NULL == client_fd) {
74 printf("New connection:\n");
77 /* Read request line (CONNECT ..) and headers (they are discarded). */
78 result = read_http_request(client_fd, buffer, sizeof(buffer));
82 } else if (result == -2) {
84 send_close_bad_request(client_fd);
89 printf(" request: %s", buffer);
92 if (0 != parse_request(buffer, host, port, &version_minor)) {
93 send_close_bad_request(client_fd);
95 printf(" bad request\n");
101 printf(" %s:%s (HTTP 1.%d)\n", host, port, version_minor);
104 /* Connect to proxy server or directly to server. */
105 if (NULL != use_proxy_host && NULL != use_proxy_port) {
106 server_socket = connect_to_host(use_proxy_host, use_proxy_port);
108 server_socket = connect_to_host(host, port);
111 if (-1 == server_socket) {
112 send_close_forwarding_failure(client_fd);
115 server_fd = fdopen(server_socket, "a+");
116 if (NULL == server_fd) {
117 send_close_forwarding_failure(client_fd);
121 /* Connect to proxy if requested (command line option). */
122 if (NULL != use_proxy_host && NULL != use_proxy_port) {
123 fprintf(server_fd, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
124 fprintf(server_fd, "\r\n");
126 /* Read response line from proxy server. */
127 result = read_http_request(server_fd, buffer, sizeof(buffer));
130 send_close_forwarding_failure(client_fd);
132 } else if (result == -2) {
135 send_close_forwarding_failure(client_fd);
139 /* Check response of proxy server. */
140 if (0 != strncmp(buffer, "HTTP/1.0 200", 12)) {
142 printf(" bad proxy response\n");
145 send_close_forwarding_failure(client_fd);
151 printf(" connection to server established\n");
154 /* We've established a connection, tell the client. */
155 fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
156 fprintf(client_fd, "\r\n");
159 /* And transfer all data between client and server transparently. */
160 transfer_data(client_socket, server_socket);
166 /* Read HTTP request line and headers (ignored).
168 * On success 0 is returned, -1 on client error (we close client descriptor in
169 * this case), -2 on unexpected EOF.
171 static int read_http_request(FILE *client_fd, char *request, size_t length) {
172 char buffer[MAX_REQUEST_LINE];
174 if (NULL == fgets(request, (int)length, client_fd)) {
175 if (ferror(client_fd)) {
176 perror("fgets(), request");
184 while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
186 if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
190 if (ferror(client_fd)) {
191 perror("fgets(), header");
199 static void send_close_bad_request(FILE *client_fd) {
200 fprintf(client_fd, "HTTP/1.0 400 Bad Request\r\n");
201 fprintf(client_fd, "\r\n");
204 static void send_close_forwarding_failure(FILE *client_fd) {
205 fprintf(client_fd, "HTTP/1.0 503 Forwarding failure\r\n");
206 fprintf(client_fd, "\r\n");
211 /* Transfer data between client and server sockets until one closes the
213 static void transfer_data(int client, int server) {
214 struct pollfd fds[2];
216 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
219 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
223 int result = poll(fds, 2, -1 /* no timeout */);
229 /* Data available from client. */
230 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
231 if (0 != read_from_write_to(client, server)) {
232 /* EOF (or other error) */
236 /* Data available from server. */
237 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
238 if (0 != read_from_write_to(server, client)) {
239 /* EOF (or other error) */
244 /* Client closed connection. */
245 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
248 /* Server closed connection. */
249 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
255 /* Read available data from socket from and write it to socket to. At maximum
256 * 4096 bytes are read/written. */
257 static int read_from_write_to(int from, int to) {
259 ssize_t size_written;
262 size_read = read(from, buffer, sizeof(buffer));
268 if (0 == size_read) {
272 size_written = write(to, buffer, (size_t)size_read);
273 if (0 > size_written) {
277 if (size_read != size_written) {
278 printf("only written %ld of %ld bytes!\n", (long int)size_read,
279 (long int)size_written);
287 static int connect_to_host(const char *hostname, const char *port) {
288 struct addrinfo gai_hints;
289 struct addrinfo *gai_result;
293 struct addrinfo *server;
295 if (NULL == hostname || NULL == port) {
299 /* Get IP of hostname server. */
300 memset(&gai_hints, 0, sizeof(gai_hints));
301 gai_hints.ai_family = AF_UNSPEC;
302 gai_hints.ai_socktype = SOCK_STREAM;
303 gai_hints.ai_protocol = 0;
304 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
305 | AI_ADDRCONFIG /* supported by this computer */
306 | AI_V4MAPPED; /* support IPv4 through IPv6 */
307 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
308 if (0 != gai_return) {
309 perror("getaddrinfo()");
313 /* Now try to connect to each server returned by getaddrinfo(), use the
314 * first successful connect. */
315 for (server = gai_result; NULL != server; server = server->ai_next) {
316 server_socket = socket(server->ai_family,
318 server->ai_protocol);
319 if (-1 == server_socket) {
320 perror("socket(), trying next");
324 if (-1 != connect(server_socket, server->ai_addr,
325 server->ai_addrlen)) {
328 perror("connect(), trying next");
330 close(server_socket);
332 /* Make sure we free the result from getaddrinfo(). */
333 freeaddrinfo(gai_result);
335 if (NULL == server) {
336 fprintf(stderr, "no server found, aborting\n");
340 return server_socket;
344 /* Parse HTTP CONNECT request string and save its parameters.
346 * The following format is expected: "CONNECT host:port HTTP/1.y".
348 * request and host must have the same size! port must be at least 6 bytes
351 static int parse_request(const char *request, char *host, char *port,
352 int *version_minor) {
353 int port_unused; /* just used to verify the port is numeric */
356 /* scanf() doesn't check spaces. */
357 if (0 != strncmp(request, "CONNECT ", 8)) {
360 /* Check request and extract data, "host:port" is not yet separated. */
361 if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
362 host, version_minor)) {
365 /* Make sure ":port" is there. */
366 if (NULL == (position = strchr(host, ':'))) {
369 /* Make sure port is numeric. */
370 if (1 != sscanf(position + 1, "%d", &port_unused)) {
373 /* Store it in *port. */
374 strncpy(port, position + 1, 5);
376 /* And remove port from host. */