1

I am intercepting some packets, and then put them into an structure.

#pragma pack(push, 1)
struct PacketHeader {
    short Size;
    short Checksum;
    short Index;
};
#pragma pack(pop)

I have a packet with PacketHeader and some other bytes that fill this structure:

struct STRUCT_SVC_ROOM_CREATE {
    PacketHeader Header;
    unsigned char TitleLength; // 1 byte
    char* RoomTitle[23];
    short Unknow;
    short Unknow2;
    short Password1;
    short Password2;
    char LastByte;
};

In the above struct, TitleLength is one byte, that in decimal can 0x17 (23) or any number. This number the numbers of chars contained in RoomTitle.

I need to set size of RoomTitle accortng to TitleLenght byte (as decimal number).

How could I modify the struct to handle the text size in the right location inside the struct?

4
  • 2
    char* RoomTitle[23] It seems extremely unlikely that you will receive, over the network, 23 pointers that are valid in your process' address space. Did you mean char RoomTitle[23]? Commented Dec 7, 2014 at 18:06
  • You can't alter the definition of the structure at run-time. You have a variable-length message coming from the wire, you need to parse it accordingly. You can't define a single struct that would magically fit a message of any size. You can define structs for fixed-size parts of the message - for example, you can define PacketFooter for fields Unknown through LastByte. But you need to determine programmatically at what offset in the message PacketFooter begins. Commented Dec 7, 2014 at 18:09
  • PacketHeader is 6 bytes long. Then comes TitleLenght, and then RoomTitle starts exatly at byte 8. Could you give me an example of how to devide the struct and define the start offset ? Commented Dec 7, 2014 at 18:16
  • You can define one structure for Header through RoomTitle, and another for Unknown through LastByte. That second structure is at a variable offset - namely, offsetof(FirstStructure, RoomTitle) + firstStructure->TitleLength. Commented Dec 7, 2014 at 18:46

1 Answer 1

1

You should do something like follows, to parse the RoomTitle from the packet received at your socket:

struct STRUCT_SVC_ROOM_CREATE {
    PacketHeader Header; // Header length is sizeof(PacketHeader)
    unsigned char TitleLength; // 1 byte
    char RoomTitle[255]; // I suspect you don't have 23 `RoomTitle[23];` char* 
                         // pointers at this point, but just a char* maximally 
                         // sized as the maximum number that TitleLength can hold 
                         // (which is 255).
    short Unknow; // Unknow length is sizeof(short)
    short Unknow2; // ... ditto aso.
    short Password1;
    short Password2;
    char LastByte;
};

As I pointed out in the code comments above

  1. Read the PacketHeader (take care of Size and CRC endianess!)
  2. Read the payload data according to PacketHeader::Size from the packet into another buffer. (Consider to check the CRC)
  3. Read the TitleLength and RoomTitle from the payload data accordingly. Take care, if you want to handle the RoomTitle data as a c-style string, it's actually terminated with '\0'. Also use the TitleLength information when copying elsewhere.
  4. Read the data with well known size coming after (take care of endianess again)

Some pseudo code (not tested):

int recv_ROOM_CREATE_packet(int sockfd, STRUCT_SVC_ROOM_CREATE* packet) {
    read(sockfd,&(packet->Header),sizeof(PacketHeader));
    read(sockfd,&(packet->TitleLength),sizeof(unsigned char));
    read(sockfd,packet->RoomTitle,packet->TitleLength);
    // ensure that packet->RoomTitle is a correctly terminated c-style string
    if(packet->TitleLength < 255) {
        packet->RoomTitle[packet->TitleLength + 1] = `\0`; 
    }
    else {
        packet->RoomTitle[254] = `\0`; 
    }
    // aso ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

I put PacketHeader inside this struct this way: STRUCT_SVC_ROOM_CREATE* RoomCreate = (STRUCT_SVC_ROOM_CREATE*)pHeader; Then I can get or set the elements RoomCreate->RoomTitle;
Should I put the text part out of the struct?
@ThiagoMarquezini You'll need a dynamic allocation to hold the RoomTitle part or an at least 255 byte holding buffer, yes.
Could you give me an example? o.o
@ThiagoMarquezini It's too complex to give you a concise example, unless you show some more of your actual code, how you parse these packets. The best I advice I can give is, to keep up with those steps given in my wanswer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.