雖然目前IPv6尚未廣泛應用,不過偶爾還是會碰到客戶要求 IPv6 compatible,所以在寫網路程式的時候,最好是『一魚兩吃』。

以下記錄的是四個最常用的structure,方便查閱:

struct sockaddr {
      unsigned short sa_family;     /* address family, AF_xxx */
      char sa_data[14];                  /* 14 bytes of protocol address */
};

struct sockaddr_in {
      short int sin_family;                /* Address family AF_INET */
      unsigned short int sin_port;    /* Port number */
      struct in_addr sin_addr;         /* Internet address */
      unsigned char sin_zero[8];     /* Same size as struct sockaddr */
};
struct in_addr {
      unsigned long s_addr;           /* Internet address */
};

struct sockaddr_in6 {
      sa_family_t sin6_family;         /* AF_INET6 */
      in_port_t sin6_port;               /* transport layer port # */
      uint32_t sin6_flowinfo;           /* IPv6 traffic class & flow info */
      struct in6_addr sin6_addr;    /* IPv6 address */
      uint32_t sin6_scope_id;        /* set of interfaces for a scope */
};
struct in6_addr {
      uint8_t s6_addr[16];            /* IPv6 address */
};


struct addrinfo
{
  int ai_flags;                           /* Input flags.  */
  int ai_family;                         /* Protocol family for socket.  */
  int ai_socktype;                    /* Socket type.  */
  int ai_protocol;                     /* Protocol for socket.  */
  socklen_t ai_addrlen;           /* Length of socket address.  */
  struct sockaddr *ai_addr;     /* Socket address for socket.  */
  char *ai_canonname;           /* Canonical name for service location.  */
  struct addrinfo *ai_next;       /* Pointer to next in list.  */
};

 

由上述整理可以看出 IPv6 的socket address structure (sockaddr_in6)已經超出原本的 sockaddr 長度,雖然還是可以做型別轉換(cast),但是我們一定還需要別的變數來記錄這個轉變。在 addrinfo 裡面的 ai_addrlen 正是用來記錄socket length的變數

創作者介紹

真實旅程

dragonspring 發表在 痞客邦 PIXNET 留言(0) 人氣()