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 11:25

>>16
You're making size assumptions
The only assumption I'm making is that chars are 8 bits. If that assumption is true, then you can portably send a 32-bit integer with the network byte order like so:

unsigned char buf[4];

send(fd, htonl(buf, 123456), sizeof buf, 0);


This method makes absolutely no assumptions about the size of short, int, or long, except for the minimum guaranteed limits specified by the C standard.

and you can't dereference void pointers.
That's why the value is converted to a pointer to unsigned char within the body of the function (which is safe since both types are guaranteed to have the same internal representation). Functions like memcpy are designed in a similar way.

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