我试图弄清楚为什么netstat在我的 Mac OS X 计算机上打印“两次获得 4”之类的东西。  
例如:
$ netstat | head -15
Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
[...]
tcp4       0      0  localhost.56892        localhost.26164        ESTABLISHED
got 4 twice
got 16 twice
got 1 twice
got 4 twice
got 4 twice
got 8 twice
该消息由 netstat 的inet.c打印,在该函数protopr中基本上执行以下操作:
- 调用
sysctlbyname(3)使用例如net.inet.tcp.pcblist_n获取多氯联苯列表(我猜?) - 循环遍历结果,收集指向各种结构的指针,其类型称为
xgn_kind xgn_kind它为它看到的每一个设置一个位。如果它两次看到它们中的任何一个,它就会抱怨消息"got %d twice"。
我已经删除了代码的相关部分:
void
protopr(uint32_t proto,   /* for sysctl version we pass proto # */
    char *name, int af)
{
  /* ... */
  if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
    if (errno != ENOENT)
      warn("sysctl: %s", mibvar);
    return;
  }
  /* ... */
  for (next = buf + ROUNDUP64(xig->xig_len);
       next < buf + len;
       next += ROUNDUP64(xgn->xgn_len))
  {
    /* ... */
    if ((which & xgn->xgn_kind) == 0) {
      which |= xgn->xgn_kind;
      /* ... */
    } else {
      printf("got %d twice\n", xgn->xgn_kind);
    }
    /* ... */
}
我的问题是,最后:
为什么会这样?是不是从那里传回了错误的数据sysctl?上面的循环是否不正确,即sysctl每种类型实际上可以返回多个结构?
请注意,我运行的是 Mac OS X —— 代码确实与FreeBSD 的.