By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,408 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,408 IT Pros & Developers. It's quick & easy.

How to read the element when I receive a packet

P: n/a
QQ
Hello I am a newbie on network programming.
I am trying to receive a packet
if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct
sockaddr*)&register_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}
The packet I am receiving has the following possible structure.
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_Cc
{
CN msgHeader;
unsigned short action;
unsigned short devType;
unsigned short marketId;
unsigned char switchId;
} Cc;

typedef struct struct_CcA
{
CN msgHeader;
unsigned int result;
unsigned short periodType;
unsigned short authPeriod;
} CcA;

I need to read the value or magicA to decide whether structure it is Cc
or CcA.
so what should I do?

Thanks a lot!

Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <11**********************@g14g2000cwa.googlegroups .com>,
QQ <ju****@yahoo.com> wrote:
Hello I am a newbie on network programming.
Just so you know, the C standard does not talk about networks
at all, so you will likely get advised to head over to a newsgroup
such as comp.unix.programming .
I am trying to receive a packet
if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct
sockaddr*)&register_addr, &addr_len))==-1){
Okay, so the bytes end up in buf (which you didn't happen to include
the definition of), and numbytes tells you how many bytes were
received. One thing you need to be careful of is that there are
only limited conditions under which network sockets promise that
you will receive -all- of one packet (and only that one packet),
so your code should be sure to test that the number of bytes received
was large enough to accomedate the complete structure you are
expecting.

The packet I am receiving has the following possible structure.
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_Cc typedef struct struct_CcA I need to read the value or magicA to decide whether structure it is Cc
or CcA.
so what should I do?


Supposing that the data of interest starts at offset J in
the buffer (because there might be headers), you could do
something like:

CN packet_CN;
packet_CN.magicA = buf[J];
J += sizeof unsigned char;
packet_CN.magicB = buf[J];
J += sizeof unsigned char;
memcpy( & packet_CN.msgLen, buf[J+2], sizeof short );
J += sizeof short;

if ( packet_CN.magicA == Cc_INDICATOR ) { /* ... */ }
else if ( packet_CN.magicA == CcA_INDICATOR ) { /* ... */ }
else { /* the magic flag wasn't valid... what will you do? */ }
There is, though, a serious problem with this code, which is that
it assumes that the size of a short on the receiving machine is
the same as the size of a short on the transmitting machine. That's
not necessarily true, and indicates that there is a problem with
the specification of what the packet layout is.
Note: you could grab both magicA and magicB out of the packet
with a single memcpy(), because of guarantees that C makes about
padding and alignments for consequative char values in structures.
However, you cannot grab all of the CN structure in one memcpy()
beause the padding and alignment constraints for the short are
not certain to be the same as what was written into the packet.
The packet probably has no padding between the elements; the
destination machine might have padding, such as if sizeof short
is 4, with there being 2 bytes of hidden padding after magicB and
before msgLen .
--
'ignorandus (Latin): "deserving not to be known"'
-- Journal of Self-Referentialism
Nov 14 '05 #2

P: n/a
QQ wrote on 07/05/05 :
Hello I am a newbie on network programming.
I am trying to receive a packet
if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct
sockaddr*)&register_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}

The packet I am receiving has the following possible structure.
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;

typedef struct struct_Cc
{
CN msgHeader;
unsigned short action;
unsigned short devType;
unsigned short marketId;
unsigned char switchId;
} Cc;

typedef struct struct_CcA
{
CN msgHeader;
unsigned int result;
unsigned short periodType;
unsigned short authPeriod;
} CcA;

I need to read the value or magicA to decide whether structure it is Cc
or CcA.
so what should I do?


If I have followed you well, you intend to map a structure to a byte
stream. Note that the result of such an operation is highly
implementation dependent.

My guess is that the stream is structured in octets and that these
'structures' are simply a way of representing the specifications of the
data. They certainely do not represent any C-structure at all.

Why is that ? Simply because the internal data representation of a
C-structure may vary from a machine to another, due to differences in
- endianess
- type range
- data alignment
- padding

The way I understand the data mapping you have submitted is (assuming
unsigned char is one octet, unsigned chort is 2 octets and unsigned int
is 4 octets network representation, that means MSB first), correct me
if I'm wrong:

Cc data structure:

