Solved

Retrieving Multicast Packet Contents

Posted on 2006-10-20
5
233 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 18
How to get time_t value that is a week old 6 164
Need example 5 107
Detect CR LF to each line 12 153
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

863 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

18 Experts available now in Live!

Get 1:1 Help Now