Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["*.{ex,exs}", "{lib,priv,test}/**/*.{ex,exs}"]
]
24 changes: 0 additions & 24 deletions config/config.exs

This file was deleted.

104 changes: 53 additions & 51 deletions example.exs
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
require Logger
#RadiusApp.start :normal,[]
# RadiusApp.start :normal,[]

#IO.puts inspect RadiusDict.Vendor.by_name("Cisco")
#IO.puts inspect RadiusDict.Attribute.by_name("Service-Type")
#IO.puts inspect RadiusDict.Value.by_name("Service-Type","Login-User")
#IO.puts inspect RadiusDict.Value.by_value(6,11)
#IO.puts inspect RadiusDict.Value.by_name("Cisco","Cisco-Disconnect-Cause","Unknown")
#IO.puts inspect RadiusDict.Value.by_value(9,195,11)
#IO.puts inspect RadiusDict.Value.by_value(9,1950,11)
# IO.puts inspect RadiusDict.Vendor.by_name("Cisco")
# IO.puts inspect RadiusDict.Attribute.by_name("Service-Type")
# IO.puts inspect RadiusDict.Value.by_name("Service-Type","Login-User")
# IO.puts inspect RadiusDict.Value.by_value(6,11)
# IO.puts inspect RadiusDict.Value.by_name("Cisco","Cisco-Disconnect-Cause","Unknown")
# IO.puts inspect RadiusDict.Value.by_value(9,195,11)
# IO.puts inspect RadiusDict.Value.by_value(9,1950,11)

secret = "112233"

attrs = [
{"User-Password","1234"},
#tagged attribute (rfc2868)
{"Tunnel-Type","PPTP"},
#equals
{"Tunnel-Type",{0,"PPTP"}},
{"Tunnel-Type",{10,"PPTP"}},
{"Service-Type","Login-User"},
#tag & value can be integer
{6,1},
#ipaddr
{"NAS-IP-Address",{1,2,3,4}},
{"NAS-IP-Address",0x12345678},
#ipv6addr
{"Login-IPv6-Host",{2003,0xefff,0,0,0,0,0,4}},
#VSA
{{"Vendor-Specific",9},[
{"Cisco-Disconnect-Cause",10},
{195,"Unknown"}
]},
#empty VSA?
{{"Vendor-Specific","Microsoft"},[]},
#some unknown attribute
{255,"123456"}
{"User-Password", "1234"},
# tagged attribute (rfc2868)
{"Tunnel-Type", "PPTP"},
# equals
{"Tunnel-Type", {0, "PPTP"}},
{"Tunnel-Type", {10, "PPTP"}},
{"Service-Type", "Login-User"},
# tag & value can be integer
{6, 1},
# ipaddr
{"NAS-IP-Address", {1, 2, 3, 4}},
{"NAS-IP-Address", 0x12345678},
# ipv6addr
{"Login-IPv6-Host", {2003, 0xEFFF, 0, 0, 0, 0, 0, 4}},
# VSA
{{"Vendor-Specific", 9},
[
{"Cisco-Disconnect-Cause", 10},
{195, "Unknown"}
]},
# empty VSA?
{{"Vendor-Specific", "Microsoft"}, []},
# some unknown attribute
{255, "123456"}
]

#for request packets, leave auth=nil will generate with random bytes
# for request packets, leave auth=nil will generate with random bytes
p = %Radius.Packet{code: "Access-Request", id: 12, auth: nil, secret: secret, attrs: attrs}
#will return an iolist
data = Radius.Packet.encode p
Logger.debug "data=#{inspect data}"
# will return an iolist
data = Radius.Packet.encode(p)
Logger.debug("data=#{inspect(data)}")

p = Radius.Packet.decode :erlang.iolist_to_binary(data),secret
Logger.debug inspect p, pretty: true
p = Radius.Packet.decode(:erlang.iolist_to_binary(data), secret)
Logger.debug(inspect(p, pretty: true))

#for response packets, set auth=request.auth to generate new HMAC-hash with it.
# for response packets, set auth=request.auth to generate new HMAC-hash with it.
p = %Radius.Packet{code: "Access-Accept", id: 12, auth: p.auth, secret: secret, attrs: p.attrs}
data = Radius.Packet.encode p
Logger.debug "data=#{inspect data}"
#password decoding SHOULD FAIL here, guess why?
p = Radius.Packet.decode :erlang.iolist_to_binary(data),p.secret
Logger.debug inspect p, pretty: true
data = Radius.Packet.encode(p)
Logger.debug("data=#{inspect(data)}")
# password decoding SHOULD FAIL here, guess why?
p = Radius.Packet.decode(:erlang.iolist_to_binary(data), p.secret)
Logger.debug(inspect(p, pretty: true))

#wrapper of gen_udp
{:ok,sk} = Radius.listen 1812
# wrapper of gen_udp
{:ok, sk} = Radius.listen(1812)

loop = fn(loop)->
#secret can be a string or a function returning a string
#{:ok,host,p} = Radius.recv sk,"123"
{:ok,host,p} = Radius.recv sk,fn(_host) -> secret end
loop = fn loop ->
# secret can be a string or a function returning a string
# {:ok,host,p} = Radius.recv sk,"123"
{:ok, host, p} = Radius.recv(sk, fn _host -> secret end)

IO.puts "From #{inspect host} : \n#{inspect p, pretty: true}"
IO.puts("From #{inspect(host)} : \n#{inspect(p, pretty: true)}")

resp = %Radius.Packet{code: "Access-Reject", id: p.id, auth: p.auth, secret: p.secret}
Radius.send sk,host,resp
Radius.send(sk, host, resp)

loop.(loop)
end

loop.(loop)
5 changes: 3 additions & 2 deletions lib/radius.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Radius do
wrapper of gen_udp.open
"""
def listen(port) do
:gen_udp.open(port, [{:active, :false}, {:mode, :binary}])
:gen_udp.open(port, [{:active, false}, {:mode, :binary}])
end

@doc """
Expand All @@ -15,8 +15,9 @@ defmodule Radius do
secret :: string | fn({host,port}) -> string
"""
def recv(sk, secret) when is_binary(secret) do
recv(sk, fn(_) -> secret end)
recv(sk, fn _ -> secret end)
end

def recv(sk, secret_fn) when is_function(secret_fn) do
{:ok, {host, port, data}} = :gen_udp.recv(sk, 5000)
secret = secret_fn.({host, port})
Expand Down
Loading