Class: RadiusRB::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/radiusrb/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(server, options = {}) ⇒ Request

Returns a new instance of Request.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/radiusrb/request.rb', line 24

def initialize(server, options = {})
  @dict = options[:dict].nil? ? Dictionary.default : options[:dict]
  @nas_ip = options[:nas_ip] || get_my_ip(@host)
  @nas_identifier = options[:nas_identifier] || @nas_ip
  @reply_timeout = options[:reply_timeout].nil? ? 60 : options[:reply_timeout].to_i
  @retries_number = options[:retries_number].nil? ? 1 : options[:retries_number].to_i

  @host, @port = server.split(":")

  @port = Socket.getservbyname("radius", "udp") unless @port
  @port = 1812 unless @port
  @port = @port.to_i	# just in case
  @socket = UDPSocket.open
  @socket.connect(@host, @port)
end

Instance Method Details

#accounting_request(status_type, name, secret, sessionid, user_attributes = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/radiusrb/request.rb', line 65

def accounting_request(status_type, name, secret, sessionid, user_attributes = {})

  @packet = Packet.new(@dict, Process.pid & 0xff)
  @packet.code = 'Accounting-Request'

  @packet.set_attribute('User-Name', name)
  @packet.set_attribute('NAS-Identifier', @nas_identifier)
  @packet.set_attribute('NAS-IP-Address', @nas_ip)
  @packet.set_attribute('Acct-Status-Type', status_type)
  @packet.set_attribute('Acct-Session-Id', sessionid)
  @packet.set_attribute('Acct-Authentic', 'RADIUS')

  user_attributes.each_pair do |name, value|
    @packet.set_attribute(name, value)
  end

  @packet.gen_acct_authenticator(secret)

  begin
    send_packet
    @recieved_packet = recv_packet(@reply_timeout)
  rescue Exception => e
    retry if (@retries_number -= 1) > 0
    raise
  end

  return true
end

#accounting_start(name, secret, sessionid, options = {}) ⇒ Object



94
95
96
# File 'lib/radiusrb/request.rb', line 94

def accounting_start(name, secret, sessionid, options = {})
  accounting_request('Start', name, secret, sessionid, options)
end

#accounting_stop(name, secret, sessionid, options = {}) ⇒ Object



102
103
104
# File 'lib/radiusrb/request.rb', line 102

def accounting_stop(name, secret, sessionid, options = {})
  accounting_request('Stop', name, secret, sessionid, options)
end

#accounting_update(name, secret, sessionid, options = {}) ⇒ Object



98
99
100
# File 'lib/radiusrb/request.rb', line 98

def accounting_update(name, secret, sessionid, options = {})
  accounting_request('Interim-Update', name, secret, sessionid, options)
end

#authenticate(name, password, secret, user_attributes = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/radiusrb/request.rb', line 40

def authenticate(name, password, secret, user_attributes = {})
  @packet = Packet.new(@dict, Process.pid & 0xff)
  @packet.gen_auth_authenticator
  @packet.code = 'Access-Request'
  @packet.set_attribute('User-Name', name)
  @packet.set_attribute('NAS-Identifier', @nas_identifier)
  @packet.set_attribute('NAS-IP-Address', @nas_ip)
  @packet.set_encoded_attribute('User-Password', password, secret)

  user_attributes.each_pair do |name, value|
    @packet.set_attribute(name, value)
  end

  begin
    send_packet
    @recieved_packet = recv_packet(@reply_timeout)
  rescue Exception => e
    retry if (@retries_number -= 1) > 0
    raise
  end

  reply = { :code => @recieved_packet.code }
  reply.merge @recieved_packet.attributes
end

#inspectObject



106
107
108
# File 'lib/radiusrb/request.rb', line 106

def inspect
  to_s
end