aboutsummaryrefslogtreecommitdiffstats
path: root/sualibrary/sua/sua_file.cpp
blob: a901992001e148f47096445e4334fb86da648ff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/***************************************************************************
                          sua_file.cpp  -  description
                             -------------------
    begin                : Tue Jan 8 2002
    copyright            : (C) 2002 by Lode Coene
    email                : lode.coene@siemens.atea.be
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/*
 *  $Id: sua_file.cpp,v 1.3 2002/11/12 11:02:49 p82609 Exp $
 *
 * SUA implementation according to SUA draft issue 13.
 *
 * Author(s): Lode Coene
 *            
 *
 * Copyright (C) 2001 by Siemens Atea, Herentals, Belgium.
 *
 * Realized in co-operation between Siemens Atea and 
 * Siemens AG, Munich, Germany.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact: gery.verwimp@siemens.atea.be
 *          lode.coene@siemens.atea.be
 *
 * The alternative comment
 * inspiration : Martine
 * "This is the module of the fallen function with the big files by
 * "Von Craenendonck!"
 * "This module contains some real schoftware"
 *
 * Purpose: This code-file defines the SUA file access functions for:
 *          - get a argument in the line(delimited by a space)
 *          - read the sua file(containing arguments for the SUA database)     
 */


#ifdef LINUX
    #include <unistd.h>
#endif

#include "sua_debug.h"
#include "sua_database.h"

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>

#include <netinet/in_systm.h>
#include <netinet/ip.h>         /* includes <netinet/in.h> also ! */
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <arpa/inet.h>

#define SUA_PORT 14001
#define SUA_PPI 4

using namespace std;

/***********************************************************************/
/* get_argument                                                        */
/***********************************************************************/
string get_argument(string line,
		    int& begin,
		    int& end,
		    unsigned int& num_of_char_in_line)
{ 
  int len = 0;
  bool cont = true;
  while  ((num_of_char_in_line <= line.length()) && (cont))
    { 
     
      if ((line[num_of_char_in_line] == ' ') ||
	  (num_of_char_in_line >= line.length()))
	{
	  end = num_of_char_in_line - 1;
	  len = num_of_char_in_line - begin;
	  cont = false;
	}
      num_of_char_in_line++;

    }

  char *temp = new char[len];
 
  // convert to lowercase characters for easier processing
  int i=0;
  for(i = 0; i < len ; i++)
    temp[i] = tolower(line[begin+i]);
  string sua_arg(temp, len);
  
#ifdef DEBUGFILE
  cout << "indexes = "<< begin << " , " << end << " , " << len << "\n";
  cout << temp << "\n";
  cout << "got argument  = " << sua_arg  << " \n";
  char v;
  cin >> v;
#endif

  begin = end + 2;
  end = 0;
  delete temp;

  return(sua_arg);
}

