Compare commits
7 Commits
2ac2bf7249
...
a157fd5851
Author | SHA1 | Date | |
---|---|---|---|
a157fd5851 | |||
ca1cb08cd9 | |||
f0d084ad9d | |||
130346b006 | |||
dbc838aa50 | |||
a321f677a4 | |||
de79fc1ff7 |
3
Makefile
3
Makefile
@@ -8,6 +8,9 @@ $(EXEC_FILE): main.o easysock.o
|
||||
|
||||
allwarn: CFLAGS+=-Wall -Wextra -pedantic
|
||||
allwarn: $(EXEC_FILE)
|
||||
|
||||
debug: CFLAGS+=-g
|
||||
debug: $(EXEC_FILE)
|
||||
|
||||
clean: $(EXEC_FILE) main.o easysock.o
|
||||
rm ./$(EXEC_FILE)
|
||||
|
2
TODO.txt
2
TODO.txt
@@ -12,4 +12,4 @@
|
||||
|
||||
---SHOULD BE DONE---- 5. Fix Makefile - Doesn't recompile if source files have changed, only if .o files have.
|
||||
|
||||
6. Check IPv6 support for remote socket
|
||||
---SHOULD BE DONE--- 6. Check IPv6 support for remote socket
|
||||
|
11
easysock.c
11
easysock.c
@@ -75,15 +75,14 @@ int create_local (int network, char transport, char* address, int port,struct so
|
||||
return socket;
|
||||
}
|
||||
|
||||
int create_remote (int network,char transport,char* address,int port) {
|
||||
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) {
|
||||
|
||||
|
||||
int socket = create_socket(network,transport);
|
||||
if (socket == -1) {
|
||||
exit(-1);
|
||||
if (socket < 0) {
|
||||
exit(socket);
|
||||
}
|
||||
struct sockaddr remote_addr_struct;
|
||||
create_addr(network,address,port,&remote_addr_struct);
|
||||
create_addr(network,address,port,remote_addr_struct);
|
||||
int addrlen;
|
||||
if (network == 4) {
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
@@ -97,7 +96,7 @@ int create_remote (int network,char transport,char* address,int port) {
|
||||
This should be set to the size of 'sockaddr_in' for IPv4, and 'sockaddr_in6' for IPv6.
|
||||
See https://stackoverflow.com/questions/73707162/socket-bind-failed-with-invalid-argument-error-for-program-running-on-macos */
|
||||
|
||||
int i = connect(socket,&remote_addr_struct,(socklen_t)addrlen);
|
||||
int i = connect(socket,remote_addr_struct,(socklen_t)addrlen);
|
||||
if (i < 0) {
|
||||
exit(errno);
|
||||
}
|
||||
|
@@ -43,12 +43,12 @@ int create_local (int network, char transport, char* address, int port,struct so
|
||||
|
||||
|
||||
/* This function utilizes the same functions as 'create_local' but _connects_ to the
|
||||
requested address. It is used for remoet sockets (client sockets). The paramters are same
|
||||
as above.
|
||||
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'.*/
|
||||
|
||||
int create_remote (int network,char transport,char* address,int port);
|
||||
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);
|
||||
|
||||
|
||||
#endif
|
||||
|
49
main.c
49
main.c
@@ -39,7 +39,7 @@ int main(int argc,char* argv[]) {
|
||||
int preferred_local_network = check_ip_ver(local_addr);
|
||||
int preferred_remote_network = check_ip_ver(remote_addr);
|
||||
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);
|
||||
}
|
||||
@@ -47,38 +47,39 @@ int main(int argc,char* argv[]) {
|
||||
char preferred_transport = 'T';
|
||||
struct sockaddr addr_struct;
|
||||
int server_sock = create_local(preferred_local_network,preferred_transport,local_addr,local_port,&addr_struct);
|
||||
int addrlen = sizeof(addr_struct);
|
||||
int addrlen;
|
||||
if (check_ip_ver(local_addr) == 4) {
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
} else if (check_ip_ver(local_addr) == 6) {
|
||||
addrlen = sizeof(struct sockaddr_in6);
|
||||
} else {
|
||||
exit(-7);
|
||||
}
|
||||
|
||||
listen(server_sock,50); /* Arbitrary number, change later */
|
||||
|
||||
printf("Listening on %s:%d\n",local_addr,local_port);
|
||||
|
||||
struct sockaddr remote_addr_struct;
|
||||
while (1) {
|
||||
int from_client = accept(server_sock,&addr_struct,(socklen_t *)&addrlen);
|
||||
int to_server = create_remote(preferred_remote_network,preferred_transport,remote_addr,remote_port);
|
||||
int from_client = accept(server_sock,&addr_struct,(socklen_t *)&addrlen);
|
||||
int to_server = create_remote(preferred_remote_network,preferred_transport,remote_addr,remote_port,&remote_addr_struct);
|
||||
|
||||
printf("Connection established to %s:%d\n",remote_addr,remote_port);
|
||||
if (fork() == 0) {
|
||||
|
||||
/* fork returns 0 for a child, so we're in the child's execution
|
||||
right now */
|
||||
close(server_sock);
|
||||
printf("Connection established to %s:%d\n",remote_addr,remote_port);
|
||||
if (fork() == 0) {
|
||||
forward_data(from_client,to_server);
|
||||
/* fork returns 0 for a child, so we're in the child's execution
|
||||
right now */
|
||||
close(server_sock);
|
||||
if (fork() == 0) {
|
||||
forward_data(from_client,to_server);
|
||||
exit(0);
|
||||
}
|
||||
if (fork() == 0) {
|
||||
forward_data(to_server,from_client);
|
||||
exit(0);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
if (fork() == 0) {
|
||||
forward_data(to_server,from_client);
|
||||
exit(0);
|
||||
}
|
||||
exit(0);
|
||||
|
||||
|
||||
}
|
||||
// recv(from_client,buffer,3000,0);
|
||||
|
||||
|
||||
// printf("%s",buffer);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user