Solved

Retrieving Multicast Packet Contents

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Certified Pentester Exam - need assistance on compiling an exploit 6 398
C Language combined operators 28 109
Grammars for C C++ and java 1 123
sameEnds challenge 3 164
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…
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 while-loops 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.

832 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