473,473 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help! Raw Socket CheckSum

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 function
returns the checksum value but I was wondering How. I only included
the top half of the c code because you probably dont need the other
half and also it is a syn flooding program so I thought I shouldn't
post the whole lot. This is from the raw socket programming tutorial
written by Mixter. Couldn't find anything better. Just FAQ's.

#define __USE_BSD /* use bsd'ish ip header */
#include <sys/socket.h> /* these headers are for a Linux system, but
*/
#include <netinet/in.h> /* the names on other systems are easy to
guess.. */
#include <netinet/ip.h>
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include <netinet/tcp.h>
#include <unistd.h>

#define P 25 /* lets flood the sendmail port */
/*PLEASE EXPLAIN THE BELOW ! */
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}

int
main (void)
{
int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP); /* open raw socket
*/
char datagram[4096]; /* this buffer will contain ip header, tcp
header,
and payload. we'll point an ip header structure
at its beginning, and a tcp header structure after
that to write the header values into it */
struct ip *iph = (struct ip *) datagram;
struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof (struct
ip);
struct sockaddr_in sin;
/* the sockaddr_in containing the dest. address is used
in sendto() to determine the datagrams path */

sin.sin_family = AF_INET;
sin.sin_port = htons (P);/* you byte-order >1byte header values to
network
byte order (not needed on big endian machines) */
sin.sin_addr.s_addr = inet_addr ("127.0.0.1");

memset (datagram, 0, 4096); /* zero out the buffer */

/* we'll now fill in the ip/tcp header values, see above for
explanations */
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ip) + sizeof (struct tcphdr); /* no
payload */
iph->ip_id = htonl (54321); /* the value doesn't matter here */
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = 6;
iph->ip_sum = 0; /* set it to 0 before computing the actual
checksum later */
iph->ip_src.s_addr = inet_addr ("1.2.3.4");/* SYN's can be blindly
spoofed */
iph->ip_dst.s_addr = sin.sin_addr.s_addr;
tcph->th_sport = htons (1234); /* arbitrary port */
tcph->th_dport = htons (P);
tcph->th_seq = random ();/* in a SYN packet, the sequence is a
random */
tcph->th_ack = 0;/* number, and the ack sequence is 0 in the 1st
packet */
tcph->th_x2 = 0;
tcph->th_off = 0; /* first and only tcp segment */
tcph->th_flags = TH_SYN; /* initial connection request */
tcph->th_win = htonl (65535); /* maximum allowed window size */
tcph->th_sum = 0;/* if you set a checksum to zero, your kernel's IP
stack
should fill in the correct checksum during transmission */
tcph->th_urp = 0;

/* HERE THE FUNCTION IS CALLED */

iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);

Thanks
Nov 14 '05 #1
6 4652
On 8 Oct 2004 19:32:14 -0700, as*******@gmail.com (Astroman) wrote in
comp.lang.c:
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 function
returns the checksum value but I was wondering How. I only included
the top half of the c code because you probably dont need the other
half and also it is a syn flooding program so I thought I shouldn't
post the whole lot. This is from the raw socket programming tutorial
written by Mixter. Couldn't find anything better. Just FAQ's.
Since standard C has no built-in support for networking at all, such
code is all off-topic here.
#define __USE_BSD /* use bsd'ish ip header */
#include <sys/socket.h> /* these headers are for a Linux system, but
*/
#include <netinet/in.h> /* the names on other systems are easy to
guess.. */
#include <netinet/ip.h>
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include <netinet/tcp.h>
#include <unistd.h>
In fact, each and every one of the headers above is a non-standard
extension, and off-topic here. There are no sockets in C.
/*PLEASE EXPLAIN THE BELOW ! */
Explain what, exactly? What is there about this function that you
don't understand? It using nothing but pointer dereference and
increment to add a number of values, using the '+=' operator. Do you
understand that? If not, what don't you understand about it.
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}


After it has finished adding up the required number of values, it does
some bit wise operations and a few more additions to fold the unsigned
long sum into the lesser rank type unsigned short.

Which of these operators do you have trouble with? '>>', '&', or '~'?

How does it work? By performing the very simple series of operations
defined in the handful of instructions. What does it do? It applies
an algorithm defined for some particular protocol to a data buffer to
generate the check value required by that protocol.

If you want to watch it work, put it in a single source code file with
a main() function. In your main() function define an array of
unsigned shorts and fill it with some data. Pass a pointer and the
length to the function. Then step through it in a debugger or add
printf() statements, and you will see how it works.

