aboutsummaryrefslogtreecommitdiffstats
path: root/sualibrary/sua/sua_dataname.cpp
blob: 2b9d40157dffd675d907d7ba95d0d3e484f935cd (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
/***************************************************************************
                          sua_dataname.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_dataname.cpp,v 1.6 2002/11/12 11:04:12 p82609 Exp $
 *
 * SUA implementation according to SUA draft issue 6.
 *
 * 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 : Vicky
 * "This module reuse some code, thus it will mostly reuse trouble."
 *
 * Purpose: This code-file defines the SUA database access functions for:
 *          SUA Name Object (host names/Global Titles):
 *          - initialise Name
 *          SUA Name List:
 *          - initialise Name List
 *          - read hostname
 *					- read global title
 *					- resolve hostname
 *					- perform Global Title Translation
 */

#include "sctp.h"

#include "sua_debug.h"
#include "sua_database.h"
#include "sua_asp_mgnt.h"
#include "sua_logging.h"
#include "sua_adapt.h"

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

#include "unistd.h"



using namespace std;

/***********************************************************************/
/* functions of the object class SUA name Object                       */
/***********************************************************************/

/***********************************************************************/
/* Sua_NameObject::initalize                                           */
/***********************************************************************/
void db_Sua_NameObject::initialize(){

  // initialise to point to a invalid SUA association
  SUA_assoc_id = 0;
  sctp_assoc_id = 0;
  GT.Translation_Type = 0;
  GT.Numbering_Plan = 0;
  GT.Nature_of_Address = 0;
  /* initialise to empty string */
  GT.digits="";
  hostname = "";

}



/***********************************************************************/
/* functions of the object class SUA NameList                          */
/***********************************************************************/

/***********************************************************************/
/* Sua_NameList::initalize                                             */
/***********************************************************************/
void db_Sua_NameList::initialize(){
  short i;
  num_of_instance = 0;
  for (i=0; i < db_MAX_REMOTE_SUA; i++) {
    instance[i].initialize();
  }
}

/***********************************************************************/
/* Sua_NameList::read_host_name                                        */
/***********************************************************************/
string db_Sua_NameList::read_host_name( unsigned int sua_assoc_id,
																			  string name
																			){

  char            *hostname;
  const char      *ip_addr_ptr;
  struct hostent  *hptr;
  char            str[INET6_ADDRSTRLEN];
  char            **pptr;
  string          addr_str;

  hostname = new char[name.length()+1];
  name.copy(hostname,name.length());
  // we are dealing with char arrays, so....
  hostname[name.length()] = '\0';
	
  if ((hptr = gethostbyname(hostname)) == NULL){
    cout << "Determination of hostname failed\n";
    return("127.0.0.1");
  }     
  
#ifdef DEBUG
  cout << "Hostname " << hptr->h_name << " has the following IP address(es)\n";
#endif	
  
  pptr = hptr->h_addr_list;
  for ( ; *pptr != NULL;pptr++)
    {
      ip_addr_ptr = inet_ntop(hptr->h_addrtype, *pptr, str,sizeof(str));
      addr_str = addr_str + ip_addr_ptr;
      if ((*(pptr+1)) != NULL)
	/* another ip address is comming after the present one */
	addr_str = addr_str + ",";
      /*else this is the last ip address */
#ifdef DEBUG
      cout << ip_addr_ptr << "\n";
#endif	
    }	
  
#ifdef DEBUG
  cout << "output IP list = " << addr_str << "\n";
#endif
  
  instance[num_of_instance].SUA_assoc_id = sua_assoc_id;
  
  instance[num_of_instance].hostname = name;
  
  return(addr_str);
  
}

/***********************************************************************/
/* Sua_NameList::read_Global_Title                                     */
/***********************************************************************/
void db_Sua_NameList::read_Global_Title( unsigned int sua_assoc_id,
					 string gt
				       )
{
  string GT_TT_str, GT_NP_str, GT_NA_str, GT_Digits_str;
  unsigned int first = 0, last = 0;
  unsigned int current = gt.find('-', first);
  
  first = 1;
  last = current - 1;
  GT_TT_str = gt.substr( first, last);
  
  first = current + 1;
  current = gt.find('-', first);
  last = current - 1;
  GT_NP_str = gt.substr( first , last);
  
  first = current + 1;
  current = gt.find('-', first);
  last = current - 1;
  GT_NA_str = gt.substr( first , last);
  
  first = current + 1;
  last = gt.size();
  GT_Digits_str = gt.substr( first , last);
  
  instance[num_of_instance].SUA_assoc_id = sua_assoc_id;
  
  instance[num_of_instance].GT.Translation_Type = atoi(GT_TT_str.c_str());
  instance[num_of_instance].GT.Numbering_Plan = atoi(GT_NP_str.c_str());
  instance[num_of_instance].GT.Nature_of_Address = atoi(GT_NA_str.c_str());
  instance[num_of_instance].GT.digits = GT_Digits_str;
  
}

/***********************************************************************/
/* db_Sua_NameList::resolve_host_name                                  */
/***********************************************************************/
signed int db_Sua_NameList::resolve_host_name ( hostname_str&  dest_name,
						pointcode_str& dest_pc
					      )
{
  struct hostent   *hptr;
  char            **pptr;
  char              str[INET6_ADDRSTRLEN];
  char             *dest_carr;
  
  /* resolving can be done via: */
  /* - local global Titel database */
  /* - resolve hostname via DNS(simplest for single hop translations) */
  dest_carr = dest_name;
  
  if ((hptr = gethostbyname( dest_carr )) == NULL)
    {
      cout << "Hostname " << dest_name << " not known in DNS.\n";
      return(-1);
    }
  
#ifdef DEBUG
  cout << "Hostname " << dest_name << " resolved to dest IP address(es)\n";
  cout << "IP address length = "<< hptr->h_length << "\n";
#endif
  
  /* initialise the length field of the structure: */
  /* length field is NOT always present in every Unix like operating system  */
  dest_pc.ipvx.ch[0] = 0;	
  
  pptr = hptr->h_addr_list;
  for ( ; (*pptr != NULL) ;pptr++)
    {
      inet_ntop(hptr->h_addrtype, *pptr, str,sizeof(str));
#ifdef DEBUG
      cout << str << "\n";
#endif
      /* got a IP address */
      dest_pc.ipvx.sa.sa_family = hptr->h_addrtype;
      if (dest_pc.ipvx.sa.sa_family == AF_INET)
	inet_pton( AF_INET,
		   str,
		   &dest_pc.ipvx.sin.sin_addr
		 );
      else if (dest_pc.ipvx.sa.sa_family == AF_INET6)
	inet_pton( AF_INET6,
		   str,
		   &dest_pc.ipvx.sin6.sin6_addr
		 );
      else
	{
	  cout << "ERROR resolve_host_name: Unknown IP addresstype\n";
	  return(-2);
	}
    }
  return(0);
};

/***********************************************************************/
/* db_Sua_NameList::resolve_GT                                         */
/***********************************************************************/
signed int db_Sua_NameList::perform_GTT ( global_title_str&  cld_in,
					  global_title_str&  clg_in,
					  global_title_str&  cld_out,
					  global_title_str&  clg_out,
					  pointcode_str&     dest_pc
					)
{
  boolean   search_gt = true, gt_entry_found = false;
  int       i, found_gt_entry;
  
  /* resolving can be done via: */
  /* - local global Titel database */
  i= 0;
  while (search_gt){
    gt_entry_found = ((instance[i].GT.Translation_Type == cld_in.Translation_Type) &&
      (instance[i].GT.Numbering_Plan == cld_in.Numbering_Plan) && 
      (instance[i].GT.Nature_of_Address == cld_in.Nature_of_Address) &&
      (instance[i].GT.digits == cld_in.digits));
  
    found_gt_entry = i;
    /* next entry of name table */
    i++;
  }

  if (gt_entry_found) {
    /* get the association number from gt database */
    dest_pc = get_destpc_addr(instance[found_gt_entry].SUA_assoc_id);
    return(0);
  }
  else
    return(-1);
};


// end of module sua_dataname.c