Commit c6238d6f authored by jerome.poulin's avatar jerome.poulin Committed by Jérôme Poulin

Increase BUF_SIZE to fit MTU, remove unused variable, cleanup on fail.

- BUF_SIZE was less than MTU resulting in sub-optimal performance, 16384
  is even more than enough to accomodate Jumbo Frames.
- Sockets weren't closed in the event of a failure to connect to remote.
- Fixed some compiler warnings about invalid data types.
parent e0ee747c
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
#include <unistd.h> #include <unistd.h>
#include <wait.h> #include <wait.h>
#define BUF_SIZE 1024 #define BUF_SIZE 16384
#define READ 0 #define READ 0
#define WRITE 1 #define WRITE 1
...@@ -74,7 +74,7 @@ bool foreground = FALSE; ...@@ -74,7 +74,7 @@ bool foreground = FALSE;
/* Program start */ /* Program start */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int c, local_port; int local_port;
pid_t pid; pid_t pid;
local_port = parse_options(argc, argv); local_port = parse_options(argc, argv);
...@@ -187,7 +187,7 @@ void sigterm_handler(int signal) { ...@@ -187,7 +187,7 @@ void sigterm_handler(int signal) {
/* Main server loop */ /* Main server loop */
void server_loop() { void server_loop() {
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
int addrlen = sizeof(client_addr); socklen_t addrlen = sizeof(client_addr);
while (TRUE) { while (TRUE) {
client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addrlen); client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addrlen);
...@@ -206,7 +206,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr) ...@@ -206,7 +206,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr)
{ {
if ((remote_sock = create_connection()) < 0) { if ((remote_sock = create_connection()) < 0) {
perror("Cannot connect to host"); perror("Cannot connect to host");
return; goto cleanup;
} }
if (fork() == 0) { // a process forwarding data from client to remote socket if (fork() == 0) { // a process forwarding data from client to remote socket
...@@ -227,6 +227,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr) ...@@ -227,6 +227,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr)
exit(0); exit(0);
} }
cleanup:
close(remote_sock); close(remote_sock);
close(client_sock); close(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