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 "RealIPResolver.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040 #include "Array_ptr.h"
00041
00042 #include "TCPSocket.h"
00043
00044 #include <iphlpapi.h>
00045
00046 #ifdef _MEMORY_DEBUG
00047 #define new DEBUG_NEW
00048 #define malloc DEBUG_MALLOC
00049 static char THIS_FILE[] = __FILE__;
00050 #endif
00051
00052 KOMODIA_NAMESPACE_START
00053
00054 #define CRealIPResolver_Class "CRealIPResolver"
00055
00056 CRealIPResolver::CRealIPResolver() : CErrorHandler()
00057 {
00058 try
00059 {
00060
00061 SetName(CRealIPResolver_Class);
00062 }
00063 ERROR_HANDLER_RETHROW("CRealIPResolver")
00064 }
00065
00066 CRealIPResolver::~CRealIPResolver()
00067 {
00068 }
00069
00070 IP CRealIPResolver::Resolve(IP aInternetIP,
00071 const std::string& rTestURL,
00072 unsigned short usPort)
00073 {
00074 try
00075 {
00076
00077 DWORD dwResult;
00078 DWORD dwIndex;
00079 dwResult=GetBestInterface(aInternetIP,
00080 &dwIndex);
00081
00082
00083 if (dwResult!=NO_ERROR)
00084 {
00085
00086 ReportErrorOS("Resolve","Failed to call routing command! (GetBestInterface)");
00087
00088
00089 return 0;
00090 }
00091
00092
00093 DWORD dwLength;
00094 dwLength=0;
00095 dwResult=GetInterfaceInfo(NULL,
00096 &dwLength);
00097
00098 if (dwResult!=NO_ERROR &&
00099 dwResult!=ERROR_INSUFFICIENT_BUFFER)
00100 {
00101
00102 ReportErrorOS("Resolve","Failed to call routing command! (GetInterfaceInfo, NULL)");
00103
00104
00105 return 0;
00106 }
00107
00108
00109 char* pData;
00110 pData=new char[dwLength];
00111
00112
00113 CArray_ptr<char> pProtection(pData);
00114
00115
00116 if (GetInterfaceInfo((PIP_INTERFACE_INFO)pData,
00117 &dwLength)!=NO_ERROR)
00118 {
00119
00120 ReportErrorOS("Resolve","Failed to call routing command! (GetInterfaceInfo)");
00121
00122
00123 return 0;
00124 }
00125
00126
00127 WCHAR pName[MAX_ADAPTER_NAME];
00128 pName[0]=0;
00129 pName[1]=0;
00130
00131
00132 PIP_INTERFACE_INFO pInfo;
00133 pInfo=(PIP_INTERFACE_INFO)pData;
00134
00135
00136 for (int iCount=0;
00137 iCount<pInfo->NumAdapters;
00138 ++iCount)
00139 if (pInfo->Adapter[iCount].Index==dwIndex)
00140 {
00141
00142 wcscpy(pName,
00143 pInfo->Adapter[iCount].Name);
00144
00145
00146 break;
00147 }
00148
00149
00150 if (!pName[0])
00151
00152 return 0;
00153
00154
00155
00156 dwLength=0;
00157 dwResult=GetAdaptersInfo(NULL,
00158 &dwLength);
00159
00160 if (dwResult!=NO_ERROR &&
00161 dwResult!=ERROR_BUFFER_OVERFLOW)
00162 {
00163
00164 ReportErrorOS("Resolve","Failed to call routing command! (GetAdaptersInfo, NULL)");
00165
00166
00167 return 0;
00168 }
00169
00170
00171 char* pAdapterRawInfo;
00172 pAdapterRawInfo=new char[dwLength];
00173
00174
00175 CArray_ptr<char> pProtection2(pAdapterRawInfo);
00176
00177
00178 if (GetAdaptersInfo((PIP_ADAPTER_INFO)pAdapterRawInfo,
00179 &dwLength)!=NO_ERROR)
00180 {
00181
00182 ReportErrorOS("Resolve","Failed to call routing command! (GetAdaptersInfo)");
00183
00184
00185 return 0;
00186 }
00187
00188
00189 std::string sSymbolicToFind;
00190 sSymbolicToFind=ConvertSymbolic((const char*)pName);
00191
00192
00193 PIP_ADAPTER_INFO pAdapterInfo;
00194 pAdapterInfo=(PIP_ADAPTER_INFO)pAdapterRawInfo;
00195
00196
00197 IPVector aVector;
00198
00199 do
00200 {
00201
00202 std::string sSymbolic;
00203 sSymbolic=pAdapterInfo->AdapterName;
00204
00205
00206 if (sSymbolic==sSymbolicToFind)
00207 {
00208
00209 aVector=ExtractIP((const void*)pAdapterInfo);
00210
00211
00212 break;
00213 }
00214
00215
00216 pAdapterInfo=pAdapterInfo->Next;
00217 } while (pInfo);
00218
00219
00220 if (aVector.empty())
00221 return 0;
00222
00223
00224 if (aVector.size()==1)
00225
00226 if (!rTestURL.empty())
00227 if (TestAddress(aVector[0],
00228 CSocketBase::StringToLong(rTestURL),
00229 usPort))
00230 return aVector[0];
00231 else
00232 return 0;
00233 else
00234 return aVector[0];
00235
00236
00237 return 0;
00238
00239 }
00240 ERROR_HANDLER_RETURN("Resolve",0)
00241 }
00242
00243 std::string CRealIPResolver::ConvertSymbolic(const char* pName)const
00244 {
00245 try
00246 {
00247
00248 std::string sData;
00249
00250
00251 int iPosition=0;
00252
00253
00254 BOOL bCopy;
00255 bCopy=FALSE;
00256
00257
00258 while (pName[iPosition] ||
00259 pName[iPosition+1])
00260 {
00261
00262 if (pName[iPosition]=='{')
00263 bCopy=TRUE;
00264
00265
00266 if (bCopy)
00267 sData+=pName[iPosition];
00268
00269
00270 iPosition+=2;
00271 }
00272
00273
00274 return sData;
00275 }
00276 ERROR_HANDLER_RETURN("ConvertSymbolic","")
00277 }
00278
00279 CRealIPResolver::IPVector CRealIPResolver::ExtractIP(const void* pData)const
00280 {
00281
00282 IPVector aBlank;
00283
00284 try
00285 {
00286
00287 IPVector aVector;
00288
00289
00290 PIP_ADAPTER_INFO pInfo;
00291 pInfo=(PIP_ADAPTER_INFO)pData;
00292
00293
00294 PIP_ADDR_STRING pAddress;
00295 pAddress=(PIP_ADDR_STRING)&(pInfo->IpAddressList);
00296
00297
00298 do
00299 {
00300
00301 IP aAddress;
00302 aAddress=CSocketBase::StringToLong(std::string((char*)&(pAddress->IpAddress)));
00303
00304
00305 aVector.push_back(aAddress);
00306
00307
00308 pAddress=pAddress->Next;
00309 } while (pAddress);
00310
00311
00312 return aVector;
00313 }
00314 ERROR_HANDLER_RETURN("ExtractIP",aBlank)
00315 }
00316
00317 BOOL CRealIPResolver::IsNATAddress(IP aIP)const
00318 {
00319 try
00320 {
00321
00322 static const iNatCount=4;
00323 static const std::string sNat[iNatCount*2]={"10.0.0.0","10.255.255.255",
00324 "127.0.0.0","127.255.255.255",
00325 "172.16.0.0","172.31.255.255",
00326 "192.168.0.0","192.168.255.255"};
00327
00328
00329 IP aTestIP;
00330 aTestIP=htonl(aIP);
00331
00332
00333 for (int iCount=0;
00334 iCount<iNatCount;
00335 ++iCount)
00336 {
00337
00338 IP aFromIP;
00339 aFromIP=htonl(CSocketBase::StringToLong(sNat[iCount*2]));
00340
00341
00342 IP aToIP;
00343 aToIP=htonl(CSocketBase::StringToLong(sNat[iCount*2+1]));
00344
00345
00346 if (aFromIP<=aTestIP &&
00347 aToIP>=aTestIP)
00348 return TRUE;
00349 }
00350
00351
00352 return FALSE;
00353 }
00354 ERROR_HANDLER_RETURN("IsNATAddress",FALSE)
00355 }
00356
00357 BOOL CRealIPResolver::TestAddress(IP aAddress,
00358 IP aTestAddress,
00359 unsigned short usPort)const
00360 {
00361 try
00362 {
00363
00364 if (!aAddress ||
00365 !aTestAddress)
00366 {
00367
00368 ReportError("TestAddress","Atleast one address is zero!");
00369
00370
00371 return FALSE;
00372 }
00373
00374
00375 CTCPSocket aSocket(FALSE);
00376 if (!aSocket.Create())
00377 {
00378
00379 ReportError("TestAddress","Failed to create socket!");
00380
00381
00382 return FALSE;
00383 }
00384
00385
00386 if (!aSocket.Bind(aAddress,
00387 0))
00388 {
00389
00390 ReportError("TestAddress","Failed to bind socket!");
00391
00392
00393 return FALSE;
00394 }
00395
00396
00397 if (!aSocket.Connect(aTestAddress,
00398 usPort))
00399
00400 return FALSE;
00401 else
00402 return TRUE;
00403 }
00404 ERROR_HANDLER_RETURN("TestAddress",FALSE)
00405 }
00406
00407 KOMODIA_NAMESPACE_END