00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <sys/socket.h>
00025 #include <netinet/in.h>
00026 #include <arpa/inet.h>
00027 #include <stdio.h>
00028
00029 #include "NATMap.h"
00030 #include "RioError.h"
00031
00032
00033
00034
00035
00036 NATData::NATData()
00037 {
00038 nat_addr = 0;
00039 nat_port = 0;
00040 }
00041 NATData::NATData( int addr, int port )
00042 {
00043 nat_addr = addr;
00044 nat_port = port;
00045 }
00046
00047 bool operator<( const NATData d1, const NATData d2 )
00048 {
00049 if( d1.nat_addr < d2.nat_addr )
00050 return true;
00051 else if( d1.nat_addr > d2.nat_addr )
00052 return false;
00053 else if( d1.nat_port < d2.nat_port )
00054 return true;
00055 else
00056 return false;
00057 };
00058
00059 map<pair<NATData, int>, NATData> nat_map;
00060
00061
00062
00063
00064
00065
00066 NATMap::NATMap()
00067 {
00068
00069 pthread_mutex_init( &m_Mutex, NULL );
00070 }
00071
00072 NATMap::~NATMap()
00073 {
00074
00075 pthread_mutex_destroy( &m_Mutex );
00076 }
00077
00078 void NATMap::insertElement( NATData server_map, int stor_id, NATData storage_map )
00079 {
00080 pair<NATData, int> current_pair( server_map, stor_id );
00081
00082 pthread_mutex_lock( &m_Mutex );
00083 nat_map[current_pair] = storage_map;
00084
00085 #ifdef RIO_DEBUG2
00086 struct in_addr ip1, ip2;
00087 ip1.s_addr = server_map.nat_addr;
00088 ip2.s_addr = storage_map.nat_addr;
00089 RioErr << "[NATMap] Inserting NAT mapping" << endl;
00090 RioErr << inet_ntoa( ip1 ) << " , "
00091 << ntohs( server_map.nat_port ) << " , "
00092 << stor_id << " ==> ";
00093 fflush( stderr );
00094 RioErr << inet_ntoa( ip2 ) << " , "
00095 << ntohs( storage_map.nat_port ) << endl;
00096 #endif
00097
00098 pthread_mutex_unlock( &m_Mutex );
00099 };
00100
00101 NATData NATMap::getElement( NATData server_map, int stor_id )
00102 {
00103 pair<NATData, int> current_pair( server_map, stor_id );
00104 NATData Data;
00105
00106 pthread_mutex_lock( &m_Mutex );
00107
00108 #ifdef RIO_DEBUG2
00109 struct in_addr ip1, ip2;
00110 ip1.s_addr = server_map.nat_addr;
00111 ip2.s_addr = nat_map[current_pair].nat_addr;
00112 RioErr << "[NATMap] Retrieving NAT mapping" << endl;
00113 RioErr << inet_ntoa( ip1 ) << " , "
00114 << ntohs( server_map.nat_port ) << " , "
00115 << stor_id << " ==> ";
00116 fflush( stderr );
00117 RioErr << inet_ntoa( ip2 ) << " , "
00118 << ntohs( nat_map[current_pair].nat_port ) << endl;
00119 #endif
00120
00121 Data = nat_map[current_pair];
00122 pthread_mutex_unlock( &m_Mutex );
00123 return Data;
00124 };
00125
00126 void NATMap::removeElement( NATData server_map, int stor_id )
00127 {
00128 pair<NATData, int> current_pair( server_map, stor_id );
00129
00130 pthread_mutex_lock( &m_Mutex );
00131 nat_map.erase( current_pair );
00132 pthread_mutex_unlock( &m_Mutex );
00133 }
00134
00135
00136 bool NATMap::findElement( NATData server_map, int stor_id )
00137 {
00138 bool isFound;
00139 map<pair<NATData, int>, NATData>::iterator Element;
00140 pair<NATData, int> current_pair( server_map, stor_id );
00141
00142 pthread_mutex_lock( &m_Mutex );
00143
00144
00145
00146
00147 Element = nat_map.find( current_pair );
00148 isFound = ( Element != nat_map.end() );
00149
00150 pthread_mutex_unlock( &m_Mutex );
00151
00152 return( isFound );
00153 }
00154
00155 #ifdef RIO_DEBUG2
00156 void NATMap::printMapping()
00157 {
00158 map<pair<NATData, int>, NATData>::iterator NAT;
00159 in_addr ip1, ip2;
00160
00161 pthread_mutex_lock( &m_Mutex );
00162
00163
00164
00165
00166 RioErr << "NATMap::printMapping imprimindo o vetor de mapeamento com "
00167 << nat_map.size() << " mapementos" << endl;
00168
00169 for( NAT = nat_map.begin(); NAT != nat_map.end(); NAT++ )
00170 {
00171 ip1.s_addr = ( *NAT ).first.first.nat_addr;
00172 ip2.s_addr = ( *NAT ).second.nat_addr;
00173 RioErr << "NATMap::printMapping mapeamento para o cliente com o IP "
00174 << inet_ntoa( ip1 ) << " e a porta "
00175 << ntohs( ( *NAT ).first.first.nat_port );
00176 fflush( stderr );
00177 RioErr << " para o servidor de armazenamento com a ID "
00178 << ( *NAT ).first.second << ": IP = " << inet_ntoa( ip2 )
00179 << " e porta " << ntohs( ( *NAT ).second.nat_port ) << endl;
00180 }
00181
00182 RioErr << "NATMap::printMapping fim da impressao do vetor com os "
00183 << "mapeamentos" << endl;
00184
00185 pthread_mutex_unlock( &m_Mutex );
00186 }
00187 #endif