master
Harald Wolff 2019-05-10 13:27:03 +02:00
parent ce680a9546
commit d90ab38bec
3 changed files with 53 additions and 3 deletions

View File

@ -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

View File

@ -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<RAT>() 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
);

View File

@ -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);
}
}