src/internet-stack/ipv4-checksum.cc
changeset 5121 872e554039c3
parent 5117 591e912d459a
parent 4684 44b325571ab4
child 5122 0326727af888
equal deleted inserted replaced
5117:591e912d459a 5121:872e554039c3
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2005,2006,2007 INRIA
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License version 2 as
       
     7  * published by the Free Software Foundation;
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    17  *
       
    18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    19  */
       
    20 #include "ipv4-checksum.h"
       
    21 
       
    22 namespace ns3 {
       
    23 
       
    24 uint16_t 
       
    25 Ipv4ChecksumCalculate (uint16_t checksum, uint8_t *buffer, uint16_t size)
       
    26 {
       
    27   /* see RFC 1071 to understand this code. */
       
    28   uint32_t sum = checksum;
       
    29   uint16_t *data = (uint16_t *) buffer;
       
    30   for (uint16_t i = 0; i < (size/2); i++) {
       
    31     sum += data[i];
       
    32   }
       
    33   if ((size % 2) != 0) {
       
    34     uint8_t tmpBuf[2];
       
    35     tmpBuf[0] = buffer[size-1];
       
    36     tmpBuf[1] = 0;
       
    37     data = (uint16_t *)tmpBuf;
       
    38     sum += *data;
       
    39   }
       
    40   while (sum >> 16) {
       
    41     sum = (sum & 0xffff) + (sum >> 16);
       
    42   }
       
    43   return sum;
       
    44 }
       
    45 
       
    46 uint16_t 
       
    47 Ipv4ChecksumComplete (uint16_t checksum)
       
    48 {
       
    49   return ~checksum;
       
    50 }
       
    51 
       
    52 }; //namespace ns3