nc 源代码分析(1)【nc用作聊天工具】

来源:互联网 发布:关口知宏 零 编辑:程序博客网 时间:2024/05/22 05:18


客户端:nc ip port

read stdin write fd

服务端: nc -l -p port

read fd write stdout

客户端和服务端都是用的nc.exe都要连接既doconnect

然后得到一个句柄netfd,

所不同的是服务端和客户端在执行readwrite的时候有所区别!



read fd write stdout

/* main :
   now we pull it all together... */
main (argc, argv)
  int argc;
  char ** argv;
{

...

      netfd = doconnect(themaddr, curport, ouraddr, ourport);

Debug (("netfd %d from port %d to port %d", netfd, ourport, curport))
      if (netfd > 0)
    if (o_zero && o_udpmode)    /* if UDP scanning... */
      netfd = udptest (netfd, themaddr);
      if (netfd > 0) {            /* Yow, are we OPEN YET?! */
        x = 0;                /* pre-exit status */
        holler ("%s [%s] %d (%s) open",
          whereto->name, whereto->addrs[0], curport, portpoop->name);
#ifdef GAPING_SECURITY_HOLE
        if (pr00gie)            /* exec is valid for outbound, too */
    doexec (netfd);
#endif /* GAPING_SECURITY_HOLE */
    if (! o_zero)
#ifdef WIN32
#ifdef GAPING_SECURITY_HOLE
    if (!pr00gie)  // doexec does the read/write for win32
#endif
#endif
      x = readwrite (netfd);    /* go shovel shit */


...


  if (Single)
    exit (x);            /* give us status on one connection */
  exit (0);            /* otherwise, we're just done */
  return(0);
} /* main */



原创粉丝点击