What are the valid characters of a hostname? This would be something like a networked computer or a web domain.
To put it in context, I am writing a PC game which connects to a remote server; so I have a field for hostname and a field for port. Obviously the port is a number in the Short range, but I need to know what all the possible hostname characters are (and any other pattern that might be required - does a hostname need to start with a letter?).
Examples of hostname include localhost
or google.com
.
A "name" (Net, Host, Gateway, or Domain name) is a text string up to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus sign (-), and period (.). Note that periods are only allowed when they serve to delimit components of "domain style names". (See RFC-921, "Domain Name System Implementation Schedule", for background). No blank or space characters are permitted as part of a name. No distinction is made between upper and lower case. The first character must be an alpha character. The last character must not be a minus sign or period. A host which serves as a GATEWAY should have "-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as Internet gateways should not use "-GATEWAY" and "-GW" as part of their names. A host which is a TAC should have "-TAC" as the last part of its host name, if it is a DoD host. Single character names or nicknames are not allowed.
This is provided in http://support.microsoft.com/kb/149044
It depends on whether you process IDNs before or after the IDN toASCII algorithm. (that is, do you see the domain name
παράδειγμα.δοκιμή
asπαράδειγμα.δοκιμή
or asxn--hxajbheg2az3al.xn--jxalpdlp
In the latter case - where you are handling IDNs through the punycode, then the old RFC 1123 rules apply:
U+0041 through U+005A (A-Z), U+0061 through U+007A (a-z) case folded as each other, U+0030 through U+0039 (0-9) and U+002D (-). [edit: and U+002E (.) of course; the rules for labels allow the others, with dots between labels, sometimes it's the obvious bits that are easiest to forget]
If you are seeing it in IDN form, the allowed characters are much varied, see http://unicode.org/reports/tr36/idn-chars.html for a handy chart of all valid characters.
Chances are your network code will deal with the punycode, but your display code (or even just passing strings to and from other layers) with the more human-readable form as nobody running a server on the السعودية. domain wants to see their server listed as being on .xn--mgberp4a5d4ar
Checkout this wiki, specifically the section Restrictions on valid host names