📄 sample_server1.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 "sample_server1.h"//////////////////////////////////////////////////////////////////////////////////////////////// !!!! WARNING !!!! ///////////////////////////// THE FOLLOWING ARE OLD COMPATIBILITY API's ///////////////////////////// NEW APPLICATIONS SHOULD NOT USE THESE API's ////////////////////////////////////////////////////////////////////////////////////////////////////* session counter for counting authenitcation request */int AAASampleServer::_sessionCount = 0;/* abort flag */bool AAASampleServer::_sendAbort = false;/* simple synchronization tool */ACE_Semaphore AAASampleServer::_semaphore(0);AAASampleAuthAnswerMessage::AAASampleAuthAnswerMessage(AAASampleServer &server, AAACommandCode code) : AAASessionMessageHandler((AAAApplicationCore&)server.GetApplicationCore(), code), _session(server){ /* * To facilitate simplicity, the registration of * the message handler to the session is done * in the handlers constructor */ _session.RegisterMessageHandler(this);}AAASampleAuthAnswerMessage::~AAASampleAuthAnswerMessage(){ /* * Likewise, the registration removal is done * in the destructor */ _session.RemoveMessageHandler(this);}AAAReturnCode AAASampleAuthAnswerMessage::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) Server: Error message received, unhandled at the moment\n")); return (AAA_ERR_SUCCESS); } /* * This is the sample state machine. For every message * received by this handler, it simply replys with * an answer message. This server functions much like * an echo server. However, if an abort flag is set * then it will send an ASR message upon receipt of * a request message. */ switch (_session.state()) { case AAASampleServer::IDLE: ACE_DEBUG((LM_DEBUG, "(%P|%t) Server: ^^^^^ New Auth Request\n")); _session.Update(AAASession::EVENT_AUTH_SUCCESS); _session.state(AAASampleServer::AUTHORIZED); _session.SendTestAuthAnswer(msg); break; case AAASampleServer::AUTHORIZED: case AAASampleServer::TERMINATING: default: ACE_DEBUG((LM_DEBUG, "(%P|%t) Server: ^^^^^ Responding for session # %d\n", _session.sessionIndex())); _session.SendTestAuthAnswer(msg); if (AAASampleServer::sendAbort()) { _session.Abort(); } break; } return (AAA_ERR_SUCCESS);}AAASampleServer::AAASampleServer(AAAApplicationCore &appCore, diameter_unsigned32_t appId) : AAAServerSession(appCore, appId), _answerMsgHandler(AAASampleAuthAnswerMessage(*this, 300)){ /* * A simple servers session counter is maintained here * since the factory will create instances of this * class. * * Note also that we store references to message handler * objects as part of the session object. This is * convinient since the bindings between message handlers * and session is by being a member variables. */ _state = AAASampleServer::IDLE; AAASampleServer::_sessionCount ++; _sessionIndex = AAASampleServer::_sessionCount;}AAAReturnCode AAASampleServer::HandleMessage(AAAMessage &msg){ /* * After being created by the factory on receipt of a * request message, this default HandleMessage is called * to process the request. * * Note that by being the default message handler. Any * message belonging to the session but does not have a * registered handler will also 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) Server: **** Unknown message received **** \n")); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleServer::HandleDisconnect(){ ACE_DEBUG((LM_DEBUG, "(%P|%t) Server: Ending session for session # %d\n", _sessionIndex)); /* * 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 events other * than the receipt of a termination request. */ return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleServer::HandleSessionTimeout(){ /* * Called on session timeout */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Server: Session timeout event for session # %d\n", _sessionIndex)); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleServer::HandleAuthLifetimeTimeout(){ /* * Called on auth lifetime timeout */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Server: Authorization lifetime timeout event for session # %d\n", _sessionIndex)); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleServer::HandleAuthGracePeriodTimeout(){ /* * Called on auth grace period timeout */ ACE_DEBUG((LM_DEBUG, "(%P|%t) Server: Authorization grace period timeout event for session # %d\n", _sessionIndex)); return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleServer::HandleAbort(){ ACE_DEBUG((LM_DEBUG, "(%P|%t) Server: Abort event for session # %d\n", _sessionIndex)); /* * Called when an abort session request is * received. Internally, the library has * received an ASR from the client and hence * the session will be terminated. * This sample does not do anything upon * receipt of this event. */ return (AAA_ERR_SUCCESS);}AAAReturnCode AAASampleServer::SendTestAuthAnswer(AAAMessage &request){ /* * Sample authorization request answer. This message * composition follows libdiamparser rules in * composing an AAAMessage. */ 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"); 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); AAAMessage acaMsg; hdr_flag flag = {0,0,0,0,0}; AAADiameterHeader h(1, 0, flag, request.hdr.code, 10000, 0, 0); acaMsg.hdr = h; acaMsg.acl.add(c_sessId); acaMsg.acl.add(c_orhost); acaMsg.acl.add(c_orrealm); /* * 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -