Module: Meshname
- Defined in:
- lib/meshname.rb
Overview
Module, which contains functions for Meshname. See github.com/zhoreeq/meshname
Class Method Summary collapse
-
.getip(meshname) ⇒ IPAddr
Calculates an IPv6 address from a Meshname (without .meshname) Links are not triggered.
-
.getname(ipv6) ⇒ String
Calculates a meshname from an IPv6 address (without .meshname).
-
.resolv(domain) ⇒ Array
Resolves a .meshname or .meship domain.
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.
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)
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.
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 |