what you don't know can hurt you
Home Files News &[SERVICES_TAB]About Contact Add New

relay.v4.c

relay.v4.c
Posted Nov 24, 2004
Authored by Thun

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.

tags | tool
systems | unix
SHA-256 | 086dfbc690fc8acaf175d245b3348248fc74d730c4f0b737150ad04bf943a604

relay.v4.c

Change Mirror Download
/*

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;
}
Login or Register to add favorites

File Archive:

March 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Mar 1st
    16 Files
  • 2
    Mar 2nd
    0 Files
  • 3
    Mar 3rd
    0 Files
  • 4
    Mar 4th
    32 Files
  • 5
    Mar 5th
    28 Files
  • 6
    Mar 6th
    42 Files
  • 7
    Mar 7th
    17 Files
  • 8
    Mar 8th
    13 Files
  • 9
    Mar 9th
    0 Files
  • 10
    Mar 10th
    0 Files
  • 11
    Mar 11th
    15 Files
  • 12
    Mar 12th
    19 Files
  • 13
    Mar 13th
    21 Files
  • 14
    Mar 14th
    38 Files
  • 15
    Mar 15th
    15 Files
  • 16
    Mar 16th
    0 Files
  • 17
    Mar 17th
    0 Files
  • 18
    Mar 18th
    10 Files
  • 19
    Mar 19th
    0 Files
  • 20
    Mar 20th
    0 Files
  • 21
    Mar 21st
    0 Files
  • 22
    Mar 22nd
    0 Files
  • 23
    Mar 23rd
    0 Files
  • 24
    Mar 24th
    0 Files
  • 25
    Mar 25th
    0 Files
  • 26
    Mar 26th
    0 Files
  • 27
    Mar 27th
    0 Files
  • 28
    Mar 28th
    0 Files
  • 29
    Mar 29th
    0 Files
  • 30
    Mar 30th
    0 Files
  • 31
    Mar 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close