为什么 connect() 会在 FreeBSD 的端口上给出间歇性的 EINVAL?

问题描述 投票:0回答:3

我的 C++ 应用程序在从 32 位 Linux 移植到 32 位 FreeBSD 8.1 时出现故障。我有一个 TCP 套接字连接,但无法连接。在调用 connect() 时,我收到错误结果 errno == EINVAL,connect() 的手册页未涵盖该错误结果。

这个错误是什么意思,哪个参数无效?该消息只是说:“无效参数”。

以下是连接的一些详细信息:

family: AF_INET
len: 16
port: 2357
addr: 10.34.49.13

但它并不总是失败。 FreeBSD 版本只有在让机器闲置几个小时后才会出现故障。但失败一次后,它会可靠地工作,直到您再次让它长时间闲置。

这是一些代码:

void setSocketOptions(const int skt);
void buildAddr(sockaddr_in &addr, const std::string &ip,
               const ushort port);
void deepBind(const int skt, const sockaddr_in &addr);


void
test(const std::string &localHost, const std::string &remoteHost,
     const ushort localPort, const ushort remotePort,
     sockaddr_in &localTCPAddr, sockaddr_in &remoteTCPAddr)
{
  const int skt = socket(AF_INET, SOCK_STREAM, 0);

  if (0 > skt) {
    clog << "Failed to create socket: (errno " << errno
         << ") " << strerror(errno) << endl;
    throw;
  }

  setSocketOptions(skt);

  // Build the localIp address and bind it to the feedback socket.  Although
  // it's not traditional for a client to bind the sending socket to a the
  // local address, we do it to prevent connect() from using an ephemeral port
  // which (our site's firewall may block).  Also build the remoteIp address.
  buildAddr(localTCPAddr, localHost, localPort);
  deepBind(skt, localTCPAddr);
  buildAddr(remoteTCPAddr, remoteHost, remotePort);

  clog << "Info: Command connect family: "
       << (remoteTCPAddr.sin_family == AF_INET ? "AF_INET" : "<unknown>")
       << " len: " << int(remoteTCPAddr.sin_len)
       << " port: " << ntohs(remoteTCPAddr.sin_port)
       << " addr: " << inet_ntoa(remoteTCPAddr.sin_addr) << endl;

  if (0 > ::connect(skt, (sockaddr*)& remoteTCPAddr, sizeof(sockaddr_in)))) {
    switch (errno) {
      case EINVAL: {
        int value = -1;
        socklen_t len = sizeof(value);
        getsockopt(skt, SOL_SOCKET, SO_ERROR, &value, &len);

        cerr << "Error: Command connect failed on local port "
             << getLocFbPort()
             << " and remote port " << remotePort
             << " to remote host '" << remoteHost
             << "' family: "
             << (remoteTCPAddr.sin_family == AF_INET ? "AF_INET" : "<unknown>")
             << " len: " << int(remoteTCPAddr.sin_len)
             << " port: " << ntohs(remoteTCPAddr.sin_port)
             << " addr: " << inet_ntoa(remoteTCPAddr.sin_addr)
             << ": Invalid argument." << endl;
        cerr << "\tgetsockopt => "
             << ((value != 0) ? strerror(value): "success") << endl;

        throw;
      }
      default: {

        cerr << "Error: Command connect failed on local port "
             << localPort << " and remote port " << remotePort
             << ": (errno " << errno << ") " << strerror(errno) << endl;
        throw;
      }
    }
  }
}


void
setSocketOptions(int skt)
{
  // See page 192 of UNIX Network Programming: The Sockets Networking API
  // Volume 1, Third Edition by W. Richard Stevens et. al. for info on using
  // ::setsockopt().

  // According to "Linux Socket Programming by Example" p. 319, we must call
  // setsockopt w/ SO_REUSEADDR option BEFORE calling bind.
  int so_reuseaddr = 1; // Enabled.
  int reuseAddrResult
    = ::setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr,
                   sizeof(so_reuseaddr));

  if (reuseAddrResult != 0) {
    cerr << "Failed to set reuse addr on socket.";
    throw;
  }

  // For every two hours of inactivity, a keepalive occurs.
  int so_keepalive = 1; // Enabled.  See page 200 for info on SO_KEEPALIVE.
  int keepAliveResult =
    ::setsockopt(skt, SOL_SOCKET, SO_KEEPALIVE, &so_keepalive,
                 sizeof(so_keepalive));

  if (keepAliveResult != 0) {
    cerr << "Failed to set keep alive on socket.";
    throw;
  }

  struct linger so_linger;

  so_linger.l_onoff = 1;  // Turn linger option on.
  so_linger.l_linger = 5; // Linger time in seconds. (See page 202)

  int lingerResult
    = ::setsockopt(skt, SOL_SOCKET, SO_LINGER, &so_linger,
                   sizeof(so_linger));

  if (lingerResult != 0) {
    cerr << "Failed to set linger on socket.";
    throw;
  }

  // Disable the Nagel algorithm on the command channel.  SOL_TCP is not
  // defined on FreeBSD