/***********************************************************************/
/* read sua file                                                       */
/***********************************************************************/
int read_sua_file(string filename, 
		  db_Sua_LocalList&        local_sua, 
		  db_Sua_RemoteList&       remote_sua,
		  db_Sua_AssociationList&  Assoc_sua,
		  db_Sua_NameList&         NameDB_sua,
		  db_Sua_ASList&           ApplicServ_sua
		 )
{
  string sua_filename;
  string sua_arg;
  string address_str,ip_addr_str;
  char ch1, ch2;
  string sua_line;
  int num_of_char = 0;
  int num_of_arg = 0;
  int num_of_lines = 0;
  int present_local_instance = 0;
  int present_remote_instance = 0;
  
#ifdef DEBUG
  cout << "file to read = " << filename  << " \n";
#endif
  cout << "SUA local sua instantation parameters(mandatory) \n";
  cout << "-s[n] x.x.x.x -l yyy [-7s 7777] [-gs tt-nat-np-digits][-u] \n";
  cout << "-s : source address \n";
  cout << "-l : source local sua portnumber \n";
  cout << "-sn: source name \n";
  cout << "-7s: source SS7 pointcode\n";
  cout << "-u : listen on local port number \n"; 
  cout << "SUA parameters of the sua tester (optional)\n";
  cout << "Not supported yet (-b 1000 -o -v) \n";
  cout << "SUA parameters of the SUA remote instantation(s) (optional)\n";
  cout << "-d[n] x.x.x.x -r yyy [-7d 7777] [-gd tt-nat-np-digits] [-a ASnumber] \n";
  cout << "-d : destination address \n";
  cout << "-r : destination sua portnumber \n";
  cout << "-dn: destination name \n";
  cout << "-7d: destination SS7 pointcode\n";
  cout << "-gs: source global title(TTID-NAT-NUMPLAN-Digits) linked to -s address or -sn name \n";
  cout << "-gd: destination global title(TTID-NAT-NUMPLAN-Digits) linked to -d address or -dn name\n";
  cout << "-a : Application Server(AS) this address belongs to \n";

  ifstream infile(filename.c_str());
  while (getline(infile, sua_line))
    {
      num_of_lines++;
      // analyse input line and split up in arguments according to 
      // the spaces in the line: each argument is then processed
      unsigned int num_of_char_in_line = 0;
      int b=0, e=0;
      bool next_arg;
     
      sua_arg = get_argument(sua_line,b,e,num_of_char_in_line);
      next_arg = false;
   
      // make selection on cmd argument
      while (num_of_char_in_line < sua_line.length())
	{
	  ch2 = ' ';
	  if (sua_arg[0] == '-')
	   {
	     ch1 = sua_arg[1];
	     ch2 = sua_arg[2];
	   }
	  else  if (sua_arg[0] == '/')
	    ch1 = sua_arg[0];
	  else
	   {
	     cout << "weird option \n";
	     ch1 = ' '; 
	   }

	  switch (ch1)
	    {
	    case 's': // read source addresses for sua
	      {
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		if (present_local_instance > 0) {
		  present_local_instance= 0;
		}
		local_sua.increase_instance();
		Assoc_sua.increase_instance();
		present_remote_instance = 0; 
		Assoc_sua.instance[Assoc_sua.num_of_instance].local_sua_id = local_sua.num_of_instance;
		Assoc_sua.instance[Assoc_sua.num_of_instance].nr_of_inbound_streams = 1;
		Assoc_sua.instance[Assoc_sua.num_of_instance].nr_of_outbound_streams = 1;   
		
		if (ch2 == 'n') {
		  ip_addr_str = Assoc_sua.read_host_name(NameDB_sua, address_str);
		  Assoc_sua.read_Source_addr(ip_addr_str);
		}
		else {
		  Assoc_sua.read_Source_addr(address_str);
		}

		present_local_instance++;
		break;
	      }
	    case 'd':
	      {  // read destination addresses for sua
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		remote_sua.increase_instance();
		if (present_remote_instance > 0) {
		  Assoc_sua.increase_instance();
		  Assoc_sua.instance[Assoc_sua.num_of_instance].Source = 
		    Assoc_sua.instance[Assoc_sua.num_of_instance-1].Source;
		  Assoc_sua.instance[Assoc_sua.num_of_instance].local_sua_id = 
		    Assoc_sua.instance[Assoc_sua.num_of_instance-1].local_sua_id;
		  Assoc_sua.instance[Assoc_sua.num_of_instance].nr_of_inbound_streams = 1;
		  Assoc_sua.instance[Assoc_sua.num_of_instance].nr_of_outbound_streams = 1;
		  present_remote_instance = 0;
		}
		if (ch2 == 'n') {
		  ip_addr_str = Assoc_sua.read_host_name(NameDB_sua, address_str);
		  Assoc_sua.read_Dest_addr(ip_addr_str);
		}
		else {
		  Assoc_sua.read_Dest_addr(address_str);
		}
		Assoc_sua.instance[Assoc_sua.num_of_instance].remote_sua_id = remote_sua.num_of_instance;
		
		present_remote_instance++;

		break;
	      }
	    case 'l':
	      { // read local portnumber for sua
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		Assoc_sua.read_Source_port(address_str);

		break;
	      }
	    case 'r':
	      { // read remote portnumber for sua
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		Assoc_sua.read_Dest_port(address_str);

		break;
	      }
	    case 'a':
	      {  // read Application Server number this remote ASP belongs to
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		
		Assoc_sua.instance[Assoc_sua.num_of_instance].asp.linked_to_AS[0] = ApplicServ_sua.read_AS(address_str, Assoc_sua.num_of_instance );
				
		break;
	      }
	    case 'b':
	      {
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
#ifdef DEBUG
		cout << "discard option b \n";
#endif
	      

	      }
	    case 'n':
	      { // read sybsytem number = SSN (valid for both local and remote)
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		remote_sua.read_ssn(address_str);
		local_sua.read_ssn(address_str);

		break;
	      }
	    case 'q': // read QOS fields for sua
	      {
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		
	
		break;
	      }
	    case 'u':
	      { // 
		break;
	      }
	    case 'm':
	      { //
		break;
	      }
	    case 'o':
	      { //
		break;
	      }
	    case 't':
	      { // number of streams incoming AND outgoing
		num_of_arg++;
		address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		Assoc_sua.read_stream_number(address_str);
		break;
	      }
	    case 'v':
	      { //
		break;
	      }
	    case 'f':
	      { // file option: we are in file mode could later be used for 
		// recursive filereading. Ignore for the moment
		num_of_arg++;
		break;
	      }
	    case 'g':
	    	{
		  /* read global title */
		  switch (ch2)
		    {
		    case 's': // read source Global Title for sua
		      {
			num_of_arg++;
			address_str = get_argument(sua_line,b,e,num_of_char_in_line);
			
			Assoc_sua.read_Source_GT(NameDB_sua, address_str);
			
			break;
		      }
		    case 'd':
		      {  // read destination Global Title for sua
			num_of_arg++;
			address_str = get_argument(sua_line,b,e,num_of_char_in_line);
			
			Assoc_sua.read_Dest_GT(NameDB_sua, address_str);
			
			break;
		      }
		    default:
		      {
			break;
		      }
		    }
		  break;
	    	}
	    case 'i':
	      {
		// ignore character, is not a error, go to next argument
		break;
	      }
	    case '7':
	      { /* read SS7 pointcodes */
		switch (ch2)
		  {
		  case 's': // read source pointcode for sua
		    {
		      num_of_arg++;
		      address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		      
		      Assoc_sua.read_Source_pointcode(address_str);
		      
		      break;
		    }
		  case 'd':
		    {  // read destination pointcode for sua
		      num_of_arg++;
		      address_str = get_argument(sua_line,b,e,num_of_char_in_line);
		      
		      Assoc_sua.read_Dest_pointcode(address_str);
		      
		      break;
		    }
		  default:
		    {
		      break;
		    }
		  }
		break;
	      }
	    case '/':
	      {
		// comment: skip this line
		num_of_char_in_line = num_of_char_in_line + sua_line.length();
	        b=0;
	        e=0;
		break;
	      }
	    default:
	      {
		break;
	      }
	    }
	  sua_arg = get_argument(sua_line,b,e,num_of_char_in_line);
	  num_of_arg++;
	}  
      // finished reading the whole line
      num_of_char = num_of_char + num_of_char_in_line;
      cout << "Line " << num_of_lines << " : " << sua_line << "\n";
    }
  
#ifdef DEBUG
  cout << "End of file: statistics \n";
  cout << "Number of lines = " << num_of_lines <<"\n";
  cout << "Number of characters in file = " << num_of_char << "\n";
  
#endif
  
  return (0);
  
} /* end of read_sua_file */

