View Source DomainName (domainname v0.1.5)

A module to describe Internet domain names, together with some useful functions (testing if a domain is a subdomain of another, etc).

This module does not implement the DNS protocol, it its only for domain names, independently of how they are used. You may use packages such as dns if you want DNS abilities.

examples

Examples

iex> d = DomainName.new!("something.example.")
iex> d2 = DomainName.new!("EXAMPLE")           
iex> d3 = DomainName.new!("fr")     
iex> DomainName.ends_with?(d, d2)
true
iex> DomainName.ends_with?(d, d3)
false

Link to this section Summary

Types

t()

DomainName is is a domain name. It is an opaque type and you should not access its fields, much less modify them.

Functions

Returns true if the domain name can be a host name (stricter syntax, see RFC 1123, section 2.1)

Decode a domain name from the list of bytes in its wire representation.

Decode a domain name from the list of bytes in its wire representation, and raises an exception if it is not a proper encoding.

Encode a domain name in DNS wire format.

Checks if a domain is a subdomain of one of the domains in the second parameter (and, if true, returns that parent domain).

Checks if a domain is a subdomain of the other one (or a list of others).

Checks if two domain names are identical (in a case-insensitive way).

Checks if a domain is equal to one of the domains in the second parameter (and, if true, returns that domain).

Returns the domain name from the list of labels l.

Returns the domain name from the subdomain s of the domain d.

Returns the domain name from the list of labels l, and raises an exception if there is a problem.

Returns the domain name from the subdomain s of the domain d, and raises an exception if there is a problem.

Returns the list of labels in the domain name.

Returns the domain name as a string.

Creates a DomainName from a string in dot-separated notation. s is the string, options[:must_be_hostname] indicates if the name must follow the stricter "host syntax". Currently, the string must be in pure ASCII (no IDN, see issue #2).

Creates a DomainName from a string in dot-separated notation and raises the exception DomainName.Invalid if options[:must_be_hostname] is true and the name does not have the proper syntax. Apart from that, see the documentation for new/2.

Returns the domain name as a string, in the original case (can be useful for things like case-randomization by resolvers).

Returns the labels of the domain name, in the original case (can be useful for things like case-randomization by resolvers).

Returns the TLD (Top-Level domain) of the domain name.

If a domain is a subdomain of the domain in the second parameter returns :ok and the short version of the first parameter.

Link to this section Types

@opaque t()

DomainName is is a domain name. It is an opaque type and you should not access its fields, much less modify them.

Link to this section Functions

@spec can_be_hostname?(t()) :: boolean()

Returns true if the domain name can be a host name (stricter syntax, see RFC 1123, section 2.1)

examples

Examples

  iex>  d = DomainName.new!("example.com")
  iex> DomainName.can_be_hostname?(d)
  true
  iex> d = DomainName.new!("example$.com")
  iex> DomainName.can_be_hostname?(d)     
  false
@spec decode([integer()]) :: {:ok, t()} | {:error, String.t()}

Decode a domain name from the list of bytes in its wire representation.

@spec decode!([integer()]) :: t()

Decode a domain name from the list of bytes in its wire representation, and raises an exception if it is not a proper encoding.

Link to this function

encode(d, options \\ [])

View Source
@spec encode(t(), [Keyword.t()]) :: [integer()]

Encode a domain name in DNS wire format.

@spec ends_with(t(), [t()]) :: {true, t()} | false

Checks if a domain is a subdomain of one of the domains in the second parameter (and, if true, returns that parent domain).

examples

Examples

iex> d = DomainName.new!("truc.machin.chose")                                                    
iex> l = [DomainName.new!("example.com"), DomainName.new!("machin.chose"), DomainName.new!("fr")]
iex> DomainName.ends_with(d, l)
{true,
  %DomainName{
   labels: ["machin", "chose"],
   name: "machin.chose",
   original: "machin.chose",
   original_labels: ["machin", "chose"]
 }}
iex> l2 = [DomainName.new!("example.com"), DomainName.new!("fr")]                                 
iex> DomainName.ends_with(d, l2)                                 
false
@spec ends_with?(t(), t()) :: boolean()

Checks if a domain is a subdomain of the other one (or a list of others).

examples

Examples

  iex> d1 = DomainName.new!("foobar.example.net") 
  iex> d2 = DomainName.new!("example.net")        
  iex> DomainName.ends_with?(d1, d2)
  true
  iex> d3 = DomainName.new!("org")         
  iex> DomainName.ends_with?(d1, d3)
  false

  iex> d = DomainName.new!("truc.machin.chose")                                                    
  iex> l = [DomainName.new!("example.com"), DomainName.new!("machin.chose"), DomainName.new!("fr")]
  iex> DomainName.ends_with?(d, l)
  true
  iex> l2 = [DomainName.new!("example.com"), DomainName.new!("fr")]                                 
  iex> DomainName.ends_with?(d, l2)                                 
  false
@spec equal?(t(), t()) :: boolean()

Checks if two domain names are identical (in a case-insensitive way).

examples

Examples

  iex>  d1 = DomainName.new!("example.com")
  iex> d2 = DomainName.new!("example.COM")
  iex> DomainName.equal?(d1, d2)
  true
  iex> d3 = DomainName.new!("example.org")
  iex> DomainName.equal?(d1, d3)          
  false
@spec is_one_of(t(), [t()]) :: {true, t()} | false

Checks if a domain is equal to one of the domains in the second parameter (and, if true, returns that domain).

examples

Examples

iex> d = DomainName.new!("machin.chose")                                                    
iex> l = [DomainName.new!("example.com"), DomainName.new!("machin.chose"), DomainName.new!("fr")]
iex> DomainName.is_one_of(d, l)
{true,
  %DomainName{
   labels: ["machin", "chose"],
   name: "machin.chose",
   original: "machin.chose",
   original_labels: ["machin", "chose"]
 }}
iex> l2 = [DomainName.new!("example.com"), DomainName.new!("chose")]                                 
iex> DomainName.is_one_of(d, l2)                                 
false

Returns the domain name from the list of labels l.

examples

Examples

iex> {:ok, r} = DomainName.join(["some", "thing", "example"])
iex> DomainName.name(r)                   
"some.thing.example"
@spec join(String.t(), t() | [String.t()]) :: {:ok, t()} | {:error, String.t()}

Returns the domain name from the subdomain s of the domain d.

examples

Examples

iex> d = DomainName.new!("thing.example")
iex> {:ok, r} = DomainName.join("some", d)
iex> DomainName.name(r)                   
"some.thing.example"
@spec join!([String.t()]) :: t()

Returns the domain name from the list of labels l, and raises an exception if there is a problem.

examples

Examples

iex> r = DomainName.join!(["some", "thing", "example"])
iex> DomainName.name(r)                   
"some.thing.example"
@spec join!(String.t(), t()) :: t()

Returns the domain name from the subdomain s of the domain d, and raises an exception if there is a problem.

examples

Examples

iex> d = DomainName.new!("thing.example")
iex> r = DomainName.join!("some", d)
iex> DomainName.name(r)                   
"some.thing.example"
@spec labels(t()) :: [String.t()]

Returns the list of labels in the domain name.

examples

Examples

iex> {:ok, d} = DomainName.new("foo.bar.example.")
iex> DomainName.labels(d)
["foo", "bar", "example"]
@spec name(t()) :: String.t()

Returns the domain name as a string.

examples

Examples

  iex> d = DomainName.new!("toto.fr") 
  iex> DomainName.name(d)
  "toto.fr"
@spec new(String.t(), [Keyword.t()]) :: {:ok, t()} | {:error, String.t()}

Creates a DomainName from a string in dot-separated notation. s is the string, options[:must_be_hostname] indicates if the name must follow the stricter "host syntax". Currently, the string must be in pure ASCII (no IDN, see issue #2).

examples

Examples

    iex> {:ok, _d} = DomainName.new("toto.fr")

    iex> DomainName.new("toto fr", [must_be_hostname: true])
    {:error, "Not a hostname (they are restricted to LDH)"}
@spec new!(String.t(), [Keyword.t()]) :: t()

Creates a DomainName from a string in dot-separated notation and raises the exception DomainName.Invalid if options[:must_be_hostname] is true and the name does not have the proper syntax. Apart from that, see the documentation for new/2.

examples

Examples

  iex> _d = DomainName.new!("toto.fr")  

  iex> _d = DomainName.new!("toto. fr", [must_be_hostname: true])
  ** (DomainName.Invalid) Not a hostname (they are restricted to LDH)
@spec original(t()) :: String.t()

Returns the domain name as a string, in the original case (can be useful for things like case-randomization by resolvers).

examples

Examples

  iex> d = DomainName.new!("toTO.Fr") 
  iex> DomainName.name(d)
  "toto.fr"
  iex> DomainName.original(d)
  "toTO.Fr"
@spec original_labels(t()) :: [String.t()]

Returns the labels of the domain name, in the original case (can be useful for things like case-randomization by resolvers).

examples

Examples

  iex> d = DomainName.new!("toTO.Fr") 
  iex> DomainName.labels(d)
  ["toto", "fr"]
  iex> DomainName.original_labels(d)
  ["toTO", "Fr"]
@spec tld(t()) :: String.t()

Returns the TLD (Top-Level domain) of the domain name.

examples

Examples

iex> {:ok, d} = DomainName.new("foo.bar.example.")
iex> DomainName.tld(d)
"example"
Link to this function

without_suffix(d, suffix)

View Source
@spec without_suffix(t(), t()) :: {:ok | :error, t()}

If a domain is a subdomain of the domain in the second parameter returns :ok and the short version of the first parameter.

examples

Examples

iex> d = DomainName.new!("truc.machin.chose")
iex> {:ok, s} = DomainName.without_suffix(d, DomainName.new!("machin.chose"))
iex> DomainName.name(s)
"truc"
iex> {:error, _d} = DomainName.without_suffix(d, DomainName.new!("machin.com"))