.. magicA
.. |
.. | magicB
.. | |
.. | | msgLen
.. | | |
.. | | | action
.. | | | |
.. | | | | devType
.. | | | | |
.. | | | | | marketId
.. | | | | | |
.. | | | | | | switchId
.. | | | | | | |
.. -+-+--+--+--+--+-
.. | | | | | | | |
.. -+-+--+--+--+--+-
.. | |
.. | \
.. |<header>|
..

CcA data structure:

.. magicA
.. |
.. | magicB
.. | |
.. | | msgLen
.. | | |
.. | | | result
.. | | | |
.. | | | | periodType
.. | | | | |
.. | | | | | authPeriod
.. | | | | | |
.. -+-+--+----+--+--
.. | | | | | | |
.. -+-+--+----+--+--
.. | |
.. | \
.. |<header>|
..

How to implement this ?

I suggest a data parser based on functions receiving the address ad
length of the stream to be analysed, the addres of an *internal*
C-structure receiving the extracted data, the address of the pointer to
the next-to-be-parsed-data and some returning and error status

For example (feel free to ask for details):

#include <stdio.h>

#define CN_LEN 4
#define Cc_LEN 7
#define CcA_LEN 8

typedef unsigned char uchar;
typedef unsigned long ulong;

typedef enum
{
MAGICA_Cc = 12, /* put your values here */
MAGICA_CcA = 34,
MAGICA_dummy
}
MAGICA;

typedef struct
{
MAGICA magicA;
unsigned magicB;
unsigned msgLen;
}
CN;

typedef struct
{
unsigned action;
unsigned devType;
unsigned marketId;
unsigned switchId;
}
Cc;

typedef struct
{
ulong result;
unsigned periodType;
unsigned authPeriod;
}
CcA;

typedef struct
{
CN cn;
union
{
Cc cc;
CcA cca;
}
u;
}
data_s;
/* implementation-dependent: 0 | 1 */
#define LITTE_ENDIAN 1

/* 16 bit */
unsigned ntoh (uchar const *p)
{
unsigned value = 0;

/* implementation-dependent */
#if LITTE_ENDIAN
/* little endian (Intel, for example) */
value |= (p[0] << (8 * 1));
value |= (p[1] << (8 * 0));
#else
/* big endian (Motorola, for example) */
value |= (p[0] << (8 * 0));
value |= (p[1] << (8 * 1));
#endif
return value;
}

/* 32 bit */
ulong ntohl (uchar const *p)
{
ulong value = 0;

/* implementation-dependent */
#if LITTE_ENDIAN
/* little endian (Intel, for example) */
value |= ((ulong) p[0] << (8 * 3));
value |= ((ulong) p[1] << (8 * 2));
value |= ((ulong) p[2] << (8 * 1));
value |= ((ulong) p[3] << (8 * 0));
#else
/* big endian (Motorola, for example) */
value |= ((ulong) p[0] << (8 * 0));
value |= ((ulong) p[1] << (8 * 1));
value |= ((ulong) p[2] << (8 * 2));
value |= ((ulong) p[3] << (8 * 3));
#endif
return value;
}

int CN_parser (uchar const *to_parse, CN * p_data, uchar const
**pp_next)
{
int err = 0;
uchar const *p = to_parse;

/* 1 octet */
p_data->magicA = *p;
p++;

/* 1 octet */
p_data->magicB = *p;
p++;

/* 2 octets */
p_data->msgLen = ntoh (p);
p += 2;

if (pp_next != NULL)
{
*pp_next = p;
}
return err;
}

int Cc_parser (uchar const *to_parse, Cc * p_data, uchar const
**pp_next)
{
int err = 0;
uchar const *p = to_parse;

/* 2 octets */
p_data->action = ntoh (p);
p += 2;

/* 2 octets */
p_data->devType = ntoh (p);
p += 2;

/* 2 octets */
p_data->marketId = ntoh (p);
p += 2;

/* 1 octets */
p_data->switchId = *p;
p++;

if (pp_next != NULL)
{
*pp_next = p;
}
return err;
}

int CcA_parser (uchar const *to_parse, CcA * p_data, uchar const
**pp_next)
{
int err = 0;
uchar const *p = to_parse;

/* 4 octets */
p_data->result = ntohl (p);
p += 4;

/* 2 octets */
p_data->periodType = ntoh (p);
p += 2;

/* 2 octets */
p_data->authPeriod = ntoh (p);
p += 2;

if (pp_next != NULL)
{
*pp_next = p;
}
return err;
}

