[Winpcap-users] capture tcp packets directly
Yale Zhang
yzhang1985 at gmail.com
Fri Jun 9 19:44:02 GMT 2006
Hi. I'm trying to build a port forwarding program that lets two computers
that are behind NATs (mutually invisible to each other) communicate via a
computer they both can connect to (bridge). To do this, I need to be able to
capture TCP packets directly and send them. Is there a way to capture TCP
packets (either without or with headers) without being superuser on UNIX
like systems or Administrator on Windows?
I tried the following program, but the socket cannot be bound. I suppose
that this is because there is no mechanism receiving TCP packets in the
network stack - only stream communication?
byte buffer[2048];
int main(int argc, char **argv)
{
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0)
{
cout << "WSAStartup() failed\n";
return 0;
}
socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP);
sockaddr_in addrLocal;
addrLocal.sin_family = AF_INET;
addrLocal.sin_addr.s_addr = inet_addr("127.0.0.1");
addrLocal.sin_port = htons(3390);
if (bind(socket, (sockaddr *)&addrLocal, sizeof(sockaddr_in)) ==
SOCKET_ERROR)
cout << "error bind" << endl;
listen(socket, 10);
while (true)
{
sockaddr_in addr;
int address_length = sizeof(addr), connection;
while ((connection = accept(g_socket, (sockaddr *)&addr,
&address_length)) < 0);
cout << "connected";
while (true)
{
int length = recvfrom(connection, (char *)buffer, 2048, 0, (sockaddr
*)&addr, &address_length);
// int length = recv(connection, (char *)buffer, 2048, 0);
cout << length << endl;
}
}
return 0;
}
Any ideas about how to do what I want or alternatives? Thanks for any help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.winpcap.org/pipermail/winpcap-users/attachments/20060609/bb88f839/attachment.htm
More information about the Winpcap-users
mailing list