// // FILE: seg.h // Description: This file contains segment definitions and interfaces to send and receive segments. // The prototypes support snp_sendseg() and snp_rcvseg() for sending to the network layer. // // Date: April 18, 2008 // April 21, 2008 **Added more detailed description of prototypes fixed ambiguities** ATC // April 26, 2008 **Added checksum descriptions** // #ifndef SEG_H #define SEG_H #include "constants.h" //Segment type definition. Used by SRT. #define SYN 0 #define SYNACK 1 #define FIN 2 #define FINACK 3 #define DATA 4 #define DATAACK 5 //segment header definition. typedef struct srt_hdr { unsigned int src_port; //source port number unsigned int dest_port; //destination port number unsigned int seq_num; //sequence number unsigned int ack_num; //ack number unsigned short int length; //segment data length unsigned short int type; //segment type unsigned short int rcv_win; //currently not used unsigned short int checksum; //checksum for this segment } srt_hdr_t; //segment definition typedef struct segment { srt_hdr_t header; char data[MAX_SEG_LEN]; } seg_t; //This is the data structure exchanged between the SNP process and the SRT process. //It contains a node ID and a segment. //For snp_sendseg(), the node ID is the destination node ID of the segment. //For snp_recvseg(), the node ID is the source node ID of the segment. typedef struct sendsegargument { int nodeID; //node ID seg_t seg; //a segment } sendseg_arg_t; //SRT process uses this function to send a segment and its destination node ID in a sendseg_arg_t structure to SNP process to send out. //Parameter network_conn is the TCP descriptor of the connection between the SRT process and the SNP process. //Return 1 if a sendseg_arg_t is succefully sent, otherwise return -1. int snp_sendseg(int network_conn, int dest_nodeID, seg_t* segPtr); //SRT process uses this function to receive a sendseg_arg_t structure which contains a segment and its src node ID from the SNP process. //Parameter network_conn is the TCP descriptor of the connection between the SRT process and the SNP process. //When a segment is received, use seglost to determine if the segment should be discarded, also check the checksum. //Return 1 if a sendseg_arg_t is succefully received, otherwise return -1. int snp_recvseg(int network_conn, int* src_nodeID, seg_t* segPtr); //SNP process uses this function to receive a sendseg_arg_t structure which contains a segment and its destination node ID from the SRT process. //Parameter tran_conn is the TCP descriptor of the connection between the SRT process and the SNP process. //Return 1 if a sendseg_arg_t is succefully received, otherwise return -1. int getsegToSend(int tran_conn, int* dest_nodeID, seg_t* segPtr); //SNP process uses this function to send a sendseg_arg_t structure which contains a segment and its src node ID to the SRT process. //Parameter tran_conn is the TCP descriptor of the connection between the SRT process and the SNP process. //Return 1 if a sendseg_arg_t is succefully sent, otherwise return -1. int forwardsegToSRT(int tran_conn, int src_nodeID, seg_t* segPtr); // for seglost(seg_t* segment): // a segment has PKT_LOST_RATE probability to be lost or invalid checksum // with PKT_LOST_RATE/2 probability, the segment is lost, this function returns 1 // If the segment is not lost, return 0. // Even the segment is not lost, the packet has PKT_LOST_RATE/2 probability to have invalid checksum // We flip a random bit in the segment to create invalid checksum int seglost(seg_t* segPtr); // //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //This function calculates checksum over the given segment. //The checksum is calculated over the segment header and segment data. //You should first clear the checksum field in segment header to be 0. //If the data has odd number of octets, add an 0 octets to calculate checksum. //Use 1s complement for checksum calculation. unsigned short checksum(seg_t* segment); //Check the checksum in the segment, //return 1 if the checksum is valid, //return -1 if the checksum is invalid int checkchecksum(seg_t* segment); #endif