c - Why is zero padding needed in sockaddr_in? -


i googled , people says "to keep same size struct sockaddr". kernel not use sockaddr directly(right?). when using it. kernel cast is. why 0 padding needed?

struct sockaddr {     unsigned short    sa_family;    // address family, af_xxx     char              sa_data[14];  // 14 bytes of protocol address };  struct sockaddr_in {     short            sin_family;   // e.g. af_inet, af_inet6     unsigned short   sin_port;     // e.g. htons(3490)     struct in_addr   sin_addr;     // see struct in_addr, below     char             sin_zero[8];  // 0 if want };  struct in_addr {     unsigned long s_addr;          // load inet_pton() }; 

the 2 more relevant pieces of information find are

talking snippet of code not clear bytes

this bug. see occur occasionaly. bug can cause undefined behaviour in applications.

followed explications

most of net code not use sockaddr_in, uses sockaddr. when use function sendto, must explicitly cast sockaddr_in, or whatever address using, sockaddr. sockaddr_in same size sockaddr, internally sizes same because of slight hack.

that hack sin_zero. length of useful data in sockaddr_in shorter sockaddr. difference padded in sockaddr_in using small buffer; buffer sin_zero.

and finally, information can found @ various places

on architectures, wont cause problems not clearing sin_zero. on other architectures might. required specification clear sin_zero, must if intend code bug free , in future.

answering question

why need 8 byte padding?

and answer

unix network programming chapter 3.2 says that, "the posix specification requires 3 members in structure: sin_family, sin_addr, , sin_port. acceptable posix-compliant implementation define additional structure members, , normal internet socket address structure. implementations add sin_zero member socket address structures @ least 16 bytes in size. "

it's kinda structure padding, maybe reserved fields in future. never use it, commented.

which consistent first link. clearing bytes tells receiver "those bytes not used on our side".


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -