[ Home | Liste | F.A.Q. | Risorse | Cerca... ]


[ Data: precedente | successivo | indice ] [ Argomento: precedente | successivo | indice ]


Archivio: Giugno 2001 ml@sikurezza.org
Soggetto: Re: calcolo checksum IP
Mittente: Bonelli Nicola
Data: 14 Jun 2001 23:38:32 -0000
Questa e' di dominio pubblico ma puo' essere ottimizzata...

---------------------------------------------------------
#include <sys/types.h>

u_short
in_cksum_ (u_short * addr, int len)
{
  register int nleft = len;
  register u_short *w = addr;
  register int sum = 0;
  u_short answer = 0;

  /*
   * Our algorithm is simple, using a 32 bit accumulator (sum), we add
   * sequential 16 bit words to it, and at the end, fold back all the
   * carry bits from the top 16 bits into the lower 16 bits.
   */
  while (nleft > 1)
    {
      sum += *w++;
      nleft -= 2;
    }

  /* mop up an odd byte, if necessary */
  if (nleft == 1)
    {
      *(u_char *) (&answer) = *(u_char *) w;
      sum += answer;
    }

  /* add back carry outs from top 16 bits to low 16 bits */
  sum = (sum >> 16) + (sum & 0xffff);   /* add hi 16 to low 16 */
  sum += (sum >> 16);           /* add carry */
  answer = ~sum;                /* truncate to 16 bits */
  return (answer);
}
---------------------------


Nicola.


-nb-
Conscience makes egotists of us all. 

Linux & C. contributor. bonelli@oltrelinux.com   

			bonelli@antifork.org     
			nicola@bodypleasure.com 

			awgn@sicurezza.org    
			awgn@1az.net          

Get my pgp.pub key at: http://awgn.antifork.org/pgp
Key fingerprint = E1FF 4CCE 5D60 F437 364E  7A6A A13E D061 6BE8 F7B8



________________________________________________________
http://www.sikurezza.org - Italian Security Mailing List




[ Home | Liste | F.A.Q. | Risorse | Cerca... ]

www.sikurezza.org - Italian Security Mailing List
(c) 1999-2005