bin/mr/mirror_reflector.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. main

   1 /***************************************
   2   $Revision: 1.5 $
   3 
   4   Wrapper for NRTM client
   5 
   6   Status: NOT REVUED, NOT TESTED
   7 
   8  Author(s):       Andrei Robachevsky
   9 
  10   ******************/ /******************
  11   Modification History:
  12         andrei (17/01/2000) Created.
  13   ******************/ /******************
  14   Copyright (c) 2000                              RIPE NCC
  15  
  16   All Rights Reserved
  17   
  18   Permission to use, copy, modify, and distribute this software and its
  19   documentation for any purpose and without fee is hereby granted,
  20   provided that the above copyright notice appear in all copies and that
  21   both that copyright notice and this permission notice appear in
  22   supporting documentation, and that the name of the author not be
  23   used in advertising or publicity pertaining to distribution of the
  24   software without specific, written prior permission.
  25   
  26   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  27   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  28   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  29   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  30   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  31   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32  ***************************************/
  33 #include <stdio.h> 
  34 #include <unistd.h>
  35 #include <sys/param.h>
  36 #include <sys/types.h>
  37 #include <netinet/in.h>
  38 #include <arpa/inet.h>
  39 #include <sys/wait.h>
  40 
  41 #include "sk.h"
  42 
  43 
  44 #define MAX_INPUT_SIZE 256
  45 #define TIMEOUT 60
  46 /* number of outstanding connections (backlog queue length) */
  47 #define BACKLOG 10
  48 
  49 #ifdef HAVE_OPTARG
  50 extern char *optarg;
  51 extern int optind, opterr, optopt;
  52 #endif
  53 
  54 
  55 int main(int argc, char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
  56 {
  57 char input[MAX_INPUT_SIZE], output[MAX_INPUT_SIZE+1];
  58 char *mserver=NULL;
  59 int listen_port=0, connect_port=0;
  60 int listening_socket, client_socket, server_socket;
  61 struct hostent *hptr;
  62 struct sockaddr_in serv_addr;
  63 struct in_addr *paddr;
  64 int nwrite;
  65 int ilen;
  66 int c;
  67 int errflg=0;
  68 int pid;
  69 char *filter_name="./ripe2rpsl";
  70 
  71 
  72      if(argc<4) errflg++;
  73 
  74      while ((c = getopt(argc, argv, "l:h:p:f:?")) != EOF)
  75      switch (c) {
  76       case 'l':
  77         listen_port = htons(atoi(optarg));
  78         break;  
  79       case 'h':
  80         mserver = optarg;
  81         break;
  82       case 'p':
  83         connect_port = htons(atoi(optarg));
  84         break;
  85       case 'f':
  86         filter_name = optarg;
  87         break;  
  88       case '?':
  89       default :
  90         errflg++;
  91         break;
  92      }
  93      if (errflg) {
  94         fprintf(stderr,"usage: mr -l listen_port -h mirror_server -p port [-f convertor]\n");
  95         exit (2);
  96      }
  97 
  98   listening_socket = SK_getsock(SOCK_STREAM, listen_port, BACKLOG, INADDR_ANY);
  99   
 100   while (1) {
 101      client_socket = SK_accept_connection(listening_socket);
 102      if(client_socket==-1) {fprintf(stderr, "cannot accept client\n"); continue; }
 103      fprintf(stderr, "client connected\n");
 104      /* get the input from the client */
 105      SK_gets(client_socket, input, MAX_INPUT_SIZE);
 106      fprintf(stderr, "input:[%s]\n", input);
 107 
 108 
 109      /* create socket to connect to the server */
 110      if ((server_socket=socket(AF_INET, SOCK_STREAM, 0))==-1){
 111        perror("socket");
 112        exit(1);
 113      }  
 114      hptr=gethostbyname(mserver);
 115      if (hptr) { 
 116         paddr=(struct in_addr *)hptr->h_addr;
 117         bzero(&serv_addr, sizeof(serv_addr));
 118         serv_addr.sin_family=AF_INET;
 119         serv_addr.sin_port=connect_port;
 120         memcpy(&serv_addr.sin_addr, paddr, sizeof(struct in_addr));
 121         fprintf(stderr,"Trying %s port %d\n", inet_ntoa(serv_addr.sin_addr), connect_port);
 122         if(connect(server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr))==-1) { 
 123           perror("connect");
 124           close(client_socket);
 125           sleep(TIMEOUT);
 126           continue;
 127         }
 128      }    
 129      fprintf(stderr, "Sending Invitation");
 130      
 131      sprintf(output, "%s\n", input);
 132      ilen=strlen(output);
 133      nwrite=SK_write(server_socket, output, ilen);
 134      if(nwrite != ilen) { perror("write"); exit(2); }
 135      fprintf(stderr, "...sent \n");
 136 
 137      if((pid=fork())==0){
 138              close(listening_socket);
 139              if(dup2(server_socket, 0)==-1) perror("dup2-serv"); ; /* provide input from the mirror server */
 140              if(dup2(client_socket, 1)==-1) perror("dup2-clnt"); ; /* direct output to the client */
 141              fprintf(stderr, "Executing convertor: %s\n", filter_name);
 142              execlp(filter_name,filter_name, NULL);
 143              fprintf(stderr, "Cannot execute %s\n", filter_name);
 144      } 
 145      fprintf(stderr, "waiting for convertor to finish...\n");
 146      wait(&pid); /* wait untill conversion finishes */
 147      fprintf(stderr, "...converting stream done\n");
 148      
 149      close(server_socket);
 150      close(client_socket);
 151 
 152 
 153   }/* main loop */
 154 
 155 }

/* [<][>][^][v][top][bottom][index][help] */