/***********************************************************************/
/* create host parameter handling                                      */
/***********************************************************************/
void sua_cr_host( string                   host_parm_str,
		  db_Sua_LocalList&        local_sua 		  
		)
{
  int    host_id;
  /* get host id from param string, use as index in local_sua table */

  host_id = 1;

  string ip_addr_str( host_parm_str, (host_parm_str.find("org_ip=")+7),host_parm_str.length());
  /* get all(=multihomed) addresses till next parameter */
  /* fill in IP address as string in local_sua */
  /*local_sua.instance[host_id].ip_addr_str = ip_addr_str;*/

  /* get the local port number for this instance */
  /* local_sua.instance[host_id].portnumber=14001 */

  /* get the local ssn for this local sua host instantiation */
  /* local_sua.instance[host_id].ssn = 5 */

  /* get the number of streams supported for this sua instantiation */
  /* local_sua.instance[host_id].streams=16 */

}
 
/***********************************************************************/
/* create association parameter handling                               */
/***********************************************************************/
void sua_cr_assoc( string                   assoc_parm_str,
		   db_Sua_AssociationList&  Assoc_sua,
		   db_Sua_LocalList&        local_sua 	
		 )
{
  int    assoc_id;
  /* get assoc id from param string, use as index in assoc_sua table */

  assoc_id = 1;

  string ip_addr_str( assoc_parm_str, (assoc_parm_str.find("dest_ip=")+8),assoc_parm_str.length());
  /* get all(=multihomed) addresses till next parameter */
  /* fill in IP address as string & network in assoc_sua */
  /* Assoc_sua.instance[assoc_id].ip_addr_str = ip_addr_str;*/

  /* get the local port number for this instance */
  /* Assoc_sua.instance[assoc_id].portnumber=14001 */

  /* get the local ssn for this local sua host instantiation */
  /* local_sua.instance[host_id].ssn = 5 */

  /* get the number of streams supported for this sua instantiation */
  /* local_sua.instance[host_id].streams=16 */

}

