module BBS::Address
What an IP address is: a caller’s own line on the internet, or one of the board’s neighbours (a proxy on the Docker network, the loopback).
BBS::Address.local?('172.18.0.5') # => true (bbs-web on the stack) BBS::Address.public?('100.64.1.2') # => false (carrier-grade NAT)
Constants
- NON_PUBLIC
-
The ranges that are not on the public internet beyond what IPAddr tells (private, loopback, link-local): this network, carrier-grade NAT, the IETF, documentation and benchmark ranges, multicast and the reserved block, IPv6 unspecified, IPv4-translated, discard, documentation and multicast.
Public Instance Methods
Source
# File lib/bbs/address.rb, line 36 def local?(ip) addr = parse(ip) or return false addr.private? || addr.loopback? || addr.link_local? end
Returns whether ip is a neighbour of the board: a private, loopback or link-local address (IPv6 unique local included). false for anything that is not an IP address.
Source
# File lib/bbs/address.rb, line 27 def parse(ip) (ip.is_a?(IPAddr) ? ip : IPAddr.new(ip.to_s)).native rescue IPAddr::Error nil end
Returns ip (a String or an IPAddr) as an IPAddr, an IPv4-mapped IPv6 address as its IPv4 one; nil when it is not an IP address.
Source
# File lib/bbs/address.rb, line 44 def public?(ip) addr = parse(ip) or return false !local?(addr) && NON_PUBLIC.none? { |range| range.family == addr.family && range.include?(addr) } end
Returns whether ip is an address on the public internet: not local? and in none of the NON_PUBLIC ranges. false for anything that is not an IP address.