Di seguito è la definizione di struttura sockaddr_storage (rfc2553). Secondo rfc2553, il sockaddr_storage dovrebbe essere allineato con il limite a 64 bit e dovrebbe essere in grado di contenere sia sockaddr_in che sockaddr_in6. Inoltre, deve avere il membro __ss_family atlest. Il resto dei campi è definito dall'implementazione.Perché la struttura sockaddr_storage è definita come è definita?
#define _SS_MAXSIZE 128 /* Implementation specific max size */
#define _SS_ALIGNSIZE (sizeof (int64_t))
/* Implementation specific desired alignment */
/*
* Definitions used for sockaddr_storage structure paddings design.
*/
#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof (sa_family_t))
#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (sa_family_t)+
_SS_PAD1SIZE + _SS_ALIGNSIZE))
struct sockaddr_storage {
sa_family_t __ss_family; /* address family */
/* Following fields are implementation specific */
char __ss_pad1[_SS_PAD1SIZE];
/* 6 byte pad, this is to make implementation
/* specific pad up to alignment field that */
/* follows explicit in the data structure */
int64_t __ss_align; /* field to force desired structure */
/* storage alignment */
char __ss_pad2[_SS_PAD2SIZE];
/* 112 byte pad to achieve desired size, */
/* _SS_MAXSIZE value minus size of ss_family */
/* __ss_pad1, __ss_align fields is 112 */
};
mia domanda è perché sockaddr_storage è definito come il modo di cui sopra? Perché non è stato definito come di seguito?
struct sockaddr_storage {
sa_family_t __ss_family; /* address family */
char __ss_pad[_SS_MAXSIZE - sizeof(sa_family_t) ]; //will there be any alignment issue here?
};
@Dale, unico campo nel sockaddr_storage, che l'utente si suppone che l'accesso è __ss_family. Quindi, anche se il resto dei campi sono allineati a 64 bit, come può essere d'aiuto? Inoltre, ho sbagliato che è sockaddr_in6 piuttosto che sockaddr_storage che dovrebbe essere allineato al limite di 64 bit. – chappar
Se una struttura o unione contiene un membro che richiede un allineamento a 64 bit (ad esempio 'uint64_t'), la struttura stessa verrà posizionata su un limite a 64 bit. Altrimenti non ci sarebbe modo di garantire che quel membro rimanga allineato. Naturalmente se la struttura stessa è allineata a 64 bit, anche il primo campo ('__ss_family') sarà allineato a 64 bit. – mark4o