Compare commits

...

5 Commits

3 changed files with 46 additions and 11 deletions

View File

@@ -44,7 +44,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest) {
return;
} else {
exit(-2);
exit(2);
}
}
@@ -52,7 +52,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest) {
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) {
int socket = create_socket(network,transport);
if (socket < 0) {
exit(socket);
exit(errno);
}
create_addr(network,address,port,addr_struct);
int addrlen;
@@ -61,7 +61,7 @@ int create_local (int network, char transport, char* address, int port,struct so
} else if (network == 6) {
addrlen = sizeof(struct sockaddr_in6);
} else {
exit(-7);
exit(7);
}
/* The value of addrlen should be the size of the 'sockaddr'.
@@ -80,7 +80,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
int socket = create_socket(network,transport);
if (socket < 0) {
exit(socket);
exit(errno);
}
create_addr(network,address,port,remote_addr_struct);
int addrlen;
@@ -89,7 +89,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
} else if (network == 6) {
addrlen = sizeof(struct sockaddr_in6);
} else {
exit(-7);
exit(7);
}
/* The value of addrlen should be the size of the 'sockaddr'.

View File

@@ -26,7 +26,7 @@ int create_socket(int network, char transport);
port is self-explanatory;
and dest is a pointer to the sockaddr struct that will be filled in.
The function exits with error code -2 if the network parameter contained neither '4'
The function exits with error code 2 if the network parameter contained neither '4'
nor '6'. */
void create_addr(int network, char* address, int port,struct sockaddr* dest);
@@ -37,7 +37,7 @@ void create_addr(int network, char* address, int port,struct sockaddr* dest);
_binds_ the addresses. It is used for local sockets (server sockets). Parameters are
same as above.
It prints the error returned by 'bind' if something went wrong, and exits with error code '-3'.*/
It prints the error returned by 'bind' if something went wrong, and exits with error code '3'.*/
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct);
@@ -46,7 +46,7 @@ int create_local (int network, char transport, char* address, int port,struct so
requested address. It is used for remote sockets (client sockets). The paramters are same
as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill.
It prints the error returned by 'connect' if something went wrong, and exits with error code '-3'.*/
It prints the error returned by 'connect' if something went wrong, and exits with error code '3'.*/
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);

41
main.c
View File

@@ -3,7 +3,8 @@
#include <unistd.h>
#include <stdlib.h>
#include "easysock.h"
#define PROG_NAME "basicproxy"
#define PROG_VER "0.01"
void forward_data(int from_fd, int to_fd) {
int n = 0;
char* buffer = malloc(3000*sizeof(char));
@@ -24,6 +25,24 @@ int check_ip_ver(char* address) {
}
void print_prog_info() {
printf("%s [Local IP address] [local port] [remote IP address] [remote port]\n",PROG_NAME);
}
void print_version_info() {
printf("%s %s\n",PROG_NAME,PROG_VER);
}
void print_help_info() {
printf("\n%s - A simple TCP proxy written in C.\n\n",PROG_NAME);
printf("Syntax - ");
print_prog_info();
printf("[Local IP address] - The local address to bind to\n"
"[local port] - The local port to listen on\n"
"[remote IP address] - The remote IP to connect to\n"
"[remote port] - The remote port to connect to\n");
printf("\nExample: %s 127.0.0.1 3000 1.2.3.4 5000 - Listen on '127.0.0.1:3000', and forward all data from there to '1.2.3.4:5000'\n\n",PROG_NAME);
}
int main(int argc,char* argv[]) {
/* argv[1] = local address
@@ -31,6 +50,22 @@ int main(int argc,char* argv[]) {
argv[3] = remote address
argv[4] = remote port */
if (argc == 2) {
if (strcmp(argv[1],"-v") == 0 || strcmp(argv[1],"--version") == 0) {
print_version_info();
exit(0);
} else if (strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0) {
print_help_info();
exit(0);
}
}
if (argc != 5) {
print_prog_info();
exit(30);
}
char* local_addr = argv[1];
int local_port = strtol(argv[2],NULL,10);
char* remote_addr = argv[3];
@@ -41,7 +76,7 @@ int main(int argc,char* argv[]) {
printf("Using %d for local\nUsing %d for remote\n",preferred_local_network,preferred_remote_network);
if ((preferred_local_network == -1) || (preferred_remote_network == -1)) {
exit(-7);
exit(7);
}
char preferred_transport = 'T';
@@ -53,7 +88,7 @@ int main(int argc,char* argv[]) {
} else if (check_ip_ver(local_addr) == 6) {
addrlen = sizeof(struct sockaddr_in6);
} else {
exit(-7);
exit(7);
}
listen(server_sock,50); /* Arbitrary number, change later */