Changeset 18781

Show
Ignore:
Timestamp:
04/27/06 16:18:45 (3 years ago)
Author:
patrick
Message:

Improve the code that uses inet_ntoa(3) or inet_ntop(3) by using
std::ostringstream. This eliminates a Visual C++ 8 compiler warning
about strcpy(3) being deprecated.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • juggler/trunk/modules/vapor/vpr/md/common/InetAddrHelpers.cpp

    r18451 r18781  
    324324      } 
    325325 
     326      std::ostringstream addr_stream; 
     327 
     328#if defined(VPR_OS_Windows) 
     329      // inet_ntoa() returns a pointer to static memory, which means that 
     330      // it is not reentrant. Unfortunately, WinSock2 does not provide 
     331      // inet_ntop(). The memory returned is guaranteed to be valid until 
     332      // the next socket call in this thread, so there should not be a 
     333      // race condition here. 
     334      const char* temp_addr = inet_ntoa(addr->sin_addr); 
     335 
     336      if ( NULL != temp_addr ) 
     337      { 
     338         addr_stream << temp_addr; 
     339      } 
     340#else 
    326341      char netaddr[18]; 
    327 #if defined(VPR_OS_Windows) 
    328       // inet_ntoa() returns a pointer to static memory, which means that 
    329       // it is not reentrant. There is a race condition here between the 
    330       // time of the return from inet_ntoa() and the time that strcpy() 
    331       // actually copies the memory. Unfortunately, WinSock2 does not 
    332       // provide inet_ntop(). 
    333       strcpy(netaddr, inet_ntoa(addr->sin_addr)); 
    334       char* temp_addr = netaddr; 
    335 #else 
    336342      const char* temp_addr = inet_ntop(addr->sin_family, &addr->sin_addr, 
    337343                                        netaddr, sizeof(netaddr)); 
    338 #endif 
    339344 
    340345      if ( NULL != temp_addr ) 
    341346      { 
     347         addr_stream << netaddr; 
     348      } 
     349#endif 
     350 
     351      if ( ! addr_stream.str().empty() ) 
     352      { 
    342353         vpr::InetAddr vpr_addr; 
    343          vpr_addr.setAddress(netaddr, 0); 
     354         vpr_addr.setAddress(addr_stream.str(), 0); 
    344355         host_addrs.push_back(vpr_addr); 
    345356      }