Solved

Retrieving Multicast Packet Contents

Posted on 2006-10-20
5
246 Views
Last Modified: 2010-04-15
I have a question concerning the retrieval of a multicast packet's contents.  I need to translate the network packets that I have
been getting via multicast protocol.  Each packet has a binary representation of the message header and includes the payload
in compressed RLC format.  Its structure is as follows:

Header
sequence number - four bytes (binary)
timestamp - four bytes (binary)
message body length - two bytes (binary)

Body
message - RLC -compressed

Knowing this structure and that my packet is received as follows

recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0)

How do I obtain the header and body contents?  I believe the RLC compression is thru
zlib.  How do I go about uncompressing the payload?

I am writing the program in C using Microsoft Visual C++.
0
Comment
Question by:jtrades
  • 3
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17778979
Hi jtrades,

struct mcpacket {
       unsigned int seqno;   //This assumes 4 byte ints which hold true for most machines. If available, uint32 fits better.
       unsigned int timestamp;
       char body[1];
};
...
struct mcpacket * pktptr;
...
recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0);
pktptr = (struct mcpacket *)recvString;

//Now &(pktptr->body) is the beginning of your compressed body and it has recvStringLen - 8 bytes .. Of course this is assuming that you read whole packet in one call ...

>I believe the RLC compression is thruzlib.  How do I go about uncompressing the payload?
Are you sure about this? I am not much into compression but if you believe this is through zlib, then this might be the API you are looking for
 int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
http://www.zlib.net/manual.html#uncompress

You have pointer to source and sourceLen ... destLen and dest pointer would be the length and poiner to your output buffer where uncompressed data would be stored.

Cheers!
sunnycoder
0
 

Author Comment

by:jtrades
ID: 17993154
The code provided by sunnycoder did not work that well.  Here's the code that I am currently using.

// Obtain packet
recvfrom (hSock, fromBuffer, PACKET_SIZE, 0, (struct sockaddr *)&fromAddr, &fromSize);

.......

// fromBuffer was passed into a function (as packet) where its contents were gathered as follows
unsigned char byte1, byte2, byte3, byte4;                  // Temporary holding place for requested byte of data
int sequence=0;                                                            // Current packet's sequence number
int timestamp=0;                                                      // Current packet's timestamp
void *UnCompDataBuff = NULL;                                    // Temporary holding place for uncompressed message
uLongf UnCompSize;                                                      // Educated guess at uncompressed message size
int body_length=0;      
char compressed_message[MAX_COMPRESSED_MSG_SIZE];      // Compressed body message
char uncompressed_message[MAX_UNCOMPRESSED_MSG_SIZE];      // Uncompressed body message

// First four bytes should be sequence number
byte1 = packet[current_byte++];
byte2 = packet[current_byte++];
byte3 = packet[current_byte++];
byte4 = packet[current_byte++];

sequence = pack (byte1,  byte2, byte3, byte4, TRUE);

// Second four bytes should be timestamp
byte1 = packet[current_byte++];
byte2 = packet[current_byte++];
byte3 = packet[current_byte++];
byte4 = packet[current_byte++];

timestamp = pack (byte1,  byte2, byte3, byte4, TRUE);

// Next two bytes should be body length of message
byte1 = packet[current_byte++];
byte2 = packet[current_byte++];

body_length = pack (byte1,  byte2, NULL, NULL, FALSE);

// Copy over body message                  
for (i=0; i<body_length && j<MAX_SIZE; i++) {
   compressed_message[j] = packet[current_byte++];
   j++;
}


// Uncompress body message (compressed RLC message)
UnCompDataBuff = malloc(UnCompSize);
uncompress((Bytef*)UnCompDataBuff, &UnCompSize, (const Bytef*)compressed_message, body_length);

if (UnCompSize < MAX_UNCOMPRESSED_MSG_SIZE) {
   strncpy (uncompressed_message, (char *)UnCompDataBuff, UnCompSize);
   uncompressed_message[UnCompSize] = '\0';
}

free (UnCompDataBuff);
0
 

Author Comment

by:jtrades
ID: 17993156
Forgot to add the pack function code ->

int
pack (unsigned char a, unsigned char b, unsigned char c, unsigned char d, int packFour)
{
      // Function is used to pack two or four characters into an int.  It uses shift operations
      // to do the packing byte by byte.

      int p = a;

      p = (p << 8) | b;

      if (packFour) {
            p = (p << 8) | c;
            p = (p << 8) | d;
      }

      return p;

} // pack
0
 

Author Comment

by:jtrades
ID: 17993170
To the administrator, I answered most of the question myself.  However, you may reward sunnycoder 100 points for providing the link to the compression code.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Coverting 24 hour time to 12 hour in C++ 15 178
Concatenate two strings Last and First Name 10 60
Want to delete all my personal data 13 146
Arduino EDI - Programming language 3 103
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question