1. Introduction
RFC 5952主要在定義IPv6 address的表示法的標準化,主要原因是因為RFC 4291 - IP Version 6 Addressing Architecture定義了IPv6 address,且提供了一些彈性表示法,卻也因為彈性導致相同的address卻有不同的表示法(換言之,不同的表示法卻是指向同一address),這會導致一些問題產生。以下的IPv6 address都是相同的:
2001:db8:0:0:1:0:0:1 2001:0db8:0:0:1:0:0:1 2001:db8::1:0:0:1 2001:db8::0:1:0:0:1 2001:0db8::1:0:0:1 2001:db8:0:0:1::1 2001:db8:0000:0:1::1 2001:DB8:0:0:1::1
2. Text Representation Flexibility of RFC 4291
2.1. Leading Zeros in a 16-Bit Field
'It is not necessary to write the leading zeros in an individual field.'
以0為開頭的字沒必要寫,因為這樣,所以0開頭的字母可寫可不寫,如:
2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001 2001:db8:aaaa:bbbb:cccc:dddd:eeee:001 2001:db8:aaaa:bbbb:cccc:dddd:eeee:01 2001:db8:aaaa:bbbb:cccc:dddd:eeee:1
2.2. Zero Compression
'A special syntax is available to compress the zeros. The use of "::" indicates one or more groups of 16 bits of zeros.'
'The "::" can only appear once in an address.' 可以將1個或多個的"0000"用::這個特殊寫法表示,不過只能出現一次如:
2001:db8:0:0:0::1 2001:db8:0:0::1 2001:db8:0::1 2001:db8::1
2.3. Uppercase or Lowercase
RFC 4291並沒有特別指明說要用大寫或小寫表示之。
3. Problems Encountered with the Flexible Model
簡單來說,就是比對會有問題,比如對eth0指定IPv6網址2001:db8:0:0:1::1,之後用ifconfig eth0| grep 來找卻找不到,因為ifconfig顯示的是2001:db8::1:0:0:1這個IPv6 address。大小寫的差異在UNIX系統更是常見,比如2001:db8:0:0:1:0:0:1和2001:DB8:0:0:1:0:0:1在比對沒有特別指明case insensitive參數就會視為不同。
4. A Recommendation for IPv6 Text Representation
4.1. Handling Leading Zeros in a 16-Bit Field
Leading zeros MUST be suppressed.0開頭的字母都必須被省略,如2001:0db8::0001這是不行的,必須寫成2001:db8::1。
4.2. "::" Usage
The use of the symbol "::" MUST be used to its maximum capability. The symbol "::" MUST NOT be used to shorten just one 16-bit 0 field.
表示式必須盡可能的簡化,而且只有一個"0000"時不能簡化。所以2001:db8:0:0:0:0:2:1是不正確的,應該被寫成2001:db8::2:1。而2001:db8::1:1:1:1:1卻是不正確的,應該被寫成2001:db8:0:1:1:1:1:1。
When there is an alternative choice in the placement of a "::", the longest run of consecutive 16-bit 0 fields MUST be shortened. When the length of the consecutive 16-bit 0 fields are equal, the first sequence of zero bits MUST be shortened.
簡而言之,就是如果有兩個可以簡化::的地方,挑表示式最小,如果都是最小,則挑第一個位置。如2001:0:0:1:0:0:0:1必須被簡化為2001::1:0:0:0:1。
4.3. Lowercase
用小寫表示之。
5. Text Representation of Special Addresses
IPv4-Mapped IPv6 addresses [RFC5214]的寫法要符合前面的規則,且後面為IPv4寫法,如0:0:0:0:0:ffff:192.0.2.1應該被寫成::ffff:192.0.2.1。
6. Notes on Combining IPv6 Addresses with Port Numbers
以下是一般結合port的寫法:
- [2001:db8::1]:80
- 2001:db8::1:80
- 2001:db8::1.80
- 2001:db8::1 port 80
- 2001:db8::1p80
- 2001:db8::1#80
建議使用第一種寫法[2001:db8::1]:80。
作者已經移除這則留言。
回覆刪除https://stackoverflow.com/questions/3736335/tell-whether-a-text-string-is-an-ipv6-address-or-ipv4-address-using-standard-c-s
回覆刪除Tell whether a text string is an IPv6 address or IPv4 address using standard C sockets API
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
struct addrinfo hint, *res = NULL;
int ret;
memset(&hint, '\0', sizeof hint);
hint.ai_family = PF_UNSPEC;
hint.ai_flags = AI_NUMERICHOST;
ret = getaddrinfo(argv[1], NULL, &hint, &res);
if (ret) {
puts("Invalid address");
puts(gai_strerror(ret));
return 1;
}
if(res->ai_family == AF_INET) {
printf("%s is an ipv4 address\n",argv[1]);
} else if (res->ai_family == AF_INET6) {
printf("%s is an ipv6 address\n",argv[1]);
} else {
printf("%s is an is unknown address format %d\n",argv[1],res->ai_family);
}
freeaddrinfo(res);
return 0;
}
$ ./a.out 127.0.0.1
127.0.0.1 is an ipv4 address
$ ./a.out ff01::01
ff01::01 is an ipv6 address
https://stackoverflow.com/questions/3736335/tell-whether-a-text-string-is-an-ipv6-address-or-ipv4-address-using-standard-c-s
回覆刪除Tell whether a text string is an IPv6 address or IPv4 address using standard C sockets API
#include <arpa/inet.h>
#include <stdio.h>
static int
ip_version(const char *src) {
char buf[16];
if (inet_pton(AF_INET, src, buf)) {
return 4;
} else if (inet_pton(AF_INET6, src, buf)) {
return 6;
}
return -1;
}
int
main(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) {
printf("%s\t%d\n", argv[i], ip_version(argv[i]));
}
return 0;
}