LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2005, 04:40 AM   #1
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Rep: Reputation: 30
TCP header checksum


Hi Guys,
I'm writing some code for calculation of the checksum using the normal TCP checksum calculation algorithm in a T/TCP header .I've got two quetions here :-

1.Do all the fields need to be converted into Networkbyteorder or can I directly covert all of them into Hexadecimal format because thats what I would use later on in the program?

2.How do I handle the data part of it?for example if my data is just a simple string "Hello world " in the data part of the T/TCP header and I need to convert that into hex before I can calculate the checksum how exactly do I go about doing it?

Note: Theres very little difference btw the T/TCP header and the TCP header ; just a couple of extra fields in the options part.

I'm using Redhat 9.0 and compiling using gcc and writing code in C++.Any suggestions are greatly appreciated .
Thanks
Arvind.
 
Old 04-08-2005, 03:20 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Re: TCP header checksum

Quote:
Originally posted by live_dont_exist
1.Do all the fields need to be converted into Networkbyteorder or can I directly covert all of them into Hexadecimal format because thats what I would use later on in the program?
Use network byte order. Hexadecimal is just for presentation, right? You don't change the values?

Quote:
2.How do I handle the data part of it?for example if my data is just a simple string "Hello world " in the data part of the T/TCP header and I need to convert that into hex before I can calculate the checksum how exactly do I go about doing it?
It's just data. No conversion. I'm wondering what you mean by hex conversion here.
 
Old 04-09-2005, 07:00 AM   #3
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
Hi ,
Thanks a lot for replying . What I meant by converting data into hex was something like this :-

I have my TCP header :-

So I go tcp->source = 8080; tcp->dest=1000 etc etc etc...

So to calculate the checksum I convert everything into Hex and add it and take a 1's complement of the end result and then store it in the header . During this process I come across the data as well . So after 20 bytes of my TCP header I have data which I have to convert into Hex so I can add it to the other hex entries and get a final result .Its something like this :-

tcp->source = 8080 ...hex convert 1F90
tcp->dest=1000....hex convert 3E8
...
data.........................."some hex"

So now I add 1F90 + 3E8 + some hex(data) and get a result...in 32 bit hex ..I take a 1's complement over here and store the result in the checksum field and then send the packet out .So the question here is ; how do I manipulate the data ; how do I know what to add?...because in the header I know the values ; I dont know about data ...hope this clarifies matters.I can write a decimal to hex function for all the integers in TCP header but what about data.
Thanks
Arvind
 
Old 04-09-2005, 07:14 AM   #4
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
do we need to include the data when calculating the checksum ?
 
Old 04-09-2005, 07:58 AM   #5
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
yeah ... i guess you do need to include data if it is a TCP header checksum...if it had been an IP header checksum I was doing I wouldn't need to include it.
thnx
Arvind
 
Old 04-09-2005, 08:53 AM   #6
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
just for extra reference , hopes not too outdated ,
doesn't show the 'conversion' of data though

http://www.netfor2.com/tcpsum.htm
 
Old 04-09-2005, 12:00 PM   #7
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
thnx mate...but already looked at that page...thnx again though...do let me know if you think of something . Was just discussing this with a colleague and she says you got to convert it into ASCII and then into binary and then into hex . This sounds okay but is there a shorter way to do it. because this seems to be an awfully long route to take.
thnx
Arvind
 
Old 04-09-2005, 02:17 PM   #8
Roptaty
LQ Newbie
 
Registered: Apr 2005
Posts: 6

Rep: Reputation: 0
You don't have to convert to ascii->hex->binary... That's preposterous. I recommend you do take a closer look at the link alred posted earlier. The calculation of the checksum is right there. Remember that the tcp protocol do not differentiate between integers, characters, floats, doubles or so... they are all bytes...
 
Old 04-10-2005, 10:02 AM   #9
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
hi,
so what you are saying here is that I really dont need to be concerned about whether it an int or char or binary ; I can just go ahead and add .So for eg if I hardcode tcp->source=8080 and write tcp->dest=1000 ; I can just go ahead and say tcp->source & tcp->dest without being too concerned about the types ; the final result will automatically be okay..is my understanding correct?
thnx
Arvind
 
Old 04-10-2005, 02:31 PM   #10
Roptaty
LQ Newbie
 
Registered: Apr 2005
Posts: 6

Rep: Reputation: 0
You have to take into account the size of the members of the structure... tcp->source and tcp->dest are already 16bit, so you can safely add these two together.. Take a look at this page which describes the TCP checksumming: http://www.ietf.org/rfc/rfc1071.txt
 
Old 04-13-2005, 05:58 AM   #11
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
Hi,
I've managed to write code where I've initialized all the headers and managed to convert the first 2 values into Hex but how do I add them in Hex? Heres what I'm talking about:-

tcp->source=8080; tcp->dest=1000;
sprintf(buffer,"%x",tcp->source);
sprintf(buffer1,"%x",tcp->dest);

//SO NOw buffer=1f90 and buffer1=3e8....this is Okay..
but how do I add these two together in Hex?...Any ideas..
Thnx
Arvind
 
Old 04-13-2005, 06:36 AM   #12
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
Okay, if you simply want to add them up, you just need to do

tcp->source + tcp->dest

HEX is just a representation. They are still just numbers!

And, guess what, I have used my CASIO fx calculator to find out that the the result is 2378!

If my CASIO can do this, your PC can :-)
 
Old 04-13-2005, 08:05 AM   #13
Roptaty
LQ Newbie
 
Registered: Apr 2005
Posts: 6

Rep: Reputation: 0
This sounds like homework...

First of all, the string "123456789ABCDEF" (assuming this is ascii), will not be sent as 01 02 03 04 05 and so on... but according to its value in the ascii table: 31 32 33 34 35 and so on... Therefore, converting the values into the string representation, will not yield the result you are expecting.
For example your buffer looks like this "in the machine":
"1f90" --> 31 66 39 30
 
Old 04-13-2005, 08:50 AM   #14
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
Homework? But he mentioned a lady colleague ... :-)
 
Old 04-13-2005, 09:24 AM   #15
Roptaty
LQ Newbie
 
Registered: Apr 2005
Posts: 6

Rep: Reputation: 0
Lady colleague, female student, you say potato, i say poteter. Sorry mate, but this still looks like someones homework assignment. Think about it... it looks like a typical exercise in network programming. "Write an application thats fills out a tcp header structure and prints it on screen." No offence intended to OT.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
finding out TCP header details at socket level hari121 Linux - Networking 0 10-25-2005 01:54 AM
catch the tcp header for modification from the kernel mprabhu Linux - Software 2 07-24-2004 04:46 AM
Woody 3.0 Open Ports 1470/tcp/uaiact 1518/tcp/vpvd What for?How can I remove them? alexxxis Debian 5 07-05-2004 05:18 PM
anyone can help me with the TCP checksum? vaaub Programming 1 02-10-2004 01:32 PM
close port 6000/tcp 515/tcp SchwipSchwap Linux - Newbie 1 09-12-2002 08:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration