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 <sys/types.h>
34   | #include <fcntl.h>
35   | #include <signal.h>
36   | #include "ud.h"
37   | #include "ud_int.h"
38   | #include "constants.h"
39   | 
40   | 
41   | 
42   | int get_current_serial()
43   | {
44   |  int fd;
45   |  char buff[64];
46   |  const char *filename;
47   |  
48   |  filename=CO_get_nrtm_cserialfile();
49   |  fd=open(filename, O_RDONLY, (mode_t)0);
50   |  if (fd < 0) { fprintf(stderr, "cannot open %s\n", filename); return(-1); }
51   |  read(fd, buff, sizeof(buff));
52   |  close(fd);
53   |  return(atoi(buff));
54   | }
55   | 
56   | int set_current_serial(int current_serial)
57   | {
58   | FILE *file;
59   | const char *filename;
60   | 
61   |  filename=CO_get_nrtm_cserialfile();
62   |  file=fopen(filename, "w+");
63   |  if (file==NULL) { fprintf(stderr, "cannot open %s\n", filename); return(-1); }
64   |  fprintf(file,"%d\n", current_serial);
65   |  fflush(file);
66   |  fclose(file);
67   |  return(0);
68   | }
69   | 
70   | 
71   | /*************************************
72   | *
73   | *
74   | **************************************/
75   | 
76   | void UD_do_nrtm()
77   | {
78   | UD_stream_t ud_stream;
79   | struct _nrtm *nrtm=calloc(1, sizeof(struct _nrtm));
80   | //int dummy_allowed;
81   | int delay;
82   | int do_nrtm=1;
83   | FILE *file, *logfile;
84   | int num_ok;
85   | int upto_last;
86   | 
87   | 
88   | /* get mode of operation: protected/unprotected (dummy) */
89   |   ud_stream.ud_mode=CO_get_update_mode();
90   | 
91   |   fprintf(stderr, "Mode of operation:\n");
92   |   if(IS_DUMMY_ALLOWED(ud_stream.ud_mode))fprintf(stderr, "* dummy allowed\n"); 
93   |    else fprintf(stderr, "* dummy not allowed\n");
94   |   if(IS_UPDATE(ud_stream.ud_mode))fprintf(stderr, "* DBupdate\n");
95   |    else fprintf(stderr, "* NRTM\n");
96   |   if(IS_STANDALONE(ud_stream.ud_mode))fprintf(stderr, "* running standalone\n");
97   |    else fprintf(stderr, "* running as a server\n");
98   |   
99   | /* get mirror server */
100  |   nrtm->server=CO_get_nrtm_host();
101  |   
102  | /* get mirror port */
103  |   nrtm->port = SK_atoport(CO_get_nrtm_port(), "tcp");
104  |   printf("XXX nrtm_port=%d\n", nrtm->port);
105  |   if(nrtm->port == -1) {
106  |     printf("Invalid service/port: %d\n", nrtm->port);
107  |     exit(-1);
108  |   }
109  |               
110  | /* get mirror version */
111  |   nrtm->version=CO_get_nrtm_version();
112  |   
113  | /* get database */
114  |   ud_stream.db_name=CO_get_database();
115  |   
116  | /* get database host*/
117  |   ud_stream.db_host=CO_get_host();
118  | 
119  | /* get database port*/
120  |   ud_stream.db_port=CO_get_database_port();
121  |     
122  | /* get database user*/
123  |   ud_stream.db_user=CO_get_user();
124  |   
125  | /* get database password*/
126  |   ud_stream.db_pswd=CO_get_password();
127  | 
128  | /* get error log facility */
129  |   ud_stream.log=CO_get_nrtm_logfile();
130  |   
131  | //  ud_stream.dummy_allowed=dummy_allowed;
132  |   ud_stream.num_skip=0;
133  |   ud_stream.load_pass=0;
134  |   ud_stream.nrtm=nrtm;
135  |   
136  |   upto_last=0; // let's start gradually if the backlog is > SBUNCH (1000) serials
137  | 
138  | /*+++ main cycle +++*/
139  | 
140  |  while(do_nrtm=CO_get_do_nrtm()) {
141  | 
142  | /* get current serial */
143  |   nrtm->current_serial=get_current_serial();
144  |   if(nrtm->current_serial == -1) {
145  |     printf("Error obtaining current serial: %d\n", nrtm->current_serial);
146  |     exit(-1);
147  |   }
148  | 
149  |    fprintf(stderr, "current_serial:\t%d\n", nrtm->current_serial);
150  |    fprintf(stderr, "conecting to server...\n");
151  |    printf("current_serial:\t%d\n", nrtm->current_serial);
152  |    printf("conecting to server...\n");
153  |       
154  |    file = get_NRTM_stream(nrtm, upto_last);
155  |    fprintf(stderr, "OK\n");
156  |    printf("OK\n");
157  | 
158  | 
159  |   if (file==NULL) { 
160  |     fprintf(stderr, "Cannot open data stream. Trying...\n");
161  |     sleep(100);
162  |     continue;
163  |   }  
164  | 
165  | 
166  |   ud_stream.stream=file;
167  | 
168  | fprintf(stderr, "starting processing stream\n");
169  | 
170  |   num_ok=UD_process_stream(&ud_stream);
171  |   
172  |   
173  | fprintf(stderr, "processing stream finished\n");  
174  |   if(num_ok<0) exit(1);
175  | 
176  | /* Now we can process serials in normal way */ 
177  |   if(num_ok==0) upto_last=1;
178  | 
179  |   set_current_serial(nrtm->current_serial+num_ok);
180  | logfile = fopen(ud_stream.log, "a+");
181  | fprintf(logfile, "forwarded to serial:\t%d\n", nrtm->current_serial+num_ok);
182  | fclose(logfile);
183  | fprintf(stderr, "forwarded to serial:\t%d\n", nrtm->current_serial+num_ok);
184  | printf("Objects received: %d\n-----------\n", num_ok);
185  | 
186  | /* get delay */
187  |   delay=CO_get_nrtm_delay();
188  | 
189  |   if (do_nrtm) sleep(delay);
190  |  }
191  |  
192  |  if(nrtm) free(nrtm);
193  |  
194  |  fprintf(stderr, "NRTM stopped\n");  
195  | 
196  | } /* UD_do_nrtm() */
197  | 
198  | 
199  | 
200  | /*************************************
201  | *
202  | *
203  | **************************************/
204  |   
205  | void UD_do_updates(void *arg)
206  | {
207  | int listening_socket = (int)arg;
208  | int connected_socket;
209  | UD_stream_t ud_stream;
210  | int dummy_allowed;
211  | int delay;
212  | int do_update=1;
213  | FILE *file, *file_ack, *logfile;
214  | int num_ok;
215  | 
216  | 
217  | /* get mode of operation: protected/unprotected (dummy) */
218  |   ud_stream.ud_mode=CO_get_update_mode();
219  | 
220  |   fprintf(stderr, "Mode of operation:\n");
221  |   if(IS_DUMMY_ALLOWED(ud_stream.ud_mode))fprintf(stderr, "* dummy allowed\n"); 
222  |    else fprintf(stderr, "* dummy not allowed\n");
223  |   if(IS_UPDATE(ud_stream.ud_mode))fprintf(stderr, "* DBupdate\n");
224  |    else fprintf(stderr, "* NRTM\n");
225  |   if(IS_STANDALONE(ud_stream.ud_mode))fprintf(stderr, "* running standalone\n");
226  |    else fprintf(stderr, "* running as a server\n");
227  | 
228  |   
229  | /* get database */
230  |   ud_stream.db_name=CO_get_database();
231  |   
232  | /* get database host*/
233  |   ud_stream.db_host=CO_get_host();
234  | 
235  | /* get database port*/
236  |   ud_stream.db_port=CO_get_database_port();
237  |     
238  | /* get database user*/
239  |   ud_stream.db_user=CO_get_user();
240  |   
241  | /* get database password*/
242  |   ud_stream.db_pswd=CO_get_password();
243  | 
244  | /* get error log facility */
245  |   ud_stream.log=CO_get_nrtm_logfile();
246  |   
247  | 
248  |   ud_stream.num_skip=0;
249  |   ud_stream.load_pass=0;
250  |   ud_stream.nrtm=NULL;
251  |   
252  | /*+++ main cycle +++*/
253  | 
254  |  while(do_update=CO_get_do_update()) {
255  | 
256  | /* accept connection */
257  |    connected_socket = SK_accept_connection(listening_socket);
258  |    file=fdopen(connected_socket, "r");
259  |    file_ack=fdopen(connected_socket, "w");
260  | 
261  |    fprintf(stderr, "Connection accepted...\n");
262  |    
263  |   if (file==NULL) { 	fprintf(stderr, "Cannot open data stream. Exiting..\n");
264  | 			exit(-1); }
265  | 
266  | 
267  |   ud_stream.stream=file;
268  | 
269  | fprintf(stderr, "starting processing object\n");
270  | 
271  |   num_ok=UD_process_stream(&ud_stream);
272  |   
273  | fprintf(stderr, "processing object finished\n");  
274  | 
275  |   if(num_ok==1) fprintf(file_ack, "%%ERROR 0\n");
276  |    else {
277  |       num_ok=(-1)*num_ok;
278  |       fprintf(file_ack, "%%ERROR %d\n",num_ok);
279  |       fprintf(file_ack, "Transaction had the following problems:\n");
280  |       if(num_ok & ERROR_U_MEM) fprintf(file_ack, "Memory allocation error\n");
281  | //      if(num_ok & ERROR_U_DBS) fprintf(file_ack, "Database (SQL) error\n");
282  | //      if(num_ok & ERROR_U_OBJ) fprintf(file_ack, "Object (RF) error\n");
283  | //      if(num_ok & ERROR_U_AUT) fprintf(file_ack, "Object authentication error\n");
284  |       if(num_ok & ERROR_U_BADOP) fprintf(file_ack, "Bad operation\n");
285  |       if(num_ok & ERROR_U_COP) fprintf(file_ack, "Conflicting operation\n");
286  |       if(num_ok & ERROR_U_NSUP) fprintf(file_ack, "Object of this type is not supported\n");
287  |       if(num_ok & ERROR_U_BUG) fprintf(file_ack, "Software bug - report to <ripe-dbm@ripe.net>\n");
288  |       if(ud_stream.error_script)fprintf(file_ack, "%s\n", ud_stream.error_script);
289  |    }
290  |    
291  |    if(ud_stream.error_script) free(ud_stream.error_script);
292  |    fclose(file_ack);   
293  | 
294  |  } /* while do_update */
295  |  
296  |  
297  |  fprintf(stderr, "server stopped\n");  
298  | 
299  | } /* UD_do_update() */
300  | 
301  | 
302  | 
303  |