Commit e0ee747c authored by Krzysztof Klis's avatar Krzysztof Klis

Added -f command line switch

parent c2c29fd6
...@@ -32,12 +32,16 @@ when using the Backfire toolchain. ...@@ -32,12 +32,16 @@ when using the Backfire toolchain.
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"] [-f (stay in foreground)]
``` ```
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 will 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 will 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
``` ```
Normally, proxy forks into the background. To make it stay in the foreground (for example for debugging purposes), use "-f" switch:
```
proxy -l 8080 -h 192.168.1.2 -p 80 -f
```
## Parsers ## Parsers
......
...@@ -68,9 +68,9 @@ void forward_data(int source_sock, int destination_sock); ...@@ -68,9 +68,9 @@ void forward_data(int source_sock, int destination_sock);
void forward_data_ext(int source_sock, int destination_sock, char *cmd); void forward_data_ext(int source_sock, int destination_sock, char *cmd);
int parse_options(int argc, char *argv[]); int parse_options(int argc, char *argv[]);
int server_sock, client_sock, remote_sock, remote_port; int server_sock, client_sock, remote_sock, remote_port = 0;
char *remote_host, *cmd_in, *cmd_out; char *remote_host, *cmd_in, *cmd_out;
bool opt_in = FALSE, opt_out = FALSE; bool foreground = FALSE;
/* Program start */ /* Program start */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
...@@ -80,7 +80,7 @@ int main(int argc, char *argv[]) { ...@@ -80,7 +80,7 @@ int main(int argc, char *argv[]) {
local_port = parse_options(argc, argv); local_port = parse_options(argc, argv);
if (local_port < 0) { if (local_port < 0) {
printf("Syntax: %s -l local_port -h remote_host -p remote_port [-i \"input parser\"] [-o \"output parser\"]\n", argv[0]); printf("Syntax: %s -l local_port -h remote_host -p remote_port [-i \"input parser\"] [-o \"output parser\"] [-f (stay in foreground)]\n", argv[0]);
return local_port; return local_port;
} }
...@@ -92,15 +92,19 @@ int main(int argc, char *argv[]) { ...@@ -92,15 +92,19 @@ int main(int argc, char *argv[]) {
signal(SIGCHLD, sigchld_handler); // prevent ended children from becoming zombies signal(SIGCHLD, sigchld_handler); // prevent ended children from becoming zombies
signal(SIGTERM, sigterm_handler); // handle KILL signal signal(SIGTERM, sigterm_handler); // handle KILL signal
switch(pid = fork()) { if (foreground) {
case 0: server_loop();
server_loop(); // daemonized child } else {
break; switch(pid = fork()) {
case -1: case 0: // deamonized child
perror("Cannot daemonize"); server_loop();
return pid; break;
default: case -1: // error
close(server_sock); perror("Cannot daemonize");
return pid;
default: // parent
close(server_sock);
}
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
...@@ -108,37 +112,31 @@ int main(int argc, char *argv[]) { ...@@ -108,37 +112,31 @@ int main(int argc, char *argv[]) {
/* Parse command line options */ /* Parse command line options */
int parse_options(int argc, char *argv[]) { int parse_options(int argc, char *argv[]) {
bool l, h, p; int c, local_port = 0;
int c, local_port;
l = h = p = FALSE;
while ((c = getopt(argc, argv, "l:h:p:i:o:")) != -1) { while ((c = getopt(argc, argv, "l:h:p:i:o:f")) != -1) {
switch(c) { switch(c) {
case 'l': case 'l':
local_port = atoi(optarg); local_port = atoi(optarg);
l = TRUE;
break; break;
case 'h': case 'h':
remote_host = optarg; remote_host = optarg;
h = TRUE;
break; break;
case 'p': case 'p':
remote_port = atoi(optarg); remote_port = atoi(optarg);
p = TRUE;
break; break;
case 'i': case 'i':
cmd_in = optarg; cmd_in = optarg;
opt_in = TRUE;
break; break;
case 'o': case 'o':
cmd_out = optarg; cmd_out = optarg;
opt_out = TRUE;
break; break;
case 'f':
foreground = TRUE;
} }
} }
if (l && h && p) { if (local_port && remote_host && remote_port) {
return local_port; return local_port;
} else { } else {
return SYNTAX_ERROR; return SYNTAX_ERROR;
...@@ -212,7 +210,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr) ...@@ -212,7 +210,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr)
} }
if (fork() == 0) { // a process forwarding data from client to remote socket if (fork() == 0) { // a process forwarding data from client to remote socket
if (opt_out) { if (cmd_out) {
forward_data_ext(client_sock, remote_sock, cmd_out); forward_data_ext(client_sock, remote_sock, cmd_out);
} else { } else {
forward_data(client_sock, remote_sock); forward_data(client_sock, remote_sock);
...@@ -221,7 +219,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr) ...@@ -221,7 +219,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr)
} }
if (fork() == 0) { // a process forwarding data from remote socket to client if (fork() == 0) { // a process forwarding data from remote socket to client
if (opt_in) { if (cmd_in) {
forward_data_ext(remote_sock, client_sock, cmd_in); forward_data_ext(remote_sock, client_sock, cmd_in);
} else { } else {
forward_data(remote_sock, client_sock); forward_data(remote_sock, client_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