/***********************************************************************/
/* read sua configuration file                                         */
/***********************************************************************/
int read_sua_conf_file( string filename, 
		        db_Sua_DatabaseList sua
		      )
{
  string sua_filename;
  string sua_arg;
  char readBuffer[256];

  cout << "SUA configuration file to read = " << filename  << " \n";

  cout << "SUA local sua instantation parameters(mandatory) \n";
  cout << "cr host:host_id = j, org_ip= t.t.t.t, org_port= yyyyy  \n";
  cout << "cr assoc:assoc_id= i, init= false, host_id = j, dest_ip = x.x.x.x, dest_port= yyyy \n";
  cout << "cr orgpc: pc = wwww, host_id = j\n";
  cout << "cr destpc: pc = wwww, rc = x \n";
  cout << "cr hostname: namegt_id= k name= www.sctp.be \n";
  cout << "cr gt:namegt_id = k,tt= tt, na= nn, np= ppp, digits= 123456789abcdef \n";
  cout << "cr route: dpc= www, assoc_id = i           AND/OR\n";
  cout << "cr route: dest_ip = x.x.x.x, assoc_id = i  AND/OR\n";
  cout << "cr route: namegt_id= k, assoc_id = i  \n";

  ifstream infile(filename.c_str());

  while (infile.getline(readBuffer,256,'\n'))
    {
      string cmdline(readBuffer,strlen(readBuffer));

      if (cmdline.length() == 0) 
	{
	  cout << "empty line?\n";
	}
      else if (cmdline.find("/") != cmdline.npos)
	{
	  /* comment line: do not execute */
	}
      else if (cmdline.find("cr host:") != cmdline.npos)
	{
	  string parm_str( cmdline, (cmdline.find("cr host:")+8),cmdline.length());
	  sua_cr_host( parm_str,
		       sua.local_sua
		     );
	} 
      else if (cmdline.find("cr assoc:") != cmdline.npos)
	{
	  string parm_str( cmdline, (cmdline.find("cr assoc:")+9),cmdline.length());
	  sua_cr_assoc( parm_str,
		        sua.AssocDB,
		        sua.local_sua 	
		      );
	} 
      else if (cmdline.find("cr destpc:") != cmdline.npos)
	{
	  
	} 
      else if (cmdline.find("cr hostname:") != cmdline.npos)
	{
	  
	} 
      else if (cmdline.find("cr gt:") != cmdline.npos)
	{
	  
	} 
      else if (cmdline.find("cr xxxx:") != cmdline.npos)
	{
	  
	} 
      else
	cout << "ERROR: Unknown " << cmdline << "command\n";
    }
} /* end of read_sua_conf_file */

// end of module sua_file.c