【问题标题】:Non-Blocking socket and poll() quirks in proxy代理中的非阻塞套接字和 poll() 怪癖
【发布时间】:2011-09-02 18:24:35
【问题描述】:

感谢这里的帮助,我得到了一个基于 poll() 的大部分工作的 socks4 代理。我正在使用这个程序来学习 C 和套接字编程。该程序仍然缺少部分写入的 send() 检查,但我相当确定这不是当前问题的原因。它可以与一些代理的 TCP 连接一起工作,例如 netcat 甚至 chrome(尽管这会引发过多的 recv() 错误)。它不适用于 Firefox 或 ssh。我不确定有什么区别,但我认为我必须以某种方式错误地处理套接字。该错误目前在非工作客户端中出现:

  1. 客户端向代理发送 socks4 连接请求(OK)
  2. 代理向客户端发送 socks4 OK (OK)
  3. 客户端将带有有效负载数据的 TCP 段发送到代理 (OK)
  4. 代理将带有有效负载数据的 TCP 段转发到服务器 (OK)
  5. 服务器将带有有效负载数据的 TCP 段发送到代理 (OK)
  6. 代理向客户端发送带有有效负载的 TCP 段 (OK)
  7. 客户端什么都不做(不正常)

我仔细检查了 tcp-segment 有效负载,它对于来自代理的 tcp 段和直接来自服务器的连接完全相同。例如,一个 ssh 客户端应该在收到一个 ssh 服务器 hello 后向服务器发送一个 ssh 客户端 hello。

如果我通过 netcat 连接(作为客户端),我可以验证我是否正确获取了横幅:

root@ubuntu# nc -X 4 -x 127.0.0.1:8000 127.0.0.1 22

SSH-2.0-OpenSSH_5.3p1 Debian-3ubuntu7

我在 wireshark 中看到 ssh 客户端也得到了这个横幅,但他什么也没做。 SSL 连接和 firefox 中的普通连接也会发生相同的行为(firefox 告诉我是否要保存“二进制数据”)。我的代码可能有问题:

#include <stdio.h>      
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>    
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <errno.h>

#define RCVBUFSIZE 1000000


void ProxyData(int rcvSocket)
{
   char rcvBuffer[RCVBUFSIZE];
   char sndBuffer[RCVBUFSIZE];
   int recvMsgSize;
   int sndMsgSize;
   char Socks4Response[] = "\x00\x5a\x00\x00\x00\x00\x00\x00";

   int errno;

   int dstSocket;
   struct sockaddr_in dstAddr;

   struct pollfd fds[2];
   int timeout_msecs = 67000; /* dont use this yet */ 

   /* Receive message from client */
   if ((recvMsgSize = recv(rcvSocket, rcvBuffer, RCVBUFSIZE, 0)) < 0)
   {
      perror("recv() failed");
      close(rcvSocket);
      exit;
   }

   /* Send Sock 4 Response... this is not robust ofc... */
   if ((rcvBuffer[0] == 0x04))
      if (send(rcvSocket, Socks4Response, 9, 0) < 0)
      {
         perror("send() failed");
         close (rcvSocket);
         exit;
      }

   /* todo implement socks error responsees*/


   /* setting up the destination socket for the socks request */
   if((dstSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
   {        
      perror("socket() failed");
      close(rcvSocket);
      exit;
   } 

   memset(&dstAddr, 0, sizeof(dstAddr));
   dstAddr.sin_family = AF_INET;
   memcpy(&dstAddr.sin_addr.s_addr, &rcvBuffer[4], 4);
   memcpy(&dstAddr.sin_port, &rcvBuffer[2], 2);


   if (connect(dstSocket, (struct sockaddr *) &dstAddr, sizeof(dstAddr)) < 0 )
   {
      perror("connect() failed");
      close(rcvSocket);
      close(dstSocket);
      exit;  
   }

   fds[0].fd = rcvSocket;
   fds[1].fd = dstSocket;
   fds[0].events = POLLIN;
   fds[1].events = POLLIN;

   signal(SIGPIPE, SIG_IGN);
   fcntl(rcvSocket, F_SETFL, O_NONBLOCK);
   fcntl(dstSocket, F_SETFL, O_NONBLOCK);

   recvMsgSize = 1; /* set this so condition doesnt fail on first while loop */
   sndMsgSize = 1; /* see above */

   while (1)
   {
         poll(fds, 2, -1); /* polling indefinately */

         /* client to server block */

         if ((fds[0].revents & POLLIN) && (recvMsgSize > 0)) /* if data is available and the socket wasnt closed before we read on it */
         {
            recvMsgSize = recv(rcvSocket, rcvBuffer, RCVBUFSIZE, 0);
            if (recvMsgSize > 0)
            {
               sndcheck = send(dstSocket, rcvBuffer, recvMsgSize, 0);
               if (sndcheck < 0)
               {
                  perror("send() dstSocket failed");
                  if ((errno != EINTR) && (errno != EWOULDBLOCK)) break; /* connection failure -> going to close() outside loop*/
               }
            }
            if (recvMsgSize == 0) shutdown(dstSocket, SHUT_WR);
            if (recvMsgSize < 0) 
            {
               perror("recv() rcvSocket failed");
               if ((errno != EINTR) && (errno != EWOULDBLOCK))  break; /*connection failure -> going to close() outside loop*/
            }
         }


         /* server to client block */

         if ((fds[1].revents & POLLIN) && (sndMsgSize > 0)) /* if data is available and the socket wasnt closed before we read on it */
         {   
            sndMsgSize = recv(dstSocket, sndBuffer, RCVBUFSIZE, 0);
            if (sndMsgSize > 0) 
            {
               if (send(rcvSocket, sndBuffer, sndMsgSize, 0) < 0)
               {
                  perror("send() rcvSocket failed");
                  if ((errno != EINTR) && (errno != EWOULDBLOCK)) break; /* connection failure -> going to close() outside loop*/
               }
            }
            if (sndMsgSize == 0) shutdown(rcvSocket, SHUT_WR);
            if (sndMsgSize < 0)
            {
               perror("recv() dstSocket failed");
               if ((errno != EINTR) && (errno != EWOULDBLOCK)) break; /* connection failure -> going to close() outside loop*/
            }
         }   


         if ((sndMsgSize == 0) && (recvMsgSize == 0)) break; /* both sockets shutdowned() cleanly, close() outside loop*/
   }                
   close(rcvSocket);
   close(dstSocket);
}

添加信息:例如,这是来自代理与 ssh 服务器的 TCP 段 - 第一个被客户端忽略并且连接超时,第二个得到 ssh 客户端响应的响应。我很困惑,因为它是完全相同的有效载荷:

0000   00 50 56 c0 00 08 00 0c 29 38 32 d4 08 00 45 00  .PV.....)82...E.
0010   00 4f 4a 21 40 00 40 06 be b4 c0 a8 58 81 c0 a8  .OJ!@.@.....X...
0020   58 01 1f 40 c1 08 a1 29 ff 93 72 1b 9c a6 50 18  X..@...)..r...P.
0030   00 b7 2f 63 00 00 53 53 48 2d 32 2e 30 2d 4f 70  ../c..SSH-2.0-Op
0040   65 6e 53 53 48 5f 35 2e 33 70 31 20 44 65 62 69  enSSH_5.3p1 Debi
0050   61 6e 2d 33 75 62 75 6e 74 75 37 0d 0a           an-3ubuntu7..


0000   00 50 56 c0 00 08 00 0c 29 38 32 d4 08 00 45 00  .PV.....)82...E.
0010   00 4f 2d c9 40 00 40 06 db 0c c0 a8 58 81 c0 a8  .O-.@.@.....X...
0020   58 01 00 16 c1 0a 74 9e bf a4 9b 43 5e c4 50 18  X.....t....C^.P.
0030   00 b7 cf bf 00 00 53 53 48 2d 32 2e 30 2d 4f 70  ......SSH-2.0-Op
0040   65 6e 53 53 48 5f 35 2e 33 70 31 20 44 65 62 69  enSSH_5.3p1 Debi
0050   61 6e 2d 33 75 62 75 6e 74 75 37 0d 0a           an-3ubuntu7..

【问题讨论】:

    标签: c sockets networking nonblocking


    【解决方案1】:

    正如您所说,您没有处理部分写入。鉴于缓冲区的极端大小,写入过早返回并非不可能。 仅用于测试:尝试将缓冲区大小降低到合理的数量(几百字节)。

    另外,为了让这个东西变得健壮,你真的需要添加一些(循环)缓冲代码。

    【讨论】:

    • 将其更改为 100 字节,行为相同。我还在wireshark中检查了数据实际上是通过网络发送的,如果写入只是部分的,不应该只有部分有效载荷吗?
    • 我是个白痴。 socks 指定了 8 字节响应,但我这样做了 (send(rcvSocket, Socks4Response, 9, 0)
    • 顺便说一句:“更改缓冲区大小”技巧仍然是在开发时追踪隐藏的清单常量的好方法。
    【解决方案2】:

    仅供任何遇到此问题的人参考,错误出在 socks4 协议实现中(响应中的一个字节到多个字节)。

    【讨论】:

    • 啊,我明白了。它被定义为 char thing[] = "...",它会自动添加一个 nul 字节。应该是 thing[8] = "...",这通常会触发编译器警告。 (我不知道 socks 协议)我看到你不使用 sizeof,而是硬编码的 9。不过,你仍然需要缓冲......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多