aboutsummaryrefslogtreecommitdiffstats
path: root/sualibrary/sua/sua_dataname.cpp
blob: b1e6914a2ab7bd2566dae28707ccfe8dcb19683a (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
/***************************************************************************
                          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.1.1.1 2002/02/04 14:30:41 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     
 */

#include "sctp.h"

#include "sua_debug.h"
#include "sua_database.h"
#include "sua_asp_mgnt.h"
#include "sua_logging.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;  
}



/***********************************************************************/
/* 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();
  }
}

string db_Sua_NameList::read_host_name(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
	
  string name_str;
  int last= name.size();
  unsigned int current = name.rfind('.');
  while(current != string::npos){
    name_str = name_str + name.substr(current+1,(last-current)) + ".";
    last= current - 1;
    current = name.rfind('.', last);
  }
  name_str = name_str + name.substr(0,last+1);

#ifdef DEBUG
  cout << "Ready for storing SUA dest hostname = " << name_str << " in NameDB\n"; 
#endif
   
		    
  return(addr_str);

}


// end of module sua_dataname.c