ln.radius/RadiusAttribute.cs

217 lines
9.1 KiB
C#

// /**
// * File: RadiusAttribute.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.IO;
using ln.types;
using System.Collections.Generic;
using System.Text;
using ln.types.net;
namespace ln.radius
{
public delegate RadiusAttribute RadiusAttributeFactory(byte attributeType);
public class RadiusAttribute
{
public byte Type { get; private set; }
public string Name => AttributeName(Type);
public virtual byte[] Bytes { get; set; }
public RadiusAttribute(byte type)
{
Type = type;
}
public byte[] ToBytes()
{
byte[] vBytes = Bytes;
byte[] bytes = new byte[vBytes.Length + 2];
bytes[0] = Type;
bytes[1] = (byte)bytes.Length;
Array.Copy(vBytes, 0, bytes, 2, vBytes.Length);
return bytes;
}
public static RadiusAttribute Read(Stream stream)
{
byte atype = (byte)stream.ReadByte();
byte alength = (byte)stream.ReadByte();
byte[] abytes = stream.ReadBytes(alength - 2);
RadiusAttribute radiusAttribute = RadiusAttribute.Create(atype);
radiusAttribute.Bytes = abytes;
return radiusAttribute;
}
public override string ToString()
{
foreach (byte by in Bytes)
{
if (by < 0x20)
return String.Format("[RadiusAttribute Type={0} {1}]", Name, BitConverter.ToString(Bytes));
}
return String.Format("[RadiusAttribute Type={0} {1}]", Name, Encoding.ASCII.GetString(Bytes));
}
static Dictionary<byte, string> attributeNames = new Dictionary<byte, string>();
static Dictionary<byte, RadiusAttributeFactory> attributeFactories = new Dictionary<byte, RadiusAttributeFactory>();
public static RadiusAttribute Create(byte attributeType)
{
if (attributeFactories.ContainsKey(attributeType))
return attributeFactories[attributeType](attributeType);
return new RadiusAttribute(attributeType);
}
public static String AttributeName(byte type)
{
if (attributeNames.ContainsKey(type))
return attributeNames[type];
return type.ToString();
}
public static void RegisterAttributeFactory(byte attributeType, string attributeName, RadiusAttributeFactory factory)
{
attributeFactories.Add(attributeType, factory);
attributeNames.Add(attributeType, attributeName);
}
static RadiusAttribute()
{
RegisterAttributeFactory(0x01, "User-Name", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x02, "User-Password", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x03, "CHAP-Password", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x04, "NAS-IP-Address", (type) => new IPv4Attribute(type));
RegisterAttributeFactory(0x05, "NAS-Port", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x06, "Service-Type", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x07, "Framed-Protocol", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x08, "Framed-IP-Address", (type) => new IPv4Attribute(type));
RegisterAttributeFactory(0x09, "Framed-IP-Netmask", (type) => new IPv4Attribute(type));
RegisterAttributeFactory(0x0A, "Framed-Routing", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x0B, "Filter-Id", (type) => new TextAttribute(type));
RegisterAttributeFactory(0x0C, "Framed-MTU", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x0D, "Framed-Compression", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x0E, "Login-IP-Host", (type) => new IPv4Attribute(type));
RegisterAttributeFactory(0x0F, "Login-Service", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x10, "Login-TCP-Port", (type) => new IntAttribute(type));
//RegisterAttributeFactory(0x11, "", (type) => new RadiusAttribute(type)); // Reserved
RegisterAttributeFactory(0x12, "Reply-Message", (type) => new TextAttribute(type));
RegisterAttributeFactory(0x13, "Callback-Number", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x14, "Callback-Id", (type) => new RadiusAttribute(type));
//RegisterAttributeFactory(0x15, "", (type) => new RadiusAttribute(type)); // Reserved
RegisterAttributeFactory(0x16, "Framed-Route", (type) => new TextAttribute(type));
RegisterAttributeFactory(0x17, "Framed-IPX-Network", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x18, "State", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x19, "Class", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x1A, "Vendor-Specific", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x1B, "Session-Timeout", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x1C, "Idle-Timeout", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x1D, "Termination-Action", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x1E, "Called-Station-Id", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x1F, "Calling-Station-Id", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x20, "NAS-Identifier", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x21, "Proxy-State", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x22, "Login-LAT-Service", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x23, "Login-LAT-Node", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x24, "Login-LAT-Group", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x25, "Framed-AppleTalk-Link", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x26, "Framed-AppleTalk-Network", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x27, "Framed-AppleTalk-Zone", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x3C, "CHAP-Challenge", (type) => new RadiusAttribute(type));
RegisterAttributeFactory(0x3D, "NAS-Port-Type", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x3E, "Port-Limit", (type) => new IntAttribute(type));
RegisterAttributeFactory(0x3F, "Login-LAT-Port", (type) => new RadiusAttribute(type));
}
class TextAttribute : RadiusAttribute
{
public String Text { get; set; }
public override byte[] Bytes
{
get => Encoding.UTF8.GetBytes(Text);
set => Text = Encoding.UTF8.GetString(value);
}
public TextAttribute(byte type)
: base(type)
{ }
public override string ToString()
{
return string.Format("[RadiusAttribute Type={0} {1}]", Name, Text);
}
}
class IPv4Attribute : RadiusAttribute
{
public IPv4 IP { get; set; }
public override byte[] Bytes
{
get => IP.IPBytes;
set => IP = new IPv4(value);
}
public IPv4Attribute(byte type)
: base(type)
{ }
public override string ToString()
{
return string.Format("[RadiusAttribute Type={0} {1}]", Name, IP.ToString());
}
}
class IntAttribute : RadiusAttribute
{
public UInt32 UInt32 { get; set; }
public override byte[] Bytes
{
get => UInt32.GetBytes(true);
set => UInt32 = BitConverter.ToUInt32(value.BigEndian(), 0);
}
public IntAttribute(byte type)
: base(type)
{ }
public override string ToString()
{
return string.Format("[RadiusAttribute Type={0} {1}]", Name, UInt32);
}
}
class TimeAttribute : RadiusAttribute
{
DateTimeOffset DateTimeOffset { get; set; }
public override byte[] Bytes
{
get => ((uint)DateTimeOffset.ToUnixTimeSeconds()).GetBytes(true);
set => DateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(BitConverter.ToUInt32(value.BigEndian(),0));
}
public TimeAttribute(byte type)
: base(type)
{ }
public override string ToString()
{
return string.Format("[RadiusAttribute Type={0} {1}]", Name, DateTimeOffset);
}
}
}
}