2012年5月25日 星期五

ipv6位址比較

ipv4因為sin_addr中s_addr結構為uint32,可以利用"等於"直接作位址比較。

typedef uint32_t in_addr_t;
struct in_addr {
in_addr_t s_addr; /* IPv4 address */
};

比較方式可以像下面這樣。

(sa->sin_addr.s_addr != (((struct sockaddr_in *)&remote)->sin_addr.s_addr))

但是v6中sin6_addr就長得不太一樣了。

struct in6_addr {
union {
uint8_t u6_addr8[16];
uint16_t u6_addr16[8];
uint32_t u6_addr32[4];
} in6_u;

    #define s6_addr in6_u.u6_addr8
    #define s6_addr16 in6_u.u6_addr16
    #define s6_addr32 in6_u.u6_addr32
};

可以看到是使用陣列方式儲存。比較的話要使用memcmp方式來達成。

memcmp(sa_v6->sin6_addr.s6_addr, (((struct sockaddr_in6 *)&remote)->sin6_addr.s6_addr), 16) != 0

這個問題的來源可以參考Comparing a peer's IPv6 address with localhost

1 則留言:

匿名 提到...

Lucky Club | Lucky Club Online Casino Review
Lucky Club is a fully licensed online casino with no deposit bonus offers. There are more than 400 slot games. Play in the casino, you  Rating: 5 · ‎Review by Lucky 카지노사이트luckclub Club