bin/mr/mirror_reflector.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- main
1 /***************************************
2 $Revision: 1.1 $
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 "socket.h"
42
43
44 #define MAX_INPUT_SIZE 256
45
46 int main(int argc, char **argv)
/* [<][>][^][v][top][bottom][index][help] */
47 {
48 char input[MAX_INPUT_SIZE], output[MAX_INPUT_SIZE+1];
49 char *mserver=NULL;
50 int listen_port=0, connect_port=0;
51 int listening_socket, client_socket, server_socket;
52 struct hostent *hptr;
53 struct sockaddr_in serv_addr;
54 struct in_addr *paddr;
55 int nread, nwrite;
56 int ilen;
57 int c;
58 sk_conn_st condat;
59 int errflg=0;
60 int pid;
61 char *filter_name="./ripe2rpsl";
62
63
64 if(argc<4) errflg++;
65
66 while ((c = getopt(argc, argv, "l:h:p:f:?")) != EOF)
67 switch (c) {
68 case 'l':
69 listen_port = htons(atoi(optarg));
70 break;
71 case 'h':
72 mserver = optarg;
73 break;
74 case 'p':
75 connect_port = htons(atoi(optarg));
76 break;
77 case 'f':
78 filter_name = optarg;
79 break;
80 case '?':
81 default :
82 errflg++;
83 break;
84 }
85 if (errflg) {
86 fprintf(stderr,"usage: mr -l listen_port -h mirror_server -p port [-f convertor]\n");
87 exit (2);
88 }
89
90 listening_socket = SK_getsock(SOCK_STREAM, listen_port, INADDR_ANY);
91
92 while (1) {
93 client_socket = SK_accept_connection(listening_socket);
94 if(client_socket==-1) {fprintf(stderr, "cannot accept client\n"); continue; }
95 fprintf(stderr, "client connected\n");
96 /* get the input from the client */
97 SK_gets(client_socket, input, MAX_INPUT_SIZE);
98 fprintf(stderr, "input:[%s]\n", input);
99
100
101 /* create socket to connect to the server */
102 if ((server_socket=socket(AF_INET, SOCK_STREAM, 0))==-1){
103 perror("socket");
104 exit(1);
105 }
106 hptr=gethostbyname(mserver);
107 if (hptr) {
108 paddr=(struct in_addr *)hptr->h_addr;
109 bzero(&serv_addr, sizeof(serv_addr));
110 serv_addr.sin_family=AF_INET;
111 serv_addr.sin_port=connect_port;
112 memcpy(&serv_addr.sin_addr, paddr, sizeof(struct in_addr));
113 fprintf(stderr,"Trying %s port %d\n", inet_ntoa(serv_addr.sin_addr), connect_port);
114 if(connect(server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr))==-1) {
115 perror("connect");
116 exit(1);
117 }
118 }
119 fprintf(stderr, "Sending Invitation");
120
121 sprintf(output, "%s\n", input);
122 ilen=strlen(output);
123 nwrite=SK_write(server_socket, output, ilen);
124 if(nwrite != ilen) { perror("write"); exit(2); }
125 fprintf(stderr, "...sent \n");
126
127 if((pid=fork())==0){
128 close(listening_socket);
129 if(dup2(server_socket, 0)==-1) perror("dup2-serv"); ; /* provide input from the mirror server */
130 if(dup2(client_socket, 1)==-1) perror("dup2-clnt"); ; /* direct output to the client */
131 fprintf(stderr, "Executing convertor: %s\n", filter_name);
132 execlp(filter_name,filter_name, NULL);
133 fprintf(stderr, "Cannot execute %s\n", filter_name);
134 }
135 fprintf(stderr, "waiting for convertor to finish...\n");
136 wait(&pid); /* wait untill conversion finishes */
137 fprintf(stderr, "...converting stream done\n");
138
139 close(server_socket);
140 close(client_socket);
141
142
143 }/* main loop */
144
145 }