Skip to content
Snippets Groups Projects
Commit 07ca0e90 authored by MILON ETHAN's avatar MILON ETHAN
Browse files

simple proxy server

parent 66be4f83
Branches
No related merge requests found
#define _GNU_SOURCE
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <pthread.h>
#include "utils/error.h"
#include "server/server.h"
#include <sys/poll.h>
#include <unistd.h>
int main(void)
void proxy_thread(int client, void *arg)
{
printf("Hello world\n");
struct sockaddr_in *addr = arg;
char buff[1024];
return 0;
int server;
if ((server = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
PERROR("socket");
return;
}
if (connect(server, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
close(server);
PERROR("connect");
return;
}
struct pollfd fds[2] = { { client, POLLIN, 0 }, { server, POLLIN, 0 } };
while (1) {
CHK(poll(fds, 2, -1));
ssize_t len;
if (fds[0].revents != 0) {
if ((len = recv(client, buff, sizeof(buff), 0)) <= 0)
break;
if (send(server, buff, len, 0) <= 0)
break;
fds[0].revents = 0;
}
if (fds[1].revents != 0) {
if ((len = recv(server, buff, sizeof(buff), 0)) <= 0)
break;
if (send(client, buff, len, 0) <= 0)
break;
fds[1].revents = 0;
}
}
close(server);
close(client);
return;
}
int main(int argc, char *argv[])
{
Server *server;
struct sockaddr_in addr;
if (argc != 4)
BLAME("usage: %s <listen-port> <remote-host> <remote-port>",
argv[0]);
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[3]));
if (!inet_aton(argv[2], &addr.sin_addr))
BLAME("invalid remote host: %s", argv[2]);
CHKN(server = server_new(atoi(argv[1])));
CHK(server_start(server, proxy_thread, &addr));
CHK(server_join(server));
return EXIT_SUCCESS;
}
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