Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!cbatt!decuac!cvl!umd5!hans From: hans@umd5.UUCP Newsgroups: comp.dcom.lans Subject: Re: Internet Protocol header checksum implementation problem Message-ID: <1522@umd5.umd.edu> Date: Wed, 8-Apr-87 14:38:40 EST Article-I.D.: umd5.1522 Posted: Wed Apr 8 14:38:40 1987 Date-Received: Sat, 11-Apr-87 06:54:40 EST References: <328@dcl-csvax.comp.lancs.ac.uk> Reply-To: hans@umd5.umd.edu (Hans Breitenlohner) Distribution: world Organization: University of Maryland, College Park Lines: 39 In article <328@dcl-csvax.comp.lancs.ac.uk> david@comp.lancs.ac.uk (David Coffield) writes: > >INTERNET HEADER CHECKSUM > >I am having a no doubt trivial problem calculating the header checksum >within an internet packet and would be grateful if someone could enlighten >me as to my mistake. > > ... > > >I have applied this procedure to packets taken directly from the network, >the final checksum is nothing like what it should be, however, the value >before taking the final one's complement is sometimes 7 sometimes 8 lower >than what is in the checksum field of the packet read. > >Any clues? Thanks. >-- You are probably using twos complement addition instead of one's complement addition when computing the sum. The difference lies in what happens to the carry when doing addition: in two's complement it is ignored, in one's com- plement it is added to the sum. For example the sum of the 16-bit octal numbers 07776 and 3 works as follows: Two's complement: 07776 is -2, -2 plus 3 gives 1 07776 + 00003 = 00001 plus carry (which gets ignored) One's complement: 07776 is -1, -1 plus 3 gives 2 07776 + 00003 = 00001 plus carry 00001 + carry = 00002 If you are working on a 32-bit machine, you can just add all the numbers, and then add the two 16-bit fields of the result (plus any carry you get from that addition). There is an additional problem with simulating one's complement in this fashion: If the result is zero, it will usually be minus zero (i.e. 07777) and not plus zero. One's complement hardware usually works by subtracting the complement instead of adding, in which case you get minus zero results only if you start out with a minus zero. I don't know if you have to worry about this special case in IP header checksums.