You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB
C

2 years ago
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include "easysock.h"
2 years ago
void forward_data(int from_fd, int to_fd) {
int n = 0;
char* buffer = malloc(3000*sizeof(char));
2 years ago
while ((n = recv(from_fd, buffer, 3000, 0)) > 0) { // read data from input socket
2 years ago
send(to_fd, buffer, n, 0); // send data to output socket
}
}
int main() {
int preferred_network = 4;
char preferred_transport = 'T';
struct sockaddr addr_struct;
int server_sock = create_local(preferred_network,preferred_transport,"127.0.0.1",3000,&addr_struct);
2 years ago
int addrlen = sizeof(addr_struct);
listen(server_sock,50); /* Arbitrary number, change later */
while (1) {
int from_client = accept(server_sock,&addr_struct,(socklen_t *)&addrlen);
int to_server = create_remote(preferred_network,preferred_transport,"127.0.0.1",5000);
if (fork() == 0) {
/* 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);
}
// recv(from_client,buffer,3000,0);
// printf("%s",buffer);
}
}