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

时间:2010-10-27 01:23:45

标签: sockets tcp porting freebsd connect

我的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

3 个答案:

答案 0 :(得分:6)

我弄清楚问题是什么,我第一次得到一个ECONNREFUSED,在Linux上我可以在短暂停顿后重试connect()并且一切都很好,但在FreeBSD上,以下的connect()重试失败与EINVAL。

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

答案 1 :(得分:3)

有趣的是FreeBSD connect() manpage没有列出EINVALA different BSD manpage州:

[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 sockaddrstruct sockaddr_in等),初始化它的代码以及对connect的实际调用。这将使协助变得更容易。

答案 2 :(得分:1)

当地的地址是什么?你是默默地忽略来自bind(2)的错误,这似乎不仅形式不好,而且可能导致这个问题开始!