| /* |
| * 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> |
| |
| #define SELECT_TIMEOUT 60 |
| |
| extern int forward(int, int); |
| |
| //static int alrm = 0; |
| |
| static void signal_handler(int sig) { |
| switch(sig) { |
| case SIGCHLD: |
| //while(wait(NULL) < 0) { |
| while(waitpid(-1, NULL, WNOHANG) < 0) { |
| if(errno == EINTR) continue; |
| perror("waitpid"); |
| return; |
| } |
| return; |
| //case SIGALRM: |
| // alrm = 1; |
| // 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. |
| |
| const char *name = strchr(argv[0], '/'); |
| if(name) name++; else name = argv[0]; |
| |
| if(argc != 3) { |
| fprintf(stderr, "Usage: %s <forward-listen-port> <data-listen-port>\n", name); |
| // Usage: %s [<bind-address>:]<forward-listen-port> [<bind-address>:]<data-listen-port> |
| // Usage: %s -d [<bind-address>:]<data-listen-port> -f [<bind-address>:]<forward-listen-port> [-f [<bind-address>:]<forward-listen-port>] [...] |
| 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); |
| //sigaction(SIGALRM, &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; |
| } |
| |
| fprintf(stderr, "\nTCP 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; |
| } |
| static const 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"); |
| |
| static const struct timeval sendtimeout = { .tv_sec = 20 }; |
| if(setsockopt(data_socket, SOL_SOCKET, SO_SNDTIMEO, &sendtimeout, sizeof sendtimeout) < 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: data"); |
| 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: forward"); |
| return 1; |
| } |
| |
| if(listen(data_socket, 1) < 0) { |
| perror("listen: data"); |
| return 1; |
| } |
| |
| if(listen(forward_socket, 256) < 0) { |
| perror("listen: forward"); |
| 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 %hu fd %d\n", |
| data_port, inet_ntoa(data_client_addr.sin_addr), ntohs(data_client_addr.sin_port), data_c_fd); |
| |
| static const struct timeval receivetimeout = { .tv_sec = 20 }; |
| if(setsockopt(data_c_fd, SOL_SOCKET, SO_RCVTIMEO, &receivetimeout, sizeof receivetimeout) < 0) perror("setsockopt"); |
| |
| |
| FD_ZERO(&fdset); |
| FD_SET(forward_socket, &fdset); |
| FD_SET(data_c_fd, &fdset); |
| int max_fd = MAX(forward_socket, data_c_fd); |
| int keep_alive_sent = 0; |
| |
| while(1) { |
| fd_set rfdset = fdset; |
| delay_tv.tv_sec = SELECT_TIMEOUT; |
| 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 %hu 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: data"); |
| 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); |
| } |
| } |
| 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 < (int)sizeof magic) { |
| if(s < 0) perror("read: data"); |
| else if(s) fprintf(stderr, "%s: 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; |
| //alarm(10); |
| //alrm = 0; |
| do { |
| s = read(data_c_fd, &packet_type, sizeof packet_type); |
| } while(s < 0 && errno == EINTR/* && !alrm*/); |
| //alarm(0); |
| if(s < (int)sizeof packet_type) { |
| if(s < 0) perror("read: data"); |
| else if(s) fprintf(stderr, "%s: 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: keep alive from client\n", argv[0]); |
| packet_type = htons(KEEP_ALIVE_REPLY); |
| if(sync_write(data_c_fd, MAGIC, 4) < 0 || |
| sync_write(data_c_fd, &packet_type, sizeof packet_type) < 0) { |
| perror("write: data"); |
| break; |
| } |
| } else if(packet_type == KEEP_ALIVE_REPLY) { |
| if(!keep_alive_sent) { |
| fprintf(stderr, "%s: Unexpected keep alive reply from client, disconnecting\n", argv[0]); |
| break; |
| } |
| keep_alive_sent--; |
| } else { |
| fprintf(stderr, "%s: Unknown packet type %hu received from client\n", argv[0], packet_type); |
| break; |
| } |
| } |
| } else { |
| if(keep_alive_sent) { |
| fprintf(stderr, "%s: Client dosen't reply the recent keep alive message in %u sec\n", |
| argv[0], (unsigned int)SELECT_TIMEOUT); |
| break; |
| } |
| uint16_t packet_type = htons(KEEP_ALIVE); |
| if(sync_write(data_c_fd, MAGIC, 4) < 0 || sync_write(data_c_fd, &packet_type, sizeof packet_type) < 0) { |
| perror("write: data"); |
| break; |
| } |
| keep_alive_sent++; |
| } |
| } |
| close(data_c_fd); |
| } |
| } |