diff --git a/RadiusAttribute.cs b/RadiusAttribute.cs index d4d5694..94df4e3 100644 --- a/RadiusAttribute.cs +++ b/RadiusAttribute.cs @@ -44,6 +44,18 @@ namespace ln.radius return bytes; } + public RadiusAttribute SetBytes(byte[] bytes) + { + this.Bytes = bytes; + return this; + } + + public RadiusAttribute SetText(string text) + { + this.AsText = text; + return this; + } + public static RadiusAttribute Read(Stream stream) @@ -169,6 +181,12 @@ namespace ln.radius return string.Format("[RadiusAttribute Type={0} {1}]", Name, IP.ToString()); } + public IPv4Attribute SetIP(IPv4 ip) + { + this.IP = ip; + return this; + } + } public class IntAttribute : RadiusAttribute @@ -188,6 +206,12 @@ namespace ln.radius { return string.Format("[RadiusAttribute Type={0} {1}]", Name, UInt32); } + + public IntAttribute SetUInt(uint value) + { + this.UInt32 = value; + return this; + } } public class TimeAttribute : RadiusAttribute diff --git a/RadiusMessage.cs b/RadiusMessage.cs index d7c29eb..b30f891 100644 --- a/RadiusMessage.cs +++ b/RadiusMessage.cs @@ -55,6 +55,14 @@ namespace ln.radius Code = radiusCode; } + public RadiusMessage ConstructReply(RadiusCode radiusCode) + { + RadiusMessage reply = new RadiusMessage(EndPoint, radiusCode); + reply.Identifier = Identifier; + reply.Authenticator = Authenticator; + return reply; + } + public RAT GetAttribute() where RAT : RadiusAttribute { foreach (RadiusAttribute radiusAttribute in radiusAttributes) @@ -71,6 +79,24 @@ namespace ln.radius throw new KeyNotFoundException(); } + public void SetAttribute(RadiusAttribute attribute) + { + foreach (RadiusAttribute radiusAttribute in radiusAttributes.ToArray()) + { + if (radiusAttribute.Type == attribute.Type) + radiusAttributes.Remove(radiusAttribute); + } + radiusAttributes.Add(attribute); + } + public void AddAttribute(RadiusAttribute attribute) + { + radiusAttributes.Add(attribute); + } + public void RemoveAttribute(RadiusAttribute attribute) + { + radiusAttributes.Remove(attribute); + } + public byte[] Authenticate(byte[] secret) { byte[] packet = ToBytes(); @@ -109,7 +135,6 @@ namespace ln.radius { using (MemoryStream memoryStream = new MemoryStream()) { - memoryStream.WriteByte((byte)Code); memoryStream.WriteByte((byte)Identifier); memoryStream.WriteShort((short)0); @@ -121,7 +146,7 @@ namespace ln.radius byte[] bytes = memoryStream.ToArray(); Array.Copy( - BitConverter.GetBytes((short)bytes.Length), 0, + BitConverter.GetBytes((short)bytes.Length).BigEndian(), 0, bytes, 2, 2 ); diff --git a/RadiusServer.cs b/RadiusServer.cs index 7fffce0..42cc86f 100644 --- a/RadiusServer.cs +++ b/RadiusServer.cs @@ -57,7 +57,8 @@ namespace ln.radius lock (udp) { - udp.Send(packet, packet.Length, message.EndPoint); + int sent = udp.Send(packet, packet.Length, message.EndPoint); + Logging.Log(LogLevel.DEBUG, "Radius sent {0} bytes to {1}", sent, message.EndPoint); } }