public
Description: Distributed system for collecting, processing and accounting network flows written in Erlang
Homepage:
Clone URL: git://github.com/ates/netspire.git
Support for binary encoding of RADIUS packets
wildchild (author)
Sat Jul 26 12:13:52 -0700 2008
commit  bf71f050c45e7752737f4d5134825d09be1bf5d7
tree    490ffed7ff9c0ba65bf5ef610dce9dfbe8fae52b
parent  427f2ae3c110d88687cc2f962e85d6138ac0effc
...
6
7
8
9
 
10
11
12
...
89
90
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
93
94
...
6
7
8
 
9
10
11
12
...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
0
@@ -6,7 +6,7 @@
0
 
0
 -include("radius.hrl").
0
 
0
--export([decode_packet/1, identify_packet/1, encrypt_password/3, attribute_value/2]).
0
+-export([decode_packet/1, encode_packet/1, identify_packet/1, encrypt_password/3, attribute_value/2]).
0
 
0
 decode_packet(Packet) ->
0
     try
0
@@ -89,6 +89,55 @@ attribute_value(Type, Packet) ->
0
             false
0
     end.
0
 
0
+encode_packet(Packet) ->
0
+    #radius_packet{code = Code, ident = Ident, auth = Auth, attrs = Attrs} = Packet,
0
+    Attrs1 = encode_attributes(Attrs, <<>>),
0
+    Length = 20 + byte_size(Attrs1),
0
+    <<Code:8, Ident:8, Length:16, Auth/binary-unit:128, Attrs1/binary>>.
0
+
0
+encode_attributes([], Bin) ->
0
+    Bin;
0
+
0
+encode_attributes([A | Attrs], Bin) ->
0
+    encode_attributes(Attrs, concat_binary([Bin, encode_attribute(A)])).
0
+
0
+encode_attribute({{Id, Type}, Value}) ->
0
+    case radius_dict_srv:lookup_attribute({Id, Type}) of
0
+        not_found ->
0
+            <<>>;
0
+        A ->
0
+            Bin = typecast_value(Value, A#attribute.type),
0
+            Size = byte_size(Bin),
0
+            Length = 7 + Size,
0
+            Length1 = 2 + Size,
0
+            <<?VENDOR_SPECIFIC:8, Length:8, Id:32, Type:8, Length1:8, Bin/binary>>
0
+    end;
0
+
0
+encode_attribute({Type, Value}) ->
0
+    case radius_dict_srv:lookup_attribute(Type) of
0
+        not_found ->
0
+            <<>>;
0
+        A ->
0
+            Bin = typecast_value(Value, A#attribute.type),
0
+            Length = 2 + byte_size(Bin),
0
+            <<Type:8, Length:8, Bin/binary>>
0
+    end.
0
+
0
+typecast_value(Value, _Type) when is_binary(Value) ->
0
+    Value;
0
+
0
+typecast_value(Value, string) ->
0
+    list_to_binary(Value);
0
+
0
+typecast_value(Value, integer) ->
0
+    <<Value:32>>;
0
+
0
+typecast_value(Value, octets) when is_list(Value) ->
0
+    list_to_binary(Value);
0
+
0
+typecast_value({A, B, C, D}, ipaddr) ->
0
+    <<A:8, B:8, C:8, D:8>>.
0
+
0
 identify_packet(1) ->
0
     {ok, 'Access-Request'};
0
 

Comments