NoPaste Service
DOWNLOAD
Language: C
Author: Matrix86
Description: Simple Tunnel
Date: 03/02/08 13:33
  1. /**************************************************************************
  2.     This program is free software; you can redistribute it and/or modify
  3.     it under the terms of the GNU General Public License as published by
  4.     the Free Software Foundation; either version 3 of the License, or
  5.     (at your option) any later version.
  6.  
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.     GNU General Public License for more details.
  11.  
  12.     You should have received a copy of the GNU General Public License
  13.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14.  
  15.    Coded By Matrix86 ----> matrix86 {AT} tuxmealux {DOT} net
  16. ****************************************************************************/
  17.  
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #include <arpa/inet.h>
  21. #include <sys/socket.h>
  22. #include <stdio.h>
  23. #include <time.h>
  24. #include <errno.h>
  25. #include <signal.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30.  
  31. #define N_BUF 6144
  32. #define BACKLOG 10
  33. #define USAGE "Usage: %s listen_port\n"
  34. #define PASS "matrix86"
  35.  
  36. void HandSigCHLD(int sig)
  37. {
  38.   int status;
  39.   pid_t wpid;
  40.   wpid = waitpid(WAIT_ANY, &status, WNOHANG);
  41. //if (wpid > 0) printf("BG child %d terminated with status %x\n", wpid, status);
  42.   return;
  43. }
  44.  
  45. void tunnel(int con){
  46.  
  47.   char buffer[N_BUF+1];
  48.   struct sockaddr_in client;
  49.   int nw,nr,ip,port,sock;
  50.   char char_ip[]="62.211.72.30";
  51.   char char_port[]="65535";
  52.  
  53.   if((nw = write(con,"Password: ",10)) == 0) _exit(1);
  54.   nr = recv(con,buffer,N_BUF,0);
  55.   if(nr < 0){
  56. //  perror("Errore recv(): ");
  57.     return;
  58.   }
  59.   buffer[nr-2] = 0;  //Elimina il carattere di invio =)
  60. // Controlla la password...
  61.   if(strcmp(buffer, PASS) != 0) return;
  62.  
  63. // Richiesta IP
  64.   if((nw = write(con,"Ip address: ",12)) == 0) _exit(1);
  65.   nr = recv(con,buffer,N_BUF,0);
  66.   if(nr < 0){
  67. //  perror("Errore recv(): ");
  68.     return;
  69.   }
  70.   buffer[nr-2] = 0;
  71.   strcpy(char_ip,buffer);
  72.  
  73. // Richiesta porta
  74.   if((nw = write(con,"Port: ",6)) == 0) _exit(1);
  75.   nr = recv(con,buffer,N_BUF,0);
  76.   if(nr < 0){
  77. //  perror("Errore recv(): ");
  78.     return;
  79.   }
  80.   buffer[nr-2] = 0;
  81.   strcpy(char_port,buffer);
  82.   port = atoi(char_port);
  83.  
  84.   sock = socket(PF_INET, SOCK_STREAM, 0);
  85.   if(sock < 0){
  86. //  perror("Errore durante la creazione del socket: ");
  87.     _exit(1);
  88.     }
  89.  
  90.   memset((void *)&client,0,sizeof(client));
  91.   client.sin_family = PF_INET;
  92.   client.sin_port = htons(port);
  93.   if ( (inet_pton(PF_INET, char_ip, &client.sin_addr)) < 0) {
  94. //  perror("Errore durante la creazione dell'indirizzo ");
  95.     _exit(1);
  96.   }
  97.  
  98.   if (connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0) {
  99. //  perror("Errore in connessione ");
  100.     _exit(1);
  101.   }
  102. // Creo due processi figli: uno riceve dal client connesso e invia al server successivo, e l'altro legge la risposta del server e la invia al client connesso!!!
  103.   if(fork() == 0){
  104.  
  105.     while(1){
  106. // Leggo le informazioni ricevute dal server...
  107.       nr = recv(sock,buffer,N_BUF,0);
  108.       if(nr < 0){
  109. //      perror("Errore recv(): ");
  110.         return;
  111.       }
  112.       if(nr == 0) break; //Connection close!
  113. // ...e le invio al client!
  114.       if((nw = send(con,buffer,nr,0)) == 0) return;
  115.     }
  116.     _exit(0);
  117.   }
  118.  
  119.   if(fork() == 0){
  120.     while(1){
  121. // Leggo le informazioni ricevute dal client...
  122.       nr = recv(con,buffer,N_BUF,0);
  123.       if(nr < 0){
  124. //      perror("Errore recv(): ");
  125.         return;
  126.       }
  127.       if(nr == 0) break; //Connection close!
  128.       buffer[nr-2] = 0; // Delete return value!
  129. // ...e le invio al server!
  130.       if((nw = send(sock,buffer,nr,0)) == 0) return;
  131.     }
  132.   _exit(0);
  133.   }
  134. //printf("Socket chiuso");
  135.   close(sock);
  136.   return;
  137. }
  138.  
  139. void usage(char name[]){
  140.   printf(USAGE,name);
  141.   return;
  142. }
  143.  
  144. int main(int argc,char *argv[]){
  145.  
  146.   struct sockaddr_in server;
  147.   int sock,conn,nw,port;
  148.   pid_t pid;
  149.   char init[] =  "**********************************************\n*              RBT-4 CREW                    *\n**********************************************\n* Tunnel coded by Matrix86 for Rbt-4 Crew    *\n* Thx to Black_Student,r080cy90r and         *\n* all my friends.                            *\n* Special Thx to my girlfriend ;)            *\n*                                            *\n**********************************************\n\n";
  150.   if(argc < 2){
  151.     usage(argv[0]);
  152.     _exit(0);
  153.   }
  154.   port = atoi(argv[1]);
  155.   signal(SIGCHLD, HandSigCHLD);
  156.   strcpy(argv[0],"[ httpd ]");
  157.   sock = socket(PF_INET, SOCK_STREAM, 0);
  158.   if(sock < 0){
  159. //  perror("Errore durante la creazione del socket: ");
  160.     _exit(1);
  161.     }
  162.  
  163.   memset((void *)&server,0,sizeof(server));
  164.  
  165.   server.sin_family = PF_INET;
  166.   server.sin_port = htons(port);
  167.   server.sin_addr.s_addr = htonl(INADDR_ANY);
  168.  
  169.   if(bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0){
  170. //  perror("Errore Bind: ");
  171.     _exit(1);
  172.   }
  173.  
  174.   if (listen(sock, BACKLOG) < 0 ) {
  175. //  perror("Errore Listen: ");
  176.     _exit(1);
  177.   }
  178.  
  179.  
  180.   while(1){
  181.  
  182.     while(((conn = accept(sock, (struct sockaddr *) NULL, NULL)) < 0) && (errno == EINTR));
  183.     if (conn < 0) {
  184. //    perror("Errore accept: ");
  185.       _exit(1);
  186.     }
  187.  
  188. // Creo un processo figlio...in modo da gestire più connessioni contemporaneamente.
  189.     if ((pid=fork()) < 0){
  190. //    perror("Errore fork() :");
  191.       _exit(1);
  192.     }
  193.  
  194.     if(pid == 0){
  195. // Siamo nel processo figlio.
  196.       close(sock);
  197.       if((nw = write(conn,init,strlen(init))) == 0) _exit(1);
  198.       tunnel(conn);
  199.       close(conn);
  200.       _exit(0);
  201.     }
  202.     else {
  203.       close(conn);
  204.     }
  205.   }
  206.  
  207.   return 0;
  208. }
  209.