blob: a93259be4224e8290e06cd610a13809b4d320140 [file] [log] [blame] [raw]
/*
* Copyright 2015-2017 Rivoreo
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*/
#include "common.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "syncrw.h"
#include <signal.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
extern int forward(int, int);
static void signal_handler(int sig) {
if(sig != SIGCHLD) return;
//while(wait(NULL) < 0) {
while(waitpid(-1, NULL, WNOHANG) < 0) {
if(errno == EINTR) continue;
perror("waitpid");
return;
}
}
int main(int argc, char **argv) {
// TODO: Use getopt(3) to parse unlimited number of forward listen ports.
// TODO: Add support for bind to a specified address instead of INADDR_ANY for each listen.
if(argc != 3) {
fprintf(stderr, "Usage: %s <forward-listen-port> <data-listen-port>\n", argv[0]);
return -1;
}
struct sigaction act = { .sa_handler = SIG_IGN };
if(sigaction(SIGPIPE, &act, NULL) < 0) {
perror("sigaction");
return 1;
}
act.sa_handler = signal_handler;
sigaction(SIGCHLD, &act, NULL);
int forward_port = atoi(argv[1]);
int data_port = atoi(argv[2]);
if(forward_port < 1 || data_port < 1) {
fprintf(stderr, "%s: Port number must be greater than 0\n", argv[0]);
return -1;
}
const char *name = strchr(argv[0], '/');
if(name) name++; else name = argv[0];
fprintf(stderr, "TCP Reverse Port Forwarding Server - %s\n"
RIVOREO_COPYRIGHT_NOTICE "\n"
LICENSE_INFORMATION "\n\n", name);
int data_socket = socket(AF_INET, SOCK_STREAM, 0);
int forward_socket = socket(AF_INET, SOCK_STREAM, 0);
if(data_socket == -1 || forward_socket == -1) {
perror("socket");
return 1;
}
int reuseaddr = 1;
if(setsockopt(data_socket, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof reuseaddr) < 0) perror("setsockopt");
if(setsockopt(forward_socket, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof reuseaddr) < 0) perror("setsockopt");
struct sockaddr_in data_listen_addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = htonl(INADDR_ANY),
.sin_port = htons(data_port)
};
while(bind(data_socket, (struct sockaddr *)&data_listen_addr, sizeof data_listen_addr) < 0) {
if(errno == EAGAIN || errno == EINTR) continue;
perror("bind");
return 1;
}
struct sockaddr_in forward_listen_addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = htonl(INADDR_ANY),
.sin_port = htons(forward_port)
};
while(bind(forward_socket, (struct sockaddr *)&forward_listen_addr, sizeof forward_listen_addr) < 0) {
if(errno == EAGAIN || errno == EINTR) continue;
perror("bind");
return 1;
}
if(listen(data_socket, 1) < 0) {
perror("listen");
return 1;
}
if(listen(forward_socket, 100) < 0) {
perror("listen");
return 1;
}
fd_set fdset;
struct timeval delay_tv;
while(1) {
struct sockaddr_in data_client_addr;
socklen_t data_addr_len = sizeof data_client_addr;
int data_c_fd;
do {
data_c_fd = accept(data_socket, (struct sockaddr *)&data_client_addr, &data_addr_len);
} while(data_c_fd == -1 && errno == EINTR);
if(data_c_fd == -1) {
perror("accept: data");
sleep(1);
continue;
}
fprintf(stderr, "connection to data port %d from %s port %d fd %d\n",
data_port, inet_ntoa(data_client_addr.sin_addr), ntohs(data_client_addr.sin_port), data_c_fd);
FD_ZERO(&fdset);
FD_SET(forward_socket, &fdset);
FD_SET(data_c_fd, &fdset);
int max_fd = MAX(forward_socket, data_c_fd);
while(1) {
fd_set rfdset = fdset;
delay_tv.tv_sec = 60;
delay_tv.tv_usec = 0;
int n = select(max_fd + 1, &rfdset, NULL, NULL, &delay_tv);
if(n < 0) {
if(errno == EINTR) continue;
perror("select");
sleep(1);
continue;
}
if(n) {
if(FD_ISSET(forward_socket, &rfdset)) {
struct sockaddr_in forward_client_addr;
socklen_t forward_addr_len = sizeof forward_client_addr;
int forward_c_fd;
do {
forward_c_fd = accept(forward_socket, (struct sockaddr *)&forward_client_addr, &forward_addr_len);
} while(forward_c_fd == -1 && errno == EINTR);
if(forward_c_fd == -1) {
perror("accept: forward");
sleep(1);
continue;
}
fprintf(stderr, "connection to forward port %d from %s port %d fd %d\n",
forward_port, inet_ntoa(forward_client_addr.sin_addr), ntohs(forward_client_addr.sin_port), data_c_fd);
uint16_t packet_type = htons(NEW_CONNECTION);
struct new_connection_packet packet;
packet.len = htonl(sizeof packet);
/*
if(gettimeofday(&packet.tv, NULL) < 0) {
perror("gettimeofday");
memset(&packet.tv, 0, sizeof packet.tv);
}
memcpy(&packet.addr, &forward_client_addr, sizeof forward_client_addr);
size_t ped_zero_len = sizeof packet.addr - sizeof forward_client_addr;
memset(&packet.addr, 0, ped_zero_len);
*/
packet.address = forward_client_addr.sin_addr;
packet.port = forward_client_addr.sin_port;
if(sync_write(data_c_fd, MAGIC, 4) < 0 || sync_write(data_c_fd, &packet_type, sizeof packet_type) < 0 || sync_write(data_c_fd, &packet, sizeof packet) < 0) {
perror("write");
break;
}
pid_t pid = fork();
if(pid < 0) {
perror("fork");
close(forward_c_fd);
continue;
}
if(pid) {
//close(data_c_fd); // Will be closed in end of the outer loop
close(forward_c_fd);
break;
} else {
close(data_socket);
close(forward_socket);
exit(forward(data_c_fd, forward_c_fd) < 0 ? 1 : 0);
//_exit(0);
}
}
if(FD_ISSET(data_c_fd, &rfdset)) {
char magic[sizeof MAGIC - 1];
int s;
do {
s = read(data_c_fd, magic, sizeof magic);
} while(s < 0 && errno == EINTR);
if(s < sizeof magic) {
if(s) fprintf(stderr, "%s: XXX closing connection due to incomplete packet (received %d byte(s))\n", argv[0], s);
break;
}
if(memcmp(magic, MAGIC, sizeof magic)) {
fprintf(stderr, "%s: Protocol mismatch\n", argv[0]);
break;
}
uint16_t packet_type;
do {
s = read(data_c_fd, &packet_type, sizeof packet_type);
} while(s < 0 && errno == EINTR);
if(s < sizeof packet_type) {
if(s) fprintf(stderr, "%s: XXX closing connection due to incomplete packet (received %d byte)\n", argv[0], s);
break;
}
packet_type = ntohs(packet_type);
if(packet_type != KEEP_ALIVE) {
fprintf(stderr, "%s: Unknown packet type %hu received from client\n", argv[0], packet_type);
break;
}
fprintf(stderr, "%s: keep alive from client\n", argv[0]);
}
} else {
uint16_t packet_type = htons(KEEP_ALIVE);
//uint16_t packet_type = KEEP_ALIVE;
if(sync_write(data_c_fd, MAGIC, 4) < 0 || sync_write(data_c_fd, &packet_type, sizeof packet_type) < 0) {
perror("write");
break;
}
}
}
close(data_c_fd);
}
}