感谢这里的帮助,我得到了一个基于poll()的主要工作的socks4代理。我正在使用这个程序学习C和socket编程。这个程序仍然缺少send()检查部分写,但我相当肯定这不是它当前问题的原因。它在一些代理TCP连接中运行正常,例如netcat甚至chrome (尽管这会抛出过量的recv()错误)。它不适用于firefox或ssh。我不知道有什么区别,但我想我一定是不正确地处理插座。当前,在不工作的客户端中会出现这样的错误:
服务器将包含有效负载数据的TCP段发送给代理(OK)<代码>H 210<代码>H 111/代码>代理将有效负载的TCP段发送给客户端(OK)H 212H 113客户端什么都不做(不确定)><代码>H 214<代码>代码>
我再次检查了tcp段的有效负载,对于来自代理的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-3 ubuntu7
我在wireshark中看到ssh客户端也得到了这个横幅,但他什么也不做。在firefox中,SSL连接和正常连接也会发生同样的行为(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..发布于 2011-09-03 12:02:43
正如您所说,您不是在处理部分写入。考虑到缓冲区的极端大小,写操作不可能过早返回。仅用于测试:尝试将缓冲区大小降到正常大小(几百字节)。
此外,要使这件事变得健壮,您确实需要添加一些(循环)缓冲代码。
发布于 2011-09-03 13:39:25
对于任何绊脚石的人来说,FYI错误是在socks4协议实现中发生的(响应中有一个字节对多个字节)。
https://stackoverflow.com/questions/7287721
复制相似问题