patch-libapp_resolver_cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. $OpenBSD: patch-libapp_resolver_cpp,v 1.1 2015/07/01 11:33:50 bluhm Exp $
  2. --- libapp/resolver.cpp.orig Tue Feb 13 00:55:08 2001
  3. +++ libapp/resolver.cpp Thu Jan 8 00:10:40 2015
  4. @@ -11,6 +11,7 @@
  5. #include "parser.h"
  6. #include "app.h"
  7. +#include "Avl.h"
  8. #include "dbg.h"
  9. #include <errno.h>
  10. @@ -162,7 +163,7 @@ static char* inet_ntoa_rev( struct in_addr in )
  11. static char host[29]; // aaa.bbb.ccc.ddd.in-addr.arpa\0
  12. BYTE qa[4];
  13. memcpy( qa, &in.s_addr, 4 );
  14. - sprintf( host, "%u.%u.%u.%u.in-addr.arpa", qa[3], qa[2], qa[1], qa[0] );
  15. + snprintf( host, sizeof(host), "%u.%u.%u.%u.in-addr.arpa", qa[3], qa[2], qa[1], qa[0] );
  16. return host;
  17. }
  18. @@ -196,9 +197,7 @@ CResolver * CResolver::GetResolver( void )
  19. bool CResolver::GetHost( CResolverResponse* pResponse, const CString& strHost )
  20. {
  21. -dbgout( "CResolver::GetHost: query for %s", (CPCHAR)strHost );
  22. - CHostInfo* pInfo;
  23. -
  24. + dbgout( "CResolver::GetHost: query for %s", (CPCHAR)strHost );
  25. // Determine if this is a number or name
  26. bool bIsNumeric = true;
  27. CPCHAR p = strHost;
  28. @@ -224,16 +223,16 @@ dbgout( "CResolver::GetHost: query for %s", (CPCHAR)st
  29. }
  30. // First search for name as given
  31. - CHostInfo info( strHost );
  32. - pInfo = m_treeHostInfo.Search( info );
  33. - if( pInfo )
  34. + CHostInfo pInfo = m_treeHostInfo[strHost];
  35. +
  36. + if( pInfo.m_strName != "" )
  37. {
  38. - if( pInfo->m_tExpire > time(NULL) )
  39. + if( pInfo.m_tExpire > time(NULL) )
  40. {
  41. - pResponse->GetHostDone( 0, strHost, pInfo->m_addr );
  42. + pResponse->GetHostDone( 0, strHost, pInfo.m_addr );
  43. return true;
  44. }
  45. - m_treeHostInfo.Delete( info );
  46. + m_treeHostInfo.erase( strHost );
  47. }
  48. // Not found - if it's unqualified, search the domain list
  49. @@ -254,15 +253,16 @@ dbgout( "CResolver::GetHost: query for %s", (CPCHAR)st
  50. strFQDN.Append( "." );
  51. strFQDN.Append( *itr );
  52. CHostInfo info( strFQDN );
  53. - CHostInfo* pInfo = m_treeHostInfo.Search( info );
  54. - if( pInfo )
  55. + CHostInfo pInfo = m_treeHostInfo[strFQDN];
  56. +
  57. + if ( pInfo.m_strName != "" )
  58. {
  59. - if( pInfo->m_tExpire > time(NULL) )
  60. + if( pInfo.m_tExpire > time(NULL) )
  61. {
  62. - pResponse->GetHostDone( 0, strHost, pInfo->m_addr );
  63. + pResponse->GetHostDone( 0, strHost, pInfo.m_addr );
  64. return true;
  65. }
  66. - m_treeHostInfo.Delete( info );
  67. + m_treeHostInfo.erase( strFQDN );
  68. }
  69. itr++;
  70. }
  71. @@ -270,143 +270,162 @@ dbgout( "CResolver::GetHost: query for %s", (CPCHAR)st
  72. // Looks like we have to send a query
  73. CHostQuery query( strHost );
  74. - CHostQuery* pQuery = m_treeHostQueries.Insert( query );
  75. - if( ! pQuery )
  76. + CHostQuery pQuery = (*m_treeHostQueries.insert( pair<CString,CHostQuery>( strHost,query ) ).first).second;
  77. +
  78. + if ( pQuery.m_strHost != "" )
  79. {
  80. - pQuery = m_treeHostQueries.Search( query );
  81. - assert( pQuery );
  82. -
  83. - pQuery->m_tExpire = time(NULL) + 4;
  84. - pQuery->m_tDelta = 4;
  85. + pQuery.m_tExpire = time(NULL) + 4;
  86. + pQuery.m_tDelta = 4;
  87. if( !strchr( strHost, '.' ) )
  88. {
  89. // Unqualified - search the domain list
  90. CDomainList::Iterator itr( m_listDomains.Begin() );
  91. assert( itr ); // should have caught this already
  92. - pQuery->m_strFQDN = strHost;
  93. - pQuery->m_strFQDN.Append( "." );
  94. - pQuery->m_strFQDN.Append( *itr );
  95. - pQuery->m_itrDomain = itr;
  96. + pQuery.m_strFQDN = strHost;
  97. + pQuery.m_strFQDN.Append( "." );
  98. + pQuery.m_strFQDN.Append( *itr );
  99. + pQuery.m_itrDomain = itr;
  100. }
  101. - pQuery->m_itrServer = m_listServers.Begin();
  102. + pQuery.m_itrServer = m_listServers.Begin();
  103. - SendQuery( *pQuery->m_itrServer, pQuery->m_strFQDN, RR_A );
  104. + SendQuery( *pQuery.m_itrServer, pQuery.m_strFQDN, RR_A );
  105. }
  106. + pQuery.AddResponse( *pResponse );
  107. - pQuery->AddResponse( pResponse );
  108. + // Speichern des geaenderten Objekts
  109. + m_treeHostQueries[strHost] = pQuery;
  110. return true;
  111. }
  112. bool CResolver::GetHost( CResolverResponse* pResponse, struct in_addr addr )
  113. {
  114. -dbgout( "CResolver::GetHost: query for %s", inet_ntoa(addr) );
  115. + dbgout( "CResolver::GetHost: query for %s", inet_ntoa(addr) );
  116. CAddrInfo info( addr );
  117. // See if we already have it
  118. - CAddrInfo* pInfo = m_treeAddrInfo.Search( info );
  119. - if( pInfo )
  120. + CAddrInfo pInfo = m_treeAddrInfo[addr];
  121. +
  122. + if( pInfo.m_addr.s_addr != INADDR_NONE )
  123. {
  124. - if( pInfo->m_tExpire > time(NULL) )
  125. + if( pInfo.m_tExpire > time(NULL) )
  126. {
  127. - pResponse->GetHostDone( 0, addr, pInfo->m_strName );
  128. + pResponse->GetHostDone( 0, addr, pInfo.m_strName );
  129. return true;
  130. }
  131. - m_treeAddrInfo.Delete( info );
  132. + m_treeAddrInfo.erase( addr );
  133. }
  134. // Nope, gotta send a query
  135. CAddrQuery query( addr );
  136. - CAddrQuery* pQuery = m_treeAddrQueries.Insert( query );
  137. - if( ! pQuery )
  138. - {
  139. - pQuery = m_treeAddrQueries.Search( query );
  140. - assert( pQuery );
  141. + CAddrQuery pQuery = (*(m_treeAddrQueries.insert( pair<in_addr,CAddrQuery>( addr, query ) ).first)).second;
  142. - pQuery->m_tExpire = time(NULL) + 4;
  143. - pQuery->m_tDelta = 4;
  144. - pQuery->m_itrServer = m_listServers.Begin();
  145. - SendQuery( *pQuery->m_itrServer, inet_ntoa_rev(addr), RR_PTR );
  146. + if ( pQuery.m_addr.s_addr != INADDR_NONE )
  147. + {
  148. + pQuery.m_tExpire = time(NULL) + 4;
  149. + pQuery.m_tDelta = 4;
  150. + pQuery.m_itrServer = m_listServers.Begin();
  151. + SendQuery( *pQuery.m_itrServer, inet_ntoa_rev(addr), RR_PTR );
  152. }
  153. + pQuery.AddResponse( *pResponse );
  154. - pQuery->AddResponse( pResponse );
  155. + // Speichern des geaenderten Objekts
  156. + m_treeAddrQueries[addr] = pQuery;
  157. return true;
  158. }
  159. -void CResolver::WalkHostTree( AvlNode<CHostQuery>* pNode )
  160. +void CResolver::IterateHostMap()
  161. {
  162. - if( !pNode ) return;
  163. - WalkHostTree( pNode->Subtree(LEFT) );
  164. - WalkHostTree( pNode->Subtree(RIGHT) );
  165. - CHostQuery& rquery = pNode->Key();
  166. - if( rquery.m_tExpire < time(NULL) )
  167. + // Empty map
  168. + if ( m_treeHostQueries.empty() ) return;
  169. +
  170. + CHostQueryMap::iterator itr = m_treeHostQueries.begin();
  171. + // Iterate over all hostname queries
  172. + while ( itr != m_treeHostQueries.end() )
  173. {
  174. - // Timed out, next nameserver
  175. - rquery.m_itrServer++;
  176. - if( rquery.m_itrServer )
  177. - {
  178. - rquery.m_tExpire = time(NULL)+4;
  179. - rquery.m_tDelta = 4;
  180. - SendQuery( *rquery.m_itrServer, rquery.m_strHost, RR_A );
  181. - return;
  182. - }
  183. + CHostQuery rquery = (*itr).second;
  184. + if( rquery.m_tExpire < time(NULL) )
  185. + {
  186. + dbgout("Nameserver timed out");
  187. + // Timed out, next nameserver
  188. + rquery.m_itrServer++;
  189. + if( rquery.m_itrServer )
  190. + {
  191. + rquery.m_tExpire = time(NULL)+4;
  192. + rquery.m_tDelta = 4;
  193. + SendQuery( *rquery.m_itrServer, rquery.m_strHost, RR_A );
  194. + return;
  195. + }
  196. - // Exhausted server list, so bump the timeout and start over
  197. - rquery.m_tExpire += rquery.m_tDelta;
  198. - rquery.m_tDelta *= 2;
  199. - rquery.m_itrServer = m_listServers.Begin();
  200. - if( rquery.m_tDelta > 30 )
  201. - {
  202. - // Everything timed out - we're all alone, and it's getting dark!
  203. - struct in_addr addr;
  204. - addr.s_addr = INADDR_NONE;
  205. - CResolverResponseList::Iterator itr( rquery.m_listResponses.Begin() );
  206. - while( itr )
  207. - {
  208. - (*itr)->GetHostDone( EAGAIN, rquery.m_strHost, addr );
  209. - itr++;
  210. - }
  211. - m_treeHostQueries.Delete( rquery );
  212. - }
  213. + dbgout("No more name servers left. Starting over");
  214. + // Exhausted server list, so bump the timeout and start over
  215. + rquery.m_tExpire += rquery.m_tDelta;
  216. + rquery.m_tDelta *= 2;
  217. + rquery.m_itrServer = m_listServers.Begin();
  218. + if( rquery.m_tDelta > 30 )
  219. + {
  220. + dbgout("Everything timed out!");
  221. + // Everything timed out - we're all alone, and it's getting dark!
  222. + struct in_addr addr;
  223. + addr.s_addr = INADDR_NONE;
  224. + CResolverResponseList::iterator itr( rquery.m_listResponses.begin() );
  225. + while( itr != rquery.m_listResponses.end() )
  226. + {
  227. + (*itr)->GetHostDone( EAGAIN, rquery.m_strHost, addr );
  228. + itr++;
  229. + }
  230. + m_treeHostQueries.erase( rquery.m_strHost );
  231. + }
  232. + }
  233. + ++itr;
  234. }
  235. }
  236. -void CResolver::WalkAddrTree( AvlNode<CAddrQuery>* pNode )
  237. +void CResolver::IterateAddrMap()
  238. {
  239. - if( !pNode ) return;
  240. - WalkAddrTree( pNode->Subtree(LEFT) );
  241. - WalkAddrTree( pNode->Subtree(RIGHT) );
  242. - CAddrQuery& rquery = pNode->Key();
  243. - if( rquery.m_tExpire < time(NULL) )
  244. + if ( m_treeAddrQueries.empty() ) return;
  245. +
  246. + CAddrQueryMap::iterator itr = m_treeAddrQueries.begin();
  247. +
  248. + // Iterate over all address queries
  249. + while ( itr != m_treeAddrQueries.end() )
  250. {
  251. - // Timed out, next nameserver
  252. - rquery.m_itrServer++;
  253. - if( rquery.m_itrServer )
  254. - {
  255. - rquery.m_tExpire = time(NULL)+4;
  256. - rquery.m_tDelta = 4;
  257. - SendQuery( *rquery.m_itrServer, inet_ntoa_rev(rquery.m_addr), RR_PTR );
  258. - return;
  259. - }
  260. + CAddrQuery rquery = (*itr).second;
  261. + if( rquery.m_tExpire < time(NULL) )
  262. + {
  263. + dbgout("Nameserver timed out");
  264. + // Timed out, next nameserver
  265. + rquery.m_itrServer++;
  266. + if( rquery.m_itrServer )
  267. + {
  268. + rquery.m_tExpire = time(NULL)+4;
  269. + rquery.m_tDelta = 4;
  270. + SendQuery( *rquery.m_itrServer, inet_ntoa_rev(rquery.m_addr), RR_PTR );
  271. + return;
  272. + }
  273. - // Exhausted server list, so bump the timeout and start over
  274. - rquery.m_tExpire += rquery.m_tDelta;
  275. - rquery.m_tDelta *= 2;
  276. - rquery.m_itrServer = m_listServers.Begin();
  277. - if( rquery.m_tDelta > 30 )
  278. - {
  279. - // Everything timed out - we're all alone, and it's getting dark!
  280. - CString host;
  281. - CResolverResponseList::Iterator itr( rquery.m_listResponses.Begin() );
  282. - while( itr )
  283. - {
  284. - (*itr)->GetHostDone( EAGAIN, rquery.m_addr, host );
  285. - itr++;
  286. - }
  287. - m_treeAddrQueries.Delete( rquery );
  288. - }
  289. + dbgout("No more name servers left. Starting over");
  290. + // Exhausted server list, so bump the timeout and start over
  291. + rquery.m_tExpire += rquery.m_tDelta;
  292. + rquery.m_tDelta *= 2;
  293. + rquery.m_itrServer = m_listServers.Begin();
  294. + if( rquery.m_tDelta > 30 )
  295. + {
  296. + dbgout("Everything timed out!");
  297. + // Everything timed out - we're all alone, and it's getting dark!
  298. + CString host;
  299. + CResolverResponseList::iterator itr( rquery.m_listResponses.begin() );
  300. + while( itr != rquery.m_listResponses.end() )
  301. + {
  302. + (*itr)->GetHostDone( EAGAIN, rquery.m_addr, host );
  303. + itr++;
  304. + }
  305. + m_treeAddrQueries.erase( rquery.m_addr );
  306. + }
  307. + }
  308. + ++itr;
  309. }
  310. }
  311. @@ -415,12 +434,12 @@ void CResolver::OnTimer( void )
  312. dbgout( "CResolver::OnTimer" );
  313. // iterate through queries, remove stale ones and respond fail
  314. - WalkHostTree( m_treeHostQueries.GetRoot() );
  315. - WalkAddrTree( m_treeAddrQueries.GetRoot() );
  316. + IterateHostMap();
  317. + IterateAddrMap();
  318. - if( m_treeHostQueries.IsEmpty() && m_treeAddrQueries.IsEmpty() )
  319. + if ( m_treeHostQueries.empty() && m_treeAddrQueries.empty() )
  320. {
  321. -dbgout( "CResolver::OnTimer: no more queries" );
  322. + dbgout( "CResolver::OnTimer: no more queries" );
  323. m_sock.Close();
  324. m_timer.Disable();
  325. }
  326. @@ -433,7 +452,7 @@ void CResolver::OnConnectDone( int err )
  327. void CResolver::OnReadReady( void )
  328. {
  329. -dbgout( "CResolver::OnReadReady" );
  330. + dbgout( "CResolver::OnReadReady" );
  331. CBuffer buf;
  332. buf.SetSize( 513 );
  333. if( !m_sock.Read( &buf ) )
  334. @@ -498,9 +517,9 @@ dbgout( "CResolver::OnReadReady" );
  335. if( qrhdr.qtype == RR_A )
  336. {
  337. // Does not exist - try next domain
  338. - CHostQuery query( qrhdr.strHost );
  339. - CHostQuery* pQuery = m_treeHostQueries.Search( query );
  340. - if( ! pQuery )
  341. + CHostQuery pQuery = m_treeHostQueries[qrhdr.strHost];
  342. +
  343. + if( pQuery.m_strHost == "" )
  344. {
  345. dbgout( "CResolver::OnReadReady: received rc=%u for unexpected host '%s'",
  346. rc, (CPCHAR)qrhdr.strHost );
  347. @@ -508,29 +527,32 @@ dbgout( "CResolver::OnReadReady" );
  348. }
  349. // If query was unqualified, bump the domain iterator
  350. - if( pQuery->m_itrDomain ) pQuery->m_itrDomain++;
  351. + if( pQuery.m_itrDomain ) pQuery.m_itrDomain++;
  352. - if( ! pQuery->m_itrDomain )
  353. + if( ! pQuery.m_itrDomain )
  354. {
  355. // Exhausted search list
  356. struct in_addr addr;
  357. addr.s_addr = INADDR_NONE;
  358. - CResolverResponseList::Iterator itr( pQuery->m_listResponses.Begin() );
  359. - while( itr )
  360. + CResolverResponseList::iterator itr( pQuery.m_listResponses.begin() );
  361. + while( itr != pQuery.m_listResponses.end() )
  362. {
  363. (*itr)->GetHostDone( ENOENT, qrhdr.strHost, addr );
  364. itr++;
  365. }
  366. - m_treeHostQueries.Delete( query );
  367. + m_treeHostQueries.erase( qrhdr.strHost );
  368. }
  369. else
  370. {
  371. - pQuery->m_strFQDN = pQuery->m_strHost;
  372. - pQuery->m_strFQDN.Append( "." );
  373. - pQuery->m_strFQDN.Append( *pQuery->m_itrDomain );
  374. - pQuery->m_itrDomain++;
  375. + pQuery.m_strFQDN = pQuery.m_strHost;
  376. + pQuery.m_strFQDN.Append( "." );
  377. + pQuery.m_strFQDN.Append( *pQuery.m_itrDomain );
  378. + pQuery.m_itrDomain++;
  379. //XXX: reset query's server iterator here?
  380. - SendQuery( *pQuery->m_itrServer, pQuery->m_strFQDN, RR_A );
  381. + SendQuery( *pQuery.m_itrServer, pQuery.m_strFQDN, RR_A );
  382. +
  383. + // Speichern des geaenderten Objekts
  384. + m_treeHostQueries[qrhdr.strHost] = pQuery;
  385. }
  386. }
  387. else if( qrhdr.qtype == RR_PTR )
  388. @@ -538,9 +560,9 @@ dbgout( "CResolver::OnReadReady" );
  389. struct in_addr addr;
  390. if( inet_aton_rev( qrhdr.strHost, &addr ) )
  391. {
  392. - CAddrQuery query( addr );
  393. - CAddrQuery* pQuery = m_treeAddrQueries.Search( query );
  394. - if( ! pQuery )
  395. + CAddrQuery pQuery = m_treeAddrQueries[addr];
  396. +
  397. + if( pQuery.m_addr.s_addr == INADDR_NONE )
  398. {
  399. dbgout( "CResolver::OnReadReady: received rc=%u for unexpected host '%s'",
  400. rc, (CPCHAR)qrhdr.strHost );
  401. @@ -548,13 +570,13 @@ dbgout( "CResolver::OnReadReady" );
  402. }
  403. CString host;
  404. - CResolverResponseList::Iterator itr( pQuery->m_listResponses.Begin() );
  405. - while( itr )
  406. + CResolverResponseList::iterator itr( pQuery.m_listResponses.begin() );
  407. + while( itr != pQuery.m_listResponses.end() )
  408. {
  409. (*itr)->GetHostDone( ENOENT, addr, host );
  410. itr++;
  411. }
  412. - m_treeAddrQueries.Delete( query );
  413. + m_treeAddrQueries.erase( addr );
  414. }
  415. }
  416. else
  417. @@ -607,9 +629,9 @@ dbgout( "CResolver::OnReadReady" );
  418. // Ignore authority and additional RR's
  419. - if( m_treeHostQueries.IsEmpty() && m_treeAddrQueries.IsEmpty() )
  420. + if( m_treeHostQueries.empty() && m_treeAddrQueries.empty() )
  421. {
  422. -dbgout( "CResolver::OnReadReady: no more queries" );
  423. + dbgout( "CResolver::OnReadReady: no more queries" );
  424. m_sock.Close();
  425. m_timer.Disable();
  426. }
  427. @@ -656,7 +678,7 @@ dbgout( "CResolver::SendQuery: host %s", (CPCHAR)strHo
  428. }
  429. if( CTimer::Repeating != m_timer.GetMode() )
  430. {
  431. -dbgout( "CResolver::SendQuery: setting timer" );
  432. + dbgout( "CResolver::SendQuery: setting timer" );
  433. m_timer.SetRepeating( 2*1000 );
  434. }
  435. @@ -688,59 +710,67 @@ dbgout( "CResolver::SendQuery: setting timer" );
  436. void CResolver::AddHostEntry( time_t tExpire, const CString& strHost, struct in_addr addr )
  437. {
  438. -dbgout( "AddHostEntry: host %s = %s", (CPCHAR)strHost, inet_ntoa(addr) );
  439. - // Add host to our host tree
  440. - m_treeHostInfo.Insert( CHostInfo( tExpire, strHost, addr ) );
  441. + dbgout( "AddHostEntry: host %s = %s", (CPCHAR)strHost, inet_ntoa(addr) );
  442. + // Add host to our host map
  443. + m_treeHostInfo[strHost] = CHostInfo( tExpire, strHost, addr);
  444. - // Add addr to our addr tree
  445. - m_treeAddrInfo.Insert( CAddrInfo( tExpire, addr, strHost ) );
  446. + // Add addr to our addr map
  447. + m_treeAddrInfo[addr] = CAddrInfo( tExpire, addr, strHost );
  448. // See if anyone is waiting for this host
  449. CHostQuery queryHost( strHost );
  450. - CHostQuery* pHostQuery = m_treeHostQueries.Search( queryHost );
  451. - if( pHostQuery )
  452. + CHostQuery pHostQuery;
  453. +
  454. + CHostQueryMap::iterator hqitr = m_treeHostQueries.find( strHost );
  455. +
  456. + if ( hqitr != m_treeHostQueries.end() )
  457. {
  458. -dbgout( "\tfound host query in tree, calling responses" );
  459. - CResolverResponseList::Iterator itr( pHostQuery->m_listResponses.Begin() );
  460. - while( itr )
  461. + pHostQuery = (*hqitr).second;
  462. + dbgout( "\tfound host query in tree, calling responses" );
  463. + CResolverResponseList::iterator itr( pHostQuery.m_listResponses.begin() );
  464. + while( itr != pHostQuery.m_listResponses.end() )
  465. {
  466. + dbgout("Calling GetHost done for host=%s and address=%s", (CPCHAR) strHost, inet_ntoa(addr));
  467. (*itr)->GetHostDone( 0, strHost, addr );
  468. itr++;
  469. }
  470. - m_treeHostQueries.Delete( queryHost );
  471. + m_treeHostQueries.erase( strHost );
  472. }
  473. CPCHAR pdot;
  474. if( (pdot = strchr( strHost, '.' )) )
  475. {
  476. // Perhaps someone is waiting on the unqualified name
  477. queryHost.m_strHost.Set( strHost, pdot - (CPCHAR)strHost );
  478. - pHostQuery = m_treeHostQueries.Search( queryHost );
  479. - if( pHostQuery )
  480. + hqitr = m_treeHostQueries.find( queryHost.m_strHost );
  481. +
  482. + if ( hqitr != m_treeHostQueries.end() )
  483. {
  484. -dbgout( "\tfound bare host query in tree, calling responses" );
  485. - CResolverResponseList::Iterator itr( pHostQuery->m_listResponses.Begin() );
  486. - while( itr )
  487. + pHostQuery = (*hqitr).second;
  488. + dbgout( "\tfound bare host query in tree, calling responses" );
  489. + CResolverResponseList::iterator itr( pHostQuery.m_listResponses.begin() );
  490. + while( itr != pHostQuery.m_listResponses.end() )
  491. {
  492. (*itr)->GetHostDone( 0, strHost, addr );
  493. itr++;
  494. }
  495. - m_treeHostQueries.Delete( queryHost );
  496. + m_treeHostQueries.erase( queryHost.m_strHost );
  497. }
  498. }
  499. // See if anyone is waiting for this addr
  500. - CAddrQuery queryAddr( addr );
  501. - CAddrQuery* pAddrQuery = m_treeAddrQueries.Search( queryAddr );
  502. - if( pAddrQuery )
  503. + CAddrQueryMap::iterator adritr = m_treeAddrQueries.find( addr );
  504. +
  505. + if ( adritr != m_treeAddrQueries.end() )
  506. {
  507. -dbgout( "\tfound addr query in tree, calling responses" );
  508. - CResolverResponseList::Iterator itr( pAddrQuery->m_listResponses.Begin() );
  509. - while( itr )
  510. + CAddrQuery pAddrQuery = (*adritr).second;
  511. + dbgout( "\tfound addr query in tree, calling responses" );
  512. + CResolverResponseList::iterator itr( pAddrQuery.m_listResponses.begin() );
  513. + while( itr != pAddrQuery.m_listResponses.end() )
  514. {
  515. (*itr)->GetHostDone( 0, addr, strHost );
  516. itr++;
  517. }
  518. - m_treeAddrQueries.Delete( queryAddr );
  519. + m_treeAddrQueries.erase( addr );
  520. }
  521. }
  522. @@ -953,7 +983,7 @@ bool CResolver::EncodeName( CPCHAR szName, PBYTE& rpbu
  523. {
  524. CPCHAR pLabel = szName;
  525. BYTE nLabelLen = 0;
  526. - BYTE nOverLen = min( 64, rlen-1 );
  527. + BYTE nOverLen = MIN( 64, rlen-1 );
  528. while( *szName && '.' != *szName && nLabelLen < nOverLen )
  529. {
  530. nLabelLen++;
  531. @@ -1012,15 +1042,18 @@ bool CResolver::ParseAnswerHeader( dns_rr_hdr* phdr, c
  532. bool CResolver::DecodeName( PCHAR pname, const CBuffer& buf, size_t& rpos )
  533. {
  534. - CPBYTE pbuf = buf.GetBuffer();
  535. - size_t buflen = buf.GetSize();
  536. + const CPBYTE pbuf = buf.GetBuffer();
  537. + const size_t buflen = buf.GetSize();
  538. size_t pos = rpos;
  539. size_t namelen = 0;
  540. + size_t iteration = 0;
  541. assert( buflen > 0 && buflen <= 512 && pos < buflen );
  542. bool bHasPtr = false;
  543. while( pbuf[pos] )
  544. {
  545. + if( iteration++ >= buflen ) return false;
  546. +
  547. UINT8 len = pbuf[pos];
  548. if( !(len & 0xC0) )
  549. {
  550. @@ -1041,7 +1074,7 @@ bool CResolver::DecodeName( PCHAR pname, const CBuffer
  551. if( (len & 0xC0) != 0xC0 || pos > buflen-2 ) return false;
  552. pos = (UINT16)(pbuf[pos] & 0x3F)*256 + (UINT16)(pbuf[pos+1]);
  553. if( pos >= buflen-1 ) return false;
  554. - rpos += 2;
  555. + if( !bHasPtr ) rpos += 2;
  556. bHasPtr = true;
  557. }
  558. }