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?
<br><br>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?<br><br>byte buffer[2048];
<br>int main(int argc, char **argv)<br>{<br> WSADATA wsa_data;<br> if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0)<br> {<br> cout << "WSAStartup() failed\n";<br> return 0;<br> }
<br> socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP);<br> sockaddr_in addrLocal;<br> addrLocal.sin_family = AF_INET;<br> addrLocal.sin_addr.s_addr = inet_addr("<a href="http://127.0.0.1">127.0.0.1</a>");<br>
addrLocal.sin_port = htons(3390);<br> <br> if (bind(socket, (sockaddr *)&addrLocal, sizeof(sockaddr_in)) == SOCKET_ERROR)<br> cout << "error bind" << endl;<br><br> listen(socket, 10);<br>
while (true)<br> {<br> sockaddr_in addr;<br> int address_length = sizeof(addr), connection;<br> while ((connection = accept(g_socket, (sockaddr *)&addr, &address_length)) < 0);<br> cout << "connected";
<br> while (true)<br> {<br> int length = recvfrom(connection, (char *)buffer, 2048, 0, (sockaddr *)&addr, &address_length);<br> // int length = recv(connection, (char *)buffer, 2048, 0);<br>
cout << length << endl;<br> }<br> }<br> return 0;<br>}<br><br>Any ideas about how to do what I want or alternatives? Thanks for any help.<br>