📄 sample_client1.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 */#include <string>#include "ace/OS.h"#include "sample_client1.h"//////////////////////////////////////////////////////////////////////////////////////////////// !!!! WARNING !!!! ///////////////////////////// THE FOLLOWING ARE OLD COMPATIBILITY API's ///////////////////////////// NEW APPLICATIONS SHOULD NOT USE THESE API's ////////////////////////////////////////////////////////////////////////////////////////////////////* session counter for indexing session holders */int AAASampleClient::_sessionCount = 0;/* simple synchronization tool */ACE_Semaphore AAASampleClient::_semaphore(0);/* * Session holder instance automatically * initializes the session client and * message handler */AAASampleSessionHolder::AAASampleSessionHolder(AAAApplicationCore &appCore, diameter_unsigned32_t appId) : _authSampleClient(appCore, appId), _authMsgHandler(_authSampleClient, 300), _acctSampleClient(appCore, appId), _acctMsgHandler(_acctSampleClient, AAASampleAccountingRequestMsg::ACR){ // do nothing }AAASampleAuthMessage::AAASampleAuthMessage(AAASampleClient &client, AAACommandCode code) : AAASessionMessageHandler((AAAApplicationCore&)client.GetApplicationCore(), code), session(client){ /* * To facilitate simplicity, the registration of * the message handler to the session is done * in the handlers constructor */ session.RegisterMessageHandler(this);}AAASampleAuthMessage::~AAASampleAuthMessage(){ /* * Likewise, the registration removal is done * in the destructor */ session.RemoveMessageHandler(this);}AAAReturnCode AAASampleAuthMessage::HandleMessage(AAAMessage &msg){ /* * For test purposes, we leave error messages unhandled * for the moment. */ if (msg.hdr.flags.e) { ACE_ERROR((LM_ERROR, "(%P|%t) Client: Error message received, unhandled at the moment\n")); return (AAA_ERR_SUCCESS); } /* * This is the sample state machine. For every message * received from the server we transition to another * state finally leading to termination. Details of * the message exchange is in the header files. * * Notice that we send a test auth only once after * receiving an answer to our first request. Then * the state transitions to termination state. On * termination state, the library will handle STR * processing sent by the server. After processing * the client will be notified by HandleDisconnect(). */ switch (session.state()) { case AAASampleClient::IDLE: if (! msg.hdr.flags.r) { ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: ^^^^^^^ Authenticated for Session # %d\n", session.sessionIndex())); session.Update(AAASession::EVENT_AUTH_SUCCESS); session.SendTestAuth(); session.state(AAASampleClient::TERMINATING); } break; case AAASampleClient::AUTHORIZED: // un-used for now break; case AAASampleClient::TERMINATING: if (! msg.hdr.flags.r) { ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Ready for termination Session # %d\n", session.sessionIndex())); } break; } return (AAA_ERR_SUCCESS);}AAASampleClient::AAASampleClient(AAAApplicationCore &appCore, diameter_unsigned32_t appId) : AAAClientSession(appCore, appId){ /* * A simple client session counter is introduced * for referencing each session */ _state = AAASampleClient::IDLE; AAASampleClient::_sessionCount ++; _sessionIndex = AAASampleClient::_sessionCount;}AAAReturnCode AAASampleClient::HandleMessage(AAAMessage &msg){ /* * This is the default message handler. Any message * belonging to the session but does not have a * registered handler will be passed to this method * * In this case, we treat any other message as an * unknown. The application may send an unknown * message error if it wishes. */ ACE_ERROR((LM_ERROR, "(%P|%t) Client: Unknown message received\n")); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleClient::HandleDisconnect(){ /* * This handler can be called when the transport * fails, when a session termination request is * received or when an application core is exiting. * * In the case of this sample, we are expecting * this function to get called on receipt of a * session termination request. In other cases, * the code here will work to. */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Ending session for session # %d\n", _sessionIndex)); End(); /* calling end will remove this session * from the database and terminate it * cleanly. */ /* * This is allows us to release the semaphore * on the main program once all the client * session has terminated. */ AAASampleClient::_sessionCount --; if (AAASampleClient::_sessionCount == 0) { AAASampleClient::_semaphore.release(); } return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleClient::HandleSessionTimeout(){ /* * Called on session timeout */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Session timeout event for session # %d\n", _sessionIndex)); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleClient::HandleAuthLifetimeTimeout(){ /* * Called on auth lifetime timeout */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Authorization lifetime timeout event for session # %d\n", _sessionIndex)); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleClient::HandleAuthGracePeriodTimeout(){ /* * Called on auth grace period timeout */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Authorization grace period timeout event for session # %d\n", _sessionIndex)); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleClient::HandleAbort(){ /* * Called when an abort session request is * received. We will never receive this because * we are acting as a client. The server session * may process this. */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Abort event for session # %d\n", _sessionIndex)); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleClient::SendTestAuth(){ /* * Sample authorization request. This message * composition follows libdiamparser rules in * composing an AAAMessage. The test command * code we use is 300. */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Sending test auth message # %d\n", _sessionIndex)); AAAAvpContainerManager cm; AAAAvpContainerEntryManager em; AAAAvpContainer *c_sessId = cm.acquire("Session-Id"); AAAAvpContainer *c_orhost = cm.acquire("Origin-Host"); AAAAvpContainer *c_orrealm = cm.acquire("Origin-Realm"); AAAAvpContainer *c_dhost = cm.acquire("Destination-Host"); AAAAvpContainer *c_drealm = cm.acquire("Destination-Realm"); AAAAvpContainer *c_authid = cm.acquire("Auth-Application-Id"); AAAAvpContainer *c_reauth = cm.acquire("Re-Auth-Request-Type"); AAAAvpContainerEntry *e; e = em.acquire(AAA_AVP_UTF8_STRING_TYPE); c_sessId->add(e); e = em.acquire(AAA_AVP_DIAMID_TYPE); c_orhost->add(e); e = em.acquire(AAA_AVP_DIAMID_TYPE); c_orrealm->add(e); e = em.acquire(AAA_AVP_DIAMID_TYPE); diameter_identity_t &dhost = e->dataRef(Type2Type<diameter_identity_t>()); c_dhost->add(e); e = em.acquire(AAA_AVP_DIAMID_TYPE); diameter_identity_t &drealm = e->dataRef(Type2Type<diameter_identity_t>()); c_drealm->add(e); e = em.acquire(AAA_AVP_UINTEGER32_TYPE); diameter_unsigned32_t &authid = e->dataRef(Type2Type<diameter_unsigned32_t>()); c_authid->add(e); e = em.acquire(AAA_AVP_UINTEGER32_TYPE); diameter_unsigned32_t &reauth = e->dataRef(Type2Type<diameter_unsigned32_t>()); c_reauth->add(e); AAAMessage authMsg; hdr_flag flag = {1,0,0,0,0}; AAADiameterHeader h(1, 0, flag, 300, 10000, 0, 0); authMsg.hdr = h; authMsg.acl.add(c_sessId); authMsg.acl.add(c_orhost); authMsg.acl.add(c_orrealm); authMsg.acl.add(c_dhost); authMsg.acl.add(c_drealm); authMsg.acl.add(c_authid); authMsg.acl.add(c_reauth); dhost = _destHost; drealm = _destRealm; authid = 8; // SAMPLE reauth = 1; /* * The use of AAAMessageControl is shown here. * It is a utility class used in sending messages * via the local session object as well as setting * the result code. */ AAAMessageControl msgControl(this); if (msgControl.Send(authMsg) != AAA_ERR_SUCCESS) { ACE_ERROR((LM_ERROR, "(%P|%t) Client: Failed sending message\n")); return (AAA_ERR_FAILURE); } else { ACE_DEBUG((LM_DEBUG, "(%P|%t) Client: Sent Auth Message\n")); } return (AAA_ERR_SUCCESS);}AAASampleAccountingClient::AAASampleAccountingClient(AAAApplicationCore &appCore, diameter_unsigned32_t appId) : AAAAccountingClientSession(appCore, appId){ rec_counter = 0;}AAASampleAccountingClient::~AAASampleAccountingClient(){ // do nothing}AAAReturnCode AAASampleAccountingClient::SendAcctMessage(RECTYPE type){ /* * Sample accounting request. This message * composition follows libdiamparser rules in * composing an AAAMessage. The commandn code * for accounting request is AAASampleAccountingRequestMsg::ACR.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -