Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between sockaddr and sockaddr_storage

Tags:

c

sockets

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];
}
like image 797
zer0uno Avatar asked Oct 22 '13 21:10

zer0uno


1 Answers

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.)

like image 166
torek Avatar answered Sep 20 '22 04:09

torek