Search code examples
clinuxsocketstcpraw-sockets

Regarding implementing a new transport protocol


I am trying to implement my own Transport Layer Protocol like TCP which will be used by some application, on top of network layer using raw sockets API in Linux. I am working on Ubuntu 14.04.

I have been able to send and receive packets.

Now in the part of implementing the Transport Protocol, I am looking forward to write some functions like

connect(int sockfd) - To establish connection to the server.

send_data(int sockfd, char* data) - To send data

receive_data(int sockfd, char* data) - To receive data

close(int sockfd) - close the connection

Also since I am trying to implement protocol like TCP, to keep the protocol reliable I want to send acknowledgement for each received data packet. I have made my own TCP like header as follows

typedef struct rtlp_hdr
{
    int         checksum; 
    short int   src_port;  //defined by us
    short int   des_port;  //defined by us
    int         seq_no;
    int         ack_no;

}rtlp_hdr;

Now in the implementation of the send_data function after I send a data packet I wait to receive the acknowledgement for the next data packet for given time, and if I don't receive any ack or I receive a corrupted ack( after checking the checksum) I resend the data. I am facing problems in creating the corresponding receive_data function for the same, like how would I know that the ack sent for the received data has been successfully delivered to the sender, since there is no ack for ack.

If anyone has any ideas what can I do or if I am going in the wrong direction please correct me. thanks in advance.

I have already written code for the connect(int sockfd) using 3-way handshaking that is working fine, I can share that.


Solution

  • As mentioned, there is no way to guarantee that a message arrives at the destination. If i understand your question right i hope the simple example below can help you.

    You have a client A, and a server B. Client A sends a packet named A1 to B. B saves the name of the last received packet, and replies to A with an ack.

    If the ack makes it to the client it sends the next packet, named A2.

    If, however, the ack is lost, the client resends the data named A1 after a while. When the server receives A1 a second time (using the saved name), it can assume that the ack was lost. The server then resends the ack, hoping that it will make it to the client this time. This continues as many times as neccessary.

    As you see, the server does not need to know if the ack has been delivered to the client. The receiving of a duplicate packet tells the server that the ack was lost. (ignoring spurious timeouts for simplicity)