Module: Meshname
- Defined in:
- lib/meshname.rb
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.
36 37 38 39 40 41 42 |
# File 'lib/meshname.rb', line 36 def self.getip meshname # decode the meshname to 16 bytes ipv6_binary = Base32.decode meshname.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)
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/meshname.rb', line 16 def self.getname ipv6 # convert IPv6 to 16 bytes ipv6_binary = ipv6.hton # encode the 16 bytes (base32) meshname = Base32.encode ipv6_binary # delete 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/meshname.rb', line 60 def self.resolv domain parts = domain.split "." case parts[-1] when "meshname" # case meshname # for decoding meshname remove .meshname tld dns_server_meshname = domain.delete_suffix ".#{parts[-1]}" # meshname to ipv6 address dns_server = self.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! { |record| IPAddr.new_ntoh record.address } return ip when "meship" # case meship return [self.getip(parts[-2])] end end |