libsua/sualibrary/sua/sua_dataname.cpp

179 lines
5.6 KiB
C++
Raw Normal View History

/***************************************************************************
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.2 2002/03/01 12:57:38 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();
}
}
/***********************************************************************/
/* Sua_NameList::read_host_name */
/***********************************************************************/
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