Class: Baykit::BayServer::Util::IpMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/baykit/bayserver/util/ip_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip_desc) ⇒ IpMatcher

Returns a new instance of IpMatcher.



9
10
11
12
13
14
15
# File 'lib/baykit/bayserver/util/ip_matcher.rb', line 9

def initialize(ip_desc)
  @match_all = (ip_desc == "*")

  if !@match_all
    parse_ip(ip_desc)
  end
end

Instance Attribute Details

#mask_addrObject (readonly)

Returns the value of attribute mask_addr.



7
8
9
# File 'lib/baykit/bayserver/util/ip_matcher.rb', line 7

def mask_addr
  @mask_addr
end

#match_allObject (readonly)

Returns the value of attribute match_all.



5
6
7
# File 'lib/baykit/bayserver/util/ip_matcher.rb', line 5

def match_all
  @match_all
end

#net_addrObject (readonly)

Returns the value of attribute net_addr.



6
7
8
# File 'lib/baykit/bayserver/util/ip_matcher.rb', line 6

def net_addr
  @net_addr
end

Instance Method Details

#get_ip_addr(ip) ⇒ Object



35
36
37
# File 'lib/baykit/bayserver/util/ip_matcher.rb', line 35

def get_ip_addr(ip)
  return IPAddr.new(ip)
end

#match(ip) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/baykit/bayserver/util/ip_matcher.rb', line 17

def match(ip)
  BayLog.debug("match_ip %s net=%s mask=%s", ip, @net_addr, @mask_addr)
  if @match_all
    return true
  else
    if ip.ipv4? != @mask_addr.ipv4?
      # IPv4 and IPv6 don't match each other
      return false
    end

    if ip & @mask_addr != @net_addr
      return false
    end

    return true
  end
end