This forwards connections on any port you want to any host & port you like. Added the ability to select which device to listen on. Based on Laq's relay.3.
086dfbc690fc8acaf175d245b3348248fc74d730c4f0b737150ad04bf943a604
/*
Modified 11/23/2004
by thun
Based on Laq's relay .3
.3a
Added ability to set device to listen on
*/
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#define VERSION "0.4"
struct ifreq ifreq;
struct sockaddr_in *saptr = NULL;
int do_connect(char *host, char *port);
int do_listen(char *port);
int once=0;
int any=1;
int main(int ac, char **av)
{
fd_set s_read, d_read;
char buffer[2048];
int s_sock, d_sock;
int fd;
int i;
if (ac < 4)
{
fprintf(stderr, "\nUsage: %s lport rhost rport [options]\n", av[0]);
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, "\t-once Run once then die.\n");
fprintf(stderr, "\t-i eth[X] Set device (example -i eth0).\n");
fprintf(stderr, "\nRelay %s by Thun based on Laq's relay .3\n\n", VERSION);
exit(1);
}
if (ac >= 5)
for (i=4; i <ac; i++)
{
//check options
if (!strcmp(av[i],"-once"))
once=1;
if (!strcmp(av[i],"-i"))
{
strncpy(ifreq.ifr_name, av[i+1], IF_NAMESIZE);
any=0;
}
}
printf("\nRelay %s\n", VERSION );
printf("\t*Relaying port %s -> %s:%s\n", av[1], av[2], av[3]);
if (once)
printf("\t*Running only once\n");
fd = open("/dev/tty", O_RDWR);
if (ioctl(fd, TIOCNOTTY, "1") < 0)
{
perror("\t** ERROR ioctl");
exit(1);
}
if (fork() != 0)
exit(1);
else
close(fd);
while(1)
{
s_sock = do_listen(av[1]);
d_sock = do_connect(av[2], av[3]);
while(1)
{
struct timeval timeout;
int status;
FD_ZERO(&s_read);
FD_SET(s_sock, &s_read);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
bzero(buffer, 2048);
select(s_sock+1, &s_read, NULL, NULL, &timeout);
if (FD_ISSET(s_sock, &s_read))
{
status = recv(s_sock, buffer, 2048, 0);
if (status == 0)
exit(1);
if (send(d_sock, buffer, status, 0) == 0)
exit(1);
}
FD_ZERO(&d_read);
FD_SET(d_sock, &d_read);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
bzero(buffer, 2048);
select(d_sock+1, &d_read, NULL, NULL, &timeout);
if (FD_ISSET(d_sock, &d_read))
{
status = recv(d_sock, buffer, 2048, 0);
if (status == 0)
exit(1);
if (send(s_sock, buffer, status, 0) == 0)
exit(1);
}
}
}
}
int do_connect(char *host, char *port)
{
struct hostent *he;
struct sockaddr_in addr;
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror("\t** ERROR socket");
exit(1);
}
addr.sin_port = htons(strtol(port, NULL, 0));
addr.sin_family = AF_INET;
he = gethostbyname(host);
if (he == NULL)
addr.sin_addr.s_addr = inet_addr(host);
else
memcpy((void *)&addr.sin_addr, he->h_addr_list[0], sizeof(struct
in_addr));
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("\t** ERROR connect");
exit(1);
}
return sock;
}
int do_listen(char *port)
{
struct sockaddr_in addr;
int mainsock;
int sock;
int len;
mainsock = socket(AF_INET, SOCK_STREAM, 0);
if (mainsock < 0)
{
perror("\t** ERROR socket");
exit(1);
}
addr.sin_family = AF_INET;
if (any)
{
printf("\t*Listening on any.\n");
addr.sin_addr.s_addr = INADDR_ANY;
}
else
{
if (ioctl(mainsock, SIOCGIFADDR, &ifreq) < 0)
{
perror("\t** ERROR ioctl");
exit(1);
}
//print out the address
saptr = (struct sockaddr_in *)&ifreq.ifr_addr;
printf("\t*Listening on %s ip %s\n",
ifreq.ifr_name,
inet_ntoa(saptr->sin_addr) );
addr.sin_addr.s_addr = inet_addr(inet_ntoa(saptr->sin_addr));
}
addr.sin_port = htons(strtol(port, NULL, 0));
if (bind(mainsock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("\t** ERROR bind");
exit(1);
}
if (listen(mainsock, 3) < 0)
{
perror("\t** ERROR listen");
exit(1);
}
while (1)
{
len = sizeof(addr);
sock = accept(mainsock, (struct sockaddr *)&addr, &len);
if (sock < 0)
{
perror("\t** ERROR accept");
exit(1);
}
if (once)
break;
if (fork() == 0)
{
close(sock);
continue;
}
else
break;
}
return sock;
}