c - Determine if ipv4 or ipv6 data structure -


in kernel module, given struct sockaddr sa_family initialized af_unspec, how can reliably determine if struct sockaddr_in or struct sockaddr_in6? on linux 3.16.0-4-686-pae (x86).

    struct sockaddr {                         unsigned short     sa_family; // af_unspec                         char               sa_data[14]; // ?                         };     struct sockaddr_in {                         unsigned short     sin_family;                         unsigned short     sin_port;                         struct in_addr     sin_addr;                         char               sin_zero[8];                         };     struct sockaddr_in6 {                         unsigned short     sin6_family;                         unsigned short     sin6_port;                         unsigned int       sin6_flowinfo;                         struct in6_addr    sin6_addr;                         unsigned int       sin6_scope_id;                         }; 

usually, when calls kernel , gives struct sockaddr, has give length of struct sockaddr. example, see sendto():

ssize_t sendto (int sockfd, const void *buf, size_t buflen, int flags,                     const struct sockaddr *addr, socklen_t addrlen); 

using size of buffer, ought able guess type of sockaddr need use:

if (addr.sa_family == af_unspec) {     switch (addrlen) {         case sizeof (struct sockaddr_in): {             addr.sa_family = af_inet;             break;         }          case sizeof (struct sockaddr_in6): {             addr.sa_family = af_inet6;             break;         }          default: {             // handle error              break;         }     } } 

in ideal world, sa_family set seither af_inet (ipv4) or af_inet6 (ipv6) already, unfortunately doesn't appear case here.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -