/*---------------*/ /* checkShouldBe */ /* *-------------------------------------------------*/ /* Computes a checksum for a packet. This can be used to set the */ /* checksum and also to verify it. It does not use exactly the */ /* same method as the Internet checksum described in Kurose&Ross. */ /* */ /* Author: J. Blustein */ /* Version: 28 July 2003 */ /* */ /* To Set A Checksum (for an outgoing packet) */ /* `packet' must have the correct data (for sequence number, ACK, */ /* message data to be passed to the other host) stored in it. */ /* This function will return the value that should be placed */ /* in the checksum field. */ /* */ /* To Verify A Checksum (for an incoming packet) */ /* `packet' should not be changed since it arrived from the other */ /* host. This function will return what the proper checksum */ /* should be. To see if it the checksum is okay or not compare */ /* the value returned by this function with the checksum stored */ /* in the checksum field: */ /* if (checkShouldBe(packet) == pkt_check(packet)) { */ /* // the packet is probably okay */ /* } else { */ /* // the packet is not okay */ /* } */ /* */ /* Note: */ /* this function does not compute the Internet checksum. That */ /* method uses bit twiddling and is too complex for a simple */ /* simulation like this assignment. If however you are */ /* interested in how to twiddle bits in C then check out */ /* Dr. Dobb's Journal for August 1995 and the code at . */ /*-----------------------------------------------------------------*/ int checkShouldBe(packet_t packet) { char * data; int checksum; data = pkt_data(packet); checksum = 0; while(NULL != data && '\0' != *data) { checksum += (int)*data; data++; } checksum += pkt_ack(packet) + pkt_seq(packet); checksum = ~checksum; return checksum; } /* checkShouldBe() */ /* EOF for checksum.c */