其他分享
首页 > 其他分享> > 内核陆地套接字连接超时

内核陆地套接字连接超时

作者:互联网

再次,我需要你的内核模块的帮助.我已经实现了一个客户端模块,但每次连接函数都返回errno = 110(超时).我不明白为什么?为了测试这个模块,我在监听模式下启动了netcat(nc -vl -p4242).你对这个神秘的bug有什么建议吗?

int             init_module(void)
{
  struct socket*        sock = NULL;
  struct sockaddr_in*   dest = {0};
  int                   retVal = 0;

  dest = (struct sockaddr_in*)kmalloc(sizeof(struct sockaddr_in), GFP_KERNEL);
  sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  dest->sin_family = AF_INET;
  dest->sin_addr.s_addr = htonl(in_aton("127.0.0.1"));
  dest->sin_port = htons(4242);
  printk(KERN_EMERG "Connect to %X:%u\n", dest->sin_addr.s_addr, 4242);

  retVal = sock->ops->connect(sock, (struct sockaddr*)dest, sizeof(struct sockaddr_in), !O_NONBLOCK);
  if (retVal >= 0) {
    printk(KERN_EMERG "Connected\n");
    sock_release(sock);
  }
  else
    printk(KERN_EMERG "Error %d\n", -retVal);

  return (0);
}

调试输出(dmesg)

Message from syslogd@century at Sun Jun 19 08:41:33 2011 ...
century kernel: Connect to 7F000001:4242

Message from syslogd@century at Sun Jun 19 08:41:54 2011 ...
century kernel: Error 110

错误代码110是“超时”.

解决方法:

in_aton( “127.0.0.1”);已经按网络字节顺序给出了地址.

通过做htonl(in_aton(“127.0.0.1”));你实际上是想连接到1.0.0.127

标签:c-3,linux,sockets,linux-kernel,kernel-module
来源: https://codeday.me/bug/20190903/1796462.html