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 the request line. Longer request lines are aborted with an
32 * error. The standard doesn't specify a maximum line length but this should
33 * 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));
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 != use_proxy_host && NULL != use_proxy_port) {
101 server_socket = connect_to_host(use_proxy_host, use_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);
116 /* Connect to proxy if requested (command line option). */
117 if (NULL != use_proxy_host && NULL != use_proxy_port) {
118 fprintf(server_fd, "CONNECT %s:%s HTTP/1.0\r\n", host, port);
119 fprintf(server_fd, "\r\n");
121 /* Read response line from proxy server. */
122 result = read_http_request(server_fd, buffer, sizeof(buffer));
125 send_close_forwarding_failure(client_fd);
127 } else if (-2 == result) {
130 send_close_forwarding_failure(client_fd);
134 /* Check response of proxy server. */
135 if (0 != strncmp(buffer, "HTTP/1.0 200", 12)) {
137 printf(" bad proxy response\n");
140 send_close_forwarding_failure(client_fd);
146 printf(" connection to server established\n");
149 /* We've established a connection, tell the client. */
150 fprintf(client_fd, "HTTP/1.0 200 Connection established\r\n");
151 fprintf(client_fd, "\r\n");
154 /* And transfer all data between client and server transparently. */
155 transfer_data(client_socket, server_socket);
161 /* Read HTTP request line and headers (ignored).
163 * On success 0 is returned, -1 on client error (we close client descriptor in
164 * this case), -2 on unexpected EOF.
166 static int read_http_request(FILE *client_fd, char *request, size_t length) {
167 char buffer[MAX_REQUEST_LINE];
169 if (NULL == fgets(request, (int)length, client_fd)) {
170 if (ferror(client_fd)) {
171 perror("fgets(), request");
179 while (NULL != fgets(buffer, MAX_REQUEST_LINE, client_fd)) {
181 if (0 == strcmp(buffer, "\n") || 0 == strcmp(buffer, "\r\n")) {
185 if (ferror(client_fd)) {
186 perror("fgets(), header");
194 static void send_close_bad_request(FILE *client_fd) {
195 fprintf(client_fd, "HTTP/1.0 400 Bad Request\r\n");
196 fprintf(client_fd, "\r\n");
199 static void send_close_forwarding_failure(FILE *client_fd) {
200 fprintf(client_fd, "HTTP/1.0 503 Forwarding failure\r\n");
201 fprintf(client_fd, "\r\n");
206 /* Transfer data between client and server sockets until one closes the
208 static void transfer_data(int client, int server) {
209 struct pollfd fds[2];
211 fds[0].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
214 fds[1].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
218 int result = poll(fds, 2, -1 /* no timeout */);
224 /* Data available from client. */
225 if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI) {
226 if (0 != read_from_write_to(client, server)) {
227 /* EOF (or other error) */
231 /* Data available from server. */
232 if (fds[1].revents & POLLIN || fds[1].revents & POLLPRI) {
233 if (0 != read_from_write_to(server, client)) {
234 /* EOF (or other error) */
239 /* Client closed connection. */
240 if (fds[0].revents & POLLERR || fds[0].revents & POLLHUP) {
243 /* Server closed connection. */
244 if (fds[1].revents & POLLERR || fds[1].revents & POLLHUP) {
250 /* Read available data from socket from and write it to socket to. At maximum
251 * 4096 bytes are read/written. */
252 static int read_from_write_to(int from, int to) {
254 ssize_t size_written;
257 size_read = read(from, buffer, sizeof(buffer));
263 if (0 == size_read) {
267 size_written = write(to, buffer, (size_t)size_read);
268 if (0 > size_written) {
272 if (size_read != size_written) {
273 printf("only written %ld of %ld bytes!\n", (long int)size_read,
274 (long int)size_written);
282 static int connect_to_host(const char *hostname, const char *port) {
283 struct addrinfo gai_hints;
284 struct addrinfo *gai_result;
288 struct addrinfo *server;
290 if (NULL == hostname || NULL == port) {
294 /* Get IP of hostname server. */
295 memset(&gai_hints, 0, sizeof(gai_hints));
296 gai_hints.ai_family = AF_UNSPEC;
297 gai_hints.ai_socktype = SOCK_STREAM;
298 gai_hints.ai_protocol = 0;
299 gai_hints.ai_flags = AI_NUMERICSERV /* given port is numeric */
300 | AI_ADDRCONFIG /* supported by this computer */
301 | AI_V4MAPPED; /* support IPv4 through IPv6 */
302 gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
303 if (0 != gai_return) {
304 perror("getaddrinfo()");
308 /* Now try to connect to each server returned by getaddrinfo(), use the
309 * first successful connect. */
310 for (server = gai_result; NULL != server; server = server->ai_next) {
311 server_socket = socket(server->ai_family,
313 server->ai_protocol);
314 if (-1 == server_socket) {
315 perror("socket(), trying next");
319 if (-1 != connect(server_socket, server->ai_addr,
320 server->ai_addrlen)) {
323 perror("connect(), trying next");
325 close(server_socket);
327 /* Make sure we free the result from getaddrinfo(). */
328 freeaddrinfo(gai_result);
330 if (NULL == server) {
331 fprintf(stderr, "no server found, aborting\n");
335 return server_socket;
339 /* Parse HTTP CONNECT request string and save its parameters.
341 * The following format is expected: "CONNECT host:port HTTP/1.y".
343 * request and host must have the same size! port must be at least 6 bytes
346 static int parse_request(const char *request, char *host, char *port,
347 int *version_minor) {
348 int port_unused; /* just used to verify the port is numeric */
351 /* scanf() doesn't check spaces. */
352 if (0 != strncmp(request, "CONNECT ", 8)) {
355 /* Check request and extract data, "host:port" is not yet separated. */
356 if (2 != sscanf(request, "CONNECT %s HTTP/1.%d",
357 host, version_minor)) {
360 /* Make sure ":port" is there. */
361 if (NULL == (position = strchr(host, ':'))) {
364 /* Make sure port is numeric. */
365 if (1 != sscanf(position + 1, "%d", &port_unused)) {
368 /* Store it in *port. */
369 strncpy(port, position + 1, 5);
371 /* And remove port from host. */