Confdump Agent  1.4.0
Socket.cpp
Go to the documentation of this file.
00001 /* 
00002  * Confdump-Agent - Dump static and runtime system configuration
00003  * Copyright (C) 2009-2012  Straton IT, SAS
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <winsock2.h>
00019 #include <ws2tcpip.h>
00020 #include <iphlpapi.h>
00021 
00022 #include "QVariant"
00023 
00024 #include "boost/scoped_array.hpp"
00025 
00026 #include "Confdump/System/Win32Dumper.hpp"
00027 #include "Confdump/System/Win32/Socket.hpp"
00028 #include "Confdump/System/Win32/errors.hpp"
00029 
00030 namespace Confdump
00031 {
00032 
00033 namespace System
00034 {
00035 
00036 namespace Win32
00037 {
00038 
00039 Result BasicTcp4Socket::allSystemInstances()
00040 {
00041     Result table;
00042 
00043     // First call to get the proper size
00044     DWORD size = 0;
00045     DWORD rc = GetTcpTable( NULL, &size, TRUE );
00046     if ( rc != ERROR_INSUFFICIENT_BUFFER )
00047         throwOnWin32Error( rc, "get the tcp connection table size" );
00048 
00049     // Second call to get the data
00050     boost::scoped_array<char> mibtablebuf( new char[size] );
00051     MIB_TCPTABLE *mibtable = reinterpret_cast<MIB_TCPTABLE*>( mibtablebuf.get() );
00052     rc = GetTcpTable( mibtable, &size, false );
00053     throwOnWin32Error( rc, "get the tcp connection table" );
00054 
00055     // Convert to objects
00056     for ( size_t i = 0; i < mibtable->dwNumEntries; ++i )
00057     {
00058         boost::shared_ptr<BasicTcp4Socket> socket( new BasicTcp4Socket( mibtable->table[i] ) );
00059         table.push_back( socket );
00060     }
00061 
00062     return table;
00063 }
00064 
00065 BasicTcp4Socket::BasicTcp4Socket( const MIB_TCPROW &row )
00066 {
00067     in_addr addr;
00068 
00069     setProperty( "protocolName", "tcp" );
00070 
00071     addr.S_un.S_addr = row.dwLocalAddr;
00072     setProperty( "localAddressString", QString( "%1:%2" )
00073         .arg( inet_ntoa( addr ) )
00074         .arg( ntohs( static_cast<quint16>( row.dwLocalPort ) ) ) );
00075 
00076     addr.S_un.S_addr = row.dwRemoteAddr;
00077     setProperty( "remoteAddressString", QString( "%1:%2" )
00078         .arg( inet_ntoa( addr ) )
00079         .arg( ntohs( static_cast<quint16>( row.dwRemotePort ) ) ) );
00080 
00081     QString state;
00082     switch ( row.dwState )
00083     {
00084     case MIB_TCP_STATE_CLOSED:     state = "CLOSE";       break;
00085     case MIB_TCP_STATE_LISTEN:     state = "LISTEN";      break;
00086     case MIB_TCP_STATE_SYN_SENT:   state = "SYN_SENT";    break;
00087     case MIB_TCP_STATE_SYN_RCVD:   state = "SYN_RECV";    break;
00088     case MIB_TCP_STATE_ESTAB:      state = "ESTABLISHED"; break;
00089     case MIB_TCP_STATE_FIN_WAIT1:  state = "FIN_WAIT1";   break;
00090     case MIB_TCP_STATE_FIN_WAIT2:  state = "FIN_WAIT2";   break;
00091     case MIB_TCP_STATE_CLOSE_WAIT: state = "CLOSE_WAIT";  break;
00092     case MIB_TCP_STATE_CLOSING:    state = "CLOSING";     break;
00093     case MIB_TCP_STATE_LAST_ACK:   state = "LAST_ACK";    break;
00094     case MIB_TCP_STATE_TIME_WAIT:  state = "TIME_WAIT";   break;
00095     case MIB_TCP_STATE_DELETE_TCB: state = "DELETE_TCB";  break;
00096     }
00097     setProperty( "stateString", state );
00098 }
00099 
00100 BasicTcp4Socket::~BasicTcp4Socket()
00101 {
00102 }
00103 
00104 }
00105 }
00106 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends