Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

struct sockaddr rant

Name: Anonymous 2012-12-27 1:50

When I look in netinet/in.h on my system it's defined with the type uint32_t. Over here (http://www.beej.us/guide/bgnet/output/html/multipage/sockaddr_inman.html) it's defined as an unsigned long. Why wouldn't they do what they did with IPv6 addresses and just using an array of chars? If they're already using the assumption that CHAR_BIT is 8 for IPv6, why carry the extra, unnecessary assumption that the implementation provides a 32-bit integer type?

The only reason I can really think of is to ensure portability to platforms that use larger chars, but since they aren't going to work with IPv6 structs, why not redesign IPv4 structs?

And if we're fixing that, why not change the following functions:

uint32_t htonl(uint32_t hostlong);
uint32_t ntohl(uint32_t netlong);


to something like:

void *htonl(void *dest, unsigned long val);
unsigned long ntohl(const void *src);


/end rant

Name: Anonymous 2012-12-28 14:22

>>27,28
But... you can't necessarily write generic endianness correction functions, even when you do know the size of the integer type. For big/little endian it's easy (when you know the size), but using a byte array to return the corrected integer will still put it at an offset if you're trying to use smaller types than the one provided. e.g. you convert an LE 24-bit integer to BE and the unsigned long is 32 bits wide, your data will start at byte_array[1] with byte_array[0]=0. Example:
uint24_t u = 0xAABBCC;
uint8_t netbuf[512];
htonl(netbuf, u);
/*
Memory seen as LTR

Memory layout of u: CCBBAA
Memory layout of u when converted to ulong: CCBBAA00
Data in netbuf: 00AABBCC
*/

And now you have zero padding that you need to remove. If you think you can overcome this, please explain how you can get any endianness to conform, no matter how silly (see middle endian for retarded stuff that sadly needs support).

Personally I use tightly packed (not standard, I know) structs and unions when preparing data for sending. My problem is entirely different but solvable. There are no signed conversion functions, but those can be made.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List