Click here to Skip to main content
15,937,779 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
How do I get this function to return a value of 1 or 0 so that I can reference it elsewhere? Thank you.
void end_packet(char *_packet)
{
TCPHEADER *tcp_header = (TCPHEADER *)_packet;
BYTE flags = ( ntohs(tcp_header->info_ctrl) & 0x003F );
int EP;
	if ( flags & 0x01 && flags & 0x10 )
		{
		EP = 1; //End
		}
	else
	{
		EP = 0; //Not End
	}
}
Posted
Comments
Sergey Alexandrovich Kryukov 18-Sep-11 22:37pm    
What's the problem? What this function is supposed to do?! (Don't answer "to return 0 or 1" -- it's obvious.)
--SA
Albert Holguin 19-Sep-11 15:32pm    
I deleted messages that are not related to question asked.

Please see my comment to the question.

How can you ask this question and declare a void function?! Make a return type, say, byte and return EP — if this type. Better yet, introduce enum type with values of 0 and 1 and return it. Is that was your problem?

—SA
 
Share this answer
 
v2
Comments
Member 7766180 18-Sep-11 22:50pm    
How can I make a return type of byte and return EP? As you know I'm new to this and this would be a tremondous help if you would show me. I learn by visual example. Thank you kind sir. How can I call or grab this value in another part of the code? Here is the whole funcion.

void decode_tcp(char *_packet)
{
TCPHEADER *tcp_header = (TCPHEADER *)_packet;
BYTE flags = ( ntohs(tcp_header->info_ctrl) & 0x003F );
int EP;
printf("\n Source Port : %ld", htons(tcp_header->source_port));
printf("\n Destination Port : %ld", htons(tcp_header->destination_port));
printf("\n Control Bits : ");

if ( flags & 0x01 ) // FIN
printf( "FIN " );
if ( flags & 0x02 ) // SYN
printf( "SYN " );
if ( flags & 0x04 ) // RST
printf( "RST " );
if ( flags & 0x08 ) // PSH
printf( "PSH " );
if ( flags & 0x10 ) // ACK
printf( "ACK " );
if ( flags & 0x20 ) // URG
printf( "URG " );
printf("\n Sequence Number : %lu", ntohl(tcp_header->seq_number));
if ( flags & 0x01 && flags & 0x10 )
{
printf("\n End of Packet " );
EP = 1;
}
else
printf("\n Not End of Packet " );
EP = 0;
}
Sergey Alexandrovich Kryukov 19-Sep-11 14:38pm    
Replace void decode_tcp with int decode_tcp and add return EP; at the very end. Optionally, replace int EP with byte VP or, better yet, with some enum type you declare before the function.
I can see really bad style here. You want to calculate something and you also print something. Avoid printing in any function having any logic, unless this is temporary, just for development. Also you are commenting something which should be a part of code.

Instead of writing

if (flags % 0x04)
...


write

if (flags & RST)
...


Declare enum TcpFlags { FIN = 1, SYN = 2, RST = 4, /* etc... */ }

You should get rid of all immediate constants hard-coded in the code; having them makes in non-supportable. Declare all constants explicitly; enum is one of the best ways to do it.

If you say you always learn be visual example, be warned: you learn next to nothing due to missing other important ways to learn.

Good luck,
--SA
Member 7766180 19-Sep-11 15:12pm    
Thank you so very much! I appreciate you taking the time to show me how it's done. Good luck to you also!
Sergey Alexandrovich Kryukov 19-Sep-11 15:29pm    
Hope it can help you.
Cheers!
--SA
Manfred Rudolf Bihy 21-Sep-11 4:09am    
Great follow up! 5+
You might want to read up on how to use functions in the C or C++ language Check out Functions at www.cplusplus.com. Or better yet, go for the Tutorial there.

It will take you a lot less time understanding the principles of C++ programming that way than trying to figure out the workings of the language by example alone. the difference will be about the same as the time an archeologist would take to decipher an unknown language as opposed to one using a dictionary!
 
Share this answer
 
Comments
Member 7766180 21-Sep-11 4:05am    
Thank you! Im finding it a little difficult as I come from VB and VBA and its sometimes hard to leave that headspace and go into a very different one. I appreciate your syggestion and the reference! Thank you!
Stefan_Lang 21-Sep-11 4:19am    
I understand. VB/VBA may not have been the best way to get started with programming - the 'B' (for Basic) isn't in that name for nothing! But since you can hardly change that now, asking and reading up on the concpets you're not familiar with is the second best thing.

FYI I upvoted for question to balance the downvotes - for all it's simplicity you were right to ask, and you asked in the right forum as well.
Member 7766180 21-Sep-11 6:45am    
Thank you!

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



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