Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Win7
I'm using a sniffer. When I use printf to console, I get no garbage on the screen, When I prin to my logfile I get garbage sometimes, and it seems to be on sequence numbers that are at least 9 characters in length. This is what is printing to my text file.
 
Sequence Number:  617122025
   /»$ȈéÛ¸^|Púð9—
 
The printf followed by the (log) text file...
printf("\n Sequence Number : %lu\n", ntohl(tcp_header->seq_number));
myfile << "\n Sequence Number: "<< ntohl(tcp_header->seq_number);
Posted 31-Oct-11 8:20am
Comments
Chuck O'Toole at 31-Oct-11 13:35pm
   
Examine your output statements. Both print a string ("sequence number") an unsigned long integer (ntohl() is defined to return ulong) and a newline ("\n") and that's all. You show us output with two distinct lines, the output from the above sequence and some junk. Clearly, the statements you show are not producing the junk output, some other statements are. You are not showing all the relevant information so there's nothing anybody can do to help you.
Member 7766180 at 31-Oct-11 13:38pm
   
Right again Chuck. Here is the complete package...
printf("\n Sequence Number : %lu\n", ntohl(tcp_header->seq_number));
myfile << "\n Sequence Number: "<< ntohl(tcp_header->seq_number);
myfile << "\n ";
 
I just need the last line to be a spacer. Am I doing it wrong?
Member 7766180 at 31-Oct-11 13:44pm
   
This works...
myfile << "\n Sequence Number: "<< ntohl(tcp_header->seq_number)<< "\n";
Member 7766180 at 31-Oct-11 13:50pm
   
Chuck, Put this in a solution so that I may give you credit.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Most likely cause is that the console will just replace non-regular characters with space (or null). But with the text file, it tries to render it as Unicode text. So your problem is not that it is not showing up on the console, but rather you may not be interpreting the data correctly. Or it may be a secure connection and thus the data will be mostly meaningless (and thus garbage like).
  Permalink  
v2
Comments
Member 7766180 at 31-Oct-11 13:34pm
   
Thank you. I am using ANSI. So how can I fix this? I am not on a secure connection.
Nishant Sivakumar at 31-Oct-11 13:38pm
   
It is possible that tcp_header->seq_number is returning an invalid value. And thus ntohl will return some random value too.
Member 7766180 at 31-Oct-11 13:41pm
   
See above I forgot to include a line. The problem is that it doesn't always print garbage. Just sometimes. I just need a space.
Nishant Sivakumar at 31-Oct-11 13:43pm
   
Yes but my point was that the u_long returned may be corresponding to some arbitrary Unicode character and thus the text file interprets it incorrectly as a character (when it is not).
Member 7766180 at 31-Oct-11 13:47pm
   
Thank you. I understand what you are saying. How long are sequence numbers? I'm getting anywhere from 6 to 9 digits.
Nishant Sivakumar at 31-Oct-11 13:48pm
   
Based on the TCP header from http://en.wikipedia.org/wiki/Transmission_Control_Protocol, 32 bits.
Member 7766180 at 31-Oct-11 13:50pm
   
Thnak you. I appreciate the input!
Nishant Sivakumar at 31-Oct-11 13:50pm
   
And since it's a u_long, that'd be 0xffffffff which is 4,294,967,295 (maximum value). So 6-9 digits seem okay to me.
Member 7766180 at 31-Oct-11 13:52pm
   
Thank you! I'm good to go. Have a nice day, Nishant.
Nishant Sivakumar at 31-Oct-11 13:53pm
   
You are welcome. Hopefully you got something out of this discussion. I was a little disappointed that I could not help you pinpoint your exact error. Oh well.
Nishant Sivakumar at 31-Oct-11 13:51pm
   
Try opening the log file in a different editor. It may just be a case of Notepad misinterpreting the text as Unicode.
Member 7766180 at 31-Oct-11 13:54pm
   
Apparently, The space was printing something at times. This seems to have fixed the problem. myfile << "\n Sequence Number: "<< ntohl(tcp_header->seq_number)<< "\n";
Nishant Sivakumar at 31-Oct-11 13:56pm
   
Interesting!
Chuck O'Toole at 31-Oct-11 14:21pm
   
<< will convert the ulong (a number) into the printable form of the number so there's no chance that it will be taking the binary long integer and interpreting the characters as unicode. This path of investigation won't get anywhere.
Espen Harlinn at 31-Oct-11 15:40pm
   
5'ed!
Nishant Sivakumar at 31-Oct-11 15:41pm
   
Thank you, Espen!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Examine your output statements. Both print a string ("sequence number") an unsigned long integer (ntohl() is defined to return ulong) and a newline ("\n") and that's all. You show us output with two distinct lines, the output from the above sequence and some junk. Clearly, the statements you show are not producing the junk output, some other statements are. You are not showing all the relevant information so there's nothing anybody can do to help you.
 
Now that you've added the "\n" to the myfile<< statement, you are just mimicing what's in the printf. However, that simple addition does not explain why you have garbage in the file. Adding "newline" just gives you, well, a new line. That extra output in the file comes from somewhere.
  Permalink  
v2
Comments
Member 7766180 at 31-Oct-11 20:52pm
   
Chuck, I have no idea where the garbage comes from. It seems to be working now. There are no other lines in sight!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 31 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100