Which are the difference between sockaddr and sockaddr_storage? I don't understand because looking at the code they look quite the same:
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
}
struct sockaddr_storage {
uint8_t ss_len;
sa_family_t ss_family;
char ss_padding[SIZE];
}
The storage
variant is meant to be "as big as the maximum possible size", and properly aligned too (so it can hold an IPv6 address, or an IPv4 address, or an ISO protocol address, or even an AF_UNIX
pathname or whatever). Think of it as a bin/barrel/breadbox/(other favorite storage item) that's big enough to hold "any socket address", no matter what kind of socket address it is. An IPv4 address (struct sockaddr_in
) is tiny and clearly can't hold an IPv6 address within it, but a struct sockaddr_storage
has a large roomy cargo area.
The original struct sockaddr
probably should have been this big, but wasn't. So this is basically a workaround for a historical error.
(The version you quoted above does not have an alignment item in it, which seems suspect.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With