Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieving Multicast Packet Contents

Posted on 2006-10-20
5
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

721 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