473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Internet Checksum

I have a problem witch calculating TCP or UDP checksum.
This is what I found:

u16 in_cksum(u16 *addr,int count)
{
register long sum = 0;

/* add 16-bit words */
while (count > 1) {
/* this is the inner loop */
sum += * (unsigned short*)addr++;
count -= 2;
}

/* add leftover byte, if any */
if (count > 0)
#if BIG_ENDIAN
sum += (*(unsigned char *)addr) << 8;
#else
sum += *(unsigned char *)addr;
#endif

/* Fold 32-bit sum to 16-bit */
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
sum = ~sum;
/*
* Return one's compliment of final sum.
*/
return (u16) sum;
}

But it doesn't work. I think that the problem is in the last while, when I
change 32-bit sum to 16-bit. But I didn't find anything else.
Please help me. (I use C not C++)
--
--
--------------------------------------
Pozdrawiam WILK
--------------------------------------
Jul 22 '05 #1
3 7722
wilk wrote:
[snip]
But it doesn't work.


Define: "doesn't work"

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
But it doesn't work.


Define: "doesn't work"


It gives the wrong answer for the sum. This isn't surprising because the
algorithm
he uses is just a simple sum, and the IP checksum is defined as the "ones
complement
of a ones complement sum". Specifically after you add 2 16 bit quantities
you need
to take the carry bit (the 17th bit) and add it back to the low end of the
word.

A little googling will for sure point you to an implementation in C. It's
only about ten
lines or so.
Jul 22 '05 #3
Użytkownik "Ron Natalie" <ro*@sensor.com> napisał w wiadomo¶ci
news:3f*********************@news.newshosting.com. ..
But it doesn't work.
Define: "doesn't work"

I check sum with tcpdump. And it's bad.
It gives the wrong answer for the sum. This isn't surprising because the
algorithm
he uses is just a simple sum, and the IP checksum is defined as the "ones
complement
of a ones complement sum". Specifically after you add 2 16 bit quantities you need
to take the carry bit (the 17th bit) and add it back to the low end of the
word.

A little googling will for sure point you to an implementation in C. It's only about ten
lines or so.

I think I've googled the hole internet world :)
And all that I found was about 5-6 codes that implementing this algorithm.
All codes were exactly implementing this algorithm, and all codes were
descripted as working Internet Checksum Alg.

I'll try to calculate the carry for each 16 bit word as you say. (I know
what is "one's complement") But I think that I don't understand this
algorithm.
It's 5th day work on it. I'm exhausted. Maybe You can give me more details.
Maybe you've got exactly the same alg. that is descripted in RFC ?

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Mercuro | last post by:
Hello i'm looking for a simple way to checksum my data. The data is 70 bytes long per record, so a 32 byte hex md5sum would increase the size of my mysql db a lot. I'm looking for something...
2
by: pradeep | last post by:
I have 2 data files, DATA1 and DATA2 , both same. My task is to: Open DATA1, compute the checksum and put it in the end of the file(don't bother about boundary conditions).close DATA1 Open...
4
by: Abby | last post by:
I have an array which contain Hex no. in each position. For examples, unsigned char data; data = 0x00; data = 0x01; data = 0x02; data = 0xE; data = 0xEF; --> This is the checksum value
2
by: Abby | last post by:
I need to do 8 bits 2's complement checksum, so I wrote code in order to see if the checksum calculation is correct. ===========================================================================...
6
by: Kevin | last post by:
I'm on Sun 0S 5.8 and need to calculate the checksum of certain sections in an ELF binary file. Specifically .text to .rodata. I'm able to parse through to the program header table and then find...
6
by: Astroman | last post by:
Hi guys and girls. This is my first time posting here so go easy :) . I was wondering if someone could please interpret how this csum() function works in the following C code. I know that the...
3
by: Andrus | last post by:
Device connected to serial port accepts data packets in the form 02 0x57 ll ll 00 00 00 00 dd..dd cc cc 02 (1 byte ) is message prefix 0x57 (1 byte) is message type (W=Write) ll ll ( 2 bytes)...
1
by: Terry | last post by:
I'm trying to calculate the checksum for UDP packet. The algorithm itself is not difficult (lots of examples out there), but what I'm having the most trouble with is determining the byte order...
4
by: Rain | last post by:
hi, need help here, does anyone know how to use or does any one have the code for checksum? I want to checksum a string to be sent using udp and checksum it again when received.. does anyone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.