Commit 15906b9f authored by Krzysztof Klis's avatar Krzysztof Klis

error statuses cleanup

parent 50a72acd
1. DESCRIPTION 1. DESCRIPTION
This is a simple proxy daemon that allows you to forward TCP requests hitting a specified port on a local host to a different port on another host. It is written in ANSI C so it takes a very little space and can be used on embedded devices. This is a simple proxy daemon that allows you to forward TCP requests hitting a specified port on a local host to a different port on another host. It is written in ANSI C so it takes a very little space and can be used on embedded devices.
2. INSTALLATION 2. INSTALLATION
On Linux compile the software using "make". On Windows use "make" from Cygwin (http://cygwin.com). On Linux compile the software using "make". On Windows use "make" from Cygwin (http://cygwin.com).
To build an executable for Tomato firmware (http://www.polarcloud.com/tomato) you need to have the mipsel toolchain installed. Fetch it with git: To build an executable for Tomato firmware (http://www.polarcloud.com/tomato) you need to have the mipsel toolchain installed. Fetch it with git:
git clone git://repo.or.cz/tomato.git tomato git clone git://repo.or.cz/tomato.git tomato
and follow instructions in the "tools/README.TXT" file. and follow instructions in the "tools/README.TXT" file.
3. USAGE 3. USAGE
Command line syntax goes as follows: Command line syntax goes as follows:
proxy -l local_port -h remote_host -p remote_port [-i "input parser"] [-o "output parser"] proxy -l local_port -h remote_host -p remote_port [-i "input parser"] [-o "output parser"]
Suppose you want to open port 8080 on a public host and forward all TCP packets to port 80 on machine 192.168.1.2 in the local network. In this case you install proxy on a public host and run it the following command: Suppose you want to open port 8080 on a public host and forward all TCP packets to port 80 on machine 192.168.1.2 in the local network. In this case you install proxy on a public host and run it the following command:
proxy -l 8080 -h 192.168.1.2 -p 80 proxy -l 8080 -h 192.168.1.2 -p 80
Input parser and output parser are commands through which incoming and outgoing packets can be forwarded. For example to use a "tee" command to log all incoming http data to incoming.txt file you can start proxy with the following options: Input parser and output parser are commands through which incoming and outgoing packets can be forwarded. For example to use a "tee" command to log all incoming http data to incoming.txt file you can start proxy with the following options:
proxy -l 8080 -h 192.168.1.2 -p 80 -i "tee -a incoming.txt" proxy -l 8080 -h 192.168.1.2 -p 80 -i "tee -a incoming.txt"
The parser command will receive data from socket to its standard input and should send parsed data to the standard output. It should also flush its output at a reasonable rate to not withhold network communication. The parser command will receive data from socket to its standard input and should send parsed data to the standard output. It should also flush its output at a reasonable rate to not withhold network communication.
...@@ -42,9 +42,20 @@ ...@@ -42,9 +42,20 @@
#include <wait.h> #include <wait.h>
#define BUF_SIZE 1024 #define BUF_SIZE 1024
#define READ 0 #define READ 0
#define WRITE 1 #define WRITE 1
#define SERVER_SOCKET_ERROR -1
#define SERVER_SETSOCKOPT_ERROR -2
#define SERVER_BIND_ERROR -3
#define SERVER_LISTEN_ERROR -4
#define CLIENT_SOCKET_ERROR -5
#define CLIENT_RESOLVE_ERROR -6
#define CLIENT_CONNECT_ERROR -7
#define CREATE_PIPE_ERROR -8
#define BROKEN_PIPE_ERROR -9
typedef enum {TRUE = 1, FALSE = 0} bool; typedef enum {TRUE = 1, FALSE = 0} bool;
int create_socket(int port); int create_socket(int port);
...@@ -157,11 +168,11 @@ int create_socket(int port) { ...@@ -157,11 +168,11 @@ int create_socket(int port) {
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return -1; return SERVER_SOCKET_ERROR;
} }
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) { if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
return -1; return SERVER_SETSOCKOPT_ERROR;
} }
memset(&server_addr, 0, sizeof(server_addr)); memset(&server_addr, 0, sizeof(server_addr));
...@@ -170,11 +181,11 @@ int create_socket(int port) { ...@@ -170,11 +181,11 @@ int create_socket(int port) {
server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) != 0) { if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) != 0) {
return -1; return SERVER_BIND_ERROR;
} }
if (listen(server_sock, 20) < 0) { if (listen(server_sock, 20) < 0) {
return -1; return SERVER_LISTEN_ERROR;
} }
return server_sock; return server_sock;
...@@ -197,7 +208,7 @@ void server_loop() { ...@@ -197,7 +208,7 @@ void server_loop() {
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
int addrlen = sizeof(client_addr); int addrlen = sizeof(client_addr);
while (1) { while (TRUE) {
client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addrlen); client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addrlen);
if (fork() == 0) { // handle client connection in a separate process if (fork() == 0) { // handle client connection in a separate process
close(server_sock); close(server_sock);
...@@ -262,7 +273,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd[]) { ...@@ -262,7 +273,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd[]) {
if (pipe(pipe_in) < 0 || pipe(pipe_out) < 0) { // create command input and output pipes if (pipe(pipe_in) < 0 || pipe(pipe_out) < 0) { // create command input and output pipes
perror("Cannot create pipe"); perror("Cannot create pipe");
exit(EXIT_FAILURE); exit(CREATE_PIPE_ERROR);
} }
if (fork() == 0) { if (fork() == 0) {
...@@ -279,7 +290,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd[]) { ...@@ -279,7 +290,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd[]) {
while ((n = recv(source_sock, buffer, BUF_SIZE, 0)) > 0) { // read data from input socket while ((n = recv(source_sock, buffer, BUF_SIZE, 0)) > 0) { // read data from input socket
if (write(pipe_in[WRITE], buffer, n) < 0) { // write data to input pipe of external command if (write(pipe_in[WRITE], buffer, n) < 0) { // write data to input pipe of external command
perror("Cannot write to pipe"); perror("Cannot write to pipe");
exit(EXIT_FAILURE); exit(BROKEN_PIPE_ERROR);
} }
if ((i = read(pipe_out[READ], buffer, BUF_SIZE)) > 0) { // read command output if ((i = read(pipe_out[READ], buffer, BUF_SIZE)) > 0) { // read command output
send(destination_sock, buffer, i, 0); // send data to output socket send(destination_sock, buffer, i, 0); // send data to output socket
...@@ -301,12 +312,12 @@ int create_connection() { ...@@ -301,12 +312,12 @@ int create_connection() {
int sock; int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return -2; return CLIENT_SOCKET_ERROR;
} }
if ((server = gethostbyname(remote_host)) == NULL) { if ((server = gethostbyname(remote_host)) == NULL) {
errno = EFAULT; errno = EFAULT;
return -3; return CLIENT_RESOLVE_ERROR;
} }
memset(&server_addr, 0, sizeof(server_addr)); memset(&server_addr, 0, sizeof(server_addr));
...@@ -315,7 +326,7 @@ int create_connection() { ...@@ -315,7 +326,7 @@ int create_connection() {
server_addr.sin_port = htons(remote_port); server_addr.sin_port = htons(remote_port);
if (connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) { if (connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
return -4; return CLIENT_CONNECT_ERROR;
} }
return sock; return sock;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment