Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a log file that runs in the main() of the program. I also have a function that is called within the main(). The fuction works fine and prints to console, the log file works fine as well, however; the return values from the fuction don't print to the log file, is there something specil that I have to do for this to happen? The function.

C++
void decode_tcp(char *_packet)
{
	TCPHEADER *tcp_header = (TCPHEADER *)_packet;
    BYTE flags = ( ntohs(tcp_header->info_ctrl) & 0x003F );
	if ( flags & 0x01 ) // FIN
		printf("\n   FIN " );
		myfile << "\n   FIN " ;
	if ( flags & 0x02 ) // SYN
		printf("\n   SYN " );
		myfile << "\n   SYN " ;
	if ( flags & 0x04 ) // RST
		printf("\n   RST " );
		myfile << "\n   RST " ;
	if ( flags & 0x08 ) // PSH
		printf("\n   PSH " );
		myfile << "\n   PSH " ;
	if ( flags & 0x10 ) // ACK
		printf("\n   ACK " );
		myfile << "\n   ACK " ;
	if ( flags & 0x20 ); // URG
		printf("\n   URG " );
		myfile << "\n   URG " ;
		printf("\n   Sequence Number  : %lu\n", ntohl(tcp_header->seq_number));
		myfile << "\n   Sequence Number  :"<< ntohl(tcp_header->seq_number);
	}
Posted
Updated 26-Oct-11 6:18am
v2
Comments
Richard MacCutchan 26-Oct-11 12:30pm    
Which part is not going to the log?
Member 7766180 26-Oct-11 12:33pm    
Everything that you see above that starts with myfile...

OK, this is not a new "solution" per se, it's just that there are so many comments in the previous one that I can't follow what "is" is "is not" true anymore.

So, if the original post is to be taken as "exact", why are you not getting compile errors on 'myfile' being undeclared? Your additional comments show the declaration of 'myfile' inside the scope of 'main()' so it should be inaccessible to the function 'decode_tcp()' so you should get compile errors. If you do not get compile errors, then you are not posting the exact code being used and are wasting our time.

If, as you now say, the output is getting to the log file but is in a format you don't like or doesn't match the 'printf' outputs, then post both outputs. I cannot see your terminal from here so I have no idea what you are looking at unless you show it here.
 
Share this answer
 
Comments
Member 7766180 26-Oct-11 13:36pm    
The Terminal
TCP Header:
Source IP: 174.129.225.194
Destination IP: 192.168.1.105
Wed Oct 26 13:34:57 2011

SYN
ACK
URG
Sequence Number : 676534648

-------------------- // --------------------
TCP Header:
No Action Required:
-------------------- // --------------------
TCP Header:
Source IP: 174.129.225.194
Destination IP: 192.168.1.105
Wed Oct 26 13:35:03 2011

SYN
ACK
URG
Sequence Number : 676534648
Member 7766180 26-Oct-11 13:38pm    
The text file..
Source IP:74.217.252.37
Destination IP:192.168.1.105
Wed Oct 26 13:34:54 2011

No Action Required:
Source IP:64.208.138.133
Destination IP:192.168.1.105
Wed Oct 26 13:34:54 2011
Member 7766180 26-Oct-11 13:38pm    
As you can see no flags or sequence number.
Member 7766180 26-Oct-11 13:51pm    
So if I do this..
void decode_tcp(char *_packet)
{
ofstream myfile;
myfile.open ("C:\\test.txt",ios::out | ios::app);
TCPHEADER *tcp_header = (TCPHEADER *)_packet;
BYTE flags = ( ntohs(tcp_header->info_ctrl) & 0x003F );

if ( flags & 0x01 ) // FIN
printf("\n FIN " );
myfile << "\n FIN " <<( flags & 0x01 ) ;
if ( flags & 0x02 ) // SYN
printf("\n SYN " );
myfile << "\n SYN " <<( flags & 0x02 );
if ( flags & 0x04 ) // RST
printf("\n RST " );
myfile << "\n RST " <<( flags & 0x04 );
if ( flags & 0x08 ) // PSH
printf("\n PSH " );
myfile << "\n PSH " <<( flags & 0x08 );
if ( flags & 0x10 ) // ACK
printf("\n ACK " );
myfile << "\n ACK " <<( flags & 0x010 );
if ( flags & 0x20 ); // URG
printf("\n URG " );
myfile << "\n URG " <<( flags & 0x020 );
printf("\n Sequence Number : %lu\n", ntohl(tcp_header->seq_number));
myfile << "\n Sequence Number :"<< ntohl(tcp_header->seq_number);
myfile.close();
}
Member 7766180 26-Oct-11 13:52pm    
I get this...
Source IP:50.22.100.250
Destination IP:192.168.1.105
Wed Oct 26 13:48:39 2011

No Action Required:
No Action Required:
Source IP:50.22.100.250
Destination IP:192.168.1.105
Wed Oct 26 13:48:39 2011

No Action Required:
No Action Required:
No Action Required:
No Action Required:
Source IP:50.22.10
FIN 0
SYN 2
RST 0
PSH 0
ACK 16
URG 0
Sequence Number :724588489
FIN 0
SYN 0
RST 0
PSH 0
ACK 16
URG 0
Sequence Number :725704959
FIN 0
SYN 0
RST 0
PSH 8
ACK 16
URG 0
Sequence Number :725704959
FIN 1
Do you notice what is wrong with these lines,
C++
if ( flags & 0x01 ) // FIN
    printf("\n   FIN " );
myfile << "\n   FIN " ;

once the indentation has been corrected?
May I suggest you always use braces to bracket the target of if, do, while etc.
 
Share this answer
 
Comments
Chuck O'Toole 26-Oct-11 17:25pm    
Good one, I didn't see that, I guess I just added the braces to match the indent :)
Richard MacCutchan 26-Oct-11 18:45pm    
I missed it first time round as well.
Member 7766180 26-Oct-11 19:48pm    
Thanks Richard. Well, well, well. It seems that this problem is not solved. It's printing all of the flags, all of the time. Not just the ones that are being reflected in printf. So maybe these are the wrong lines to do the myfile on?
Member 7766180 26-Oct-11 22:17pm    
Thanks Richard I added the brackets and now it prints when needed.
Also, One other thinh occasionaly I get this garble stuff, what could that be?

ACK
Sequence Number: 41183825
»ìtjQ*GPúð¢
Source IP:207.46.15.253
Destination IP:192.168.1.105
Wed Oct 26 22:12:45 2011

ACK
Sequence Number: 41185285
»ìtp*GPúðKš
No Action Required:
Source IP:207.46.15.253
Destination IP:192.168.1.105
Wed Oct 26 22:12:45 2011

PSH
ACK
Sequence Number: 41186745
»ìtu¹*GPúðˆü
Richard MacCutchan 27-Oct-11 4:55am    
Looking at your modified code below, I cannot see any reason for the garbage characters. Take a look around the next block where it prints the Source and Destination values.
I don't see anything that Opens / Creates "myfile" or even declares it.

1) if you don't associate a file with the stream, where do you think the output goes?

2) if you don't post the entire relevant code, you won't get any help
 
Share this answer
 
Comments
Member 7766180 26-Oct-11 12:50pm    
The code from the main().
else // to 0 else
{//IP To 0 Else Open
printf("\n Source IP: %s", ipSrc);
myfile << "\n Source IP:" << ipSrc;
printf("\n Destination IP: %s", ipDest);
myfile << "\n Destination IP:" << ipDest;
////////////////////////////////////////
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ("\n %s", asctime (timeinfo) );
myfile << "\n "<< asctime (timeinfo) ;
////////////////////////////////////////////
decode_tcp(&packet[ip_header_size]);
char *_packet;
_packet = &packet[ip_header_size];
TCPHEADER *tcp_header = (TCPHEADER *)_packet;
BYTE flags = ( ntohs(tcp_header->info_ctrl) & 0x003F );

I tried declaring, opening and closing on the function, but it prints it completely seperate from everything else. My guess is that somehow I have to put myfile..... somewhere on the the function in the main() part?

Chuck O'Toole 26-Oct-11 12:54pm    
Depends on what the log is for. If it's just going to be a copy ouf the output you print, probably not, you can open it in the beginning of the program, close it just before exit. However, if the log is going to be used as a diagnostic, to note the progress of the program so you can see how far it got before it crashes, then you should always open / write / close. I do it on each line but if there are lots of lines in a group, you can just wrap the group with open / dump stuff / close.
----------------------
Did you change the message that I replied to? Now my message doesn't make sense in the context of the rewritten reply. Don't do that or at least leave the original message there and add to it, not replace the whole thing.
Member 7766180 26-Oct-11 12:59pm    
Sorry, you were quicker than I. I shan't do that again! I'm just copying the output of print. I tried declaring, and opening at the very start of main, and closing it just before exit, it prints ecerything but this function.
Chuck O'Toole 26-Oct-11 13:04pm    
The code from main that you posted STILL DOES NOT SHOW THE OPENING OF THE FILE. So this is irrevalent too.
Member 7766180 26-Oct-11 13:10pm    
The opening...
int main( int _argc, char *_argv[] )
{//Main Open
ofstream myfile;
myfile.open ("C:\\test.txt",ios::out | ios::app);
struct sockaddr_in sock_sniff;
SOCKET sniff_socket = -1;
WSAData sa_data;
This may be of interest:
ACE[^] and the ACE tutorial by Umar Syyid[^]

ACE is mostly used to develop portable and highly efficient networked solutions, and the library comes with a wide range of examples on how to develop networked applications. It also comes with a very flexible logging facility :)

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Member 7766180 26-Oct-11 18:13pm    
Thank you, I will check it out.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900