Class: Radiustar::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/radiustar/packet.rb

Constant Summary collapse

CODES =
{ 'Access-Request' => 1,        'Access-Accept' => 2,
'Access-Reject' => 3,         'Accounting-Request' => 4,
'Accounting-Response' => 5,   'Access-Challenge' => 11,
'Status-Server' => 12,        'Status-Client' => 13 }
HDRLEN =

size of packet header

1 + 1 + 2 + 16
P_HDR =

pack template for header

"CCna16a*"
P_ATTR =

pack template for attribute

"CCa*"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary, id, data = nil) ⇒ Packet

Returns a new instance of Packet.



20
21
22
23
24
25
26
27
28
29
# File 'lib/radiustar/packet.rb', line 20

def initialize(dictionary, id, data = nil)
  @dict = dictionary
  @id = id
  unset_all_attributes
  if data
    @packed = data
    self.unpack
  end
  self
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



18
19
20
# File 'lib/radiustar/packet.rb', line 18

def attributes
  @attributes
end

#codeObject

Returns the value of attribute code.



17
18
19
# File 'lib/radiustar/packet.rb', line 17

def code
  @code
end

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/radiustar/packet.rb', line 18

def id
  @id
end

Instance Method Details

#attribute(name) ⇒ Object



63
64
65
# File 'lib/radiustar/packet.rb', line 63

def attribute(name)
  @attributes[name]
end

#gen_authenticatorObject

Generate an authenticator. It will try to use /dev/urandom if possible, or the system rand call if that’s not available.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/radiustar/packet.rb', line 41

def gen_authenticator
  authenticator = []
  8.times do
    authenticator << rand(65536)
  end
  authenticator.pack("n8")
  if (File.exist?("/dev/urandom"))
    File.open("/dev/urandom") do |urandom|
      authenticator = urandom.read(16)
    end
  end
  @authenticator = authenticator
end

#increment_idObject



31
32
33
# File 'lib/radiustar/packet.rb', line 31

def increment_id
  @id = (@id + 1) & 0xff
end

#packObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/radiustar/packet.rb', line 75

def pack

  attstr = ""
  @attributes.each_pair do |attribute, value|
    attribute = @dict.find_attribute_by_name(attribute)
    anum = attribute.id
    val = case attribute.type
    when "string"
      value
    when "integer"
      [attribute.has_values? ? attribute.find_values_by_id(value) : value].pack("N")
    when "ipaddr"
      [inet_aton(value)].pack("N")
    when "date"
      [value].pack("N")
    when "time"
      [value].pack("N")
    else
      next
    end
    attstr += [attribute.id, val.length + 2, val].pack(P_ATTR)
  end

  @packed = [CODES[@code], @id, attstr.length + HDRLEN, @authenticator, attstr].pack(P_HDR)
end

#set_attribute(name, value) ⇒ Object



55
56
57
# File 'lib/radiustar/packet.rb', line 55

def set_attribute(name, value)
  @attributes[name] = value
end

#set_encoded_attribute(name, value, secret) ⇒ Object



71
72
73
# File 'lib/radiustar/packet.rb', line 71

def set_encoded_attribute(name, value, secret)
  @attributes[name] = encode(value, secret)
end

#to_aObject



35
36
37
# File 'lib/radiustar/packet.rb', line 35

def to_a
  @attributes.to_a
end

#unset_all_attributesObject



67
68
69
# File 'lib/radiustar/packet.rb', line 67

def unset_all_attributes
  @attributes = Hash.new
end

#unset_attribute(name) ⇒ Object



59
60
61
# File 'lib/radiustar/packet.rb', line 59

def unset_attribute(name)
  @attributes[name] = nil
end