Solved

Retrieving Multicast Packet Contents

Posted on 2006-10-20
5
221 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now