## utils/mr/mr.c

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

## DEFINITIONS

This source file includes following functions.

1. main

       1 /***************************************
       2   $Revision: 1.4 $
       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,2001,2002                    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
      34 #include "rip.h"
      35
      36 #include <stdio.h>
      37 #include <unistd.h>
      38 #include <sys/param.h>
      39 #include <sys/types.h>
      40 #include <netinet/in.h>
      41 #include <arpa/inet.h>
      42 #include <sys/wait.h>
      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 extern char *optarg;
      50
      51
      52 int main(int argc, char **argv)
         /* [<][>][^][v][top][bottom][index][help] */
      53 {
      54 char input[MAX_INPUT_SIZE], output[MAX_INPUT_SIZE+1];
      55 char *mserver=NULL;
      56 int listen_port=0, connect_port=0;
      57 int listening_socket, client_socket, server_socket;
      58 sk_conn_st client_conn;
      59 struct hostent *hptr;
      60 struct sockaddr_in serv_addr;
      61 struct in_addr *paddr;
      62 int res;
      63 struct timeval timeout;
      64 int ilen;
      65 int c;
      66 int errflg=0;
      67 int pid;
      68 char *filter_name="./ripe2rpsl";
      69
      70
      71      if(argc<4) errflg++;
      72
      73      while ((c = getopt(argc, argv, "l:h:p:f:?")) != EOF)
      74      switch (c) {
      75       case 'l':
      76         listen_port = atoi(optarg);
      77         break;
      78       case 'h':
      79         mserver = optarg;
      80         break;
      81       case 'p':
      82         connect_port = htons(atoi(optarg));
      83         break;
      84       case 'f':
      85         filter_name = optarg;
      86         break;
      87       case '?':
      88       default :
      89         errflg++;
      90         break;
      91      }
      92      if (errflg) {
      93         fprintf(stderr,"usage: mr -l listen_port -h mirror_server -p port [-f convertor]\n");
      94         exit (2);
      95      }
      96
      97   listening_socket = SK_getsock(SOCK_STREAM, listen_port, BACKLOG, INADDR_ANY);
      98   bzero(&timeout, sizeof(timeout));
      99   timeout.tv_sec=TIMEOUT;
     100
     101   while (1) {
     102      client_socket = SK_accept_connection(listening_socket);
     103      if(client_socket==-1) {fprintf(stderr, "cannot accept client\n"); continue; }
     104      fprintf(stderr, "client connected\n");
     105      SK_cd_make(&client_conn, client_socket, 0);
     106      /* get the input from the client */
     107      SK_cd_gets(&client_conn, input, MAX_INPUT_SIZE);
     108      fprintf(stderr, "input:[%s]\n", input);
     109
     110
     111      /* create socket to connect to the server */
     112      if ((server_socket=socket(AF_INET, SOCK_STREAM, 0))==-1){
     113        perror("socket");
     114        exit(1);
     115      }
     116      hptr=gethostbyname(mserver);
     117      if (hptr) {
     118         paddr=(struct in_addr *)hptr->h_addr;
     119         bzero(&serv_addr, sizeof(serv_addr));
     120         serv_addr.sin_family=AF_INET;
     121         serv_addr.sin_port=connect_port;
     122         memcpy(&serv_addr.sin_addr, paddr, sizeof(struct in_addr));
     123         fprintf(stderr,"Trying %s port %d\n", inet_ntoa(serv_addr.sin_addr), connect_port);
     124         if(connect(server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr))==-1) {
     125           perror("connect");
     126           close(client_socket);
     127           close(server_socket);
     128           sleep(TIMEOUT);
     129           continue;
     130         }
     131      } else {
     132       /* resolver error */
     133       fprintf(stderr,"Cannot resolve name %s\n", mserver);
     134       close(client_socket);
     135       close(server_socket);
     136       sleep(TIMEOUT);
     137       continue;
     138      }
     139
     140      fprintf(stderr, "Sending Invitation");
     141
     142      sprintf(output, "%s\n", input);
     143      res = SK_puts(server_socket, output, &timeout);
     144      if(res < 0) {
     145         perror("write");
     146         sleep(TIMEOUT);
     147         continue;
     148      }
     149      fprintf(stderr, "...sent \n");
     150
     151      if((pid=fork())==0){
     152              close(listening_socket);
     153              if(dup2(server_socket, 0)==-1) perror("dup2-serv"); ; /* provide input from the mirror server */
     154              if(dup2(client_socket, 1)==-1) perror("dup2-clnt"); ; /* direct output to the client */
     155              fprintf(stderr, "Executing convertor: %s\n", filter_name);
     156              execlp(filter_name,filter_name, NULL);
     157              fprintf(stderr, "Cannot execute %s\n", filter_name);
     158      }
     159      fprintf(stderr, "waiting for convertor to finish...\n");
     160      wait(&pid); /* wait untill conversion finishes */
     161      fprintf(stderr, "...converting stream done\n");
     162
     163      close(server_socket);
     164      close(client_socket);
     165
     166
     167   }/* main loop */
     168
     169 }

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