Libjingle一个虽小但却很严重的bug - 误导人的SocketAddress构造函数参数名称

本文分析了Libjingle中的SocketAddress类构造函数存在的问题:当使用主机名而非IP地址作为参数时,inet_addr函数无法正确解析,导致构造的对象无效。此问题可能导致sendto操作失败并返回WSAEADDRNOTAVAIL错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Libjingle+Linphone for Windows的voice call测试中, 遇到了一些问题. 而这些问题的root cause竟然源于Google code的一些小bug. 这里先指出一个.

 

SocketAddress这个类的其中一个构造函数是:

  // Creates the address with the given host and port.  If use_dns is true,
  // the hostname will be immediately resolved to an IP (which may block for
  // several seconds if DNS is not available).  Alternately, set use_dns to
  // false, and then call Resolve() to complete resolution later, or use
  // SetResolvedIP to set the IP explictly.
  SocketAddress(const std::string& hostname, int port);

 这个构造函数的参数看起来很简单, 一般人很少注意上面那堆罗嗦的注释, 第一个参数是hostname, 第二参数是port. 问题就出在hostname上面.

 

那这个构造函数的实现是什么呢?

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

 hostname被传递的顺序是SocketAddress()->SetIP()->StringToIP()->inet_aton()->inet_addr()

 

那我们再来看看inet_addr()的说明, 这是一个系统函数. Refer to:

http://msdn.microsoft.com/en-us/library/ms738563%28v=vs.85%29.aspx

写道
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.

 也就是说inet_addr()函数的参数是一个IP地址字符串, 如"192.168.1.1".

 

而SocketAddress类的构造函数的参数按照字面意思应该是hostname (主机名),  所以如果传入hostname, 最终inet_addr()函数将无法正确解析, 那么构造的对象将会代表一个无效的地址.

 

偶被这个隐藏的小bug捉弄了好几天, 差点放弃Libjingle的测试, 幸亏最终静下心来仔细分析了一下.  如果传入hostname (比如"localhost") 构造这样一个SocketAddress对象, 然后调用SendTo()发送数据, 会返回-1, 系统错误码为WSAEADDRNOTAVAIL (10049) Cannot assign requested address.

写道
被请求的地址在它的环境中是不合法的。通常地在bind()函数试图将一个本地机器不合法的
地址绑扎到套接字时产生。它也可能在connect()、sendto()、WSAConnect()、WSAJoinLeaf()
或WSASendTo()函数调用时因远程机器的远程地址或端口号非法(如0地址或0端口号)而
产生。
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值