GenerateAARE() static private method

Server generates AARE message.
static private GenerateAARE ( GXDLMSSettings settings, GXByteBuffer data, AssociationResult result, SourceDiagnostic diagnostic, GXICipher cipher, GXByteBuffer encryptedData ) : void
settings GXDLMSSettings
data GXByteBuffer
result AssociationResult
diagnostic SourceDiagnostic
cipher GXICipher
encryptedData GXByteBuffer
return void
コード例 #1
0
 /// <summary>
 /// Parse AARQ request that client send and returns AARE request.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 byte[] HandleAARQRequest(byte[] data)
 {
     int index = 0, error;
     byte frame;            
     List<byte> arr = new List<byte>(data);
     bool packetFull, wrongCrc;
     byte command;
     m_Base.GetDataFromFrame(arr, index, out frame, true, out error, false, out packetFull, out wrongCrc, out command, false);
     if (!packetFull)
     {
         throw new GXDLMSException("Not enough data to parse frame.");
     }
     if (wrongCrc)
     {
         throw new GXDLMSException("Wrong Checksum.");
     }
     GXAPDU aarq = new GXAPDU(null);
     aarq.UseLN = this.UseLogicalNameReferencing;
     int pos = 0;
     aarq.EncodeData(arr.ToArray(), ref pos);
     AssociationResult result = AssociationResult.Accepted;
     SourceDiagnostic diagnostic = SourceDiagnostic.None;
     m_Base.Authentication = aarq.Authentication;
     m_Base.CtoSChallenge = null;
     m_Base.StoCChallenge = null;
     if (aarq.Authentication >= Authentication.High)
     {
         m_Base.CtoSChallenge = aarq.Password;
     }
     if (this.UseLogicalNameReferencing != aarq.UseLN)
     {
         result = AssociationResult.PermanentRejected;
         diagnostic = SourceDiagnostic.ApplicationContextNameNotSupported;
     }
     else
     {
         GXAuthentication auth = null;
         foreach (GXAuthentication it in Authentications)
         {
             if (it.Type == aarq.Authentication)
             {
                 auth = it;
                 break;
             }
         }                
         if (auth == null)
         {
             result = AssociationResult.PermanentRejected;
             //If authentication is required.
             if (aarq.Authentication == Authentication.None)
             {
                 diagnostic = SourceDiagnostic.AuthenticationRequired;
             }
             else
             {
                 diagnostic = SourceDiagnostic.AuthenticationMechanismNameNotRecognised;
             }
         }
         //If authentication is used check pw.
         else if (aarq.Authentication != Authentication.None)
         {                    
             if (aarq.Authentication == Authentication.Low)
             {
                 //If Low authentication is used and pw don't match.
                 if (aarq.Password == null || string.Compare(auth.Password, ASCIIEncoding.ASCII.GetString(aarq.Password)) != 0)
                 {
                     result = AssociationResult.PermanentRejected;
                     diagnostic = SourceDiagnostic.AuthenticationFailure;
                 }
             }
             else //If High authentication is used.
             {
                 m_Base.StoCChallenge = GXDLMS.GenerateChallenge();
                 System.Diagnostics.Debug.WriteLine("StoC: " + BitConverter.ToString(m_Base.StoCChallenge));
                 result = AssociationResult.Accepted;
                 diagnostic = SourceDiagnostic.AuthenticationRequired;
             }
         }
     }
     //Generate AARE packet.
     List<byte> buff = new List<byte>();
     byte[] conformanceBlock;
     if (UseLogicalNameReferencing)
     {
         conformanceBlock = LNSettings.m_ConformanceBlock;
     }
     else
     {
         conformanceBlock = SNSettings.m_ConformanceBlock;
     }
     aarq.GenerateAARE(buff, aarq.Authentication, m_Base.StoCChallenge, MaxReceivePDUSize, conformanceBlock, result, diagnostic);
     if (this.InterfaceType == InterfaceType.General)
     {
         buff.InsertRange(0, Gurux.DLMS.Internal.GXCommon.LLCReplyBytes);
     }
     m_Base.ExpectedFrame = 0;
     m_Base.FrameSequence = -1;
     m_Base.ReceiveSequenceNo = 1;
     m_Base.SendSequenceNo = 0;            
     return m_Base.AddFrame(m_Base.GenerateIFrame(), false, buff, 0, buff.Count);
 }
コード例 #2
0
ファイル: GXDLMSServer.cs プロジェクト: skycod/Gurux.DLMS.Net
 /// <summary>
 /// Parse AARQ request that cliend send and returns AARE request.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 byte[] HandleAARQRequest(byte[] data)
 {
     int index = 0, error;
     byte frame;            
     List<byte> arr = new List<byte>(data);
     bool packetFull, wrongCrc;
     byte command;
     m_Base.GetDataFromFrame(arr, index, out frame, true, out error, false, out packetFull, out wrongCrc, out command);
     if (!packetFull)
     {
         throw new GXDLMSException("Not enought data to parse frame.");
     }
     if (wrongCrc)
     {
         throw new GXDLMSException("Wrong Checksum.");
     }
     GXAPDU aarq = new GXAPDU(null);
     aarq.UseLN = this.UseLogicalNameReferencing;
     int pos = 0;
     aarq.EncodeData(arr.ToArray(), ref pos);
     AssociationResult result = AssociationResult.Accepted;
     SourceDiagnostic diagnostic = SourceDiagnostic.None;
     if (this.UseLogicalNameReferencing != aarq.UseLN)
     {
         result = AssociationResult.PermanentRejected;
         diagnostic = SourceDiagnostic.ApplicationContextNameNotSupported;
     }
     else
     {
         GXAuthentication auth = null;
         foreach (GXAuthentication it in Authentications)
         {
             if (it.Type == aarq.Authentication)
             {
                 auth = it;
                 break;
             }
         }
         if (auth == null)
         {
             result = AssociationResult.PermanentRejected;
             //If authentication is required.
             if (aarq.Authentication == Authentication.None)
             {
                 diagnostic = SourceDiagnostic.AuthenticationRequired;
             }
             else
             {
                 diagnostic = SourceDiagnostic.AuthenticationMechanismNameNotRecognised;
             }
         }
         //If authentication is used check pw.
         else if (aarq.Authentication != Authentication.None && auth.Password != aarq.Password && string.IsNullOrEmpty(auth.Password) != string.IsNullOrEmpty((aarq.Password)))
         {
             result = AssociationResult.PermanentRejected;
             diagnostic = SourceDiagnostic.AuthenticationFailure;
         }
     }
     //Generate AARE packet.
     List<byte> buff = new List<byte>();
     byte[] conformanceBlock;
     if (UseLogicalNameReferencing)
     {
         conformanceBlock = LNSettings.m_ConformanceBlock;
     }
     else
     {
         conformanceBlock = SNSettings.m_ConformanceBlock;
     }
     aarq.GenerateAARE(buff, MaxReceivePDUSize, conformanceBlock, result, diagnostic);
     if (this.InterfaceType == InterfaceType.General)
     {
         buff.InsertRange(0, new byte[] { 0xE6, 0xE7, 0x00 });
     }
     m_Base.ExpectedFrame = 0;
     m_Base.FrameSequence = -1;            
     return m_Base.AddFrame(m_Base.GenerateIFrame(), false, buff, 0, buff.Count);
 }