【发布时间】:2011-09-02 18:24:35
【问题描述】:
感谢这里的帮助,我得到了一个基于 poll() 的大部分工作的 socks4 代理。我正在使用这个程序来学习 C 和套接字编程。该程序仍然缺少部分写入的 send() 检查,但我相当确定这不是当前问题的原因。它可以与一些代理的 TCP 连接一起工作,例如 netcat 甚至 chrome(尽管这会引发过多的 recv() 错误)。它不适用于 Firefox 或 ssh。我不确定有什么区别,但我认为我必须以某种方式错误地处理套接字。该错误目前在非工作客户端中出现:
- 客户端向代理发送 socks4 连接请求(OK)
- 代理向客户端发送 socks4 OK (OK)
- 客户端将带有有效负载数据的 TCP 段发送到代理 (OK)
- 代理将带有有效负载数据的 TCP 段转发到服务器 (OK)
- 服务器将带有有效负载数据的 TCP 段发送到代理 (OK)
- 代理向客户端发送带有有效负载的 TCP 段 (OK)
- 客户端什么都不做(不正常)
我仔细检查了 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