As to why this is the algorithm used to generate a check value for
data in this protocol, you will have to ask somebody responsible for
specifying the protocol. It is neither a C language nor a C coding
issue.

[snip the off-topic code]

Note that if your question is about the particular function, all you
needed to do was post the function, which is completely stand-alone
and needs no header files at all. But you certainly need to ask a
more specific question. If you do not understand the operation of the
code, tell us what part you don't understand. If you don't understand
the concept of some sort of verification value, check sum, CRC, or
otherwise, in communication protocols, that's a different matter and
not a C language issue at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
> /*PLEASE EXPLAIN THE BELOW ! */
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}


This is OT and you don't clarify what you don't understand.

I can try explaining the C code itself. The algorithm assumes sizeof(short)
= 2. It sums up the values from the array. It then redefines 'sum' as 2*(16
high order bits of 'sum') + (16 low order bits of 'sum'). There is no
overflow because 'sum' is a long. Finally, it negates the sum (which many
checksum algorithms do) and returns it as a short for future transmission.

To understand more about checksum algos, try a different newsgroup.
Nov 14 '05 #3
Method Man finally wrote on Saturday 09 October 2004 03:28 am:
/*PLEASE EXPLAIN THE BELOW ! */
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
I can try explaining the C code itself. The algorithm assumes
sizeof(short) = 2. It sums up the values from the array. It then redefines
'sum' as 2*(16 high order bits of 'sum') + (16 low order bits of 'sum').


I may be confused, but I don't think that 2*(16 high order bits) + (16 low
order bits) describes what happens. As I read it, it adds the high order
bits to the low order bits, then it adds that result to the high order bits
of the result. For instance, if the number is AE9CF88A,

AE9C + F88A = 0001A726. ( sum = (sum >> 16) + (sum & 0xFFFF) )
0001A726 + 0001 = 0001A727. ( sum += (sum >> 16) )

Note: (various ways of interpreting your description)

2*(AE9C + F88A) = 34E4E
2* AE9C + F88A = 255C2
2* 0001 + A727 = A729
2*(0001 + A727) = 14E50

I'm new at this, so please correct me if I'm wrong, but that is how I
understood what was going on with that algorithm (minus the negation).
Thanks,
jab3

Nov 14 '05 #4
Hi guys, thanks for that. You have answered my question, sorry for not
being more specific. I have also found a few rfc's to explain it for
me as well. Sorry for posting in the wrong newsgroup but thanks for
the help.
Nov 14 '05 #5

"jab3" <ma****@helpdesk.org> wrote in message
news:gN********************@comcast.com...
Method Man finally wrote on Saturday 09 October 2004 03:28 am:
/*PLEASE EXPLAIN THE BELOW ! */
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
I can try explaining the C code itself. The algorithm assumes
sizeof(short) = 2. It sums up the values from the array. It then
redefines 'sum' as 2*(16 high order bits of 'sum') + (16 low order bits of 'sum').


I may be confused, but I don't think that 2*(16 high order bits) + (16 low
order bits) describes what happens. As I read it, it adds the high order
bits to the low order bits, then it adds that result to the high order

bits of the result.


Yes, you're correct.
Nov 14 '05 #6
On Sat, 9 Oct 2004 03:28:34 -0400, "Method Man" <a@b.c> wrote in
comp.lang.c:
/*PLEASE EXPLAIN THE BELOW ! */
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
This is OT and you don't clarify what you don't understand.

I can try explaining the C code itself. The algorithm assumes sizeof(short)
= 2.


No, it does not make any assumptions at all about sizeof(short). Not
in bits or bytes. In fact this code would work unchanged on
implementations where sizeof(short) is 1 and sizeof(long) is 2. It
would also work unchanged on implementations where sizeof(short) is 1
and sizeof(long) is 1.
It sums up the values from the array.
It sums the values from the array into an unsigned long, which is
possibly wider than unsigned short.
It then redefines 'sum' as 2*(16
high order bits of 'sum') + (16 low order bits of 'sum').
Nonsense, it does not 'redefine' anything. 'sum' was defined as an
unsigned long at the start, and an unsigned long it remains. It
performs a computation on sum.
There is no
overflow because 'sum' is a long.
'sum' is an unsigned long, and hence it subject to modulo wrap around
if a calculation produces an out-of-range value. If it were a signed
long, overflow would be possible, producing undefined behavior.
Finally, it negates the sum (which many
checksum algorithms do) and returns it as a short for future transmission.
No, it returns it as an unsigned short, and that is quite different
since the expression '~sum' could well be outside the range of values
for a short, which would produce an implementation-defined result.
To understand more about checksum algos, try a different newsgroup.


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.