184 lines
5.4 KiB
C++
184 lines
5.4 KiB
C++
//----------------------------------------------------------------------------
|
|
//
|
|
// Copyright (C) Intel Corporation, 2006 - 2007.
|
|
//
|
|
// File: TunnelManager.cpp
|
|
//
|
|
// Contents: Class for mapping between full address (including address and
|
|
// port)and tunnels.
|
|
//
|
|
// Notes:
|
|
//----------------------------------------------------------------------------
|
|
|
|
#include "TunnelManager.h"
|
|
#include <ace/OS_NS_ctype.h>
|
|
#include <vector>
|
|
const ACE_CString port_delimiter = ":";
|
|
|
|
//-----------------------------------------
|
|
// Utility function
|
|
//-----------------------------------------
|
|
ACE_CString toupper(const ACE_CString &str)
|
|
{
|
|
ACE_CString ret(str);
|
|
for(size_t i = 0, len = str.length(); i < len; ++i)
|
|
ret[i] = ACE_OS::ace_toupper(str[i]);
|
|
return ret;
|
|
}
|
|
|
|
//-----------------------------------------
|
|
// Construct Port_address from full address string
|
|
//-----------------------------------------
|
|
Port_Address::Port_Address(const ACE_CString &fullAddress):
|
|
port()
|
|
{
|
|
ACE_CString::size_type sep = fullAddress.find(":",0);
|
|
if (sep == ACE_CString::npos) {
|
|
return;
|
|
}
|
|
fqdn = fullAddress.substr(0,sep);
|
|
|
|
port = ACE_OS::atoi(fullAddress.substr(sep+1, ACE_CString::npos).c_str());
|
|
}
|
|
|
|
Port_Address::Port_Address(const ACE_CString &new_fqdn, ACE_UINT32 new_port)
|
|
{
|
|
fqdn = new_fqdn;
|
|
port = new_port;
|
|
}
|
|
|
|
bool Port_Address::operator== (const Port_Address& other)
|
|
{
|
|
return ((other.fqdn == fqdn) && (other.port == port));
|
|
}
|
|
//-----------------------------------------
|
|
// Routine Description:
|
|
// buildFullAddress : build full address including address and port
|
|
// Arguments:
|
|
// address : string AMT address
|
|
// port : ACE_UINT32 AMT port
|
|
// tunnel: pointer to AMT_Tunnel_Consumer object.
|
|
// Returns:
|
|
// Trow exception if exist map entry with same
|
|
//-----------------------------------------
|
|
ACE_CString Tunnel_Manager::buildFullAddress(const ACE_CString &address, ACE_UINT32 port)
|
|
{
|
|
char buf[32];
|
|
sprintf_s(buf, 32, "%d", port);
|
|
ACE_CString full_address(toupper(address + port_delimiter + ACE_CString(buf)));
|
|
return full_address;
|
|
}
|
|
|
|
|
|
//-----------------------------------------
|
|
// Routine Description:
|
|
// add_tunnel : add to _tunnel_map map full address key and AMT_Tunnel_Consumer * value
|
|
// Arguments:
|
|
// address : ACE_CString AMT address
|
|
// port : ACE_UINT32 AMT port
|
|
// tunnel : pointer to AMT_Tunnel_Consumer object.
|
|
// Returns:
|
|
// @retval 0 If a new entry is bound successfully.
|
|
// @retval 1 If an attempt is made to bind an existing entry.
|
|
// @retval -1 If failures occur.
|
|
//-----------------------------------------
|
|
int Tunnel_Manager::add_tunnel( const AMT_Tunnel_Handler* tunnel,
|
|
const ACE_CString &address,
|
|
const ACE_UINT32 port)
|
|
{
|
|
ACE_CString full_address(toupper(buildFullAddress(address,port)));
|
|
|
|
if (NULL != find_tunnel(full_address))
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
_tunnel_map[full_address] = (AMT_Tunnel_Handler*)tunnel;
|
|
return 0;
|
|
}
|
|
|
|
//-----------------------------------------
|
|
// Routine Description:
|
|
// delete_tunnel :delete from _tunnel_map map full address key
|
|
// Arguments:
|
|
// address : ACE_CString AMT address
|
|
// port : ACE_UINT32 AMT port
|
|
//-----------------------------------------
|
|
int Tunnel_Manager::delete_tunnel(const ACE_CString &address, ACE_UINT32 port)
|
|
{
|
|
return (int)_tunnel_map.erase(toupper(buildFullAddress(address,port)));
|
|
}
|
|
|
|
//-----------------------------------------
|
|
//Routine Description:
|
|
// tunnel_exists :find in _tunnel_map AMT_Tunnel_Consumer * value by full address
|
|
//Arguments:
|
|
// full_address : ACE_CString full address including address and port
|
|
//Returns:
|
|
// AMT_Tunnel_Consumer * value on success
|
|
// NULL on failure
|
|
//-----------------------------------------
|
|
bool Tunnel_Manager::tunnel_exists( const ACE_CString &address,
|
|
const ACE_UINT32 port)
|
|
{
|
|
return (find_tunnel(address,port) != NULL);
|
|
}
|
|
|
|
//-----------------------------------------
|
|
// Routine Description:
|
|
// find_Tunnel :find in _tunnel_map AMT_Tunnel_Consumer * value by full address
|
|
// Arguments:
|
|
// full_address : ACE_CString full address including address and port
|
|
// Returns:
|
|
// AMT_Tunnel_Consumer * value on success
|
|
// NULL on failure
|
|
//-----------------------------------------
|
|
AMT_Tunnel_Handler* Tunnel_Manager::find_tunnel(const ACE_CString &full_address)
|
|
{
|
|
AMT_Tunnel_Handler* tunnel = NULL;
|
|
TUNNEL_MAP::iterator tunnel_iterator;
|
|
|
|
if (!_tunnel_map.empty())
|
|
{
|
|
if ((tunnel_iterator = _tunnel_map.find(toupper(full_address))) != _tunnel_map.end())
|
|
{
|
|
tunnel = tunnel_iterator->second;
|
|
}
|
|
}
|
|
return tunnel;
|
|
}
|
|
|
|
//-----------------------------------------
|
|
// Routine Description:
|
|
// find_tunnel :find in _tunnel_map AMT_Tunnel_Consumer * value by address and port
|
|
// Arguments:
|
|
// address : ACE_CString AMT address
|
|
// port : ACE_UINT32 AMT port
|
|
// Returns:
|
|
// AMT_Tunnel_Consumer * value on success
|
|
// NULL on failure
|
|
//-----------------------------------------
|
|
AMT_Tunnel_Handler* Tunnel_Manager::find_tunnel(const ACE_CString &address,
|
|
const ACE_UINT32 port)
|
|
{
|
|
return find_tunnel(buildFullAddress(address,port));
|
|
}
|
|
|
|
//-----------------------------------------
|
|
// Return all open port that this tunnel have
|
|
//-----------------------------------------
|
|
void Tunnel_Manager::get_open_ports(const AMT_Tunnel_Handler* tunnel,
|
|
ACE_Vector<Port_Address> &ports)
|
|
{
|
|
TUNNEL_MAP::iterator tunnel_map_iterator;
|
|
for(tunnel_map_iterator =_tunnel_map.begin();
|
|
tunnel_map_iterator!=_tunnel_map.end();
|
|
tunnel_map_iterator++)
|
|
{
|
|
if(tunnel_map_iterator->second == tunnel)
|
|
{
|
|
ports.push_back(Port_Address(tunnel_map_iterator->first));
|
|
}
|
|
}
|
|
}
|