Main Page | Class Hierarchy | Class List | File List | Class Members

diameter_parser_api.h

00001 /* BEGIN_COPYRIGHT                                                        */
00002 /*                                                                        */
00003 /* Open Diameter: Open-source software for the Diameter and               */
00004 /*                Diameter related protocols                              */
00005 /*                                                                        */
00006 /* Copyright (C) 2002-2004 Open Diameter Project                          */
00007 /*                                                                        */
00008 /* This library is free software; you can redistribute it and/or modify   */
00009 /* it under the terms of the GNU Lesser General Public License as         */
00010 /* published by the Free Software Foundation; either version 2.1 of the   */
00011 /* License, or (at your option) any later version.                        */
00012 /*                                                                        */
00013 /* This library is distributed in the hope that it will be useful,        */
00014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
00015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      */
00016 /* Lesser General Public License for more details.                        */
00017 /*                                                                        */
00018 /* You should have received a copy of the GNU Lesser General Public       */
00019 /* License along with this library; if not, write to the Free Software    */
00020 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307    */
00021 /* USA.                                                                   */
00022 /*                                                                        */
00023 /* In addition, when you copy and redistribute some or the entire part of */
00024 /* the source code of this software with or without modification, you     */
00025 /* MUST include this copyright notice in each copy.                       */
00026 /*                                                                        */
00027 /* If you make any changes that are appeared to be useful, please send    */
00028 /* sources that include the changed part to                               */
00029 /* diameter-developers@lists.sourceforge.net so that we can reflect your  */
00030 /* changes to one unified version of this software.                       */
00031 /*                                                                        */
00032 /* END_COPYRIGHT                                                          */
00033 // $Id: diameter_parser_api.h,v 1.42 2004/12/30 14:50:48 vfajardo Exp $
00034 
00035 #ifndef __DIAMETER_PARSER_API_H__
00036 #define __DIAMETER_PARSER_API_H__
00037 
00038 #include <resultcodes.h>
00039 #include <vector>
00040 #include <map>
00041 #include <string>
00042 #include <list>
00043 #include <boost/function/function0.hpp>
00044 #include <boost/function/function1.hpp>
00045 
00046 #include <ace/Basic_Types.h>
00047 #include <ace/INET_Addr.h>
00048 #include <ace/Message_Block.h>
00049 #include <ace/Atomic_Op.h>
00050 #include <ace/Synch.h>
00051 #include <ace/Singleton.h>
00052 #include <ace/Malloc_T.h>
00053 #include <ace/Log_Msg.h>
00054 
00055 #include "framework.h"
00056 
00060 #define AAA_VERSION_MAJOR  0x01
00061 #define AAA_VERSION_MINOR  0x00
00062 #define AAA_VERSION_MICRO  0x05
00063 
00067 #if defined (WIN32)
00068 #  if defined (DIAMETERPARSER_EXPORTS)
00069 #    define DIAMETERPARSER_EXPORT ACE_Proper_Export_Flag
00070 #    define DIAMETERPARSER_EXPORT_ONLY ACE_Proper_Export_Flag
00071 #    define DIAMETERPARSER_SINGLETON_DECLARATION(T) ACE_EXPORT_SINGLETON_DECLARATION (T)
00072 #    define DIAMETERPARSER_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) ACE_EXPORT_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK)
00073 #  else
00074 #    define DIAMETERPARSER_EXPORT ACE_Proper_Import_Flag
00075 #    define DIAMETERPARSER_EXPORT_ONLY
00076 #    define DIAMETERPARSER_SINGLETON_DECLARATION(T) ACE_IMPORT_SINGLETON_DECLARATION (T)
00077 #    define DIAMETERPARSER_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) ACE_IMPORT_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK)
00078 #  endif   /* ! DIAMETERPARSER_EXPORTS */
00079 #else
00080 #  define DIAMETERPARSER_EXPORT
00081 #  define DIAMETERPARSER_EXPORT_ONLY
00082 #  define DIAMETERPARSER_SINGLETON_DECLARATION(T)
00083 #  define DIAMETERPARSER_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK)
00084 #endif     /* WIN32 */
00085 
00089 typedef char AAA_INT8;
00090 typedef unsigned char AAA_UINT8;
00091 
00097 typedef ACE_INET_Addr IP_ADDR;
00098 
00103 typedef ACE_UINT32 AAACommandCode;
00104 
00111 typedef ACE_UINT32 AAAVendorId;
00112 
00118 typedef ACE_UINT32 AAA_AVPCode;
00119 
00124 typedef void* AAASessionHandle;
00125 
00132 typedef ACE_UINT32 AAAApplicationId;
00133 
00139 typedef ACE_UINT32 AAAResultCode;
00140 
00206 typedef enum {
00207     AAA_ERR_NOT_FOUND =     -2,
00208     AAA_ERR_FAILURE =       -1,
00209     AAA_ERR_SUCCESS =        0,
00210     AAA_ERR_NOMEM,
00211     AAA_ERR_PROTO,
00212     AAA_ERR_SECURITY,
00213     AAA_ERR_PARAMETER,
00214     AAA_ERR_CONFIG,
00215     AAA_ERR_UNKNOWN_CMD,
00216     AAA_ERR_MISSING_AVP,
00217     AAA_ERR_ALREADY_INIT,
00218     AAA_ERR_TIMED_OUT,
00219     AAA_ERR_CANNOT_SEND_MSG,
00220     AAA_ERR_ALREADY_REGISTERED,
00221     AAA_ERR_CANNOT_REGISTER,
00222     AAA_ERR_NOT_INITIALIZED,
00223     AAA_ERR_NETWORK_ERROR,
00224     AAA_ERR_MSG_UNPROCESSED,
00225     AAA_ERR_INVALID_STATE,
00226     AAA_ERR_PARSING_FAILED,
00227     AAA_ERR_UNKNOWN_SESSION,
00228     AAA_ERR_PARSING_ERROR,
00229     AAA_ERR_INCOMPLETE,
00230     AAA_ERR_NOSERVICE
00231 } AAAReturnCode;
00232 
00237 typedef enum {
00238     AAA_AVP_DATA_TYPE,
00239     AAA_AVP_STRING_TYPE,
00240     AAA_AVP_ADDRESS_TYPE,          
00241     AAA_AVP_INTEGER32_TYPE,
00242     AAA_AVP_INTEGER64_TYPE,
00243     AAA_AVP_UINTEGER32_TYPE,
00244     AAA_AVP_UINTEGER64_TYPE,
00245     AAA_AVP_UTF8_STRING_TYPE,
00246     AAA_AVP_ENUM_TYPE,
00247     AAA_AVP_DIAMID_TYPE,
00248     AAA_AVP_DIAMURI_TYPE,
00249     AAA_AVP_GROUPED_TYPE,
00250     AAA_AVP_TIME_TYPE,
00251     AAA_AVP_IPFILTER_RULE_TYPE,
00252     AAA_AVP_CUSTOM_TYPE,
00253 } AAA_AVPDataType;
00254 
00255 
00300 typedef enum {
00301     AAA_AVP_FLAG_NONE =                 0,
00302     AAA_AVP_FLAG_MANDATORY =            0x1,
00303     AAA_AVP_FLAG_RESERVED =             0x2,
00304     AAA_AVP_FLAG_VENDOR_SPECIFIC =      0x4,
00305     AAA_AVP_FLAG_END_TO_END_ENCRYPT =   0x10,
00306     AAA_AVP_FLAG_UNKNOWN =              0x10000,
00307     AAA_AVP_FLAG_ENCRYPT =              0x40000,
00308 } AAA_AVPFlagEnum;
00309 
00310 typedef ACE_UINT32 AAA_AVPFlag;
00311 
00319 class DIAMETERPARSER_EXPORT AAADictionaryEntry
00320 {
00321  public:
00332   AAADictionaryEntry(AAA_AVPCode code, 
00333                      const char *name, 
00334                      AAA_AVPDataType type, 
00335                      AAAVendorId id, 
00336                      AAA_AVPFlag flg,
00337                      int proto);
00338 
00339   AAA_AVPCode     avpCode;  
00340   std::string     avpName;
00341   AAA_AVPDataType avpType;  
00342   AAAVendorId     vendorId; 
00343   AAA_AVPFlag     flags;    
00344   int             protocol; 
00345 };
00346 
00353 typedef enum {
00354     AAA_ACCT_EVENT = 1,
00355     AAA_ACCT_START = 2,
00356     AAA_ACCT_INTERIM = 3,
00357     AAA_ACCT_STOP = 4
00358 } AAAAcctMessageType;
00359 
00363 typedef enum {
00364     AAA_STYPE_AUTHENTICATION,  
00365     AAA_STYPE_ACCOUNTING       
00366 } AAASessionType;
00367 
00373 #define AAA_NO_VENDOR_ID                                0
00374 
00375 #define AAA_BASE_APPLICATION_ID                         0
00376 
00380 typedef ACE_INT32 diameter_integer32_t;
00381 typedef ACE_UINT64 diameter_integer64_t;
00382 typedef ACE_UINT32 diameter_unsigned32_t;
00383 typedef ACE_UINT64 diameter_unsigned64_t;
00384 typedef diameter_unsigned32_t diameter_enumerated_t;
00385 typedef std::string diameter_octetstring_t;
00386 typedef diameter_octetstring_t diameter_utf8string_t;  
00387 typedef struct {
00388  public:
00389   std::string fqdn;
00390   ACE_UINT16 port; 
00391   AAA_UINT8 transport:2;
00392   AAA_UINT8 protocol:2;
00393   AAA_UINT8 scheme:2;
00394 } diameter_uri_t;
00395 typedef diameter_utf8string_t diameter_identity_t;
00396 typedef diameter_octetstring_t diameter_ipaddress_t;   // XXX: this
00397                                                        // will be
00398                                                        // deprecated.
00399                                                        // Use
00400                                                        // diameter_address_t
00401                                                        // instead.
00402 typedef struct {
00403   public:
00404   ACE_UINT16 type;
00405   diameter_octetstring_t value;
00406 } diameter_address_t;
00407 
00408 // http://www.iana.org/assignments/address-family-numbers
00409 enum {
00410     AAA_ADDR_FAMILY_RESERVED    = 0,   // Reserved
00411     AAA_ADDR_FAMILY_IPV4        = 1,   // IP (IP version 4)
00412     AAA_ADDR_FAMILY_IPV6        = 2,   // IP6 (IP version 6)
00413     AAA_ADDR_FAMILY_NASP        = 3,   // NSAP
00414     AAA_ADDR_FAMILY_HDLC        = 4,   // HDLC (8-bit multidrop)
00415     AAA_ADDR_FAMILY_BBN         = 5,   // 5    BBN 1822
00416     AAA_ADDR_FAMILY_802         = 6,   // 802 (includes all 802 media plus Ethernet "canonical format")
00417     AAA_ADDR_FAMILY_E163        = 7,   // E.163
00418     AAA_ADDR_FAMILY_E164        = 8,   // E.164 (SMDS, Frame Relay, ATM)
00419     AAA_ADDR_FAMILY_F69         = 9,   // F.69 (Telex)
00420     AAA_ADDR_FAMILY_X121        = 10,  // X.121 (X.25, Frame Relay)
00421     AAA_ADDR_FAMILY_IPX         = 11,  // IPX
00422     AAA_ADDR_FAMILY_APPLETALK   = 12,  // Appletalk
00423     AAA_ADDR_FAMILY_DECIV       = 13,  // Decnet IV
00424     AAA_ADDR_FAMILY_BANYAN      = 14,  // Banyan Vines
00425     AAA_ADDR_FAMILY_E164_NSAP   = 15,  // E.164 with NSAP format subaddress 
00426     AAA_ADDR_FAMILY_DNS         = 16,  // DNS (Domain Name System)
00427     AAA_ADDR_FAMILY_DN          = 17,  // Distinguished Name
00428     AAA_ADDR_FAMILY_AS          = 18,  // AS Number
00429     AAA_ADDR_FAMILY_XTP4        = 19,  // XTP over IP version 4
00430     AAA_ADDR_FAMILY_XTP6        = 20,  // XTP over IP version 6
00431     AAA_ADDR_FAMILY_XTP         = 21,  // XTP native mode XTP
00432     AAA_ADDR_FAMILY_FBRCH_PORT  = 22,  // Fibre Channel World-Wide Port Name
00433     AAA_ADDR_FAMILY_FBRCH_NODE  = 23,  // Fibre Channel World-Wide Node Name
00434     AAA_ADDR_FAMILY_GWID        = 24  // GWID
00435 };
00436 
00437 typedef class AAAAvpContainerList diameter_grouped_t;
00438 
00442 typedef diameter_unsigned32_t diameter_time_t;
00443 
00444 class AAA_UINT8_RANGE
00445 {
00446   public:
00447   AAA_UINT8_RANGE(AAA_UINT8 first, AAA_UINT8 last) : first(first), last(last)
00448   {}
00449   AAA_UINT8_RANGE() 
00450   {}
00451   AAA_UINT8 first;
00452   AAA_UINT8 last;
00453 };
00454 
00455 class AAA_UINT16_RANGE
00456 {
00457   public:
00458   AAA_UINT16_RANGE(ACE_UINT16 first, ACE_UINT16 last) : 
00459   first(first), last(last)
00460   {}
00461   AAA_UINT16_RANGE(ACE_UINT16 single) : first(single), last(single)
00462   {}
00463   AAA_UINT16_RANGE() 
00464   {}
00465   ACE_UINT16 first;
00466   ACE_UINT16 last;
00467 };
00468 
00469 enum {
00470   AAA_IPFILTER_RULE_SRCDST_EXACT,
00471   AAA_IPFILTER_RULE_SRCDST_MASK,
00472   AAA_IPFILTER_RULE_SRCDST_KEYWORD_ANY,
00473   AAA_IPFILTER_RULE_SRCDST_KEYWORD_ASSIGNED
00474 };
00475 
00476 class AAA_IPFILTER_RULE_SRCDST
00477 {
00478  public:
00479   AAA_IPFILTER_RULE_SRCDST(AAA_UINT8 repr=AAA_IPFILTER_RULE_SRCDST_EXACT,
00480                            diameter_utf8string_t ipno=std::string(),
00481                            AAA_UINT8 bits=0,
00482                            bool mod=true) 
00483     : modifier(mod), representation(repr), ipno(ipno), bits(bits)
00484     {}
00485   bool modifier; 
00486   AAA_UINT8
00487   representation; 
00501   diameter_utf8string_t ipno; 
00502   AAA_UINT8 bits;
00503 
00504   std::list<AAA_UINT16_RANGE> portRangeList;   
00505 };
00506 
00507 enum {
00508   AAA_IPFILTER_RULE_IP_OPTION_SSRR=1,
00509   AAA_IPFILTER_RULE_IP_OPTION_LSRR,
00510   AAA_IPFILTER_RULE_IP_OPTION_RR,
00511   AAA_IPFILTER_RULE_IP_OPTION_TS
00512 };
00513 
00514 enum {
00515   AAA_IPFILTER_RULE_TCP_OPTION_MSS=1,
00516   AAA_IPFILTER_RULE_TCP_OPTION_WINDOW,
00517   AAA_IPFILTER_RULE_TCP_OPTION_SACK,
00518   AAA_IPFILTER_RULE_TCP_OPTION_TS,
00519   AAA_IPFILTER_RULE_TCP_OPTION_CC
00520 };
00521 
00522 enum {
00523   AAA_IPFILTER_RULE_TCP_FLAG_FIN=1,
00524   AAA_IPFILTER_RULE_TCP_FLAG_SYN, 
00525   AAA_IPFILTER_RULE_TCP_FLAG_RST, 
00526   AAA_IPFILTER_RULE_TCP_FLAG_PSH,
00527   AAA_IPFILTER_RULE_TCP_FLAG_ACK,
00528   AAA_IPFILTER_RULE_TCP_FLAG_URG
00529 };
00530 
00532 class diameter_ipfilter_rule_t 
00533 {
00534   public:
00535   diameter_ipfilter_rule_t() : frag(false), established(false), setup(false)
00536   {}
00537 
00538   AAA_UINT8 action; 
00542   AAA_UINT8 dir; 
00546   AAA_UINT8 proto; 
00550   AAA_IPFILTER_RULE_SRCDST src, dst;
00551 
00553   bool frag; /* indicates fragmented packets except for the first
00554                 fragment */
00555   std::list<int> ipOptionList; 
00560   std::list<int> tcpOptionList; 
00564   bool established; 
00566   bool setup; 
00569   std::list<int> tcpFlagList; 
00573   std::list<AAA_UINT8_RANGE> icmpTypeRangeList; 
00574 } ;
00575 
00576 enum {
00577   AAA_IPFILTER_RULE_ACTION_PERMIT,
00578   AAA_IPFILTER_RULE_ACTION_DENY 
00579 };
00580 
00581 enum {
00582   AAA_IPFILTER_RULE_DIRECTION_IN,
00583   AAA_IPFILTER_RULE_DIRECTION_OUT 
00584 };
00585 
00594 typedef diameter_octetstring_t avp_t;
00595 
00599 enum {
00600   TRANSPORT_PROTO_TCP = 0,
00601   TRANSPORT_PROTO_SCTP,
00602   TRANSPORT_PROTO_UDP,
00603 };
00604 
00608 enum {
00609   AAA_PROTO_DIAMETER = 0,
00610   AAA_PROTO_RADIUS,
00611   AAA_PROTO_TACACSPLUS,
00612 };
00613 
00617 enum {
00618   AAA_SCHEME_AAA = 0,
00619   AAA_SCHEME_AAAS
00620 };
00621 
00625 enum {
00626   AUTH_REQUEST_TYPE_AUTHENTICATION_ONLY = 1,
00627   AUTH_REQUEST_TYPE_AUTHORIZE_ONLY = 2,
00628   AUTH_REQUEST_TYPE_AUTHORIZE_AUTHENTICATE = 3
00629 };
00630 
00634 #define DIAMETER_DEFAULT_PORT 10001
00635 
00641 enum  {
00642   NORMAL  = 0,   
00643   BUG = 1,       
00644 };
00645   
00649 enum {
00650   MISSING_CONTAINER = 1,
00651   TOO_MUCH_AVP_ENTRIES,
00652   TOO_LESS_AVP_ENTRIES,
00653   PROHIBITED_CONTAINER,
00654   INVALID_CONTAINER_PARAM,
00655   INVALID_CONTAINER_CONTENTS,
00656   UNSUPPORTED_FUNCTIONALITY,
00657   INVALID_PARSER_USAGE,
00658   MISSING_AVP_DICTIONARY_ENTRY,
00659   MISSING_AVP_VALUE_PARSER
00660 };
00661 
00665 typedef enum {
00666     AAA_ADDRESS_RESERVED = 0,  
00667     AAA_ADDRESS_IP,   // IP (IP version 4)
00668     AAA_ADDRESS_IP6,  // IP6 (IP version 6)
00669     AAA_ADDRESS_NSAP, // NSAP
00670     AAA_ADDRESS_HDLC, // (8-bit multidrop)
00671     AAA_ADDRESS_BBN,  // 1822
00672     AAA_ADDRESS_802,  // 802 (includes all 802 media plus Ethernet "canonical format")
00673     AAA_ADDRESS_E163, // E.163   
00674     AAA_ADDRESS_E164, // E.164 (SMDS, Frame Relay, ATM)
00675     AAA_ADDRESS_F69,  // F.69 (Telex)
00676     AAA_ADDRESS_X121, // (X.25, Frame Relay)
00677     AAA_ADDRESS_IPX,  // IPX
00678     AAA_ADDRESS_ATALK, // Appletalk
00679     AAA_ADDRESS_DECIV, // Decnet IV
00680     AAA_ADDRESS_BVINE, // Banyan Vines
00681     AAA_ADDRESS_E164N, // E.164 with NSAP format subaddress
00682     AAA_ADDRESS_DNS,   // DNS (Domain Name System)
00683     AAA_ADDRESS_DN,    // Distinguished Name
00684     AAA_ADDRESS_ASN,   // AS Number
00685     AAA_ADDRESS_XTPV4, // XTP over IP version 4 
00686     AAA_ADDRESS_XTPV6, // XTP over IP version 6
00687     AAA_ADDRESS_XTP,   // native mode XTP
00688     AAA_ADDRESS_FCP,   // Fibre Channel World-Wide Port Name
00689     AAA_ADDRESS_FCN,   // Fibre Channel World-Wide Node Name
00690     AAA_ADDRESS_GWID,  // GWID
00691     AAA_ADDRESS_RESERVED2 = 65535
00692 } AAA_ADDRESS;
00693 
00694 
00699 class DIAMETERPARSER_EXPORT AAAErrorStatus
00700 {
00701    private:
00702       int type;     
00704       int code;     
00706       std::string avp;   
00708    public:
00712       AAAErrorStatus(void) {
00713          type = NORMAL;
00714          code = AAA_SUCCESS;
00715       };
00716 
00724       void get(int &type, int &code, std::string &avp);
00725 
00732       void get(int &type, int &code);
00733 
00740       void set(int type, int code);
00741 
00749       void set(int type, int code, AAADictionaryEntry* data);
00750 };
00751 
00752 typedef ACE_Malloc<ACE_LOCAL_MEMORY_POOL, ACE_SYNCH_MUTEX> AAAMalloc;
00753 typedef ACE_Allocator_Adapter<AAAMalloc> AAAAllocator;
00754 
00755 #define AAA_MEMORY_MANAGER_NAME "AAA_Memory_Manager"
00756 
00767 class DIAMETERPARSER_EXPORT AAAMemoryManager : public AAAAllocator
00768 {
00769   friend class ACE_Singleton<AAAMemoryManager, ACE_Recursive_Thread_Mutex>; 
00771  private:
00775   AAAMemoryManager() : AAAAllocator(AAA_MEMORY_MANAGER_NAME) {}
00776 };
00777 
00778 typedef ACE_Singleton<AAAMemoryManager, ACE_Recursive_Thread_Mutex> AAAMemoryManager_S;
00779 DIAMETERPARSER_SINGLETON_DECLARE(ACE_Singleton, AAAMemoryManager, ACE_Recursive_Thread_Mutex);
00780 
00785 class AAAAvpContainerEntry
00786 {
00787      friend class AAAAvpContainerEntryManager; 
00788      friend class AAAAvpContainer; 
00790   public:
00794     AAA_AVPDataType& dataType() { return this->type; }
00795 
00801     template <class T> inline T& dataRef(Type2Type<T>)
00802       {
00803         return *((T*)data);
00804       }
00805 
00811     template <class T> inline T* dataPtr(Type2Type<T>)
00812       {
00813         return (T*)data;
00814       }
00815 
00816   protected:
00818     AAAAvpContainerEntry(int type) : type((AAA_AVPDataType)type) {}
00819 
00821     virtual ~AAAAvpContainerEntry() {}
00822 
00828     void* operator new(size_t s) 
00829     { return AAAMemoryManager_S::instance()->malloc(s); }
00830 
00836     void operator delete(void *p) 
00837     { AAAMemoryManager_S::instance()->free(p); }
00838 
00839     AAA_AVPDataType type; 
00841     void*           data; 
00842 };
00843 
00844 
00847 template <class T>
00848 class AAATypeSpecificAvpContainerEntry 
00849 : public AAAAvpContainerEntry
00850 {
00851  public:
00857   AAATypeSpecificAvpContainerEntry(int type) : AAAAvpContainerEntry(type) 
00858     { 
00859       data = (new(AAAMemoryManager_S::instance()->malloc(sizeof(T))) T);
00860     }
00861 
00865   ~AAATypeSpecificAvpContainerEntry() 
00866     { 
00867       ((T*)data)->T::~T();
00868       AAAMemoryManager_S::instance()->free(data);
00869     }
00870 
00874   inline T* dataPtr() const { return (T*)data; }
00875 
00879   inline T& dataRef() const { return *((T*)data); }
00880 
00886   void* operator new(size_t s) 
00887   { return AAAMemoryManager_S::instance()->malloc(s); }
00888 
00895   void operator delete(void *p) 
00896   { AAAMemoryManager_S::instance()->free(p); }
00897 
00898 };
00899 
00900 
00901 enum AAAAvpParseType {
00902   PARSE_TYPE_FIXED_HEAD = 0,
00903   PARSE_TYPE_FIXED_TAIL,
00904   PARSE_TYPE_REQUIRED, 
00905   PARSE_TYPE_OPTIONAL
00906 };
00907 
00912 class DIAMETERPARSER_EXPORT AAAAvpContainer 
00913 : public std::vector<AAAAvpContainerEntry*>
00914 {
00915       friend class AAAAvpContainerList; 
00917    public:
00921       AAAAvpContainer() : flag(false), parseType(PARSE_TYPE_OPTIONAL)
00922          {}
00923 
00927       ~AAAAvpContainer() {}
00928 
00935       inline void releaseEntries() 
00936         { for (unsigned i=0; i<size(); i++) { delete (*this)[i]; }}
00937 
00944       inline void add(AAAAvpContainerEntry* e) { resize(size()+1, e); }
00945 
00952       void remove(AAAAvpContainerEntry* e);
00953 
00958       inline const char* getAvpName() const { return avpName.c_str(); }
00959 
00965       inline void setAvpName(const char* name) 
00966       { 
00967         avpName = std::string(name);
00968       }
00969 
00975       void* operator new(size_t s) 
00976         { return AAAMemoryManager_S::instance()->malloc(s); }
00977 
00983       void operator delete(void *p) 
00984         { AAAMemoryManager_S::instance()->free(p); }
00985 
00989       inline AAAAvpParseType& ParseType() { return parseType; }
00990 
00991    protected:
00992       std::string avpName;        
00994       bool flag;  
00996       AAAAvpParseType parseType;  
00997 };
00998 
00999 
01005 class DIAMETERPARSER_EXPORT AAAAvpContainerList 
01006 : public std::list<AAAAvpContainer*>
01007   
01008 {
01009    public:
01013       AAAAvpContainerList() {}
01014 
01018       ~AAAAvpContainerList();
01019 
01025       inline void add(AAAAvpContainer* c) { push_back(c); }
01026 
01032       inline void prepend(AAAAvpContainer* c) { push_front(c); }
01033 
01039       void releaseContainers();
01040 
01047       AAAAvpContainer* search(const char* name);
01048 
01054       void* operator new(size_t s) 
01055         { return AAAMemoryManager_S::instance()->malloc(s); }
01056 
01063       void* operator new(size_t s, void*p) 
01064         { return p; }
01065 
01071       void operator delete(void *p) 
01072         { AAAMemoryManager_S::instance()->free(p); }
01073 
01074       void operator delete(void *p, void*q)
01075         { AAAMemoryManager_S::instance()->free(p); }    
01076 
01077       AAAAvpContainer* search(AAADictionaryEntry*);
01079       void reset(); 
01081  private:
01082       AAAAvpContainer* search(const char*, bool);  
01083       AAAAvpContainer* search(bool);         
01084 };
01085 
01094 class DIAMETERPARSER_EXPORT AAAAvpContainerEntryManager
01095 {
01096    public:
01103       AAAAvpContainerEntry *acquire(AAA_AVPDataType type);
01104 
01110       inline void release(AAAAvpContainerEntry* entry) { delete entry; }
01111 };
01112 
01121 class DIAMETERPARSER_EXPORT AAAAvpContainerManager
01122 {
01123    public:
01130       AAAAvpContainer *acquire(const char* name);
01131 
01137       void release(AAAAvpContainer* entry);
01138 };
01139 
01143 struct hdr_flag {
01144   AAA_UINT8 r:1;  
01145   AAA_UINT8 p:1;  
01146   AAA_UINT8 e:1;  
01147   AAA_UINT8 t:1;  
01148   AAA_UINT8 rsvd:4; 
01149 };
01150 
01154 typedef bool boolean_t;
01155 
01157 class AAADictionaryHandle {};
01158 
01163 class DIAMETERPARSER_EXPORT AAADictionaryManager
01164 {
01165    public:
01171       AAADictionaryManager(int p=-1) : protocol(p) { }
01172     
01180       void init(char *dictFile);
01181 
01190       boolean_t getCommandCode(char *commandName,
01191                                AAACommandCode *commandCode,
01192                                AAAApplicationId *appId);
01193 
01202       AAADictionaryHandle *getDictHandle(AAACommandCode code, 
01203                                          AAAApplicationId id,
01204                                          int rflag);
01205 
01212       AAADictionaryHandle *getDictHandle(char *cmdName);
01213     
01214    private:
01215       int protocol;
01216 };
01217 
01218 class AAAEmptyClass {};
01219 
01224 template <class RAWDATA, class APPDATA, class DICTDATA = AAAEmptyClass>
01225 class DIAMETERPARSER_EXPORT_ONLY AAAParser
01226 {
01227    public:
01231       AAAParser() {}
01232 
01236       virtual ~AAAParser() {};
01237 
01241       virtual void parseRawToApp();
01242 
01246       virtual void parseAppToRaw();
01247 
01253       void setRawData(RAWDATA data) { rawData = data; }
01254 
01260       void setAppData(APPDATA data) { appData = data; }
01261 
01267       void setDictData(DICTDATA data) { dictData = data; }
01268 
01272       RAWDATA getRawData() { return rawData; }
01273 
01277       template <class T> void getRawData(T*& data) { data = (T*)rawData; }
01278 
01282       APPDATA getAppData() { return appData; }
01283 
01287       template <class T> void getAppData(T*& data) { data = (T*)appData; }
01288 
01292       DICTDATA getDictData() { return dictData; }
01293 
01297       template <class T> void getDictData(T*& data) { data = (T*)dictData; }
01298 
01299    private:
01300       RAWDATA rawData;   
01302       APPDATA appData;   
01304       DICTDATA dictData;  
01305 };
01306 
01308 enum DiameterParserError {
01309   DictionaryError,
01310   HeaderError,
01311   PayloadError
01312 };
01313 
01322 class AAAMessageBlock: public ACE_Message_Block
01323 {
01324    public:
01332       static AAAMessageBlock* Acquire(char *buf, ACE_UINT32 s) { return new AAAMessageBlock(buf,s); }
01333 
01340       static AAAMessageBlock* Acquire(ACE_UINT32 s) { return new AAAMessageBlock(s); }
01341 
01348       static AAAMessageBlock* Acquire(AAAMessageBlock* b) { return new AAAMessageBlock(b); }
01349 
01356       void Release() { release(); }
01357 
01358   protected:
01366       AAAMessageBlock(char *buf, ACE_UINT32 s) { init(buf, s); }
01367 
01374       AAAMessageBlock(ACE_UINT32 s) { init(s); }
01375 
01382       AAAMessageBlock(AAAMessageBlock *b) : ACE_Message_Block((const ACE_Message_Block&)*b,0) {}
01383 
01389       void* operator new(size_t s) 
01390       { return AAAMemoryManager_S::instance()->malloc(s); }
01391 
01397       void operator delete(void *p) 
01398       { AAAMemoryManager_S::instance()->free(p); }
01399 
01400 private:
01404       ~AAAMessageBlock() {}
01405 };
01406 
01412 class AAA_MessageBlockScope
01413 {
01414     public:
01415        AAA_MessageBlockScope(ACE_Message_Block *&mb) : mb_(mb) { }
01416        ~AAA_MessageBlockScope(void) { mb_->release(); }
01417 
01418     protected:
01419        ACE_Message_Block *&mb_;
01420 };
01421 
01425 enum ParseOption {
01426   PARSE_LOOSE = 0,
01427   PARSE_STRICT = 1,
01428 };
01429 
01433 class AAADictionaryOption
01434 {
01435 public:
01436     AAADictionaryOption() {}
01437     AAADictionaryOption(ParseOption opt, int id) :
01438         option(opt), protocolId(id) {}
01439     
01440     ParseOption option;
01441     int protocolId;
01442 };
01443 
01444 class AAADiameterHeader;
01445 class AAAMessage;
01446 
01448 typedef AAAParser<AAAMessageBlock*, AAADiameterHeader*, ParseOption>
01449 HeaderParser;
01450 
01452 typedef AAAParser<AAAMessageBlock*, AAADiameterHeader*, AAADictionaryOption*>
01453 HeaderParserWithProtocol;
01454 
01456 typedef AAAParser<AAAMessageBlock*, AAAAvpContainerList*, AAADictionaryHandle*>
01457 PayloadParser;
01458 
01468 void DIAMETERPARSER_EXPORT_ONLY HeaderParser::parseRawToApp();// throw(AAAErrorStatus);
01469 
01480 void DIAMETERPARSER_EXPORT_ONLY HeaderParser::parseAppToRaw();// throw(AAAErrorStatus);
01481 
01494 void DIAMETERPARSER_EXPORT_ONLY PayloadParser::parseRawToApp();// throw(AAAErrorStatus);
01495 
01508 void DIAMETERPARSER_EXPORT_ONLY PayloadParser::parseAppToRaw();// throw(AAAErrorStatus);
01509 
01513 #define HEADER_SIZE 20
01514 
01519 class DIAMETERPARSER_EXPORT AAADiameterHeader
01520 {
01521       friend class AAAParser<AAAMessageBlock*, AAADiameterHeader*, ParseOption>; 
01522       friend class AAAParser<AAAMessageBlock*, AAADiameterHeader*, AAADictionaryOption*>; 
01524    public:
01536       AAADiameterHeader(AAA_UINT8 ver, 
01537                         ACE_UINT32 length, 
01538                         struct hdr_flag flags, 
01539                         AAACommandCode code, 
01540                         AAAApplicationId appId, 
01541                         ACE_UINT32 hh, 
01542                         ACE_UINT32 ee) {
01543           this->ver = ver; 
01544           this->length = length; 
01545           this->flags = flags;
01546           this->code = code; 
01547           this->appId = appId; 
01548           this->hh = hh;
01549           this->ee = ee;
01550       }
01551 
01555       AAADiameterHeader() {}
01556 
01560       inline AAADictionaryHandle *getDictHandle() { return dictHandle; }
01561 
01565       const char* getCommandName();
01566 
01567       AAA_UINT8 ver; 
01569       ACE_UINT32 length:24; 
01571       struct hdr_flag flags; 
01573       AAACommandCode code:24; 
01575       AAAApplicationId appId; 
01577       ACE_UINT32 hh; 
01579       ACE_UINT32 ee; 
01581  private:
01582       AAADictionaryHandle* dictHandle;
01583 };
01584 
01593 class DIAMETERPARSER_EXPORT AAAMessage
01594 {
01595    public:
01596       AAADiameterHeader hdr; 
01598       AAAAvpContainerList acl; 
01600       AAAErrorStatus status; 
01602       IP_ADDR originator; 
01604       IP_ADDR sender; 
01606       time_t secondsTillExpire; 
01608       time_t startTime; 
01609 };
01610 
01611 typedef AAAParser<AAAMessageBlock*, AAAAvpContainerEntry*, 
01612                   AAADictionaryEntry*> AvpValueParser;
01613 
01620 void DIAMETERPARSER_EXPORT_ONLY AvpValueParser::parseRawToApp();// throw(AAAErrorStatus);
01621 
01628 void DIAMETERPARSER_EXPORT_ONLY AvpValueParser::parseAppToRaw();// throw(AAAErrorStatus);
01629 
01634 typedef boost::function1<AAAAvpContainerEntry*, int> AvpContainerEntryFunctor;
01635 
01640 typedef boost::function0<AvpValueParser*> AvpValueParserFunctor;
01641 
01644 template <class T>
01645 class AvpContainerEntryCreator 
01646 {
01647  public:
01653   AAAAvpContainerEntry* operator()(int type) { return new T(type); }
01654 };
01655 
01657 template <class T>
01658 class AvpValueParserCreator
01659 {
01660  public:
01664   AvpValueParser* operator()() { return new T(); }
01665 };
01666 
01672 class DIAMETERPARSER_EXPORT AvpType {
01673  public:
01674 
01684   AvpType(char* name, AAA_AVPDataType type, ACE_UINT32 size,
01685           AvpValueParserFunctor parserCreator,
01686           AvpContainerEntryFunctor containerEntryCreator) :
01687     name(name), type(type), size(size),
01688     parserCreator(parserCreator),
01689     containerEntryCreator(containerEntryCreator) {}
01690 
01694   char* getName(void) { return name; };
01695 
01699   AAA_AVPDataType getType(void) { return type; };
01700 
01704   ACE_UINT32 getMinSize(void) { return size; };
01705 
01710   AvpValueParser* createParser() { return parserCreator(); }
01711 
01716     AAAAvpContainerEntry* createContainerEntry(int type) 
01717     { return containerEntryCreator(type); }
01718 
01719  private:
01720 
01721     char *name; 
01723     AAA_AVPDataType type; 
01725     ACE_UINT32 size;  
01727     AvpValueParserFunctor parserCreator; 
01729     AvpContainerEntryFunctor containerEntryCreator; 
01730 };
01731 
01751 class DIAMETERPARSER_EXPORT AvpTypeList_S : public std::list<AvpType*> 
01752 {
01753   friend class ACE_Singleton<AvpTypeList_S, ACE_Recursive_Thread_Mutex>; 
01754  public:
01760   AvpType* search(ACE_UINT32 type) 
01761     {
01762       for (iterator i = begin(); i != end(); i++)
01763         {
01764           if ((ACE_UINT32)((*i)->getType()) == type)
01765             return *i;
01766         }
01767       return NULL;
01768     }
01774   AvpType* search(const char* name)
01775     {
01776       for (iterator i = begin(); i != end(); i++)
01777         {
01778           if (ACE_OS::strcmp((*i)->getName(), name) == 0)
01779             return *i;
01780         }
01781       return NULL;
01782     }
01788   void add(AvpType* avpType) {
01789     mutex.acquire(); 
01790     push_back(avpType); 
01791     mutex.release();
01792   }
01793  private:
01797   AvpTypeList_S(void) { registerDefaultTypes(); };
01798 
01802   ~AvpTypeList_S(void) {};
01803 
01807   void registerDefaultTypes();
01808 
01809   ACE_Thread_Mutex mutex; 
01810 };
01811 
01812 typedef ACE_Singleton<AvpTypeList_S, ACE_Recursive_Thread_Mutex> AvpTypeList;
01813 DIAMETERPARSER_SINGLETON_DECLARE(ACE_Singleton, AvpTypeList_S, ACE_Recursive_Thread_Mutex);
01814 
01819 class UTF8Checker
01820 {
01821  public:
01825   UTF8Checker() {}
01826 
01830   int operator()(const char *data, unsigned length, bool nullCheck=false)
01831   {
01832     unsigned i = 0;
01833     while (i<length)
01834       {
01835         unsigned char b = data[i++];
01836       
01837         // Check the first octet of the current UTF8 character.
01838         // Null check
01839         if (b == 0x00) { if (nullCheck) return -1; }
01840         // The first bit check.
01841         else if ((b >> 7) == 0x00) { continue; }  // 7-bit ASCII character.
01842 
01843         // If the first 7 bits are all '1's, this is not an UTF8 character.
01844         if ((b >> 1) == 0x76) { return -1; } // Out of UTF8 character range.
01845 
01846         b <<= 1;
01847         // Count the number of '1' of the first octet
01848         int count=0;
01849         for (count=0; count<5; count++) { if ((b && 0x40) == 0) break; }
01850         
01851         // The count value must be greater than 0.
01852         if (count==0) { return -1; } // Out of UTF8 character range.
01853 
01854         // Check remaining octet(s)
01855         for (int j=0; j<count; j++) { 
01856           if (i >= length) { return -1; }
01857           if ((data[i++] >> 6) != 0x02) { return -1; }
01858         }
01859       }
01860     return 0;
01861   }
01862 };
01863 
01867 class AAALogMsg : public ACE_Log_Msg
01868 {
01869        friend class ACE_Singleton<AAALogMsg, ACE_Recursive_Thread_Mutex>;    
01871     private:
01872         /* 
01873          * protected constructors/destructors to prevent derivation
01874          */
01875         AAALogMsg() { }
01876         ~AAALogMsg() { }
01877 };
01878 
01879 typedef ACE_Singleton<AAALogMsg, ACE_Recursive_Thread_Mutex> AAALogMsg_S;
01880 DIAMETERPARSER_SINGLETON_DECLARE(ACE_Singleton, AAALogMsg, ACE_Recursive_Thread_Mutex);
01881 
01882 #define AAA_LOG AAALogMsg_S::instance()->log
01883 
01885 template <typename T>
01886 class AAA_ScholarAttribute
01887 {
01888  public:
01889   AAA_ScholarAttribute() : isSet(false) {}
01890   AAA_ScholarAttribute(T &val) : value(val), isSet(true) {}
01891   virtual ~AAA_ScholarAttribute() {}
01892   inline void Clear() { isSet = false; }
01893   inline void Set(T val) { value=val; isSet=true; }
01894   virtual void CopyFrom(AAAAvpContainer &c) 
01895   { 
01896     value=c[0]->dataRef(Type2Type<T>()); isSet=true; 
01897   }
01898   void CopyTo(AAAAvpContainer &c, AAA_AVPDataType t) 
01899   {
01900     AAAAvpContainerEntryManager em;
01901     AAAAvpContainerEntry *e = em.acquire(t);
01902     e->dataRef(Type2Type<T>()) = value;
01903     c.add(e);
01904   }
01905   inline bool operator==(const T& v) const { return value == v; }
01906   inline T& operator()() { return value; }
01907   inline T& operator=(T v) { isSet = true; value=v; return value; }
01908   inline bool& IsSet() { return isSet; }
01909 
01910  protected:
01911   T value;
01912   bool isSet;
01913 };
01914 
01916 template <typename T>
01917 class AAA_GroupedScholarAttribute : public AAA_ScholarAttribute<T>
01918 {
01919 public:
01920   void CopyFrom(AAAAvpContainer &c)
01921   {
01922       AAAAvpContainerList& cl = 
01923         c[0]->dataRef(Type2Type<AAAAvpContainerList>());
01924       value.CopyFrom(cl);
01925       isSet = true;
01926   }
01927   void CopyTo(AAAAvpContainer &c)
01928   {
01929     AAAAvpContainerEntryManager em;
01930     AAAAvpContainerEntry *e = em.acquire(AAA_AVP_GROUPED_TYPE);
01931     value.CopyTo(e->dataRef(Type2Type<AAAAvpContainerList>()));
01932     c.add(e);
01933   }
01934   inline T& operator=(T v) { isSet = true; value=v; return value; }
01935 };
01936 
01937 template <typename T>
01938 class AAA_VectorAttribute : public std::vector<T>
01939 {
01940  public:
01941   AAA_VectorAttribute() : isSet(false) {}
01942   virtual ~AAA_VectorAttribute() {}
01943   inline void Clear() { isSet = false; std::vector<T>::clear(); }
01944   inline std::vector<T>& operator()() { return *this; }
01945   inline std::vector<T>& operator=(std::vector<T>& value) 
01946   { 
01947     isSet = true; 
01948     (std::vector<T>&)(*this)=value; 
01949         return *this; 
01950   }
01951   virtual void CopyFrom(AAAAvpContainer &c) 
01952   { 
01953     isSet = true; 
01954     if (size() < c.size())
01955       resize(c.size());
01956     for (unsigned i=0; i<c.size(); i++)
01957       (*this)[i] = c[i]->dataRef(Type2Type<T>());
01958   }
01959   void CopyTo(AAAAvpContainer &c, AAA_AVPDataType t) 
01960   {
01961     AAAAvpContainerEntryManager em;
01962     AAAAvpContainerEntry *e = em.acquire(t);
01963     for (unsigned i=0; i<size(); i++)
01964       {
01965         e = em.acquire(t);
01966         e->dataRef(Type2Type<T>()) = (*this)[i];
01967         c.add(e);
01968       }
01969   }
01970   inline bool& IsSet() { return isSet; }
01971 
01972 protected:
01973   bool isSet;
01974 };
01975 
01976 template <typename T>
01977 class AAA_GroupedVectorAttribute : public AAA_VectorAttribute<T>
01978 {
01979 public:
01980   void CopyFrom(AAAAvpContainer &c)
01981   {
01982     isSet = true; 
01983     if (size() < c.size())
01984       resize(c.size());
01985     for (unsigned i=0; i<c.size(); i++)
01986       {
01987         AAAAvpContainerList& cl = 
01988           c[i]->dataRef(Type2Type<AAAAvpContainerList>());
01989         (*this)[i].CopyFrom(cl);
01990         isSet = true;
01991       }
01992   }
01993   void CopyTo(AAAAvpContainer &c)
01994   {
01995     AAAAvpContainerEntryManager em;
01996     AAAAvpContainerEntry *e;
01997     for (unsigned i=0; i<size(); i++)
01998       {
01999         e = em.acquire(AAA_AVP_GROUPED_TYPE);
02000         (*this)[i].CopyTo(e->dataRef(Type2Type<AAAAvpContainerList>()));
02001         e->dataRef(Type2Type<T>()) = (*this)[i];
02002         c.add(e);
02003       }
02004   }
02005   inline T& operator=(T v) 
02006     { 
02007       isSet = true; 
02008       (std::vector<T>&)(*this)=v; 
02009       return value; 
02010     }
02011 };
02012 
02015 template<class D, AAA_AVPDataType t>
02016 class AAA_AvpWidget {
02017     public:
02018         AAA_AvpWidget(char *name) {
02019             AAAAvpContainerManager cm;
02020             m_cAvp = cm.acquire(name);
02021         }
02022         AAA_AvpWidget(char *name, D &value) {
02023             AAAAvpContainerManager cm;
02024             m_cAvp = cm.acquire(name);
02025             Get() = value;
02026         }
02027         AAA_AvpWidget(AAAAvpContainer *avp) :
02028             m_cAvp(avp) {
02029         }
02030         ~AAA_AvpWidget() {
02031         }
02032         D &Get() {
02033             AAAAvpContainerEntryManager em;
02034             AAAAvpContainerEntry *e = em.acquire(t);
02035             m_cAvp->add(e);            
02036             return e->dataRef(Type2Type<D>());
02037         }
02038         AAAAvpContainer *operator()() {
02039             return m_cAvp;
02040         }
02041         bool empty() {
02042             return (m_cAvp->size() == 0);
02043         }
02044     private:
02045         AAAAvpContainer *m_cAvp;
02046 };
02047 
02048 typedef AAA_AvpWidget<diameter_identity_t,
02049                       AAA_AVP_DIAMID_TYPE> AAA_IdentityAvpWidget;
02050 typedef AAA_AvpWidget<diameter_address_t,
02051                       AAA_AVP_ADDRESS_TYPE> AAA_AddressAvpWidget;
02052 typedef AAA_AvpWidget<diameter_integer32_t,
02053                       AAA_AVP_INTEGER32_TYPE> AAA_Int32AvpWidget;
02054 typedef AAA_AvpWidget<diameter_unsigned32_t,
02055                       AAA_AVP_UINTEGER32_TYPE> AAA_UInt32AvpWidget;
02056 typedef AAA_AvpWidget<diameter_integer64_t,
02057                       AAA_AVP_INTEGER64_TYPE> AAA_Int64AvpWidget;
02058 typedef AAA_AvpWidget<diameter_unsigned64_t,
02059                       AAA_AVP_UINTEGER64_TYPE> AAA_UInt64AvpWidget;
02060 typedef AAA_AvpWidget<diameter_utf8string_t,
02061                      AAA_AVP_UTF8_STRING_TYPE> AAA_Utf8AvpWidget;
02062 typedef AAA_AvpWidget<diameter_grouped_t,
02063                      AAA_AVP_GROUPED_TYPE> AAA_GroupedAvpWidget;
02064 typedef AAA_AvpWidget<diameter_octetstring_t,
02065                      AAA_AVP_STRING_TYPE> AAA_StringAvpWidget;
02066 typedef AAA_AvpWidget<diameter_uri_t,
02067                      AAA_AVP_DIAMURI_TYPE> AAA_DiamUriAvpWidget;
02068 typedef AAA_AvpWidget<diameter_enumerated_t,
02069                      AAA_AVP_ENUM_TYPE> AAA_EnumAvpWidget;
02070 
02075 template<class D, AAA_AVPDataType t>
02076 class AAA_AvpContainerWidget
02077 {
02078     public:
02079        AAA_AvpContainerWidget(AAAAvpContainerList &lst) :
02080            m_List(lst) {
02081        }
02082        D *GetAvp(char *name, unsigned int index=0) {
02083           AAAAvpContainer* c = m_List.search(name);
02084           if (c && (index < c->size())) {
02085               AAAAvpContainerEntry *e = (*c)[index];
02086               return e->dataPtr(Type2Type<D>());
02087           }
02088           return (0);
02089        }
02090        D &AddAvp(char *name) {
02091           AAAAvpContainer* c = m_List.search(name);
02092           if (! c) {
02093               AAA_AvpWidget<D, t> avpWidget(name);
02094               m_List.add(avpWidget());
02095               return avpWidget.Get();
02096           }
02097           else if (c->size() == 0) {
02098               AAA_AvpWidget<D, t> avpWidget(c);
02099               m_List.add(avpWidget());
02100               return avpWidget.Get();
02101           }
02102           else {
02103               return (*c)[0]->dataRef(Type2Type<D>());
02104           }
02105        }
02106        void AddAvp(AAA_AvpContainerWidget<D, t> &avp) {
02107            m_List.add(avp());
02108        }
02109        void DelAvp(char *name) {
02110           std::list<AAAAvpContainer*>::iterator i;
02111           for (i=m_List.begin(); i!=m_List.end();i++) {
02112               AAAAvpContainer *c = *i;
02113               if (ACE_OS::strcmp(c->getAvpName(), name) == 0) {
02114                   m_List.erase(i);
02115                   AAAAvpContainerManager cm;
02116                   cm.release(c);
02117                   break;
02118               }
02119           }
02120        }
02121 
02122     private:
02123        AAAAvpContainerList &m_List;
02124 };
02125 
02126 typedef AAA_AvpContainerWidget<diameter_identity_t, AAA_AVP_DIAMID_TYPE>
02127            AAA_IdentityAvpContainerWidget;
02128 typedef AAA_AvpContainerWidget<diameter_address_t, AAA_AVP_ADDRESS_TYPE>
02129            AAA_AddressAvpContainerWidget;
02130 typedef AAA_AvpContainerWidget<diameter_integer32_t, AAA_AVP_INTEGER32_TYPE>
02131            AAA_Int32AvpContainerWidget;
02132 typedef AAA_AvpContainerWidget<diameter_unsigned32_t, AAA_AVP_UINTEGER32_TYPE>
02133            AAA_UInt32AvpContainerWidget;
02134 typedef AAA_AvpContainerWidget<diameter_integer64_t, AAA_AVP_INTEGER64_TYPE>
02135            AAA_Int64AvpContainerWidget;
02136 typedef AAA_AvpContainerWidget<diameter_unsigned64_t, AAA_AVP_UINTEGER64_TYPE>
02137            AAA_UInt64AvpContainerWidget;
02138 typedef AAA_AvpContainerWidget<diameter_utf8string_t, AAA_AVP_UTF8_STRING_TYPE>
02139            AAA_Utf8AvpContainerWidget;
02140 typedef AAA_AvpContainerWidget<diameter_grouped_t, AAA_AVP_GROUPED_TYPE>
02141            AAA_GroupedAvpContainerWidget;
02142 typedef AAA_AvpContainerWidget<diameter_octetstring_t, AAA_AVP_STRING_TYPE>
02143            AAA_StringAvpContainerWidget;
02144 typedef AAA_AvpContainerWidget<diameter_uri_t, AAA_AVP_DIAMURI_TYPE>
02145            AAA_DiamUriAvpContainerWidget;
02146 typedef AAA_AvpContainerWidget<diameter_enumerated_t, AAA_AVP_ENUM_TYPE>
02147            AAA_EnumAvpContainerWidget;
02148 
02153 class AAA_MsgResultCode
02154 {
02155     public:
02156         typedef enum {
02157             RCODE_NON_PRESENT,
02158             RCODE_INFORMATIONAL,
02159             RCODE_SUCCESS,
02160             RCODE_PROTOCOL_ERROR,
02161             RCODE_TRANSIENT_FAILURE,
02162             RCODE_PERMANENT_FAILURE
02163        } RCODE;
02164     
02165     public:
02166        AAA_MsgResultCode(AAAMessage &msg) :
02167            m_Msg(msg) {
02168        }
02169        diameter_unsigned32_t ResultCode() {
02170            AAA_UInt32AvpContainerWidget resultCode(m_Msg.acl);
02171            diameter_unsigned32_t *rc = resultCode.GetAvp("Result-Code");
02172            return (rc) ? *rc : 0;
02173        }
02174        void ResultCode(diameter_unsigned32_t c) {
02175            AAA_UInt32AvpContainerWidget resultCode(m_Msg.acl);
02176            resultCode.AddAvp("Result-Code") = c;
02177        }
02178        RCODE InterpretedResultCode() {
02179            diameter_unsigned32_t code = ResultCode();
02180            if (code) {
02181                for (int i=RCODE_INFORMATIONAL;
02182                     i<=RCODE_PERMANENT_FAILURE;
02183                     i++) {
02184                    code -= 1000;
02185                    if ((code/1000) == 0) {
02186                        return RCODE(i);
02187                    }
02188                }
02189                return (RCODE_PERMANENT_FAILURE);
02190            }
02191            return (RCODE_NON_PRESENT);
02192        }
02193     
02194     private:
02195        AAAMessage &m_Msg;
02196 };
02197 
02200 class AAA_MsgDump {
02201    public:
02202       static void Dump(AAAMessage &msg) {
02203           AAA_LOG(LM_INFO, "(%P|%t) Message header dump\n");
02204           AAA_LOG(LM_INFO, "          version = %d\n", int(msg.hdr.ver));
02205           AAA_LOG(LM_INFO, "          length  = %d\n", int(msg.hdr.length));
02206           AAA_LOG(LM_INFO, "     flags(r,p,e) = (%d,%d,%d)\n",
02207                     int(msg.hdr.flags.r), int(msg.hdr.flags.p),
02208                     int(msg.hdr.flags.e));
02209           AAA_LOG(LM_INFO, "          command = %d\n", int(msg.hdr.code));
02210           AAA_LOG(LM_INFO, "       hop-by-hop = %d\n", int(msg.hdr.hh));
02211           AAA_LOG(LM_INFO, "       end-to-end = %d\n", int(msg.hdr.ee));
02212           AAA_LOG(LM_INFO, "   Application id = %d\n", int(msg.hdr.appId));
02213       }
02214 };
02215 
02216 //
02217 // General 
02218 //
02219 #define AAA_PROTOCOL_VERSION              0x1
02220 #define AAA_FLG_SET                       0x1
02221 #define AAA_FLG_CLR                       0x0
02222 
02226 class AAA_MsgWidget
02227 {
02228     public:
02229         AAA_MsgWidget(int code,
02230                       int request = true,
02231                       int appId = AAA_BASE_APPLICATION_ID) :
02232            m_Msg(std::auto_ptr<AAAMessage>(new AAAMessage)) {
02233            ACE_OS::memset(&(m_Msg->hdr), 0, sizeof(m_Msg->hdr));
02234            m_Msg->hdr.ver = AAA_PROTOCOL_VERSION;
02235            m_Msg->hdr.length = 0;
02236            m_Msg->hdr.flags.r = request ? AAA_FLG_SET : AAA_FLG_CLR;
02237            m_Msg->hdr.flags.p = AAA_FLG_CLR;
02238            m_Msg->hdr.flags.e = AAA_FLG_CLR;
02239            m_Msg->hdr.code = code;
02240            m_Msg->hdr.appId = appId;
02241         }
02242         ~AAA_MsgWidget() {
02243         }
02244         std::auto_ptr<AAAMessage> &operator()() {
02245            return m_Msg;
02246         }
02247         AAAMessage &Release() {
02248            return *(m_Msg.release());
02249         }
02250         void Dump() {
02251            if (m_Msg.get()) {
02252                AAA_MsgDump::Dump(*m_Msg);
02253                return;
02254            }
02255            AAA_LOG(LM_INFO, "Msg widget is un-assigned\n");
02256         }
02257     private:
02258         std::auto_ptr<AAAMessage> m_Msg;
02259 };
02260 
02264 class AAA_DiameterMsgParserWidget
02265 {
02266 public:
02267     virtual int ParseRawToApp(AAA_MsgWidget &msg,
02268                               AAAMessageBlock& rawBuf,
02269                               int option) {
02270        HeaderParser hp;
02271        hp.setRawData(&rawBuf);
02272        hp.setAppData(&msg()->hdr);
02273        hp.setDictData((ParseOption)option);
02274        hp.parseRawToApp();
02275        rawBuf.size(msg()->hdr.length);
02276 
02277        PayloadParser pp;
02278        pp.setRawData(&rawBuf);
02279        pp.setAppData(&msg()->acl);
02280        pp.setDictData(msg()->hdr.getDictHandle());
02281        pp.parseRawToApp();
02282        return (msg()->hdr.length);
02283     }
02284     virtual int ParseAppToRaw(AAA_MsgWidget &msg,
02285                               AAAMessageBlock& rawBuf,
02286                               int option) {
02287        HeaderParser hp;
02288        hp.setRawData(&rawBuf);
02289        hp.setAppData(&msg()->hdr);
02290        hp.setDictData((ParseOption)option);
02291        hp.parseAppToRaw();
02292 
02293        PayloadParser pp;
02294        pp.setRawData(&rawBuf);
02295        pp.setAppData(&msg()->acl);
02296        pp.setDictData(msg()->hdr.getDictHandle());
02297        pp.parseAppToRaw();
02298 
02299        msg()->hdr.length = rawBuf.wr_ptr() - rawBuf.base();
02300        rawBuf.wr_ptr(rawBuf.base());
02301        hp.parseAppToRaw();
02302        return (msg()->hdr.length);
02303     }
02304 };
02305 
02309 class AAA_DiameterMsgParserWidgetChecked : 
02310     public AAA_DiameterMsgParserWidget
02311 {
02312 public:
02313     virtual int ParseRawToApp(AAA_MsgWidget &msg,
02314                               void *buf,
02315                               int bufSize,
02316                               int option) {
02317         AAAMessageBlock *aBuffer = AAAMessageBlock::Acquire
02318             ((char*)buf, bufSize);
02319         ACE_Message_Block *aBlock = aBuffer;
02320         AAA_MessageBlockScope block_guard(aBlock); 
02321         return ParseRawToApp(msg, *aBuffer, option);
02322     }
02323     virtual int ParseRawToApp(AAA_MsgWidget &msg,
02324                               AAAMessageBlock& rawBuf,
02325                               int option) {
02326         try {
02327             return AAA_DiameterMsgParserWidget::ParseRawToApp
02328                       (msg, rawBuf, option);
02329         }
02330         catch (AAAErrorStatus &st) {
02331             ErrorDump(st);
02332         }
02333         catch (...) {
02334             AAA_LOG(LM_INFO, "Parser error: Unknown fatal !!!\n");
02335             exit (0);
02336         }
02337         return (-1);
02338     }
02339     virtual int ParseAppToRaw(AAA_MsgWidget &msg,
02340                               void *buf,
02341                               int bufSize,
02342                               int option) {
02343         AAAMessageBlock *aBuffer = AAAMessageBlock::Acquire
02344             ((char*)buf, bufSize);
02345         ACE_Message_Block *aBlock = aBuffer;
02346         AAA_MessageBlockScope block_guard(aBlock); 
02347         return ParseAppToRaw(msg, *aBuffer, option);
02348     }
02349     virtual int ParseAppToRaw(AAA_MsgWidget &msg,
02350                               AAAMessageBlock& rawBuf,
02351                               int option) {
02352         try {
02353             return AAA_DiameterMsgParserWidget::ParseAppToRaw
02354                       (msg, rawBuf, option);
02355         }
02356         catch (AAAErrorStatus &st) {
02357             ErrorDump(st);
02358         }
02359         catch (...) {
02360             AAA_LOG(LM_INFO, "Parser error: Unknown fatal !!!\n");
02361             exit (0);
02362         }
02363         return (-1);
02364     }
02365     static void ErrorDump(AAAErrorStatus &st) {
02366         AAA_LOG(LM_INFO, "Parser error: ");
02367         int type, code;
02368         std::string avp;
02369         st.get(type, code, avp);
02370         AAA_LOG(LM_INFO, "Error type=%d, code=%d, name=%s\n",
02371                 type, code, avp.data());
02372     }
02373 };
02374 
02375 #endif /* __DIAMETER_PARSER_API_H__ */
02376 
02377 
02378 

Generated on Mon Jan 10 20:16:50 2005 for Open Diameter C++ API by  doxygen 1.3.9.1