public Request(PacketType packetType, string nasIpAddress, ServiceType serviceType, string userName) { _packet = new Packet(packetType); _packet.Attributes.Add(new IpAddressAttribute(AttributeType.NasIpAddress, nasIpAddress)); _packet.Attributes.Add(new ServiceTypeAttribute(serviceType)); _packet.Attributes.Add(new StringAttribute(AttributeType.UserName, userName)); }
/// <summary> /// Parses data received from radius server and creates Packet. /// </summary> /// <param name="source">Data received from radius server</param> /// <returns>Radius packet</returns> //TODO: Implement check of Request Autheticator in packet. public static Packet Parse(byte[] source) { Packet result = null; var offset = 0; if (source.Length < MinPacketLength) return null; var code = source[offset]; offset += PacketCodeLength; foreach (var value in Enum.GetValues(typeof(PacketType)).Cast<int>().Where(value => value == code)) result = new Packet((PacketType)value); if (result == null) result = new Packet(); result._id = source[offset]; offset += IdentifierLength + PacketLengthLength; result._requestAuthenticator = new byte[RequestAuthenticatorLength]; Array.Copy(source, offset, result._requestAuthenticator, 0, RequestAuthenticatorLength); offset += RequestAuthenticatorLength; while (offset + 1 < source.Length) { var attributeLength = source[offset + 1]; if (attributeLength < 3) break; var attribyteArray = new byte[attributeLength]; Array.Copy(source, offset, attribyteArray, 0, attributeLength); var attribute = Attribute.Parse(attribyteArray); if (attribute != null) result._attributes.Add(attribute); offset += attributeLength; } return result; }