#ifndef SOL_TCP
#define SOL_TCP (::getprotobyname("TCP")->p_proto)
#endif

  unsigned int tcpNoDelay = 1;
  int noDelayResult
    = ::setsockopt(skt, SOL_TCP, TCP_NODELAY, &tcpNoDelay,
                   sizeof(tcpNoDelay));

  if (noDelayResult != 0) {
    cerr << "Failed to set tcp no delay on socket.";
    throw;
  }
}

void
buildAddr(sockaddr_in &addr, const std::string &ip, const ushort port)
{
  memset(&addr, 0, sizeof(sockaddr_in)); // Clear all fields.
  addr.sin_len    = sizeof(sockaddr_in);
  addr.sin_family = AF_INET;             // Set the address family
  addr.sin_port   = htons(port);         // Set the port.

  if (0 == inet_aton(ip.c_str(), &addr.sin_addr)) {
    cerr << "BuildAddr IP.";
    throw;
  }
};

void
deepBind(const int skt, const sockaddr_in &addr)
{
  // Bind the requested port.
  if (0 <= ::bind(skt, (sockaddr *)&addr, sizeof(addr))) {
    return;
  }

  // If the port is already in use, wait up to 100 seconds.
  int count = 0;
  ushort port = ntohs(addr.sin_port);

  while ((errno == EADDRINUSE) && (count < 10)) {
    clog << "Waiting for port " << port << " to become available..."
         << endl;
    ::sleep(10);
    ++count;
    if (0 <= ::bind(skt, (sockaddr*)&addr, sizeof(addr))) {
      return;
    }
  }

  cerr << "Error: failed to bind port.";
  throw;
}

这是 EINVAL 时的示例输出(这里并不总是失败,有时它会成功,但在通过套接字发送的第一个数据包被扰乱时会失败):

Info: Command connect family: AF_INET len: 16 port: 2357 addr: 10.34.49.13
Error: Command connect failed on local port 2355 and remote port 2357 to remote host '10.34.49.13' family: AF_INET len: 16 port: 2357 addr: 10.34.49.13: Invalid argument.
    getsockopt => success
sockets tcp freebsd porting
3个回答
6
投票

我弄清楚了问题所在,我首先得到了 ECONNREFUSED,在 Linux 上,我可以在短暂的暂停后重试 connect() ,一切都很好,但在 FreeBSD 上,以下重试 connect() 失败,并显示 EINVAL .

解决方案是当 ECONNREFUSED 进一步备份时,而是开始重试回到上面 test() 定义的开头。经过此更改,代码现在可以正常工作了。


3
投票

有趣的是 FreeBSD connect() 手册页没有列出

EINVAL
不同的 BSD 手册页指出:

[EINVAL]    An invalid argument was detected (e.g., address_len is
            not valid for the address family, the specified
            address family is invalid).

根据不同 BSD 风格的不同文档,我敢说 FreeBSD 中可能存在未记录的返回代码可能性,请参阅 here 例如。

我的建议是在调用

sizeof
之前打印出您的地址长度和
connect
以及套接字地址结构的内容 - 这有望帮助您找出问题所在。

除此之外,最好向我们展示您用于设置连接的代码。这包括用于套接字地址的类型(

struct sockaddr
struct sockaddr_in
等)、初始化它的代码以及对
connect
的实际调用。这将使帮助变得更加容易。


1
投票

本地地址是什么?您默默地忽略了来自

bind(2)
的错误,这看起来不仅是不好的形式,而且可能会导致此问题开始!

© www.soinside.com 2019 - 2024. All rights reserved.