http://blogs.msdn.com/b/oldnewthing/archive/2012/04/12/10292868.aspx
Themaximum length of a DNS name is 255 octets. This is spelled out in RFC 1035 section 2.3.4. A customer didn'tunderstand why the DnsValidateName was rejecting thefollowing string:
(63letters).(63 letters).(63 letters).(62 letters)
Thedocumentation says
Returns ERROR_INVALID_NAME if the DNS name
· Is longer than 255 octets.
· Contains a label longer than 63 octets.
· ... other criteria not relevant here...
The length of the domain name passed in is 63+1+63+1+63+1+62=254characters, just under the length limit of 255. Why is it rejecting this namethat is under the limit?
Becausethe limit isn't the number of characters; it's the number of octets.
Section3.3 says that a domain-name is represented as a seriesof labels, and is terminated by a label of length zero. (The labelof length zero represents the root label.) A label consists of alength octet followed by that number of octets representing the name itself.Therefore, the domain name www.microsoft.com is encoded as follows:
3 | 'w' | 'w' | 'w' | 9 | 'm' | 'i' | 'c' | 'r' | 'o' | 's' | 'o' | 'f' | 't' | 3 | 'c' | 'o' | 'm' | 0 |
Technically, www.microsoft.com isshorthand for www.microsoft.com. with a trailing period, and thetrailing zero byte encodes that implied period.
Ifyou sit down and do the math, you'll see that the the readable maximum lengthof an ASCII DNS name is 253 characters: You don't encode the dots, but you doencode the length bytes, so they cancel out, except for the length byte of thefirst label and the length byte of the root label, for an additional cost oftwo bytes. (On the off chance that you explicitly specified the root label,don't count it towards the 253-character limit.)
Ifyou use UTF-8 encoding, then the maximum length is harder to describe sinceUTF-8 is a variable-length encoding.
http://blog.chinaunix.net/uid-26098488-id-2979143.html
客户报告说测试dns时如果输入的域名长度为128位字符,就会发出错误的dns包,看代码什么的都没有问题,可是wireshark抓到得就是错误的包,估计是
dns长度有限制, 于是问google, 一大堆结果说什么的都有,有的说长度不能超过20个
字符,有的说2-46,还有的说国际域名长度不能超过67个字符,结果测试下来都不对,没办法,只能找rfc,
结果一看就明白了(以后要坚定了,这种问题直接找rfc才最有效),rfc1035中有说明, 为了和现有的域名兼
用,域名应该满足由各个lables以点号分隔,各lable长度应该在63个字符之内,而且总长度应该不超过255.
简单的说,以下格式中:
***.***.com
***.***.cn
***.***.org
等等的域名中,***的长度应该不超过63,而总长度不应该超过255.
--------附上 rfc1035 [page10]中的相关内容:
3. DOMAIN NAME SPACE AND RR DEFINITIONS
3.1. Name space definitions
Domain names in messages are expressed in terms of a sequence oflabels.
Each label is represented as a one octet length field followed bythat
number of octets. Sinceevery domain name ends with the null label of
the root, a domain name is terminated by a length byte ofzero. The
high order two bits of every length octet must be zero, and the
remaining six bits of the length field limit the label to 63octets or
less.
To simplify implementations, the total length of a domain name(i.e.,
label octets and label length octets) is restricted to 255 octetsor
less.
Although labels can contain any 8 bit values in octets that makeup a
label, it is strongly recommended that labels follow the preferred
syntax described elsewhere in this memo, which is compatible with
existing host naming conventions. Name servers and resolvers must
compare labels in a case-insensitive manner (i.e., A=a), assumingASCII
with zero parity. Non-alphabetic codes must match exactly.