00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #include "stdafx.h"
00037 #include "Interfaces.h"
00038 
00039 #include "ErrorHandlerMacros.h"
00040 
00041 #ifdef _MEMORY_DEBUG 
00042     #define new    DEBUG_NEW  
00043     #define malloc DEBUG_MALLOC  
00044     static char THIS_FILE[] = __FILE__;  
00045 #endif
00046 
00047 KOMODIA_NAMESPACE_START
00048 
00049 #define CInterfaces_Class "CInterfaces"
00050 #define CHECK_POSITION(METHOD_NAME,RETURN_VALUE)    \
00051     if (m_iPosition>=m_iStructures)\
00052     {\
00053         ReportError(METHOD_NAME,"Passed over!");\
00054         return RETURN_VALUE;\
00055     }
00056 
00057 CInterfaces::CInterfaces(int iMaxInterfaces) : CSocketBase(),
00058                                                m_iStructures(0),
00059                                                m_iPosition(0)
00060 
00061 {
00062     try
00063     {
00064         
00065         SetName(CInterfaces_Class);
00066 
00067         
00068         m_iMaxInterfaces=iMaxInterfaces;
00069 
00070         
00071         if (!iMaxInterfaces)
00072             m_pInfo=NULL;
00073         else
00074             m_pInfo=new INTERFACE_INFO[m_iMaxInterfaces];
00075     }
00076     ERROR_HANDLER("CInterfaces")
00077 }
00078 
00079 CInterfaces::~CInterfaces()
00080 {
00081     try
00082     {
00083         delete m_pInfo;
00084     }
00085     ERROR_HANDLER("~CInterfaces")
00086 }
00087 
00088 BOOL CInterfaces::GetInterfaces()
00089 {
00090     if (!m_iMaxInterfaces)
00091     {
00092         
00093         ReportError("GetInterfaces","You constructed the class with 0 parameter!");
00094 
00095         
00096         return FALSE;
00097     }
00098 
00099     try
00100     {
00101         
00102         SOCKET aSocket;
00103         aSocket=socket(AF_INET,SOCK_DGRAM,0);
00104 
00105         
00106         if (aSocket==INVALID_SOCKET)
00107         {
00108             
00109             SetLastError("GetInterfaces");
00110 
00111             
00112             return FALSE;
00113         }
00114 
00115         
00116         unsigned long ulBytes;
00117         if (WSAIoctl(aSocket,
00118                      SIO_GET_INTERFACE_LIST,
00119                      NULL,
00120                      NULL,
00121                      m_pInfo,
00122                      sizeof(INTERFACE_INFO)*m_iMaxInterfaces,
00123                      &ulBytes,
00124                      NULL,
00125                      NULL))
00126         {
00127             
00128             SetLastError("GetInterfaces");
00129 
00130             
00131             closesocket(aSocket);
00132 
00133             
00134             return FALSE;
00135         }
00136 
00137         
00138         m_iStructures=ulBytes/sizeof(INTERFACE_INFO);
00139 
00140         
00141         m_iPosition=0;
00142 
00143         
00144         closesocket(aSocket);
00145 
00146         
00147         return TRUE;
00148     }
00149     ERROR_HANDLER_RETURN("GetInterfaces",FALSE)
00150 }
00151 
00152 long CInterfaces::GetAddress()const
00153 {
00154     CHECK_POSITION("GetAddress",0)
00155     
00156     try
00157     {
00158         return (m_pInfo+m_iPosition)->iiAddress.AddressIn.sin_addr.S_un.S_addr;
00159     }
00160     ERROR_HANDLER_RETURN("GetAddress",0);
00161 }
00162 
00163 BOOL CInterfaces::MoveNext()
00164 {
00165     ++m_iPosition;
00166     return m_iPosition<m_iStructures;
00167 }
00168 
00169 long CInterfaces::GetMask()const
00170 {
00171     CHECK_POSITION("GetMask",0)
00172     
00173     try
00174     {
00175         return m_pInfo[m_iPosition].iiNetmask.AddressIn.sin_addr.S_un.S_addr;
00176     }
00177     ERROR_HANDLER_RETURN("GetMask",0);
00178 }
00179 
00180 long CInterfaces::GetBroadcast()const
00181 {
00182     CHECK_POSITION("GetBroadcast",0)
00183     
00184     try
00185     {
00186         return m_pInfo[m_iPosition].iiBroadcastAddress.AddressIn.sin_addr.S_un.S_addr;
00187     }
00188     ERROR_HANDLER_RETURN("GetBroadcast",0);
00189 }
00190 
00191 BOOL CInterfaces::IsRunning()const
00192 {
00193     return GetFlags() & IFF_UP;
00194 }
00195 
00196 BOOL CInterfaces::IsBroadcast()const
00197 {
00198     return GetFlags() & IFF_BROADCAST;
00199 }
00200 
00201 BOOL CInterfaces::IsLoopback()const
00202 {
00203     return GetFlags() & IFF_LOOPBACK;
00204 }
00205 
00206 BOOL CInterfaces::IsPPP()const
00207 {
00208     return GetFlags() & IFF_POINTTOPOINT;
00209 }
00210 
00211 BOOL CInterfaces::IsMulticast()const
00212 {
00213     return GetFlags() & IFF_MULTICAST;
00214 }
00215 
00216 long CInterfaces::GetFlags()const
00217 {
00218     CHECK_POSITION("GetFlags",0)
00219     
00220     try
00221     {
00222         return m_pInfo[m_iPosition].iiFlags;
00223     }
00224     ERROR_HANDLER_RETURN("GetFlags",0);
00225 }
00226 
00227 KOMODIA_NAMESPACE_END