⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eap_peerfsm.cxx

📁 Diameter协议栈
💻 CXX
📖 第 1 页 / 共 2 页
字号:
/* BEGIN_COPYRIGHT                                                        *//*                                                                        *//* Open Diameter: Open-source software for the Diameter and               *//*                Diameter related protocols                              *//*                                                                        *//* Copyright (C) 2002-2004 Open Diameter Project                          *//*                                                                        *//* This library is free software; you can redistribute it and/or modify   *//* it under the terms of the GNU Lesser General Public License as         *//* published by the Free Software Foundation; either version 2.1 of the   *//* License, or (at your option) any later version.                        *//*                                                                        *//* This library is distributed in the hope that it will be useful,        *//* but WITHOUT ANY WARRANTY; without even the implied warranty of         *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *//* Lesser General Public License for more details.                        *//*                                                                        *//* You should have received a copy of the GNU Lesser General Public       *//* License along with this library; if not, write to the Free Software    *//* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307    *//* USA.                                                                   *//*                                                                        *//* In addition, when you copy and redistribute some or the entire part of *//* the source code of this software with or without modification, you     *//* MUST include this copyright notice in each copy.                       *//*                                                                        *//* If you make any changes that are appeared to be useful, please send    *//* sources that include the changed part to                               *//* diameter-developers@lists.sourceforge.net so that we can reflect your  *//* changes to one unified version of this software.                       *//*                                                                        *//* END_COPYRIGHT                                                          */// $Id: eap_peerfsm.cxx,v 1.51 2004/08/10 16:41:06 vfajardo Exp $//// eap_peerfsm.cxx:  Peer state machine// Written by Yoshihiro Ohba#include <ace/Singleton.h>#include <ace/Message_Block.h>#include <ace/Message_Queue.h>#include <ace/Synch.h>#include <ace/OS.h>#include "eap.hxx"#include "eap_peerfsm.hxx"#include "eap_log.hxx"#include "eap_parser.hxx"intInputIdentityMethodRequest::call(){  return futureResult.set(stateMachine->InputIdentity());}/// Action class for EAP. class EapPeerAction : public AAA_Action<EapPeerSwitchStateMachine>{ public:  virtual void operator()(EapPeerSwitchStateMachine&) {} protected:  EapPeerAction() {}  virtual ~EapPeerAction() {}};/// State table used by EapPeerSwitchStateMachine.class EapPeerSwitchStateTable_S :   public AAA_StateTable<EapPeerSwitchStateMachine>{  friend class ACE_Singleton<EapPeerSwitchStateTable_S, 			     ACE_Recursive_Thread_Mutex>;private:  class AcInitialize : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      // allowNotification=TRUE;      sm.NotificationAllowed() = true;      sm.MethodState() = EapPeerSwitchStateMachine::NONE;      sm.Event(EvUCT);    }  };  class AcRetransmit : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      EAP_LOG(LM_DEBUG, "Peer:  Detect duplicate request.\n");      SubAcSendResp(sm);      sm.Event(EvUCT);    }  };  class AcCheckTimeoutCondition : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      if (sm.Policy().IsSatisfied() && 	  sm.Decision() == EapPeerSwitchStateMachine::COND_SUCC)	sm.Event(EvSgSuccess);      else	sm.Event(EvSgFailure);    }  };  class AcSendSuccess : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      sm.CancelTimer();      EAP_LOG(LM_DEBUG, "Peer:  Success.\n");      if (sm.KeyData().size()>0)	sm.KeyAvailable() = true;      sm.Success();    }  };  class AcSendFailure : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      sm.CancelTimer();      EAP_LOG(LM_DEBUG, "Peer:  Failure.\n");      sm.Failure();    }  };  class AcBuildResp : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      AAAMessageBlock *msg = sm.GetTxMessage();      EAP_LOG(LM_DEBUG, "Peer:  Sent Response.\n");      // Compose the PDU.      EapHeaderParser headerParser;      EapHeader header;      header.code = Response;      header.identifier = sm.CurrentIdentifier();      header.length = msg->length();      headerParser.setAppData(&header);      headerParser.setRawData(msg);      headerParser.parseAppToRaw();      // Set the wr_ptr to the tail of the message.      msg->wr_ptr(msg->base()+header.length);      sm.LastIdValidity() = true;      // Set the received identifier to the currentId      sm.LastIdentifier() = sm.CurrentIdentifier();      EAP_LOG(LM_DEBUG, "checking key availability.\n");      if (sm.MethodState() == EapPeerSwitchStateMachine::NONE ||	  sm.MethodState() == EapPeerSwitchStateMachine::INIT)	goto next;      // Check the availability of a key.      if (sm.MethodStateMachine().KeyAvailable())	{	  EAP_LOG(LM_DEBUG, "key available.\n");	  sm.KeyData() = sm.MethodStateMachine().KeyData();	}    next:      // Send it.      SubAcSendResp(sm);    }  };  class AcDoIntegrityCheck : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      EAP_LOG(LM_DEBUG, "Peer:  Do Integrity Check.\n");      sm.MethodStateMachine().Notify	(EapMethodStateMachine::EvSgIntegrityCheck);    }  };  class AcDoPolicyCheck : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      EAP_LOG(LM_DEBUG, "Peer:  Do Policy Check.\n");      EapType &type = sm.ReqMethod();      bool allowMethod;      if (type == EapType(1))	// This is Request/Identity.  If this is the first method, it	// accepts the request.	{	  if (sm.CurrentMethod() == EapType(0))	    allowMethod = true;	  else	    allowMethod = false;	}      else	{	  allowMethod = sm.Policy().Allow(type);	}      if (allowMethod) {	// Set the current method type	sm.CurrentMethod() = type;	// Switch to new method	EAP_LOG(LM_DEBUG, "Peer:  Creating Method.\n");	// Delete the old method (if any)	sm.DeleteMethodStateMachine();	// Create a new method with a specific type	sm.CreateMethodStateMachine(type, Peer);	// Change the method state to INIT.	sm.MethodState() = EapPeerSwitchStateMachine::INIT;	// Start the method	sm.MethodStateMachine().Start();	// Enter the method	sm.Event(EvSgEnterMethod);      }       else {	sm.Event(EvSgNoEnterMethod);      }    }  };  class AcDiscard : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      EAP_LOG(LM_DEBUG, "Peer:  Message Discarded.\n");      sm.DiscardCount()++;      sm.Event(EvUCT);    }  };  class AcBuildNak : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      // Create a type list from the current policy.      EapType type = sm.CurrentMethod();      EapNak nak;      EapTypeList &tList = nak.TypeList();      sm.Policy().MakeTypeList(tList, type.IsVSE());      // Calculate NAK type and payload size.      int payloadLength = tList.size();      EapNakDict::NakType nakType = EapNakDict::LegacyNak;      if (tList.IsVSE())	{	  payloadLength *= 8;	  nakType = EapNakDict::ExtendedNak;	}      // Create NAK.      AAAMessageBlock *msg = AAAMessageBlock::Acquire(4 + payloadLength);       // Use parser to set Type field.      EapNakParser payloadParser;      payloadParser.setDictData(NULL);      payloadParser.setAppData(&nak);      payloadParser.setRawData(msg);      EapNakDict dict(nakType);      payloadParser.setDictData(&dict);      payloadParser.parseAppToRaw();      // Set the message to the session.      sm.SetTxMessage(msg);      EAP_LOG(LM_DEBUG, "Peer:  Sent Nak.\n");      // Compose the PDU.      EapHeaderParser headerParser;      EapHeader header;      header.code = Response;      header.identifier = sm.CurrentIdentifier();      header.length = msg->length();      headerParser.setAppData(&header);      headerParser.setRawData(msg);      headerParser.parseAppToRaw();          // Set the wr_ptr to the tail of the message.      msg->wr_ptr(msg->base()+header.length);      sm.LastIdValidity() = true;      // Send it.      SubAcSendResp(sm);    }  };  class AcParseNotification : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      AAAMessageBlock *msg = sm.GetRxMessage();      EAP_LOG(LM_DEBUG, "Peer: Parsing Notification request.\n");      EapRequestNotificationParser parser;      // Check the Notification.      EapNotification notification;      parser.setRawData(msg);      parser.setAppData(&notification);      try { parser.parseRawToApp(); }      catch (...)	{	  // When an identity request contains non-UTF8 string, silently	  // discard it as if it were not received.	  EAP_LOG(LM_ERROR, "Peer: Notification parsing failed.\n");	  sm.Event(EvElse);	  return;	}      // Output notification to the application.        sm.Notification(sm.NotificationString());      sm.Event(EvSgNotificationOutputFinished);    }  };  class AcBuildNotification : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      AAAMessageBlock *msg = AAAMessageBlock::Acquire(5);      EapResponse response(EapType(2)); // Notification      // Use parser to set Type field.      EapResponseParser payloadParser;      payloadParser.setAppData(&response);      payloadParser.setRawData(msg);      payloadParser.parseAppToRaw();      // Set the message to the session.      sm.SetTxMessage(msg);      sm.Event(EvUCT);    }  };  class AcParseIdentity : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      AAAMessageBlock *msg = sm.GetRxMessage();      EAP_LOG(LM_DEBUG, "Peer: Parsing Identity request.\n");      // Check the Identity.      EapIdentity id;      EapRequestIdentityParser parser;      parser.setAppData(&id);      parser.setRawData(msg);      try { parser.parseRawToApp(); }      catch (...)	{	  // When an identity request contains non-UTF8 string, silently	  // discard it as if it were not received.	  EAP_LOG(LM_ERROR, "Peer: Identity request parsing failed.\n");	  sm.Event(EvElse);	  return;	}      sm.AuthenticatorIdentity() = id.Identity();      // Get identity from the application.  Since calling      // InputIdenity() typically involves in user input, a new task      // is created for exectuting InputIdenity().      sm.InputIdentityTask().open();      sm.FutureIdentity() = sm.InputIdentityTask().InputIdentity();      if (sm.FutureIdentity().ready())	{	  sm.FutureIdentity().get(sm.PeerIdentity());	  sm.InputIdentityTask().suspend();	  sm.Notify(EvSgUserInputFinished);	}      else	{	  // Immediate check for the first time.	  sm.ScheduleTimer(EvToCheckIdentity, 0, 100000, 			   sm.InputIdentityTimerType());	  return;	}    }  };  class AcBuildIdentity : public EapPeerAction  {    void operator()(EapPeerSwitchStateMachine &sm)    {      EAP_LOG(LM_DEBUG, "Peer: Building Identity response.\n");      // Prepare the Response      EapIdentity id;      std::string& idString = id.Identity() = sm.PeerIdentity();      AAAMessageBlock *msg = AAAMessageBlock::Acquire(5 + idString.size());          EapResponseIdentityParser parser;      parser.setAppData(&id);      parser.setRawData(msg);      try { parser.parseAppToRaw(); }      catch (...)	{	  EAP_LOG(LM_ERROR, "Peer: Identity data is not UTF8 formatted.\n");	  sm.Event(EvElse);	  return;	}      // Set the message to the session.      sm.SetTxMessage(msg);          // Set the last id validity.      sm.LastIdValidity() = true;      sm.Event(EvUCT);    }  };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -