

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Goal: Long-distance links tunneled through TCP.
DUE: Friday, April 28. The late deadline will be Monday, April 31; you have only one late deadline to play with.
We’re going to tunnel 417-net packets through TCP. We’ll use TCP to get long-distance links that would support routing protocols (PA5); there’s no sense building a routing protocol on top of the current multicast scheme. Your implementation should extend previous assignments (support the print neighbor table and sendmsg commands, limit message transmission rate) with a new command “connect” that takes an IP address and port, then treats the connection as a neighbor.
One end of the tunnel will listen; allow the kernel to choose a port, then print whatever it has chosen for you in the call to bind. (socket, bind, listen, (select), accept). Print the port the kernel has chosen for you using:
printf("listening on port: %d\n", ntohs(sin.sin_port));
The listening socket can accept in a non-blocking mode if the listening socket is passed to select() as if it were for reading, then, when ready, invokes accept (instead of read). You’ll never read() off the listening socket. After you’ve accepted a TCP socket, send periodic hello messages, as in PA2, across the newly-accepted TCP connection as well.
The connect command should be parsed as follows:
struct sockaddr_in sin; sscanf(command, "connect %d.%d.%d.%d:%d", &a1, &a2, &a3, &a4, &port); sin.sin_addr.s_addr = htonl((a1<<24)+(a2<<16)+(a3<<8)+a4); sin.sin_port = htons(port);
The connect system call can also be made non-blocking; it’s tricky, but very fun to setsockopt O NONBLOCK, then finish the connection setup when, you guessed it, select returns that the socket is ready for reading. Good stuff, but extra fun for you with no credit at all. After you’ve connected, send periodic hello messages, as in PA2, across the TCP connection as well. Upon receiving the first hello message from the TCP connection, print the source address of the hello message as follows:
printf("new neighbor: %u\n", ntohl(received_packet.header.source_address));
If you read zero bytes from the TCP connection, tear down the adjacency: remove the neighbor from the list of neighbors, send no more hello packets. If the periodic write returns -1, (especially if errno is EBADF, but reasonable for all other errors too), remove the neighbor, close the socket, and send no more hello packets. If no messages are received in two minutes, close the connection, remove the neighbor, and send no more hello packets. You may implement a “close” command if you like; doesn’t matter to me.
Expect to update your neighbor table with one field: a TCP socket number used to reach that neighbor, if necessary. Although socket zero is valid and socket −1 is not, you may choose either to be the “invalid” or “not a tcp neighbor” indication because socket zero is deterministically stdin and you’ll not write to stdin. To construct the rdfds mask for select, include all that you currently do, then find additional sockets by iterating through the neighbor table. It’s not ideal—best is to have a separate list pairing socket descriptors with function pointers (callbacks) to invoke when the socket is ready for reading—but works.
Sendmsg must work over the new TCP adjacency. Please run sendmsg through the same rate-limiting as for other neighbors even though it is less important to do so over TCP. Ensure that the destination address in sendmsg packets is set correctly. If so, the only needed parameter to the callback is the packet to be sent—the sending callback can find the address of the neighbor by consulting the neighbor table.