@@ -308,7 +308,7 @@ class DNSQuery final : public SimpleUdpClient
308308                    continue ;
309309                }
310310                int  ip = *((int  *) (&str_respones[index_inner]));
311-                 std::string str_ip = NetHelper::int32_to_string_addr  (ip);
311+                 std::string str_ip = NetHelper::ipv4_to_string_addr  (ip);
312312                ips.emplace_back (std::move (str_ip));
313313                index_inner += 4 ;
314314            }
@@ -319,27 +319,27 @@ class DNSQuery final : public SimpleUdpClient
319319        return  ret;
320320    }
321321
322-     bool   analyze_queries ( int  &index,  const  std::string &str_respones, std::string &str_host)
322+     int  getName ( const  std::string &str_respones,  int  index_inner , std::string &str_host)
323323    {
324-         bool  ret = false ;
325-         int  index_inner = index;
326-         str_host.clear ();
327-         FUNCTION_BEGIN ;
324+         int  jump = 0 ;
325+         while  (str_respones[index_inner] != 0 )
326+         {
328327            if  (!check_index (index_inner, str_respones))
329328            {
330-                 FUNCTION_LEAVE;
329+                 index_inner = str_respones.size ();
330+                 break ;
331331            }
332- 
333-             int  jump = 0 ;
334-             while  (str_respones[index_inner] != 0 )
332+             if  (jump == 0 )
335333            {
336-                 if  (! check_index (index_inner, str_respones)) 
337-                 { 
338-                      break ; 
339-                 } 
340-                 if  (jump ==  0 ) 
334+                 jump = ( unsigned   char ) str_respones[index_inner++]; 
335+             } 
336+             else 
337+             { 
338+                 if  (jump &  0xc0 )  // encounter a pointer 
341339                {
342-                     jump = (unsigned  char ) str_respones[index_inner++];
340+                     // just move on
341+                     ++index_inner;
342+                     jump = 0 ;
343343                }
344344                else 
345345                {
@@ -352,11 +352,37 @@ class DNSQuery final : public SimpleUdpClient
352352                    jump = 0 ;
353353                }
354354            }
355-             if  (str_host.size ())
355+         }
356+         if  (str_host.size ())
357+         {
358+             str_host.erase (str_host.size () - 1 );
359+         }
360+         ++index_inner; // jump '\0'
361+         return  index_inner;
362+     }
363+ 
364+     bool  analyze_queries (int  &index, const  std::string &str_respones, std::string &str_host)
365+     {
366+         bool  ret = false ;
367+         int  index_inner = index;
368+         str_host.clear ();
369+         FUNCTION_BEGIN ;
370+             if  (!check_index (index_inner, str_respones))
371+             {
372+                 FUNCTION_LEAVE;
373+             }
374+ 
375+             if  (str_respones[index_inner] & 0xc0 ) // name is a pointer
356376            {
357-                 str_host.erase (str_host.size () - 1 );
377+                 int  index_pointer = (((~0xc0  & 0xff ) & str_respones[index_inner]) << 8 ) |
378+                         (str_respones[index_inner + 1 ] & 0xff );
379+                 getName (str_respones, index_pointer, str_host);
380+                 index_inner += 2 ;
381+             }
382+             else 
383+             { // name can be labels combines pointer, which is not handled
384+                 index_inner = getName (str_respones, index_inner, str_host);
358385            }
359-             ++index_inner; // jump '\0'
360386
361387            if  (!check_index (index_inner + 4 , str_respones))
362388            {
@@ -472,7 +498,9 @@ class DNSQuery final : public SimpleUdpClient
472498
473499    bool  check_index (int  index, const  std::string &respones)
474500    {
475-         if  (index >= respones.size ())
501+         // 如果index溢出,index将变为一个负数,但是respones.size()返回的是一个size_t(unsigned int),比较会隐式转换
502+         // 将index这个int提升为unsigned int, 这时会是一个巨大的数。从而可能带来意外的结果。这里不判断index>=0,结果也是正确的。
503+         if  (index >= 0  && index >= respones.size ())
476504        {
477505            return  false ;
478506        }
0 commit comments