[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

IP Bouncer.. source included.




Ok an IP bouncer is something that runs on a host that basically accepts
connections on one port and redirects them to another specified host and port.
The one below is for tcp connections but it could also be for udp with
a little work. I actually have the code for a 'server' version of this that
you connect to it, tell it which host you want and then it opens a 
connection to it. It's on another machine at the moment which is down.


I had the sent to me via IRC a while ago and have used it off and on. Ive
been meaning to fine tune it a bit as it's supposed to chew CPU a bit...

-------cut here--------cut here--------cut here---------cut here-------------
/* This file is telserv.c and is part of the Telnet Server package v. 1.0,
   written by "Hal-9000".  Much of this package was developed by Richard
   Stephens and my thanks go to "Xanadude" for providing me with that
   section.

   To compile, type "cc -O -s telserv.c -o telserv". */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERV_TCP_PORT 12345 /* port I'll listen for connections on */
#define REM_HOST_ADDR "128.128.128.7" /* host I will bounce to */
#define REM_TCP_PORT 23               /* port I will bounce to */

main()
{
  int sockfd, newsockfd, clilen, childpid;
  struct sockaddr_in  cli_addr, serv_addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family      = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port        = htons(SERV_TCP_PORT);
  bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  listen(sockfd, 5);
  while (1) {
    clilen = sizeof(cli_addr);
    newsockfd=accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    fcntl(newsockfd,F_SETFL,O_NDELAY);
    childpid = fork();
    if (childpid == 0) {         /* child process */
      close(sockfd);             /* close original socket */
      telcli(newsockfd);         /* process the request */
      exit(0);
    }

    close(newsockfd);            /* parent process */
    wait(0);
    }
  }

telcli(clisockfd)
{
  int servsockfd;
  struct sockaddr_in  serv_addr;
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family       = AF_INET;
  serv_addr.sin_addr.s_addr  = inet_addr(REM_HOST_ADDR);
  serv_addr.sin_port         = htons(REM_TCP_PORT);
  servsockfd = socket(AF_INET, SOCK_STREAM, 0);
  connect(servsockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  fcntl(servsockfd,F_SETFL,O_NDELAY);
  communicate(servsockfd,clisockfd);
  close(servsockfd);
  exit(0);
}

communicate(servsockfd,clisockfd)  {
   char rec[1];
   int num;
   extern int errno;
   while (1) {
     num=read(servsockfd,rec,1);
	 if ((num==-1) && (errno != EWOULDBLOCK)) return;
	 if (num==0) return;
     if (num==1) write(clisockfd,rec,1);
     num=read(clisockfd,rec,1);
	 if ((num==-1) && (errno != EWOULDBLOCK)) return;
	 if (num==0) return;
	 if (num==1) write(servsockfd,rec,1);
     }
}