📄 client_test.cxx
字号:
ACE_Semaphore &sem, bool pickup=false) : handle(MyJobHandle (AAA_GroupedJob::Create(task.Job(), this, "NAS"))), diameter(boost::shared_ptr<MyDiameterEapClientSession> (new MyDiameterEapClientSession(appCore, handle))), eap(boost::shared_ptr<MyPassThroughAuthSwitchStateMachine> (new MyPassThroughAuthSwitchStateMachine (*task.reactor(), handle))), semaphore(sem), rxChannel(PassThroughAuthChannel(*eap)), peerTxChannel(0), method(EapContinuedPolicyElement(EapType(1))) { if (pickup) eap->Policy().InitialPolicyElement(&method); semaphore.acquire(); } ~NAS_Application() {} void Start(Channel *c) { peerTxChannel = c; diameter->Start(); eap->RetransmissionInterval() = 5; eap->Start(); } Channel* RxChannel() { return &rxChannel; } Channel& PeerTxChannel() { return *peerTxChannel; } MyPassThroughAuthSwitchStateMachine& Eap() { return *eap; } MyDiameterEapClientSession &Diameter() { return *diameter; } ACE_Semaphore& Semaphore() { return semaphore; } private: MyJobHandle handle; boost::shared_ptr<MyDiameterEapClientSession> diameter; boost::shared_ptr<MyPassThroughAuthSwitchStateMachine> eap; ACE_Semaphore &semaphore; PassThroughAuthChannel rxChannel; Channel *peerTxChannel; EapContinuedPolicyElement method;};// ----------------- Definition --------------void MyPeerSwitchStateMachine::Send(AAAMessageBlock *b){ std::cout << "EAP Response sent from peer" << std::endl; JobData(Type2Type<PeerApplication>()).TxChannel().Transmit(b);}void MyPeerSwitchStateMachine::Success() { TotalSuccess++; std::cout << "Authentication success detected at peer" << std::endl; std::cout << "Welcome to the world, " << PeerIdentity() << " !!! (" << TotalSuccess.value() << ")" << std::endl; Stop(); JobData(Type2Type<PeerApplication>()).Semaphore().release(); }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>()).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>()).Semaphore().release(); }std::string& MyPeerSwitchStateMachine::InputIdentity() { identity = std::string("ohba");#if 0 std::cout << "Input username (within 10sec.): " << std::endl; std::cin >> identity; std::cout << "username = " << identity << std::endl;#endif ACE_OS::sleep(2); return identity; }void MyPassThroughAuthSwitchStateMachine::Send(AAAMessageBlock *b) { std::cout << "EAP Request sent from passthrough authenticator" << std::endl; JobData(Type2Type<NAS_Application>()).PeerTxChannel().Transmit(b); }void MyPassThroughAuthSwitchStateMachine::Success(AAAMessageBlock *b) { std::cout << "EAP Success sent from passthrough authenticator" << std::endl; JobData(Type2Type<NAS_Application>()).PeerTxChannel().Transmit(b); Stop(); JobData(Type2Type<NAS_Application>()).Semaphore().release(); }void MyPassThroughAuthSwitchStateMachine::Success() { std::cout << "Success without an EAP Success" << std::endl; Stop(); JobData(Type2Type<NAS_Application>()).Semaphore().release(); }void MyPassThroughAuthSwitchStateMachine::Failure(AAAMessageBlock *b) { std::cout << "EAP Failure sent from passthrough authenticator" << std::endl; JobData(Type2Type<NAS_Application>()).PeerTxChannel().Transmit(b); Stop(); JobData(Type2Type<NAS_Application>()).Semaphore().release(); }void MyPassThroughAuthSwitchStateMachine::Failure() { std::cout << "Failure without an EAP Failure" << std::endl; Stop(); JobData(Type2Type<NAS_Application>()).Semaphore().release(); }void MyPassThroughAuthSwitchStateMachine::Abort() { std::cout << "Session aborted for an error in state machine" << std::endl; Stop(); JobData(Type2Type<NAS_Application>()).Diameter().Stop(); JobData(Type2Type<NAS_Application>()).Semaphore().release(); }void MyPassThroughAuthSwitchStateMachine::ForwardResponse(AAAMessageBlock *b) { // if this is the first message from the peer, then create the // authenticator on the EAP server and start it. if (b) std::cout << "Passthrough authenticator is forwarding an EAP-Response " << "to EAP server" << std::endl; else std::cout << "Passthrough authenticator is sending a null EAP message" << "to EAP server to start EAP." << std::endl; std::string eapMsg; if (b) { std::cout << "Length = " << (unsigned)b->size() << std::endl; eapMsg.assign(b->base(), b->size()); } JobData(Type2Type<NAS_Application>()).Diameter().ForwardResponse(eapMsg); }void MyDiameterEapClientSession::Abort() { std::cout << "Diameter EAP client session aborted." << std::endl; DiameterEapClientSession::Stop(); JobData(Type2Type<NAS_Application>()).Eap().Abort(); }void MyDiameterEapClientSession::SignalContinue(std::string &eapMsg) { AAAMessageBlock *msg = AAAMessageBlock::Acquire((char*)eapMsg.data(), (ACE_UINT32)eapMsg.length()); JobData(Type2Type<NAS_Application>()).Eap().AAA_Continue(msg); msg->Release(); }void MyDiameterEapClientSession::SignalSuccess(std::string &eapMsg) { AAAMessageBlock *msg = 0; if (eapMsg.length() > 0) { msg = AAAMessageBlock::Acquire((char*)eapMsg.data(), (ACE_UINT32)eapMsg.length()); } JobData(Type2Type<NAS_Application>()).Eap().AAA_Success(msg); if (msg) msg->Release(); }void MyDiameterEapClientSession::SignalFailure(std::string &eapMsg) { AAAMessageBlock *msg = 0; if (eapMsg.length() > 0) msg = AAAMessageBlock::Acquire((char*)eapMsg.data(), (ACE_UINT32)eapMsg.length()); else AAA_LOG(LM_DEBUG, "SignalFailure without EAP-Failure.\n"); JobData(Type2Type<NAS_Application>()).Eap().AAA_Failure(msg); if (msg) msg->Release(); }void MyDiameterEapClientSession::SignalReauthentication() { JobData(Type2Type<NAS_Application>()).Eap().Restart(); }voidMyDiameterEapClientSession::SetDestinationRealm(AAA_ScholarAttribute<diameter_utf8string_t> &realm){ std::string& userName = JobData(Type2Type<NAS_Application>()).Eap().PeerIdentity(); size_t pos = userName.find('@'); if (pos != std::string::npos) { pos ++; realm.Set(std::string(userName, pos, userName.length() - pos)); } else { realm.Set(std::string("research2.org")); }}voidMyDiameterEapClientSession::SetUserName(AAA_ScholarAttribute<diameter_utf8string_t> &username){ std::string& userName = JobData(Type2Type<NAS_Application>()).Eap().PeerIdentity(); size_t pos = 0; if ((pos = userName.find('@')) != std::string::npos)// Modified by Santiago Zapata Hernandez for UMU// Old// username.Set(std::string(userName.substr(pos)));// New username.Set(std::string(userName, 0, pos));// End New else username = userName;}class MyInitializer{ public: MyInitializer(EapTask &t, AAAApplicationCore &appCore) : task(t), applicationCore(appCore) { Start(); } ~MyInitializer() { Stop(); } private: void Start() { InitEapTask(); InitApplicationCore(); } void Stop() { task.Stop(); } void InitApplicationCore() { ACE_DEBUG((LM_DEBUG, "[%N] Application starting\n")); if (applicationCore.Open("config/client.local.xml", task) != AAA_ERR_SUCCESS) { ACE_ERROR((LM_ERROR, "[%N] Can't open configuraiton file.")); exit(1); } } void InitEapTask() { ACE_DEBUG((LM_DEBUG, "[%N] EAP Task starting.\n")); methodRegistrar.registerMethod (std::string("Identity"), EapType(1), Authenticator, myAuthIdentityCreator); methodRegistrar.registerMethod (std::string("MD5-Challenge"), EapType(4), Peer, myPeerMD5ChallengeCreator); try { // Task starts with two threads in the thread pool. task.Start(10); } catch (...) { ACE_ERROR((LM_ERROR, "[%N]Task failed to start.\n")); exit(1); } } EapTask &task; AAAApplicationCore &applicationCore; EapMethodRegistrar methodRegistrar; EapMethodStateMachineCreator<MyEapPeerMD5ChallengeStateMachine> myPeerMD5ChallengeCreator; EapMethodStateMachineCreator<MyEapAuthIdentityStateMachine> myAuthIdentityCreator;};intmain(int argc, char *argv[]){ EapTask task; AAAApplicationCore applicationCore; MyInitializer initializer(task, applicationCore);#if defined(WIN32) #define num 100#else int num; std::cout << "Input number of sessions: "; std::cin >> num;#endif ACE_Semaphore semaphore(2*num); TotalSuccess=0; std::auto_ptr<PeerApplication> peerApp[num]; std::auto_ptr<NAS_Application> nasApp[num]; for (int i=0; i<num; i++) { peerApp[i] = std::auto_ptr<PeerApplication>(new PeerApplication(task, semaphore)); nasApp[i] = std::auto_ptr<NAS_Application> (new NAS_Application(task, applicationCore, semaphore, true)); peerApp[i]->Start(nasApp[i]->RxChannel()); nasApp[i]->Start(peerApp[i]->RxChannel()); } // Block until the EAP conversation completes. for (int i=0; i<2*num; i++) semaphore.acquire(); std::cout << "Total number of sessions : " << num << std::endl; std::cout << "Total number of success : " << TotalSuccess.value() << std::endl; for (int i=0; i<num; i++) { peerApp[i].reset(); nasApp[i].reset(); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -