Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to send a packet from client to server.

The packet:
  • the struct of the packet has two union
  • union header and union data
  • union data has a lot of struct inside

Example:
struct test1
struct test2

Then test1 has a lot of data

Example
int num1;
int num2;

Now I set
packet.data.test1.num1 = 1
packet.data.test1.num2 = 2

My sha1check function:
char* sha1check(const char* source){
TCHAR	stemp[1024];
_tcscpy(stemp,_TEXT(source));
CSHA1 sha1;
sha1.Update((UINT_8*)stemp, _tcslen(stemp)*sizeof(TCHAR));
sha1.Final();
sha1.ReportHashStl(srtemp, CSHA1::REPORT_HEX_SHORT);
return (char*)srtemp.c_str();
}

Using this function to get the sha1 value including the packet.data.test1.num1 and the packet.data.test1.num2 value



And then set the sha1 value in the header of part of packet. When server received the packet, will also using sha1 function to check packet.data and get a sha1

If packet.data.test1.num1 or packet.data.test1.num2 value has been changed, that means the sha1 checked by server is not equal to the sha1 value of header of packet will be recorded and rejected.

So I want to get the string or binary or char of the packet to input the data in the sha1 function... but I don't know how to do it.
Posted
Updated 2-Dec-09 3:52am
v16

In your code you are using _tcscpy() and _tcslen(), but these functions will not work if you have non-ascii values within your structures, which you do (int num1 for example).

In order for this to work you will need to calculate the length of your data in some other way, possibly by the sizeof() operator, or just counting the type and number of elementary items. You can then use a pointer to the start of your struct/union and cast it to a BYTE type, and send that to the SHA routine. There is little point in copying the data from one place to another just to change the casting, as this merely adds unnecessary overhead to your program.

wrote:
ps: my english is poor...sorry :(


But your explanation is still good. And you do not need to apologise for your English; I don't expect I can speak any of your native language.
 
Share this answer
 
I am not sure what you mean here. A struct is merely a description for the compiler to allow you to insert or extract elements from a composite field. A union is a composite of structs or other elements. A single char cannot be viewed as anything more than that.

Perhaps you could clarify what you are trying to achieve.
 
Share this answer
 
Probably you need a simple cast to char *, anyway you didn't provide enough details to get a meaningful answer.
:)
 
Share this answer
 

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