Class: Radiustar::Request

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/radiustar/request.rb', line 7

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/radiustar/request.rb', line 48

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



77
78
79
# File 'lib/radiustar/request.rb', line 77

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

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



85
86
87
# File 'lib/radiustar/request.rb', line 85

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

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



81
82
83
# File 'lib/radiustar/request.rb', line 81

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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/radiustar/request.rb', line 23

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



89
90
91
# File 'lib/radiustar/request.rb', line 89

def inspect
  to_s
end