{"id":7873,"date":"2012-04-12T07:00:00","date_gmt":"2012-04-12T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/04\/12\/what-is-the-real-maximum-length-of-a-dns-name\/"},"modified":"2021-01-05T20:50:26","modified_gmt":"2021-01-06T04:50:26","slug":"20120412-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120412-00\/?p=7873","title":{"rendered":"What is the real maximum length of a DNS name?"},"content":{"rendered":"<p>The maximum length of a DNS name is 255 octets. This is spelled out in <a href=\"http:\/\/tools.ietf.org\/html\/rfc1035\">RFC 1035<\/a> <a href=\"http:\/\/www.freesoft.org\/CIE\/RFC\/1035\/9.htm\">section 2.3.4<\/a>. A customer didn&#8217;t understand why the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms682032(VS.85).aspx\"> DnsValidateName<\/a> was rejecting the following string:<\/p>\n<blockquote class=\"m\"><p>(63 letters).(63 letters).(63 letters).(62 letters)<\/p><\/blockquote>\n<p>The documentation says<\/p>\n<blockquote class=\"q\">\n<p>Returns ERROR_INVALID_NAME if the DNS name<\/p>\n<ul>\n<li>Is longer than 255 octets.<\/li>\n<li>Contains a label longer than 63 octets.<\/li>\n<li>&#8230; other criteria not relevant here&#8230;<\/li>\n<\/ul>\n<p>The length of the domain name passed in is 63+1+63+1+63+1+62=254 characters, just under the length limit of 255. Why is it rejecting this name that is under the limit?<\/p>\n<\/blockquote>\n<p>Because the limit isn&#8217;t the number of characters; it&#8217;s the number of octets.<\/p>\n<p>Section 3.3 says that a <i>domain-name<\/i> is represented as a series of <i>labels<\/i>, and is terminated by a label of length zero. (The label of length zero <a href=\"http:\/\/www.freesoft.org\/CIE\/RFC\/1035\/11.htm\"> represents the root label<\/a>.) A label consists of a length octet followed by that number of octets representing the name itself. Therefore, the domain name <code>www.microsoft.com<\/code> is encoded as follows:<\/p>\n<table style=\"text-align: center; border-collapse: collapse;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 2em; padding: 0;\"><code>3<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'w'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'w'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'w'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>9<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'m'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'i'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'c'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'r'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'o'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'s'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'o'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'f'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'t'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>3<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'c'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'o'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>'m'<\/code><\/td>\n<td style=\"width: 2em; padding: 0;\"><code>0<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Technically, <code>www.microsoft.com<\/code> is shorthand for <code>www.microsoft.com.<\/code> with a trailing period, and the trailing zero byte encodes that implied period.<\/p>\n<p>If you sit down and do the math, you&#8217;ll see that the the readable maximum length of an ASCII DNS name is 253 characters: You don&#8217;t encode the dots, but you do encode the length bytes, so they cancel out, except for the length byte of the first label and the length byte of the root label, for an additional cost of two bytes. (On the off chance that you explicitly specified the root label, don&#8217;t count it towards the 253-character limit.)<\/p>\n<p>If you use UTF-8 encoding, then the maximum length is harder to describe since UTF-8 is a variable-length encoding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s 255, but you have to count it the right way.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[104],"class_list":["post-7873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-tipssupport"],"acf":[],"blog_post_summary":"<p>It&#8217;s 255, but you have to count it the right way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/7873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=7873"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/7873\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=7873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=7873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=7873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}