00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2005,2006,2007 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 #include "ipv4-checksum.h" 00021 00022 namespace ns3 { 00023 00024 uint16_t 00025 Ipv4ChecksumCalculate (uint16_t checksum, uint8_t *buffer, uint16_t size) 00026 { 00027 /* see RFC 1071 to understand this code. */ 00028 uint32_t sum = checksum; 00029 uint16_t *data = (uint16_t *) buffer; 00030 for (uint16_t i = 0; i < (size/2); i++) { 00031 sum += data[i]; 00032 } 00033 if ((size % 2) != 0) { 00034 uint8_t tmpBuf[2]; 00035 tmpBuf[0] = buffer[size-1]; 00036 tmpBuf[1] = 0; 00037 data = (uint16_t *)tmpBuf; 00038 sum += *data; 00039 } 00040 while (sum >> 16) { 00041 sum = (sum & 0xffff) + (sum >> 16); 00042 } 00043 return sum; 00044 } 00045 00046 uint16_t 00047 Ipv4ChecksumComplete (uint16_t checksum) 00048 { 00049 return ~checksum; 00050 } 00051 00052 }; //namespace ns3