Re: <netinet/ip.h> & <netinet/tcp.h> bug?
Re: <netinet/ip.h> & <netinet/tcp.h> bug?
- Subject: Re: <netinet/ip.h> & <netinet/tcp.h> bug?
- From: Quinn <email@hidden>
- Date: Thu, 13 Jun 2002 10:32:30 +0100
At 13:28 +0800 13/6/02, Matthew Drayton wrote:
I am moving some code over to CodeWarrior from Project Builder and I think I
have discovered a bug in <netinet/ip.h> and <netinet/tcp.h>. The bitfields of
struct ip and struct tcphdr are declared as u_ints ( 4 bytes ) but shouldn't
they be u_chars ( 1 byte )?
Just for point of reference, here's one of those structures from the
Mac OS X 10.1 headers.
struct ip {
#ifdef _IP_VHL
u_char ip_vhl; /* version << 4 | header length >> 2 */
#else
#if BYTE_ORDER == LITTLE_ENDIAN
u_int ip_hl:4, /* header length */
ip_v:4; /* version */
#endif
#if BYTE_ORDER == BIG_ENDIAN
u_int ip_v:4, /* version */
ip_hl:4; /* header length */
#endif
#endif /* not _IP_VHL */
u_char ip_tos; /* type of service */
u_short ip_len; /* total length */
u_short ip_id; /* identification */
u_short ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_char ip_ttl; /* time to live */
u_char ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src,ip_dst; /* source and dest address */
};
#ifdef _IP_VHL
#define IP_MAKE_VHL(v, hl) ((v) << 4 | (hl))
#define IP_VHL_HL(vhl) ((vhl) & 0x0f)
#define IP_VHL_V(vhl) ((vhl) >> 4)
#define IP_VHL_BORING 0x45
#endif
My impression is that either a) your compiler rigorously supports C's
bit field width specifier, in which case it doesn't matter whether
it's u_int or u_char because it'll always take 4 bits, or b) you
define _IP_VHL and then use the accessor macros.
Interestingly enough, it seems that the kernel code does both,
depending on which module you're compiling. Weird.
So, if CW doesn't give you the right structure you should define
_IP_VHL and use the macros.
Unfortunately this doesn't work for struct tcphdr, which doesn't have
an alternative set of accessors. You might try monkeying with CW's
#pragma pack. If that doesn't help, you probably should define your
own copy of the structure. It's also worth reporting it to
Metrowerks, because it runs counter to their long term compatibility
goal. You might also want to file a bug against the TCP header to
request similar accessors to those for struct ip.
S+E
--
Quinn "The Eskimo!" <
http://www.apple.com/developer/>
Apple Developer Technical Support * Networking, Communications, Hardware
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.