zf8848 / libjingle

Automatically exported from code.google.com/p/libjingle
0 stars 0 forks source link

The confusing construtor parameter name of SocketAddress #141

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I failed for a long time because of this litte but serious bug about the 
construtor parameter name of SocketAddress.

One of the constrtors of SocketAddress is:
SocketAddress(const std::string& hostname, int port); 

Seems the 1st parameter should be passed as a hostname. 

But refer to the flow of this constructor:
# SocketAddress::SocketAddress(const std::string& hostname, int port) {  
#   SetIP(hostname);  
#   SetPort(port);  
# }  
#   
# void SocketAddress::SetIP(const std::string& hostname) {  
#   hostname_ = hostname;  
#   ip_ = StringToIP(hostname);  
# }  
#   
# uint32 SocketAddress::StringToIP(const std::string& hostname) {  
#   uint32 ip = 0;  
#   StringToIP(hostname, &ip);  
#   return ip;  
# }  
#   
# bool SocketAddress::StringToIP(const std::string& hostname, uint32* ip) {  
#   in_addr addr;  
#   if (inet_aton(hostname.c_str(), &addr) == 0)  
#     return false;  
#   *ip = NetworkToHost32(addr.s_addr);  
#   return true;  
# }  
#   
# #ifdef WIN32  
# // Win32 doesn't provide inet_aton, so we add our own version here.  
# // Since inet_addr returns 0xFFFFFFFF on error, if we get this value  
# // we need to test the input to see if the address really was 
255.255.255.255.  
# // This is slightly fragile, but better than doing nothing.  
# int inet_aton(const char* cp, struct in_addr* inp) {  
#   inp->s_addr = inet_addr(cp);  
#   return (inp->s_addr == INADDR_NONE &&  
#           strcmp(cp, "255.255.255.255") != 0) ? 0 : 1;  
# }  
# #endif  // WIN32 

Eventually, the hostname will be passed into the inet_addr(). And the 
definition of this function is:
The inet_addr function converts a string containing an IPv4 dotted-decimal 
address into a proper address for the IN_ADDR structure.
Parameters

cp [in]

A NULL-terminated character string representing a number expressed in the 
Internet standard ".'' (dotted) notation.

So if you pass a hostname to SocketAddress, the inet_addr() cannot parse it 
successfully, and the SocketAddress object will represent an invalid address. 
And if you use this object, usually you will come across with the error: 
WSAEADDRNOTAVAIL (10049) Cannot assign requested address.

Original issue reported on code.google.com by zhangsan...@gmail.com on 19 Feb 2011 at 3:54

GoogleCodeExporter commented 9 years ago
So you'd better pass the 1st parameter with ip address string.

Original comment by haojies...@gmail.com on 19 Feb 2011 at 4:11

GoogleCodeExporter commented 9 years ago

Original comment by jun...@google.com on 16 Nov 2011 at 11:40

GoogleCodeExporter commented 9 years ago
This is fixed in newer version. The current SetIP function should look like:
void SocketAddress::SetIP(const std::string& hostname) {
  hostname_ = hostname;
  literal_ = IPFromString(hostname, &ip_);
  if (!literal_) {
    ip_ = IPAddress(INADDR_ANY);
  }
}

Original comment by jun...@google.com on 17 Nov 2011 at 12:26