class Blondy::DHCPD::Dispatcher

Public Class Methods

dispatch(data, ip, port) click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 10
def dispatch(data, ip, port)
  @data = DHCP::Message.from_udp_payload(data) rescue raise(IncorrectMessage, 'Incorrect message received.')
  raise(IncorrectMessage, 'Incorrect message received.') unless @data
  set_hwaddr
  reply
end

Private Class Methods

call(method) click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 33
def call(method)
    @reply = OpenStruct.new
    send(method)
end
create_reply() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 57
def create_reply
  set_pool_data
  set_src
  set_other
  @reply
end
discover_handler() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 38
def discover_handler
    @reply.data = DHCP::Offer.new
    create_reply
end
handle(msg_class) click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 24
def handle(msg_class)
  if %w{discover request inform release}.include?(msg_class)
    @pool = Pool.query(@data.hwaddr, msg_class.to_sym)
    @pool ? call("#{msg_class}_handler".to_sym) : false
  else
    raise NoMessageHandler, 'No appropriate handler for message.'
  end
end
inform_handler() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 52
def inform_handler
  @reply.data = DHCP::ACK.new
  create_reply
end
release_handler() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 48
def release_handler
  @reply.data = nil
end
reply() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 19
def reply
  msg_class = @data.class.to_s.gsub(/^.*::/, '').downcase
  handle(msg_class)
end
request_handler() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 43
def request_handler
    @reply.data = DHCP::ACK.new
    create_reply
end
set_hwaddr() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 64
def set_hwaddr
  DHCP::Message.class_eval {attr_accessor :hwaddr}
  @data.hwaddr = @data.chaddr.take(@data.hlen).map {|x| x.to_s(16).size<2 ? '0'+x.to_s(16) : x.to_s(16)}.join(':')
end
set_other() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 89
def set_other
  @reply.data.siaddr = IPAddr.new(Blondy::DHCPD::CONFIG['server_ip']).to_i
  @reply.data.xid = @data.xid if @data.xid
  @reply.data.chaddr = @data.chaddr
end
set_pool_data() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 82
def set_pool_data
  @reply.data.yiaddr = @pool.data.yiaddr
  @reply.data.fname = @pool.data.fname
  @reply.data.options = @pool.data.options
  @reply.reply_addr = @pool.reply_addr
end
set_src() click to toggle source
# File lib/blondy/dhcpd/dispatcher.rb, line 69
def set_src
  if @data.giaddr == 0 and @data.ciaddr != 0
    @reply.ip = IPAddr.new(@data.ciaddr, family = Socket::AF_INET).to_s
    @reply.port = 68
  elsif @data.giaddr != 0
    @reply.ip = IPAddr.new(@data.giaddr, family = Socket::AF_INET).to_s
    @reply.port = 67
  else
    @reply.ip = @reply.reply_addr
    @reply.port = 68
  end
end