Click here to Skip to main content
15,914,010 members
Home / Discussions / C#
   

C#

 
GeneralEvent Logging Strategy and Architecture Pin
Brady Kelly28-Mar-07 21:19
Brady Kelly28-Mar-07 21:19 
Questionsending string data to parallel port? Pin
Rohit Dev28-Mar-07 21:10
Rohit Dev28-Mar-07 21:10 
AnswerRe: sending string data to parallel port? Pin
m@u28-Mar-07 23:17
m@u28-Mar-07 23:17 
GeneralRe: sending string data to parallel port? Pin
Rohit Dev29-Mar-07 20:21
Rohit Dev29-Mar-07 20:21 
QuestionQuestion regarding ListView control? Pin
Khoramdin28-Mar-07 20:28
Khoramdin28-Mar-07 20:28 
AnswerRe: Question regarding ListView control? Pin
Saikek29-Mar-07 4:07
Saikek29-Mar-07 4:07 
Questionerror in loading application Pin
ArchaBhandare28-Mar-07 19:55
ArchaBhandare28-Mar-07 19:55 
QuestionTCP Checksum Generator Pin
Kr0d28-Mar-07 19:53
Kr0d28-Mar-07 19:53 
Hey all,
I'm writing a program that can modify packets in c#.
Everything is going fine except my TCP checksum generator where it seems to work for some packets but with a few slight changes it generates the wrong thing. It only seems to be doing this when sending packets to my test box, otherwise it works fine.

Could the community please have a look at my code and try and find out what is wrong? i know my programming style Isn't exactly neat although any help would be appreciated.

I pass it the whole packet and it sends back a string of the checksum in hex format.

Cheers
-Glen
<br />
private static string GenerateTCPChecksum(byte[] packet)<br />
{<br />
//Clears the existing checksum<br />
packet[0x32] = 0x00;<br />
packet[0x33] = 0x00;<br />
<br />
int sum = 0;<br />
//Gets the IP header length<br />
string ipHeaderLength = Convert.ToString(packet[0x0e], 16);<br />
ipHeaderLength = "" + ipHeaderLength[1];<br />
//Gets the packet size not including the ethernet part<br />
int packetSize = Convert.ToInt32(Convert.ToString(packet[0x10], 16).PadLeft(2, '0') +<br />
Convert.ToString(packet[0x11], 16).PadLeft(2, '0'), 16);<br />
//TCP packet size<br />
int length = packetSize - (Int32.Parse(ipHeaderLength) * 4); //TCP packet size<br />
<br />
//Adds up the whole TCP packet<br />
for (int i = 0x22; i < packet.Length; i++)<br />
{<br />
try<br />
{<br />
sum = sum + Convert.ToInt32(Convert.ToString(packet, 16).PadLeft(2, '0') +<br />
Convert.ToString(packet[i + 1], 16).PadLeft(2, '0'), 16);<br />
}<br />
catch (IndexOutOfRangeException)<br />
{<br />
sum = sum + Convert.ToInt32(Convert.ToString(packet, 16).PadLeft(2, '0') + "00", 16);<br />
}<br />
i++;<br />
}<br />
<br />
//Adds the psudoheader which is the IP addresses, protocol (TCP) and length<br />
sum = sum + (Convert.ToInt32(Convert.ToString(packet[0x1a], 16).PadLeft(2, '0') +<br />
Convert.ToString(packet[0x1b], 16).PadLeft(2, '0'), 16)) +<br />
(Convert.ToInt32(Convert.ToString(packet[0x1c], 16).PadLeft(2, '0') +<br />
Convert.ToString(packet[0x1d], 16).PadLeft(2, '0'), 16)) +<br />
(Convert.ToInt32(Convert.ToString(packet[0x1e], 16).PadLeft(2, '0') +<br />
Convert.ToString(packet[0x1f], 16).PadLeft(2, '0'), 16)) +<br />
(Convert.ToInt32(Convert.ToString(packet[0x20], 16).PadLeft(2, '0') +<br />
Convert.ToString(packet[0x21], 16).PadLeft(2, '0'), 16)) +<br />
0x06 + length;<br />
string s = Convert.ToString(sum, 16);<br />
string carry = "00";<br />
//Seperates carry and the rest<br />
if (s.Length >= 5)<br />
{<br />
carry = s.Substring(0, s.Length - 4);<br />
s = s.Substring(s.Length - 4, 4);<br />
}<br />
//Adds the carry to the rest<br />
sum = Convert.ToInt32(s, 16);<br />
sum = sum + Convert.ToInt32(carry, 16);<br />
//Inverts number(one's complement)<br />
sum = 0xffff - sum;<br />
s = Convert.ToString(sum, 16).PadLeft(4, '0');<br />
return s; //Returns a string representation of the hex checksum<br />
}

QuestionI have an issue on ZedGraph Control Pin
pashitech28-Mar-07 19:03
pashitech28-Mar-07 19:03 
AnswerRe: I have an issue on ZedGraph Control Pin
Pete O'Hanlon28-Mar-07 22:14
mvePete O'Hanlon28-Mar-07 22:14 
QuestionForm Control or Component, Delete, Property Notification [modified] Pin
Cnight Stalker28-Mar-07 19:00
Cnight Stalker28-Mar-07 19:00 
QuestionperformClick() Pin
quick228-Mar-07 18:36
quick228-Mar-07 18:36 
AnswerRe: performClick() Pin
giddy_guitarist31-Mar-07 5:38
giddy_guitarist31-Mar-07 5:38 
QuestionXML File processing Pin
satsumatable28-Mar-07 17:27
satsumatable28-Mar-07 17:27 
QuestionHow to add Restart Dialog to Installer Pin
remex_1980_junyongwu28-Mar-07 17:13
remex_1980_junyongwu28-Mar-07 17:13 
QuestionSnippets Pin
Mike Hankey28-Mar-07 17:07
mveMike Hankey28-Mar-07 17:07 
QuestionHow to reconnect to the server when the ethernet cable unplugged and replugged using Socket Pin
engsrini28-Mar-07 16:34
engsrini28-Mar-07 16:34 
Questionhow to use the a interface declared in a dll Pin
goldli28-Mar-07 16:21
goldli28-Mar-07 16:21 
AnswerRe: how to use the a interface declared in a dll Pin
wulixu28-Mar-07 17:31
wulixu28-Mar-07 17:31 
GeneralRe: how to use the a interface declared in a dll [modified] Pin
goldli28-Mar-07 19:15
goldli28-Mar-07 19:15 
Question"Attempted to read or write protected memory." Pin
User 137680028-Mar-07 14:58
User 137680028-Mar-07 14:58 
AnswerRe: "Attempted to read or write protected memory." Pin
Dewald30-Mar-07 23:25
Dewald30-Mar-07 23:25 
QuestionWindows Forms book suggestions? Pin
Mark J. Miller28-Mar-07 12:11
Mark J. Miller28-Mar-07 12:11 
AnswerRe: Windows Forms book suggestions? Pin
J$28-Mar-07 12:54
J$28-Mar-07 12:54 
GeneralRe: Windows Forms book suggestions? Pin
Mark J. Miller28-Mar-07 13:30
Mark J. Miller28-Mar-07 13:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.