📄 test2.cxx
字号:
ACE_Semaphore &semaphore;};class StandAloneAuthChannel : public PANA_PaaEventInterface{ public: StandAloneAuthChannel(PANA_PaaSessionChannel &ch, MyStandAloneAuthSwitchStateMachine &s) : eap(s), pana(ch, *this) { pana.EnableDhcpBootstrap() = true; } virtual ~StandAloneAuthChannel() { } void EapStart(bool &nap) { eap.Stop(); eap.Start(); } void EapResponse(AAAMessageBlock *response, bool nap) { eap.Receive(response); } void EapAltReject() { } void Authorize(PANA_AuthorizationArgs &args) { PANA_AuthScriptCtl::Print(args); } 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; } void Notification(diameter_octetstring_t &msg) { std::cout << "PANA notification: " << msg << std::endl; } bool IsUserAuthorized() { return true; } void Disconnect(ACE_UINT32 cause) { eap.Stop(); } void Timeout(PANA_TID id) { eap.Stop(); } void Error(ACE_UINT32 resultCode) { eap.Stop(); } MyStandAloneAuthSwitchStateMachine& Eap() { return eap; } MyStandAloneAuthSwitchStateMachine &eap; PANA_PaaSession pana;};class PeerApplication : public AAA_JobData{ public: PeerApplication(EapTask &task, ACE_Semaphore &sem) : handle(EapJobHandle(AAA_GroupedJob::Create(task.Job(), this, "peer"))), eap(boost::shared_ptr<MyPeerSwitchStateMachine> (new MyPeerSwitchStateMachine(*task.reactor(), handle))), semaphore(sem), channel(task.node, *eap, sem), method(EapContinuedPolicyElement(EapType(ARCHIE_METHOD_TYPE))) { eap->Policy().InitialPolicyElement(&method); } ~PeerApplication() {} PeerChannel &Channel() { return channel; } MyPeerSwitchStateMachine& Eap() { return *eap; } ACE_Semaphore& Semaphore() { return semaphore; } private: EapJobHandle handle; boost::shared_ptr<MyPeerSwitchStateMachine> eap; ACE_Semaphore &semaphore; PeerChannel channel; EapContinuedPolicyElement method;};// My application session (not used in this test program).class StandAloneAuthApplication : public AAA_JobData{ public: StandAloneAuthApplication(PANA_PaaSessionChannel &ch, ACE_Semaphore &sem) : handle(EapJobHandle (AAA_GroupedJob::Create(ch.Node().Task().Job(), this, "standalone"))), eap(boost::shared_ptr<MyStandAloneAuthSwitchStateMachine> (new MyStandAloneAuthSwitchStateMachine (*ch.Node().Task().reactor(), handle))), semaphore(sem), channel(ch, *eap), identityMethod(EapContinuedPolicyElement(EapType(1))), archieMethod(EapContinuedPolicyElement(EapType(ARCHIE_METHOD_TYPE))) { // Policy settings for the authenticator identityMethod.AddContinuedPolicyElement (&archieMethod, EapContinuedPolicyElement::PolicyOnSuccess); eap->Policy().InitialPolicyElement(&identityMethod); } ~StandAloneAuthApplication() {} void Start() { eap->Start(); } StandAloneAuthChannel &Channel() { return channel; } MyStandAloneAuthSwitchStateMachine& Eap() { return *eap; } ACE_Semaphore& Semaphore() { return semaphore; } private: EapJobHandle handle; boost::shared_ptr<MyStandAloneAuthSwitchStateMachine> eap; ACE_Semaphore &semaphore; StandAloneAuthChannel channel; EapContinuedPolicyElement identityMethod; EapContinuedPolicyElement archieMethod;};// ----------------- Definition --------------void MyPeerSwitchStateMachine::Send(AAAMessageBlock *b){ std::cout << "EAP Response sent from peer" << std::endl; JobData(Type2Type<PeerApplication>()).Channel().pana.EapSendResponse(b);}void MyPeerSwitchStateMachine::Success() { std::cout << "Authentication success detected at peer" << std::endl; std::cout << "Welcome to the world, " << PeerIdentity() << " !!!" << std::endl; JobData(Type2Type<PeerApplication>()).Channel().pana.EapSuccess(); }void MyPeerSwitchStateMachine::Failure() { std::cout << "Authentication failure detected at peer" << std::endl; std::cout << "Sorry, " << PeerIdentity() << " try next time !!!" << std::endl; Stop(); JobData(Type2Type<PeerApplication>()).Channel().pana.EapFailure(); JobData(Type2Type<PeerApplication>()).Semaphore().release(); }void MyPeerSwitchStateMachine::Notification(std::string &str) { std::cout << "Following notification received" << std::endl; std::cout << str << std::endl; }void MyPeerSwitchStateMachine::Abort() { std::cout << "Peer aborted for an error in state machine" << std::endl; JobData(Type2Type<PeerApplication>()).Channel().pana.EapFailure(); JobData(Type2Type<PeerApplication>()).Semaphore().release(); }std::string& MyPeerSwitchStateMachine::InputIdentity() { if (gUserName.length() > 0) { return gUserName; } std::cout << "Input username (within 10sec.): " << std::endl; //std::cin >> identity; identity = "user1@isp.net"; std::cout << "username = " << identity << std::endl; return identity; }void MyStandAloneAuthSwitchStateMachine::Send(AAAMessageBlock *b) { std::cout << "EAP Request sent from authenticator" << std::endl; JobData(Type2Type<StandAloneAuthApplication>()).Channel(). pana.EapSendRequest(b); }void MyStandAloneAuthSwitchStateMachine::Success(AAAMessageBlock *b) { std::cout << "EAP success sent from authenticator" << std::endl; JobData(Type2Type<StandAloneAuthApplication>()).Channel(). pana.EapSuccess(b); }void MyStandAloneAuthSwitchStateMachine::Success() { std::cout << "Success without an EAP Success" << std::endl; JobData(Type2Type<StandAloneAuthApplication>()).Channel(). pana.EapSuccess(); }void MyStandAloneAuthSwitchStateMachine::Failure(AAAMessageBlock *b) { std::cout << "EAP Failure sent from authenticator" << std::endl; JobData(Type2Type<StandAloneAuthApplication>()).Channel(). pana.EapFailure(b); Stop(); }void MyStandAloneAuthSwitchStateMachine::Failure() { std::cout << "Failure without an EAP Failure" << std::endl; JobData(Type2Type<StandAloneAuthApplication>()).Channel(). pana.EapFailure(); Stop(); }void MyStandAloneAuthSwitchStateMachine::Abort() { std::cout << "Session aborted for an error in state machine" << std::endl; JobData(Type2Type<StandAloneAuthApplication>()).Channel(). pana.EapFailure(); Stop(); JobData(Type2Type<StandAloneAuthApplication>()).Semaphore().release(); }class StandAloneAuthAppFactory : public PANA_PaaSessionFactory{ public: StandAloneAuthAppFactory(EapTask &t, ACE_Semaphore &s) : PANA_PaaSessionFactory(t.node), task(t), sem(s) { } PANA_PaaSession *Create() { StandAloneAuthApplication *app = new StandAloneAuthApplication (*this, sem); if (app) { return &(app->Channel().pana); } return (0); } protected: EapTask &task; ACE_Semaphore &sem;};int main(int argc, char **argv){ std::string cfgfile; std::string userdb; bool b_client = false; // Gather command line options ACE_Get_Opt opt(argc, argv, "cf:u:U:", 1); for (int c; (c = opt()) != (-1); ) { switch (c) { case 'f': cfgfile.assign(opt.optarg); break; case 'c': b_client = true; break; case 'u': userdb.assign(opt.optarg); break; case 'U': gUserName.assign(opt.optarg); break; } } if ((opt.argc() < 1) || (cfgfile.length() == 0)) { std::cout << "Usage: pana_test [-c] -f [configuration file]" << std::endl; return (0); } else if (! b_client && (userdb.length() == 0)) { std::cout << "Usage: pana_test [-c] -f [configuration file] -u [user db]" << std::endl; return (0); } // Initialize the log. EapLogMsg_S::instance()->open("EAP", ACE_Log_Msg::STDERR); EapLogMsg_S::instance()->enable_debug_messages(); // Register the mapping from EapType to the creator of the // user-defined method class for each user-defined method // implementation. EapMethodStateMachineCreator<MyEapAuthIdentityStateMachine> myAuthIdentityCreator; EapMethodStateMachineCreator<MyEapPeerArchieStateMachine> myPeerArchieCreator; EapMethodStateMachineCreator<MyEapAuthArchieStateMachine> myAuthArchieCreator; EapMethodRegistrar methodRegistrar; methodRegistrar.registerMethod (std::string("Identity"), EapType(1), Authenticator, myAuthIdentityCreator); methodRegistrar.registerMethod (std::string("Archie"), EapType(ARCHIE_METHOD_TYPE), Peer, myPeerArchieCreator); methodRegistrar.registerMethod (std::string("Archie"), EapType(ARCHIE_METHOD_TYPE), Authenticator, myAuthArchieCreator); EapTask task(cfgfile); try { // Task starts with two threads in the thread pool. task.Start(5); } catch (...) { std::cout << "Task failed to start" << std::endl; exit(1); } // Set shared secret.#if 0 char secret[64] = { char(0x70), char(0x3a), char(0x97), char(0x9c), char(0xc5), char(0xd7), char(0xb0), char(0x8d), char(0x37), char(0x5c), char(0x10), char(0xd7), char(0x60), char(0xfb), char(0x7f), char(0x51), char(0x54), char(0x50), char(0xec), char(0x65), char(0x9e), char(0x14), char(0xdb), char(0x35), char(0x41), char(0xb), char(0xae), char(0xd8), char(0x50), char(0x2b), char(0x5c), char(0x3b), char(0x5e), char(0x6c), char(0xbf), char(0x77), char(0xe7), char(0x1a), char(0xe0), char(0x5b), char(0x5), char(0xe0), char(0xf7), char(0xb0), char(0xcb), char(0x83), char(0xff), char(0x24), char(0x6e), char(0x87), char(0x49), char(0xcf), char(0x7f), char(0xc6), char(0x1b), char(0xc2), char(0xcf), char(0x64), char(0x13), char(0xc6), char(0xd5), char(0xb3), char(0x5f), char(0x9) };#endif char secret[64]; ifstream ss("config/shared_secret.bin", ios::binary | ios::in); if (ss) { ss.read(secret, sizeof(secret)); sharedSecret.assign((char*)secret, sizeof secret); } else { printf("can't open config/shared_secret.bin"); exit (1); } ACE_Semaphore semaphore(0); try { if (b_client) { PeerApplication peerApp(task, semaphore); peerApp.Channel().Discover(); semaphore.acquire(); task.Stop(); } else { USER_DB_OPEN(userdb); StandAloneAuthAppFactory factory(task, semaphore); semaphore.acquire(); task.Stop(); USER_DB_CLOSE(); } } catch (PANA_Exception &e) { std::cout << "PANA exception: " << e.description() << std::endl; } catch (...) { std::cout << "Unknown exception ... aborting" << std::endl; } task.Stop(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -