/// <summary> /// /// </summary> /// <param name="avp"></param> /// <param name="dict"></param> /// <returns></returns> private byte[] ProcessNestedGroupedAvp(Avp avp, Dictionary <int, AttributeInfo> dict) { //Process the Further nested Avps `````````````here. Untested Code //Copy Nested Avp Code to GroupedBytes int AvpCode = avp.AvpCode; //Change the Length to Dynamic Length of Avp byte[] nestedAvpBytes = new byte[1024]; int destOffset = 0; //Copy Grouped Avp Code to GroupedBytes int gAvpCode = IPAddress.HostToNetworkOrder(AvpCode); Buffer.BlockCopy(BitConverter.GetBytes(gAvpCode), 0, nestedAvpBytes, 0, 4); destOffset += 4; //AvpFlags {V M P r r r r r} byte nestedAvpFlags = (byte)(0 << 0); nestedAvpFlags = (byte)(0 << 1); nestedAvpFlags = (byte)(0 << 2); nestedAvpFlags = (byte)(0 << 3); nestedAvpFlags = (byte)(0 << 5); if (avp.isVendorSpecific) { nestedAvpFlags = (byte)(0 << 7); } if (isMandatory) { nestedAvpFlags = (byte)(1 << 6); } Buffer.BlockCopy(BitConverter.GetBytes(nestedAvpFlags), 0, nestedAvpBytes, destOffset, 1); destOffset += 1; //Leaving 3 Bytes Place Holder for Copying Actual Length of Grouped Avp at the end of method because we can know this length at the end of copying all Avps destOffset += 3; //Avp Value Bytes int Padding = 0; byte[] retValueBytes = GetAvpValueBytes(avp, ref Padding, dict); //Grouped Avp Length int lenOfNestedAvp = IPAddress.HostToNetworkOrder(destOffset); Buffer.BlockCopy(BitConverter.GetBytes(lenOfNestedAvp), 1, nestedAvpBytes, 5, 3); byte[] retVal = new byte[destOffset]; Buffer.BlockCopy(nestedAvpBytes, 0, retVal, 0, destOffset); return(retVal); }
/// <summary> /// /// </summary> /// <param name="stackContext"></param> private static void Process_CEA(object sender, object e) { Common.StackLog.Write2TraceLog("Process_CEA", "Entering Process_CEA"); Message cea = (e as Message); Avp ResultCode = cea.avps.Find(a => a.AvpCode == 268); if ((int)ResultCode.AvpValue == 2001) { Common.StackLog.Write2TraceLog("Process_CEA", "Recieved CEA with Succcess Code"); //Update Peer Entry in PeerTable UpdateConnectionState(stackContext.peers[0], PeerState.OPEN); } else { Common.StackLog.Write2TraceLog("Process_CEA", "Error in Connecting to the Remote Peer"); cea.PrintMessage(); } Common.StackLog.Write2TraceLog("Process_CEA", "Exiting Process_CEA"); }
/// <summary> /// This method Creates CAPABILITY EXCHANGE REQUEST MESSAGE /// </summary> /// <returns></returns> public Message CapabilityExchangeRequest(StackContext stackContext) { Message cerReq = new Message(stackContext); cerReq.CommandCode = DiameterMessageCode.CAPABILITY_EXCHANGE; cerReq.EndtoEndIdentifier = cerReq.GetEndToEndIdentifier(); cerReq.HopbyHopIdentifier = cerReq.GetHopByHopIdentifier(); cerReq.ApplicationID = 0; //Set Mandatory Attributes cerReq.avps.Add(new Avp(DiameterAvpCode.Origin_Host, stackContext.OrigionHost) { isMandatory = true }); cerReq.avps.Add(new Avp(DiameterAvpCode.Origin_Realm, stackContext.OrigionRealm) { isMandatory = true }); cerReq.avps.Add(new Avp(DiameterAvpCode.Host_IP_Address, IPAddress.Parse(Utility.LocalIPAddress())) { isMandatory = true }); cerReq.avps.Add(new Avp(DiameterAvpCode.Vendor_Id, 42452) { isMandatory = true }); cerReq.avps.Add(new Avp(DiameterAvpCode.Product_Name, "EMI Networks (SMC-PVT) LTD.") { isMandatory = true }); cerReq.avps.Add(new Avp(DiameterAvpCode.Origin_State_Id, 1)); cerReq.avps.Add(new Avp(DiameterAvpCode.Supported_Vendor_Id, 10415)); cerReq.avps.Add(new Avp(DiameterAvpCode.Auth_Application_Id, 167772151)); //cerReq.avps.Add(new Avp(DiameterAvpCode.Inband_Security_Id, 1234)); //Not Recommended in CER/CEA Messages cerReq.avps.Add(new Avp(DiameterAvpCode.Acct_Application_Id, 0)); ////Create Vendor_Specific_Application_Id Grouped Avp Avp avpVendorSpecificAppId = new Avp(DiameterAvpCode.Vendor_Specific_Application_Id) { isGrouped = true }; Avp avpVendorId = new Avp(DiameterAvpCode.Vendor_Id, 11); Avp avpAuthApplicationId = new Avp(DiameterAvpCode.Auth_Application_Id, 167772151); avpVendorSpecificAppId.groupedAvps.Add(avpVendorId); //Add Avp to Grouped Avp avpVendorSpecificAppId.groupedAvps.Add(avpAuthApplicationId); //Add Avp to Grouped Avp cerReq.avps.Add(avpVendorSpecificAppId); //Now Add Avp to CER Command //Second Loop avpVendorSpecificAppId = new Avp(DiameterAvpCode.Vendor_Specific_Application_Id) { isGrouped = true }; avpVendorSpecificAppId.groupedAvps.Clear(); avpVendorSpecificAppId.groupedAvps.Add(avpVendorId); Avp avpAcctApplicationId = new Avp(DiameterAvpCode.Acct_Application_Id, 0); avpVendorSpecificAppId.groupedAvps.Add(avpAcctApplicationId); cerReq.avps.Add(avpVendorSpecificAppId); cerReq.avps.Add(new Avp(DiameterAvpCode.Firmware_Revision, 1)); return(cerReq); }
public static sbyte[] encodeAvp(Avp avp) { sbyte[] bytes = new sbyte[0]; return(bytes); }
/// <summary> /// Returns Grouped Avp Bytes Including Nested Grouped Avps /// </summary> /// <param name="AvpCode"></param> /// <returns></returns> private byte[] GetGroupedAvpBytes(Avp gAvp, Dictionary <int, AttributeInfo> dict) { //Dest Array Offset int destOffset = 0; int AvpCode = gAvp.AvpCode; //We may change the Length to Dynamic Length of Avp, If we can figure out to calculate it before processing byte[] groupedAvpBytes = new byte[1024]; //Copy Grouped Avp Code to GroupedBytes int gAvpCode = IPAddress.HostToNetworkOrder(AvpCode); Buffer.BlockCopy(BitConverter.GetBytes(gAvpCode), 0, groupedAvpBytes, 0, 4); destOffset += 4; //Set Flags for the the Avp AvpFlags {V M P r r r r r} byte gAvpFlags = (byte)(0 << 0); gAvpFlags = (byte)(0 << 1); gAvpFlags = (byte)(0 << 2); gAvpFlags = (byte)(0 << 3); gAvpFlags = (byte)(0 << 5); if (gAvp.isVendorSpecific) { gAvpFlags = (byte)(1 << 7); } if (isMandatory) { gAvpFlags = (byte)(1 << 6); } Buffer.BlockCopy(BitConverter.GetBytes(gAvpFlags), 0, groupedAvpBytes, destOffset, 1); destOffset += 1; //Leaving 3 Bytes Place Holder for Copying Actual Length of Grouped Avp at the end of method because we can know this length at the end of copying all Avps destOffset += 3; if (gAvp.isVendorSpecific) { UInt32 vId = (UInt32)IPAddress.HostToNetworkOrder(gAvp.VendorId); Buffer.BlockCopy(BitConverter.GetBytes(vId), 0, groupedAvpBytes, destOffset, 4); destOffset += 4; } //Now Copy All Avps inside Grouped Avp foreach (Avp avp in gAvp.groupedAvps) { byte[] totalAvpBytes = GetAvpBytes(avp, dict); Buffer.BlockCopy(totalAvpBytes, 0, groupedAvpBytes, destOffset, totalAvpBytes.Length); destOffset += totalAvpBytes.Length; } //Set the Length of Grouped Avp UInt32 LenOfGroupedAvp = (UInt32)IPAddress.HostToNetworkOrder(destOffset); Buffer.BlockCopy(BitConverter.GetBytes(LenOfGroupedAvp), 1, groupedAvpBytes, 5, 3); byte[] retVal = new byte[destOffset]; Buffer.BlockCopy(groupedAvpBytes, 0, retVal, 0, destOffset); return(retVal); }
/// <summary> /// /// </summary> /// <returns></returns> public byte[] GetAvpValueBytes(Avp avp, ref int Padding, Dictionary <int, AttributeInfo> dict) { int AvpCode = avp.AvpCode; object Value = avp.AvpValue; if (!dict.ContainsKey(AvpCode)) { Common.StackLog.Write2ErrorLog("GetValueBytes", "Lookup For Avp [" + AvpCode + "] "); Exception exp = new Exception("Avp Code:" + AvpCode.ToString() + " not found in dictionary"); Common.StackLog.Write2ErrorLog("GetValueBytes", "Lookup For Avp [" + AvpCode + "] "); throw exp; } AttributeInfo avpInfo = dict[AvpCode]; // Console.WriteLine("Success"); switch (avpInfo.DataType) { case "Address": { short addressFamily = 1; addressFamily = IPAddress.HostToNetworkOrder(addressFamily); byte[] addressFamilyBytes = BitConverter.GetBytes(addressFamily); byte[] addressBytes = IPAddress.Parse(Value.ToString()).GetAddressBytes(); //Calculate Padding int remainder = (addressFamilyBytes.Length + addressBytes.Length) % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[addressFamilyBytes.Length + addressBytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(addressFamilyBytes, 0, retVal, 0, addressFamilyBytes.Length); System.Buffer.BlockCopy(addressBytes, 0, retVal, 2, addressBytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "OctetString": { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); byte[] OctetStringBytes = encoding.GetBytes(Value.ToString()); //Calculate Padding int remainder = (OctetStringBytes.Length) % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[OctetStringBytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(OctetStringBytes, 0, retVal, 0, OctetStringBytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "DiamIdent": { byte[] identityBytes = System.Text.Encoding.UTF8.GetBytes(Value.ToString()); //Calculate Padding int remainder = identityBytes.Length % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[identityBytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(identityBytes, 0, retVal, 0, identityBytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "Unsigned32": { byte[] UINTBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)Value)); //Calculate Padding int remainder = UINTBytes.Length % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[UINTBytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(UINTBytes, 0, retVal, 0, UINTBytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "Integer32": { byte[] UINTBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)Value)); //Calculate Padding int remainder = UINTBytes.Length % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[UINTBytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(UINTBytes, 0, retVal, 0, UINTBytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "Integer8": { long avpVal = Convert.ToInt64(Value); byte[] U64Bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(avpVal)); //Calculate Padding int remainder = U64Bytes.Length % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[U64Bytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(U64Bytes, 0, retVal, 0, U64Bytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "Unsigned64": { long avpVal = Convert.ToInt64(Value); byte[] U64Bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(avpVal)); //Calculate Padding int remainder = U64Bytes.Length % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[U64Bytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(U64Bytes, 0, retVal, 0, U64Bytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "Integer64": { long avpVal = Convert.ToInt64(Value); byte[] U64Bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(avpVal)); //Calculate Padding int remainder = U64Bytes.Length % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[U64Bytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(U64Bytes, 0, retVal, 0, U64Bytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "UTF8String": { byte[] UTF8Bytes = Encoding.UTF8.GetBytes(Value.ToString()); //Calculate Padding int remainder = (UTF8Bytes.Length) % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[UTF8Bytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(UTF8Bytes, 0, retVal, 0, UTF8Bytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "Enumerated": { byte[] enumratedBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)Value)); //Calculate Padding int remainder = enumratedBytes.Length % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[enumratedBytes.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(enumratedBytes, 0, retVal, 0, enumratedBytes.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "Time": { ulong seconds = Convert.ToUInt64(Value); //byte[] ntpTimeStamp = DiameterType.NtpTimestamp2Bytes(seconds) ; byte[] ntpTimeStamp = DiameterType.ConvertToNtp(seconds); //Calculate Padding int remainder = (ntpTimeStamp.Length) % 4; if (remainder == 0) { Padding = 0; } else { Padding = 4 - remainder; } byte[] retVal = new byte[ntpTimeStamp.Length + Padding]; //Copy Value Bytes System.Buffer.BlockCopy(ntpTimeStamp, 0, retVal, 0, ntpTimeStamp.Length); //Add Padding Bytes if (Padding > 0) { int destOffSet = retVal.Length - Padding; System.Buffer.BlockCopy(Enumerable.Repeat((byte)0x0, Padding).ToArray(), 0, retVal, destOffSet, Padding); } return(retVal); } case "Grouped": { // Process Group AVP here byte[] retVal = GetGroupedAvpBytes(avp, dict); return(retVal); } default: { return(null); } } /// Diameter Types //• OctetString //• Integer32 //• Integer64 //• Unsigned32 //• Unsigned64 //• Float32 //• Float64 //• Grouped //• Address //• Time //• UTF8String //• DiameterIdentity //• DiameterURI //• Enumerated //• IPFilterRule //• QoSFilterRule }
/// <summary> /// /// </summary> /// <param name="avp"></param> /// <returns></returns> public byte[] GetAvpBytes(Avp avp, Dictionary <int, AttributeInfo> dict) { int offset = 0; //Avp Value Bytes int Padding = 0; byte[] retValueBytes = GetAvpValueBytes(avp, ref Padding, dict); //If Grouped Avp then Above Method will return all Grouped AvpBytes, Take Care of Paddings Here or in Grouped Avp itself. if (avp.isGrouped) { return(retValueBytes); } // if (avp.isVendorSpecific) { avp.AvpLength = retValueBytes.Length + 12; } else { avp.AvpLength = retValueBytes.Length + 8; } //Initialize Return Array byte[] AvpBytesToRet = new byte[avp.AvpLength]; //Add Header Bytes //Avp Code int AvpCode = IPAddress.HostToNetworkOrder(avp.AvpCode); Buffer.BlockCopy(BitConverter.GetBytes(AvpCode), 0, AvpBytesToRet, offset, 4); offset += 4; //AvpFlags {V M P r r r r r} byte flags = (byte)(0 << 0); flags = (byte)(0 << 1); flags = (byte)(0 << 2); flags = (byte)(0 << 3); flags = (byte)(0 << 5); if (avp.isVendorSpecific) { flags = (byte)(1 << 7); } if (isMandatory) { flags = (byte)(1 << 6); } Buffer.BlockCopy(BitConverter.GetBytes(flags), 0, AvpBytesToRet, offset, 1); offset += 1; //AvpLength, Discard 0th Byte and Always excluding the Paddings from length int Len = avp.AvpLength - Padding; int lenOfAvpToCopy = IPAddress.HostToNetworkOrder(Len); Buffer.BlockCopy(BitConverter.GetBytes(lenOfAvpToCopy), 1, AvpBytesToRet, offset, 3); offset += 3; //Vendor ID if (avp.isVendorSpecific) { UInt32 vId = (UInt32)IPAddress.HostToNetworkOrder(avp.VendorId); Buffer.BlockCopy(BitConverter.GetBytes(vId), 0, AvpBytesToRet, offset, 4); offset += 4; } //Add Value Byes Buffer.BlockCopy(retValueBytes, 0, AvpBytesToRet, offset, retValueBytes.Length); offset += retValueBytes.Length - Padding; return(AvpBytesToRet); }