Reasoning behind C sockets sockaddr and sockaddr_storage

I’m looking at functions such as connect() and bind() in C sockets and notice that they take a pointer to a sockaddr struct. I’ve been reading and to make your application AF-Independent, it is useful to use the sockaddr_storage struct pointer and cast it to a sockaddr pointer because of all the extra space it has for larger addresses.

What I am wondering is how functions like connect() and bind() that ask for a sockaddr pointer go about accessing the data from a pointer that points at a larger structure than the one it is expecting. Sure, you pass it the size of the structure you are providing it, but what is the actual syntax that the functions use to get the IP Address off the pointers to larger structures that you have cast to struct *sockaddr?

It’s probably because I come from OOP languages, but it seems like kind of a hack and a bit messy.

Leave a Comment