📄 test2.cxx
字号:
/* 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: Test2.cxx,v 1.34 2006/03/10 01:30:14 vfajardo Exp $ #include <fstream>#include "eap.hxx"#include "eap_peerfsm.hxx"#include "eap_authfsm.hxx"#include "eap_identity.hxx"#include "eap_policy.hxx"#include "eap_method_registrar.hxx"#include "eap_fsm.hxx"#include "eap_log.hxx"#include "eap_md5.hxx"#include "eap_archie.hxx"#include "eap_archie_fsm.hxx"#include "pana_node.h"#include "pana_client_fsm.h"#include "pana_paa_fsm.h"#include "pana_paa_factory.h"#include "ace/Get_Opt.h"#include <openssl/rand.h>#include <iostream>#include "user_db.h"#include "pana_auth_script.h"class MyPeerSwitchStateMachine;class MyStandAloneAuthSwitchStateMachine;class StandAloneAuthApplication;static std::string gUserName;/// Task class used in this sample program.class EapTask : public AAA_Task{ public: /// Constructor. EapTask(std::string &cfgfile) : AAA_Task(AAA_SCHED_WFQ, "EAP"), node(*this, cfgfile) { } /// Destructor. ~EapTask() {} PANA_Node node;};/* --------------------------------------------------------- Brief explanation on what is done in this sample program. --------------------------------------------------------- This program includes three test cases from which the program will prompt you to choose one. o Case 1 is for EAP conversation between a peer and an authenticator *without* a passthrough authenticator in between. o Case 2 is for EAP conversation between a peer and an authenticator *with* a passthrough authenticator in between. All cases completes with success after EAP-Archie authentication method is performed. In both cases, the peer session entity will prompt you to input a username. Once the username is input, it is carried in an Response/Archie-Response message and sent to the (backend) authenticator. In Case 2, passthrough authenticator will forward the response to the backend authenticator. The authenticator or passthrough authenticator will retransmit the Request/Archie-Request message until you input the username or the number of retransmission reaches its maximum value (the maximum value is set to 3 in this program). For the latter case, the authenticator will stop its state machine and the peer will timeout and fail. Message passing between the entities are based on callbacks. For example, when an authenticator sends an Request, Send() callback function is called from the authenticator state machine. In this program, the contents of Send() is simply calling the Receive() function of the communicating peer entity. For example, in Case 1, Send() of the authenticator calls Receive() of the peer. Case 1: Peer Authenticator | | | Request/Archie-Request | |<-------------------------| | Response/Archie-Response| |------------------------->| | Request/Archie-Confirm | |<-------------------------| | Response/Archie-Finish | |------------------------->| | | | Success | |<-------------------------|Case 2: Peer PassThough Backend Authenticator Authenticator | | Null message | | |------------------------->| | | | | | Request/Archie-Request | | Request/Archie-Request |<-------------------------| |<-------------------------| | | | | | Response/Archie-Response| | |------------------------->| Response/Archie-Response| | |------------------------->| | | Request/Archie-Confirm | | Request/Archie-Confirm |<-------------------------| |<-------------------------| | | Response/Archie-Finish | | |------------------------->| Response/Archie-Finish | | |------------------------->| | | | | | Success | | Success |<-------------------------| |<-------------------------| */static std::string sharedSecret;// Class definition for authenticator identity method for my application.class MyEapAuthIdentityStateMachine : public EapAuthIdentityStateMachine{ friend class EapMethodStateMachineCreator<MyEapAuthIdentityStateMachine>;public: MyEapAuthIdentityStateMachine(EapSwitchStateMachine &s) : EapAuthIdentityStateMachine(s) {} // Reimplemented from EapAuthIdentityStateMachine. ProcessIdentityResult ProcessIdentity(std::string& identity) { if (USER_DB_LOOKUP(identity)) { std::cout << "Valid Identity received : " << identity << std::endl; return EapAuthIdentityStateMachine::Success; } std::cout << "Invalid Identity received : " << identity << std::endl; return EapAuthIdentityStateMachine::Failure; }private: ~MyEapAuthIdentityStateMachine() {} };// Class definition for peer archie state machine.class MyEapPeerArchieStateMachine : public EapPeerArchieStateMachine{ friend class EapMethodStateMachineCreator<MyEapPeerArchieStateMachine>;public: MyEapPeerArchieStateMachine(EapSwitchStateMachine &s) : EapPeerArchieStateMachine(s) {} /// This pure virtual function is a callback used when a shared-secret /// needs to be obtained. std::string& InputSharedSecret() { return ::sharedSecret; } /// This pure virtual function is a callback used when an AuthID /// needs to be obtained. std::string& InputIdentity() { std::cout << "Received an Archie-Request from " << AuthID() << std::endl; std::cout << "username = " << AuthSwitchStateMachine().PeerIdentity() << std::endl; return AuthSwitchStateMachine().PeerIdentity(); }private: ~MyEapPeerArchieStateMachine() {} };// Class definition for authenticator identity method for my application.class MyEapAuthArchieStateMachine : public EapAuthArchieStateMachine{ friend class EapMethodStateMachineCreator<MyEapAuthArchieStateMachine>;public: MyEapAuthArchieStateMachine(EapSwitchStateMachine &s) : EapAuthArchieStateMachine(s) {} /// This pure virtual function is a callback used when a shared-secret /// needs to be obtained. std::string& InputSharedSecret() { return ::sharedSecret; } /// This pure virtual function is a callback used when an AuthID /// needs to be obtained. std::string& InputIdentity() { static std::string serverID("myserver@opendiameter.org"); return serverID; }private: ~MyEapAuthArchieStateMachine() {} };class MyPeerSwitchStateMachine: public EapPeerSwitchStateMachine{ public: MyPeerSwitchStateMachine(ACE_Reactor &r, EapJobHandle& h) : EapPeerSwitchStateMachine(r, h) {} void Send(AAAMessageBlock *b); void Success(); void Failure(); void Notification(std::string &str); void Abort(); std::string& InputIdentity();private: std::string identity;};class MyStandAloneAuthSwitchStateMachine : public EapStandAloneAuthSwitchStateMachine{ public: MyStandAloneAuthSwitchStateMachine(ACE_Reactor &r, EapJobHandle& h) : EapStandAloneAuthSwitchStateMachine(r, h) {} void Send(AAAMessageBlock *b); void Success(AAAMessageBlock *b); void Success(); void Failure(AAAMessageBlock *b); void Failure(); void Abort(); private:};class PeerChannel : public PANA_ClientEventInterface{ public: PeerChannel(PANA_Node &n, MyPeerSwitchStateMachine &s, ACE_Semaphore &sem) : eap(s), pana(n, *this), semaphore(sem) { pana.EnableDhcpBootstrap() = true; } virtual ~PeerChannel() { } void Discover() { pana.Start(); // discovery } void EapStart(bool &nap) { eap.Stop(); eap.Start(); } void ChooseISP(const PANA_CfgProviderList &list, PANA_CfgProviderInfo *&choice) { } void EapRequest(AAAMessageBlock *request, bool nap) { eap.Receive(request); } void EapAltReject() { } void Authorize(PANA_AuthorizationArgs &args) { PANA_AuthScriptCtl::Print(args); typedef enum { REAUTH, UPDATE, PING } TEST_STATE; static TEST_STATE testState = REAUTH; switch (testState) { case REAUTH: // just for testing -- reauthenticate ourselves pana.EapReAuthenticate(); testState = UPDATE; break; case UPDATE: { ACE_INET_Addr newAddr("192.168.1.1:0"); std::string message("message"); pana.UpdatePostPanaAddress(newAddr, message); } testState = PING; break; case PING: pana.Ping(); testState = REAUTH; break; } } void Notification(diameter_octetstring_t &msg) { std::cout << "PANA notification: " << msg << std::endl; } bool IsKeyAvailable(diameter_octetstring_t &key) { if (eap.KeyAvailable()) { for (int i=0; i<32; i++) { char c[100]; const char* p = eap.KeyData().data(); sprintf(c, "%02X ", *(unsigned char*)(p+i)); std::cout << c; if ((i+1) % 16 == 0) std::cout << std::endl; } std::cout << "Assigning key" << std::endl; key.assign(eap.KeyData().data(), eap.KeyData().size()); return true; } return false; } bool ResumeSession() { return false; } void Disconnect(ACE_UINT32 cause) { eap.Stop(); } void Error(ACE_UINT32 resultCode) { } void Stop() { pana.Stop(); } MyPeerSwitchStateMachine &eap; PANA_PacSession pana;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -