Module: Meshname

Defined in:
lib/meshname.rb

Overview

Module, which contains functions for Meshname. See github.com/zhoreeq/meshname

Class Method Summary collapse

Class Method Details

.getip(meshname) ⇒ IPAddr

Calculates an IPv6 address from a Meshname (without .meshname) Links are not triggered. The mesh name is treated like a .meship domain.

Examples:

Meshname.getip "aikqcxee4cg5k5mqx7gwdt6p64" => #<IPAddr: IPv6:0215:015c:84e0:8dd5:7590:bfcd:61cf:cff7/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
Meshname.getip("aikqcxee4cg5k5mqx7gwdt6p64").to_s => "215:15c:84e0:8dd5:7590:bfcd:61cf:cff7"

Parameters:

  • meshname (String)

Returns:

  • (IPAddr)

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
# File 'lib/meshname.rb', line 41

def self.getip meshname
  raise ArgumentError, 'Cannot convert argument to string' if ! meshname.respond_to? :to_s

  # decode the meshname to 16 bytes
  ipv6_binary = Base32.decode meshname.to_s.upcase
  # convert the 16 bytes to a ipv6
  ipv6 = IPAddr.new_ntoh ipv6_binary
  return ipv6
end

.getname(ipv6) ⇒ String

Calculates a meshname from an IPv6 address (without .meshname)

Examples:

Meshname.getname IPAddr.new("215:15c:84e0:8dd5:7590:bfcd:61cf:cff7") => "aikqcxee4cg5k5mqx7gwdt6p64"

Create a .nameip domain

"#{Meshname.getname IPAddr.new("215:15c:84e0:8dd5:7590:bfcd:61cf:cff7")}.meship" => "aikqcxee4cg5k5mqx7gwdt6p64.meship"

Parameters:

  • ipv6 (IPAddr)

Returns:

  • (String)

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/meshname.rb', line 18

def self.getname ipv6
  raise ArgumentError, 'IPv6 must be of type IPAddr' if ! ipv6.is_a? IPAddr
  raise ArgumentError, 'IP address must be a IPv6' if ! ipv6.ipv6?

  # convert IPv6 to 16 bytes
  ipv6_binary = ipv6.hton
  # encode the 16 bytes (base32)
  meshname = Base32.encode ipv6_binary
  # delete the padding and convert up in down letters
  meshname.delete! '='
  meshname.downcase!

  return meshname
end

.resolv(domain) ⇒ Array

Resolves a .meshname or .meship domain. Links are taken into account.

An array of IP addresses (type: IPAddr) is always returned. The reason for the array is that a name server can store several AAAA records for a domain name. In order to keep the result the same, an array is always returned with a .meship domain, but this then only contains one entry.

Examples:

Resolv a .meship domain

Meshname.resolv("aikqcxee4cg5k5mqx7gwdt6p64.meship") => [#<IPAddr: IPv6:0215:015c:84e0:8dd5:7590:bfcd:61cf:cff7/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>]
Meshname.resolv("aikqcxee4cg5k5mqx7gwdt6p64.meship")[0].to_s => "215:15c:84e0:8dd5:7590:bfcd:61cf:cff7"

Resolv a .meshname domain

Meshname.resolv("aikqcxee4cg5k5mqx7gwdt6p64.meshname") => [#<IPAddr: IPv6:0215:015c:84e0:8dd5:7590:bfcd:61cf:cff7/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>]
Meshname.resolv("aikqcxee4cg5k5mqx7gwdt6p64.meshname")[0].to_s => "215:15c:84e0:8dd5:7590:bfcd:61cf:cff7"

Parameters:

  • domain (String)

Returns:

  • (Array)

    Array of IPAddr



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/meshname.rb', line 67

def self.resolv domain
  raise 'Cannot convert argument to string' if ! domain.respond_to? :to_s

  parts = domain.to_s.split '.'
  case parts[-1]
  when 'meshname'  # case meshname
    # for decoding meshname remove .meshname tld
    dns_server_meshname = domain.to_s.delete_suffix ".#{parts[-1]}"

    # meshname to ipv6 address
    dns_server = getip dns_server_meshname

    # create DNSResolver for using custom nameserver
    dns_resolver = Resolv::DNS.new nameserver: [dns_server.to_s]
    # request nameserver for domain (that's why also subdomains possible)
    ip = dns_resolver.getaddresses domain
    # transform Resolv::IPv6 to IPAddr
    ip.map! do |record|
      IPAddr.new_ntoh record.address
    end

    return ip
  when 'meship'  # case meship
    return [getip(parts[-2])]
  else
    raise ArgumentError, 'Unknown TLD'
  end
end