randolf.ca
1.00
Randolf Richardson's C++ classes
|
This rhostname class provides an object-oriented internet hostname. More...
#include <randolf/rhostname>
Public Member Functions | |
rhostname () noexcept | |
Instantiate an empty rhostname that doesn't qualify as a properly-formatted hostname (because the minimum length of a valid hostname is 1 character). | |
rhostname (const char *hostname, const int len=-1, const int flags=rlabel::HOSTNAME_DEFAULT) | |
Instantiate an rhostname. | |
rhostname (const std::string &hostname, const int flags=rlabel::HOSTNAME_DEFAULT) | |
Instantiate an rhostname. | |
~rhostname () noexcept | |
Destructor. | |
rhostname & | clear () |
Clear this rhostname's underlying hostname and reset all states. | |
rhostname & | fqdn (const bool mode) noexcept |
Specify whether this hostname is an FQDN (has a final dot). | |
const bool | is_fqdn () noexcept |
Find out whether this hostname is an FQDN (it has a final dot). | |
const bool | is_utf8 () noexcept |
Find out whether this hostname is an internationalized internet domain name (it has at least one label that has at least one UTF8/punycode character). | |
std::string | label (const int index=0, const int flags=rlabel::HOSTNAME_DEFAULT) |
Extract a specific label from the underlying hostname. | |
uint | labels () |
Find out how many labels the underlying hostname is comprised of. | |
rhostname & | set (const char *hostname, int len=-1, const int flags=rlabel::HOSTNAME_DEFAULT) |
Replace this rhostname's underlying hostname with a new hostname. | |
rhostname & | set (const std::string &hostname, const int flags=rlabel::HOSTNAME_DEFAULT) |
Replace this rhostname's underlying hostname with a new hostname. | |
std::string | to_dns_rr () |
Convert the underlying hostname to a DNS RR and return it as an std::string. | |
std::string | to_string (const int flags=rlabel::HOSTNAME_DEFAULT) noexcept |
Return as an std::string the hostname that this rhostname represents. | |
std::string | to_utf8 () |
Convert the underlying hostname to UTF-8 and return it as an std::string. | |
std::string | to_xn () |
Convert the underlying hostname to punycode and return it as an std::string. |
This rhostname class provides an object-oriented internet hostname.
Some of the key features are:
#include
lines as needed)std::string
(which tracks its own string length)Validation of the format of a hostname from a variety of angles is helpful in ensuring that hostnames received from elsewhere comply with internet standards.
I created this class to make it easier to write internet server daemons. I also use it in my rmailaddr class when extracting the domain-part
.
An ASCIIZ string is a C-string (char* array) that includes a terminating null (0) character at the end.
I use the term "ASCIIZ string" to indicate an array of characters that's terminated by a 0 (a.k.a., null). Although this is very much the same as a C-string, the difference is that in many API functions a C-string must often be accompanied by its length value. When referring to an ASCIIZ string, I'm intentionally indicating that the length of the string is not needed because the string is null-terminated. (This term was also commonly used in assembly language programming in the 1970s, 1980s, and 1990s, and as far as I know is still used by machine language programmers today.)
Parameter stacking is supported (with methods that return rhostname*
); in this example, notice that semicolons (";") and "h." references are omittted (when compared with the above):
|
inlinenoexcept |
Instantiate an empty rhostname that doesn't qualify as a properly-formatted hostname (because the minimum length of a valid hostname is 1 character).
Instantiating an empty rhostname is particularly useful for header-file definitions; for example:
|
inline |
Instantiate an rhostname.
std::invalid_argument | If the hostname is improperly formatted (flags can limit or eliminate conditions for throwing this exception) |
std::out_of_range | if any DNS RR formatted label's size would require reaching beyond the end of the hostname provided (this is called a buffer overrun, which is a potential security risk that can also cause a segmentation fault) |
hostname | The hostname as a std::string object |
flags | rlabel::HOSTNAME_DNS_RR Convert all labels from DNS RR format rlabel::HOSTNAME_UTF8 Convert all labels to raw UTF-8 format (optional) rlabel::HOSTNAME_XN Convert all labels to punycode format (optional) |
References randolf::rlabel::HOSTNAME_DEFAULT, and set().
|
inline |
Instantiate an rhostname.
std::invalid_argument | If the hostname is improperly formatted (flags can limit or eliminate conditions for throwing this exception) |
std::out_of_range | if any DNS RR formatted label's size would require reaching beyond the end of the hostname provided (this is called a buffer overrun, which is a potential security risk that can also cause a segmentation fault) |
hostname | The hostname as a char* array |
len | Length of hostname string (-1 = ASCIIZ string) |
flags | rlabel::HOSTNAME_DNS_RR Convert all labels from DNS RR format rlabel::HOSTNAME_UTF8 Convert all labels to raw UTF-8 format (optional) rlabel::HOSTNAME_XN Convert all labels to punycode format (optional) |
References randolf::rlabel::HOSTNAME_DEFAULT, and set().
|
inlinenoexcept |
Destructor.
|
inline |
Clear this rhostname's underlying hostname and reset all states.
References rhostname(), and clear().
|
inlinenoexcept |
Find out whether this hostname is an FQDN (it has a final dot).
|
inlinenoexcept |
Specify whether this hostname is an FQDN (has a final dot).
mode | TRUE = ensure this rhostname is an FQDN (has a final dot) FALSE = ensure this rhostname is not an FQDN (does not have a final dot) |
References rhostname().
|
inlinenoexcept |
Find out whether this hostname is an internationalized internet domain name (it has at least one label that has at least one UTF8/punycode character).
|
inline |
Extract a specific label from the underlying hostname.
The index
parameter begins at 0 for the first label. If index
is a negative integer, the counting begins at -1 for the last label.
std::out_of_range | if index is out of range (this is the exception that the std::vector::at method throws) |
index | Which label to extract, with the first label beginning at index 0 (a negative index value starts from the end with -1 as the last label) |
flags | rlabel::HOSTNAME_WITHOUT_EXCEPTIONS returns an empty std::string ("" ) instead of an exception being thrownrlabel::HOSTNAME_DNS_RR Convert label to DNS RR format rlabel::HOSTNAME_UTF8 Convert label to raw UTF-8 format (optional) rlabel::HOSTNAME_XN Convert label to punycode format (optional) |
References randolf::rlabel::HOSTNAME_DEFAULT, and randolf::rlabel::HOSTNAME_WITHOUT_EXCEPTIONS.
|
inline |
Find out how many labels the underlying hostname is comprised of.
|
inline |
Replace this rhostname's underlying hostname with a new hostname.
std::invalid_argument | If the hostname is improperly formatted (flags can limit or eliminate conditions for throwing this exception) |
std::out_of_range | if any DNS RR formatted label's size would require reaching beyond the end of the hostname provided (this is called a buffer overrun, which is a potential security risk that can also cause a segmentation fault) |
hostname | The hostname as a std::string object |
flags | rlabel::HOSTNAME_DNS_RR Convert all labels from DNS RR format rlabel::HOSTNAME_UTF8 Convert all labels to raw UTF-8 format (optional) rlabel::HOSTNAME_XN Convert all labels to punycode format (optional) |
References rhostname(), randolf::rlabel::HOSTNAME_DEFAULT, and set().
Referenced by rhostname(), rhostname(), and set().
|
inline |
Replace this rhostname's underlying hostname with a new hostname.
std::invalid_argument | If the hostname is improperly formatted (flags can limit or eliminate conditions for throwing this exception) |
std::out_of_range | if any DNS RR formatted label's size would require reaching beyond the end of the hostname provided (this is called a buffer overrun, which is a potential security risk that can also cause a segmentation fault) |
hostname | The hostname as a char* array |
len | Length of hostname string (-1 = ASCIIZ string) Providing a zero-length string is equivalent to calling clear() |
flags | rlabel::HOSTNAME_DNS_RR Convert all labels from DNS RR format rlabel::HOSTNAME_UTF8 Convert all labels to raw UTF-8 format (optional) rlabel::HOSTNAME_XN Convert all labels to punycode format (optional) |
References rhostname(), clear(), randolf::rlabel::HOSTNAME_DEFAULT, and randolf::rlabel::HOSTNAME_DNS_RR.
|
inline |
Convert the underlying hostname to a DNS RR and return it as an std::string.
References randolf::rlabel::HOSTNAME_DNS_RR, and to_string().
|
inlinenoexcept |
Return as an std::string
the hostname that this rhostname
represents.
flags | rlabel::HOSTNAME_FQDN_OPT includes the trailing period in the resulting std::string if is_fqdn is TRUE rlabel::HOSTNAME_DNS_RR Convert all labels from DNS RR format rlabel::HOSTNAME_UTF8 Convert all labels to raw UTF-8 format (optional) rlabel::HOSTNAME_XN Convert all labels to punycode format (optional) |
References randolf::rlabel::HOSTNAME_DEFAULT, randolf::rlabel::HOSTNAME_DNS_RR, and randolf::rlabel::HOSTNAME_FQDN_OPT.
Referenced by to_dns_rr(), to_utf8(), and to_xn().
|
inline |
Convert the underlying hostname to UTF-8 and return it as an std::string.
References randolf::rlabel::HOSTNAME_UTF8, and to_string().
|
inline |
Convert the underlying hostname to punycode and return it as an std::string.
References randolf::rlabel::HOSTNAME_XN, and to_string().