int parser (uchar const *buf, size_t len, data_s * p_data)
{
/* the parameters should not be unary expressions nor functions */
#define min(a, b) ((a)<(b)?(a):(b))

int err;

/* is the length meaningful ? */
if (len >= CN_LEN + min (Cc_LEN, CcA_LEN))
{
/* parse the header */
uchar const *p_next = buf;

err = CN_parser (p_next, &p_data->cn, &p_next);

if (!err && p_next != NULL)
{
switch (p_data->cn.magicA)
{
case MAGICA_Cc:
err = Cc_parser (p_next, &p_data->u.cc, NULL);
break;

case MAGICA_CcA:
err = CcA_parser (p_next, &p_data->u.cca, NULL);
break;
default:
err = 1;
}
}
}
else
{
err = 1;
}
return err;
#undef min
}

void print (data_s * p_data)
{
#define PRT_D(data, field) \
printf ("%15s = %ld\n", #field, (ulong) data.field)

#define PRT_X(data, field) \
printf ("%15s = %lX\n", #field, (ulong) data.field)

PRT_D (p_data->cn, magicA);
PRT_D (p_data->cn, magicB);
PRT_X (p_data->cn, msgLen);

switch (p_data->cn.magicA)
{
case MAGICA_Cc:

PRT_X (p_data->u.cc, action);
PRT_X (p_data->u.cc, devType);
PRT_X (p_data->u.cc, marketId);
PRT_X (p_data->u.cc, switchId);

break;

case MAGICA_CcA:
PRT_X (p_data->u.cca, result);
PRT_X (p_data->u.cca, periodType);
PRT_X (p_data->u.cca, authPeriod);
break;
default:
;
}
printf ("\n");
}

int main (void)
{
/* simulation data */
uchar const buf_Cc[] =
{
/* header */
MAGICA_Cc,
0,
0,
7,
/* data */
/* action */
0x12,
0x34,
/* devType */
0x56,
0x78,
/* marketId */
0x9A,
0xBC,
/* switchId */
0xD,
};

uchar const buf_CcA[] =
{
/* header */
MAGICA_CcA,
0,
0,
8,
/* data */
/* result */
0x12,
0x34,
0x56,
0x78,
/* periodType */
0x9A,
0xBC,
/* authPeriod */
0xDE,
0xF0,
};

int err;

data_s data =
{0};

err = parser (buf_Cc, sizeof buf_Cc, &data);
print (&data);

err = parser (buf_CcA, sizeof buf_CcA, &data);
print (&data);

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

Nov 14 '05 #3

P: n/a
QQ
Thank you very much! It is really helpful!
Emmanuel Delahaye wrote:
QQ wrote on 07/05/05 :
Hello I am a newbie on network programming.
I am trying to receive a packet
if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct
sockaddr*)&register_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}

The packet I am receiving has the following possible structure.
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;

typedef struct struct_Cc
{
CN msgHeader;
unsigned short action;
unsigned short devType;T
unsigned short marketId;
unsigned char switchId;
} Cc;

typedef struct struct_CcA
{
CN msgHeader;
unsigned int result;
unsigned short periodType;
unsigned short authPeriod;
} CcA;

I need to read the value or magicA to decide whether structure it is Cc or CcA.
so what should I do?
If I have followed you well, you intend to map a structure to a byte
stream. Note that the result of such an operation is highly
implementation dependent.

My guess is that the stream is structured in octets and that these
'structures' are simply a way of representing the specifications of

the data. They certainely do not represent any C-structure at all.

Why is that ? Simply because the internal data representation of a
C-structure may vary from a machine to another, due to differences in
- endianess
- type range
- data alignment
- padding

The way I understand the data mapping you have submitted is (assuming unsigned char is one octet, unsigned chort is 2 octets and unsigned int is 4 octets network representation, that means MSB first), correct me if I'm wrong:

Cc data structure:

. magicA
. |
. | magicB
. | |
. | | msgLen
. | | |
. | | | action
. | | | |
. | | | | devType
. | | | | |
. | | | | | marketId
. | | | | | |
. | | | | | | switchId
. | | | | | | |
. -+-+--+--+--+--+-
. | | | | | | | |
. -+-+--+--+--+--+-
. | |
. | \
. |<header>|
.

CcA data structure:

. magicA
. |
. | magicB
. | |
. | | msgLen
. | | |
. | | | result
. | | | |
. | | | | periodType
. | | | | |
. | | | | | authPeriod
. | | | | | |
. -+-+--+----+--+--
. | | | | | | |
. -+-+--+----+--+--
. | |
. | \
. |<header>|
.

How to implement this ?

I suggest a data parser based on functions receiving the address ad
length of the stream to be analysed, the addres of an *internal*
C-structure receiving the extracted data, the address of the pointer to the next-to-be-parsed-data and some returning and error status

For example (feel free to ask for details):

#include <stdio.h>

#define CN_LEN 4
#define Cc_LEN 7
#define CcA_LEN 8

typedef unsigned char uchar;
typedef unsigned long ulong;

typedef enum
{
MAGICA_Cc = 12, /* put your values here */
MAGICA_CcA = 34,
MAGICA_dummy
}
MAGICA;

typedef struct
{
MAGICA magicA;
unsigned magicB;
unsigned msgLen;
}
CN;

typedef struct
{
unsigned action;
unsigned devType;
unsigned marketId;
unsigned switchId;
}
Cc;

typedef struct
{
ulong result;
unsigned periodType;
unsigned authPeriod;
}
CcA;

typedef struct
{
CN cn;
union
{
Cc cc;
CcA cca;
}
u;
}
data_s;
/* implementation-dependent: 0 | 1 */
#define LITTE_ENDIAN 1

/* 16 bit */
unsigned ntoh (uchar const *p)
{
unsigned value = 0;

/* implementation-dependent */
#if LITTE_ENDIAN
/* little endian (Intel, for example) */
value |= (p[0] << (8 * 1));
value |= (p[1] << (8 * 0));
#else
/* big endian (Motorola, for example) */
value |= (p[0] << (8 * 0));
value |= (p[1] << (8 * 1));
#endif
return value;
}

/* 32 bit */
ulong ntohl (uchar const *p)
{
ulong value = 0;

/* implementation-dependent */
#if LITTE_ENDIAN
/* little endian (Intel, for example) */
value |= ((ulong) p[0] << (8 * 3));
value |= ((ulong) p[1] << (8 * 2));
value |= ((ulong) p[2] << (8 * 1));
value |= ((ulong) p[3] << (8 * 0));
#else
/* big endian (Motorola, for example) */
value |= ((ulong) p[0] << (8 * 0));
value |= ((ulong) p[1] << (8 * 1));
value |= ((ulong) p[2] << (8 * 2));
value |= ((ulong) p[3] << (8 * 3));
#endif
return value;
}

int CN_parser (uchar const *to_parse, CN * p_data, uchar const
**pp_next)
{
int err = 0;
uchar const *p = to_parse;

/* 1 octet */
p_data->magicA = *p;
p++;

/* 1 octet */
p_data->magicB = *p;
p++;

/* 2 octets */
p_data->msgLen = ntoh (p);
p += 2;

if (pp_next != NULL)
{
*pp_next = p;
}
return err;
}

int Cc_parser (uchar const *to_parse, Cc * p_data, uchar const
**pp_next)
{
int err = 0;
uchar const *p = to_parse;

/* 2 octets */
p_data->action = ntoh (p);
p += 2;

/* 2 octets */
p_data->devType = ntoh (p);
p += 2;

/* 2 octets */
p_data->marketId = ntoh (p);
p += 2;

/* 1 octets */
p_data->switchId = *p;
p++;

if (pp_next != NULL)
{
*pp_next = p;
}
return err;
}

int CcA_parser (uchar const *to_parse, CcA * p_data, uchar const
**pp_next)
{
int err = 0;
uchar const *p = to_parse;

/* 4 octets */
p_data->result = ntohl (p);
p += 4;

/* 2 octets */
p_data->periodType = ntoh (p);
p += 2;

/* 2 octets */
p_data->authPeriod = ntoh (p);
p += 2;

if (pp_next != NULL)
{
*pp_next = p;
}
return err;
}

int parser (uchar const *buf, size_t len, data_s * p_data)
{
/* the parameters should not be unary expressions nor functions */
#define min(a, b) ((a)<(b)?(a):(b))

int err;

/* is the length meaningful ? */
if (len >= CN_LEN + min (Cc_LEN, CcA_LEN))
{
/* parse the header */
uchar const *p_next = buf;

err = CN_parser (p_next, &p_data->cn, &p_next);

if (!err && p_next != NULL)
{
switch (p_data->cn.magicA)
{
case MAGICA_Cc:
err = Cc_parser (p_next, &p_data->u.cc, NULL);
break;

case MAGICA_CcA:
err = CcA_parser (p_next, &p_data->u.cca, NULL);
break;
default:
err = 1;
}
}
}
else
{
err = 1;
}
return err;
#undef min
}

void print (data_s * p_data)
{
#define PRT_D(data, field) \
printf ("%15s = %ld\n", #field, (ulong) data.field)

#define PRT_X(data, field) \
printf ("%15s = %lX\n", #field, (ulong) data.field)

PRT_D (p_data->cn, magicA);
PRT_D (p_data->cn, magicB);
PRT_X (p_data->cn, msgLen);

switch (p_data->cn.magicA)
{
case MAGICA_Cc:

PRT_X (p_data->u.cc, action);
PRT_X (p_data->u.cc, devType);
PRT_X (p_data->u.cc, marketId);
PRT_X (p_data->u.cc, switchId);

break;

case MAGICA_CcA:
PRT_X (p_data->u.cca, result);
PRT_X (p_data->u.cca, periodType);
PRT_X (p_data->u.cca, authPeriod);
break;
default:
;
}
printf ("\n");
}

int main (void)
{
/* simulation data */
uchar const buf_Cc[] =
{
/* header */
MAGICA_Cc,
0,
0,
7,
/* data */
/* action */
0x12,
0x34,
/* devType */
0x56,
0x78,
/* marketId */
0x9A,
0xBC,
/* switchId */
0xD,
};

uchar const buf_CcA[] =
{
/* header */
MAGICA_CcA,
0,
0,
8,
/* data */
/* result */
0x12,
0x34,
0x56,
0x78,
/* periodType */
0x9A,
0xBC,
/* authPeriod */
0xDE,
0xF0,
};

int err;

data_s data =
{0};

err = parser (buf_Cc, sizeof buf_Cc, &data);
print (&data);

err = parser (buf_CcA, sizeof buf_CcA, &data);
print (&data);

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"


Nov 14 '05 #4

P: n/a
In article <mn***********************@YOURBRAnoos.fr>
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:

[snippage]
/* 32 bit */
ulong ntohl (uchar const *p)
{
ulong value = 0;

/* implementation-dependent */
#if LITTE_ENDIAN
/* little endian (Intel, for example) */
value |= ((ulong) p[0] << (8 * 3));
value |= ((ulong) p[1] << (8 * 2));
value |= ((ulong) p[2] << (8 * 1));
value |= ((ulong) p[3] << (8 * 0));
#else
/* big endian (Motorola, for example) */
value |= ((ulong) p[0] << (8 * 0));
value |= ((ulong) p[1] << (8 * 1));
value |= ((ulong) p[2] << (8 * 2));
value |= ((ulong) p[3] << (8 * 3));
#endif
return value;
}


It is worth pointing out that this ntohl() conflicts with a
not-Standard-C-but-still-fairly-common ntohl() found in a lot of
systems that also have networking support.

The only real conflict is that the ntohl() above supports both
"big-endian" and "little-endian" *network* orders, regardless
of the host byte order, while the "usual" ntohl() supports only
big-endian (TCP/IP) byte order.

The "#if" test above is testing whether you want a *network* order
that is big-endian. If LITTLE_ENDIAN is defined, you get a big-endian
network-byte-order value computed from the four octets whose values
are stored in p[0] through p[3] -- the value is p[0] * 16777216,
plus p[1] * 65536, plus p[2] * 256, plus p[3].

If you code ntohl() (and its companion htonl()) using multiply and
divide/remainder (or, equivalently, shift-and-mask), the resulting
C code is as portable as it can ever be. You can do the same with
ntohs() and htons(), which are two more non-standard-but-likewise-
fairy-common functions for working with 32-bit values.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.