Skip to main content

Module domainname

Module domainname 

Source
Available on crate feature net only.
Expand description

Domain name type for DNS programming.

This module provides a type-safe abstraction for DNS domain names, ensuring compliance with RFC 1035 domain name specifications.

§RFC 1035 Domain Name Rules

According to RFC 1035 §2.3.4:

  • Total length: 1-253 characters
  • Each label (segment separated by dots): 1-63 characters
  • Valid characters: letters (a-z, A-Z), digits (0-9), and hyphens (-)
  • Labels cannot start or end with a hyphen
  • Domain names are case-insensitive (stored in lowercase internally)
  • Only ASCII characters are allowed
  • Labels CAN start with digits (unlike RFC 1123 hostnames)

§Domain Name vs Hostname

The key difference between DomainName and Hostname:

  • DomainName (RFC 1035): Labels can start with digits (e.g., “123.example.com”)
  • Hostname (RFC 1123): Labels must start with letters (e.g., “www.example.com”)

§Examples

use bare_types::net::DomainName;

// Create a domain name
let domain = DomainName::new("example.com")?;

// Check depth (number of labels)
assert_eq!(domain.depth(), 2);

// Check if it's a subdomain
let parent = DomainName::new("example.com")?;
let child = DomainName::new("www.example.com")?;
assert!(child.is_subdomain_of(&parent));

// Get the string representation
assert_eq!(domain.as_str(), "example.com");

// Parse from string
let domain: DomainName = "123.example.com".parse()?;

Structs§

DomainName
A DNS domain name.

Enums§

DomainNameError
Error type for domain name validation.