00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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 } AAAReturnCode;
00230
00235 typedef enum {
00236 AAA_AVP_DATA_TYPE,
00237 AAA_AVP_STRING_TYPE,
00238 AAA_AVP_ADDRESS_TYPE,
00239 AAA_AVP_INTEGER32_TYPE,
00240 AAA_AVP_INTEGER64_TYPE,
00241 AAA_AVP_UINTEGER32_TYPE,
00242 AAA_AVP_UINTEGER64_TYPE,
00243 AAA_AVP_UTF8_STRING_TYPE,
00244 AAA_AVP_ENUM_TYPE,
00245 AAA_AVP_DIAMID_TYPE,
00246 AAA_AVP_DIAMURI_TYPE,
00247 AAA_AVP_GROUPED_TYPE,
00248 AAA_AVP_TIME_TYPE,
00249 AAA_AVP_IPFILTER_RULE_TYPE,
00250 AAA_AVP_CUSTOM_TYPE,
00251 } AAA_AVPDataType;
00252
00253
00298 typedef enum {
00299 AAA_AVP_FLAG_NONE = 0,
00300 AAA_AVP_FLAG_MANDATORY = 0x1,
00301 AAA_AVP_FLAG_RESERVED = 0x2,
00302 AAA_AVP_FLAG_VENDOR_SPECIFIC = 0x4,
00303 AAA_AVP_FLAG_END_TO_END_ENCRYPT = 0x10,
00304 AAA_AVP_FLAG_UNKNOWN = 0x10000,
00305 AAA_AVP_FLAG_ENCRYPT = 0x40000,
00306 } AAA_AVPFlagEnum;
00307
00308 typedef ACE_UINT32 AAA_AVPFlag;
00309
00317 class DIAMETERPARSER_EXPORT AAADictionaryEntry
00318 {
00319 public:
00330 AAADictionaryEntry(AAA_AVPCode code,
00331 const char *name,
00332 AAA_AVPDataType type,
00333 AAAVendorId id,
00334 AAA_AVPFlag flg,
00335 int proto);
00336
00337 AAA_AVPCode avpCode;
00338 std::string avpName;
00339 AAA_AVPDataType avpType;
00340 AAAVendorId vendorId;
00341 AAA_AVPFlag flags;
00342 int protocol;
00343 };
00344
00351 typedef enum {
00352 AAA_ACCT_EVENT = 1,
00353 AAA_ACCT_START = 2,
00354 AAA_ACCT_INTERIM = 3,
00355 AAA_ACCT_STOP = 4
00356 } AAAAcctMessageType;
00357
00361 typedef enum {
00362 AAA_STYPE_AUTHENTICATION,
00363 AAA_STYPE_ACCOUNTING
00364 } AAASessionType;
00365
00371 #define AAA_NO_VENDOR_ID 0
00372
00373 #define AAA_BASE_APPLICATION_ID 0
00374
00378 typedef ACE_INT32 diameter_integer32_t;
00379 typedef ACE_UINT64 diameter_integer64_t;
00380 typedef ACE_UINT32 diameter_unsigned32_t;
00381 typedef ACE_UINT64 diameter_unsigned64_t;
00382 typedef diameter_unsigned32_t diameter_enumerated_t;
00383 typedef std::string diameter_octetstring_t;
00384 typedef diameter_octetstring_t diameter_utf8string_t;
00385 typedef struct {
00386 public:
00387 std::string fqdn;
00388 ACE_UINT16 port;
00389 AAA_UINT8 transport:2;
00390 AAA_UINT8 protocol:2;
00391 AAA_UINT8 scheme:2;
00392 } diameter_uri_t;
00393 typedef diameter_utf8string_t diameter_identity_t;
00394 typedef diameter_octetstring_t diameter_ipaddress_t;
00395
00396
00397
00398
00399
00400 typedef struct {
00401 public:
00402 ACE_UINT16 type;
00403 diameter_octetstring_t value;
00404 } diameter_address_t;
00405
00406 typedef class AAAAvpContainerList diameter_grouped_t;
00407
00411 typedef diameter_unsigned32_t diameter_time_t;
00412
00413 class AAA_UINT8_RANGE
00414 {
00415 public:
00416 AAA_UINT8_RANGE(AAA_UINT8 first, AAA_UINT8 last) : first(first), last(last)
00417 {}
00418 AAA_UINT8_RANGE()
00419 {}
00420 AAA_UINT8 first;
00421 AAA_UINT8 last;
00422 };
00423
00424 class AAA_UINT16_RANGE
00425 {
00426 public:
00427 AAA_UINT16_RANGE(ACE_UINT16 first, ACE_UINT16 last) :
00428 first(first), last(last)
00429 {}
00430 AAA_UINT16_RANGE(ACE_UINT16 single) : first(single), last(single)
00431 {}
00432 AAA_UINT16_RANGE()
00433 {}
00434 ACE_UINT16 first;
00435 ACE_UINT16 last;
00436 };
00437
00438 enum {
00439 AAA_IPFILTER_RULE_SRCDST_EXACT,
00440 AAA_IPFILTER_RULE_SRCDST_MASK,
00441 AAA_IPFILTER_RULE_SRCDST_KEYWORD_ANY,
00442 AAA_IPFILTER_RULE_SRCDST_KEYWORD_ASSIGNED
00443 };
00444
00445 class AAA_IPFILTER_RULE_SRCDST
00446 {
00447 public:
00448 AAA_IPFILTER_RULE_SRCDST(AAA_UINT8 repr=AAA_IPFILTER_RULE_SRCDST_EXACT,
00449 diameter_utf8string_t ipno=std::string(),
00450 AAA_UINT8 bits=0,
00451 bool mod=true)
00452 : modifier(mod), representation(repr), ipno(ipno), bits(bits)
00453 {}
00454 bool modifier;
00455 AAA_UINT8
00456 representation;
00470 diameter_utf8string_t ipno;
00471 AAA_UINT8 bits;
00472
00473 std::list<AAA_UINT16_RANGE> portRangeList;
00474 };
00475
00476 enum {
00477 AAA_IPFILTER_RULE_IP_OPTION_SSRR=1,
00478 AAA_IPFILTER_RULE_IP_OPTION_LSRR,
00479 AAA_IPFILTER_RULE_IP_OPTION_RR,
00480 AAA_IPFILTER_RULE_IP_OPTION_TS
00481 };
00482
00483 enum {
00484 AAA_IPFILTER_RULE_TCP_OPTION_MSS=1,
00485 AAA_IPFILTER_RULE_TCP_OPTION_WINDOW,
00486 AAA_IPFILTER_RULE_TCP_OPTION_SACK,
00487 AAA_IPFILTER_RULE_TCP_OPTION_TS,
00488 AAA_IPFILTER_RULE_TCP_OPTION_CC
00489 };
00490
00491 enum {
00492 AAA_IPFILTER_RULE_TCP_FLAG_FIN=1,
00493 AAA_IPFILTER_RULE_TCP_FLAG_SYN,
00494 AAA_IPFILTER_RULE_TCP_FLAG_RST,
00495 AAA_IPFILTER_RULE_TCP_FLAG_PSH,
00496 AAA_IPFILTER_RULE_TCP_FLAG_ACK,
00497 AAA_IPFILTER_RULE_TCP_FLAG_URG
00498 };
00499
00501 class diameter_ipfilter_rule_t
00502 {
00503 public:
00504 diameter_ipfilter_rule_t() : frag(false), established(false), setup(false)
00505 {}
00506
00507 AAA_UINT8 action;
00511 AAA_UINT8 dir;
00515 AAA_UINT8 proto;
00519 AAA_IPFILTER_RULE_SRCDST src, dst;
00520
00522 bool frag;
00523
00524 std::list<int> ipOptionList;
00529 std::list<int> tcpOptionList;
00533 bool established;
00535 bool setup;
00538 std::list<int> tcpFlagList;
00542 std::list<AAA_UINT8_RANGE> icmpTypeRangeList;
00543 } ;
00544
00545 enum {
00546 AAA_IPFILTER_RULE_ACTION_PERMIT,
00547 AAA_IPFILTER_RULE_ACTION_DENY
00548 };
00549
00550 enum {
00551 AAA_IPFILTER_RULE_DIRECTION_IN,
00552 AAA_IPFILTER_RULE_DIRECTION_OUT
00553 };
00554
00563 typedef diameter_octetstring_t avp_t;
00564
00568 enum {
00569 TRANSPORT_PROTO_TCP = 0,
00570 TRANSPORT_PROTO_SCTP,
00571 TRANSPORT_PROTO_UDP,
00572 };
00573
00577 enum {
00578 AAA_PROTO_DIAMETER = 0,
00579 AAA_PROTO_RADIUS,
00580 AAA_PROTO_TACACSPLUS,
00581 };
00582
00586 enum {
00587 AAA_SCHEME_AAA = 0,
00588 AAA_SCHEME_AAAS
00589 };
00590
00594 enum {
00595 AUTH_REQUEST_TYPE_AUTHENTICATION_ONLY = 1,
00596 AUTH_REQUEST_TYPE_AUTHORIZE_ONLY = 2,
00597 AUTH_REQUEST_TYPE_AUTHORIZE_AUTHENTICATE = 3
00598 };
00599
00603 #define DIAMETER_DEFAULT_PORT 10001
00604
00610 enum {
00611 NORMAL = 0,
00612 BUG = 1,
00613 };
00614
00618 enum {
00619 MISSING_CONTAINER = 1,
00620 TOO_MUCH_AVP_ENTRIES,
00621 TOO_LESS_AVP_ENTRIES,
00622 PROHIBITED_CONTAINER,
00623 INVALID_CONTAINER_PARAM,
00624 INVALID_CONTAINER_CONTENTS,
00625 UNSUPPORTED_FUNCTIONALITY,
00626 INVALID_PARSER_USAGE,
00627 MISSING_AVP_DICTIONARY_ENTRY,
00628 MISSING_AVP_VALUE_PARSER
00629 };
00630
00634 typedef enum {
00635 AAA_ADDRESS_RESERVED = 0,
00636 AAA_ADDRESS_IP,
00637 AAA_ADDRESS_IP6,
00638 AAA_ADDRESS_NSAP,
00639 AAA_ADDRESS_HDLC,
00640 AAA_ADDRESS_BBN,
00641 AAA_ADDRESS_802,
00642 AAA_ADDRESS_E163,
00643 AAA_ADDRESS_E164,
00644 AAA_ADDRESS_F69,
00645 AAA_ADDRESS_X121,
00646 AAA_ADDRESS_IPX,
00647 AAA_ADDRESS_ATALK,
00648 AAA_ADDRESS_DECIV,
00649 AAA_ADDRESS_BVINE,
00650 AAA_ADDRESS_E164N,
00651 AAA_ADDRESS_DNS,
00652 AAA_ADDRESS_DN,
00653 AAA_ADDRESS_ASN,
00654 AAA_ADDRESS_XTPV4,
00655 AAA_ADDRESS_XTPV6,
00656 AAA_ADDRESS_XTP,
00657 AAA_ADDRESS_FCP,
00658 AAA_ADDRESS_FCN,
00659 AAA_ADDRESS_GWID,
00660 AAA_ADDRESS_RESERVED2 = 65535
00661 } AAA_ADDRESS;
00662
00663
00668 class DIAMETERPARSER_EXPORT AAAErrorStatus
00669 {
00670 private:
00671 int type;
00673 int code;
00675 std::string avp;
00677 public:
00681 AAAErrorStatus(void) {
00682 type = NORMAL;
00683 code = DIAMETER_SUCCESS;
00684 };
00685
00693 void get(int &type, int &code, std::string &avp);
00694
00701 void get(int &type, int &code);
00702
00709 void set(int type, int code);
00710
00718 void set(int type, int code, AAADictionaryEntry* data);
00719 };
00720
00721 typedef ACE_Malloc<ACE_LOCAL_MEMORY_POOL, ACE_SYNCH_MUTEX> AAAMalloc;
00722 typedef ACE_Allocator_Adapter<AAAMalloc> AAAAllocator;
00723
00724 #define AAA_MEMORY_MANAGER_NAME "AAA_Memory_Manager"
00725
00736 class DIAMETERPARSER_EXPORT AAAMemoryManager : public AAAAllocator
00737 {
00738 friend class ACE_Singleton<AAAMemoryManager, ACE_Recursive_Thread_Mutex>;
00740 private:
00744 AAAMemoryManager() : AAAAllocator(AAA_MEMORY_MANAGER_NAME) {}
00745 };
00746
00747 typedef ACE_Singleton<AAAMemoryManager, ACE_Recursive_Thread_Mutex> AAAMemoryManager_S;
00748 DIAMETERPARSER_SINGLETON_DECLARE(ACE_Singleton, AAAMemoryManager, ACE_Recursive_Thread_Mutex);
00749
00754 class AAAAvpContainerEntry
00755 {
00756 friend class AAAAvpContainerEntryManager;
00757 friend class AAAAvpContainer;
00759 public:
00763 AAA_AVPDataType& dataType() { return this->type; }
00764
00770 template <class T> inline T& dataRef(Type2Type<T>)
00771 {
00772 return *((T*)data);
00773 }
00774
00780 template <class T> inline T* dataPtr(Type2Type<T>)
00781 {
00782 return (T*)data;
00783 }
00784
00785 protected:
00787 AAAAvpContainerEntry(int type) : type((AAA_AVPDataType)type) {}
00788
00790 ~AAAAvpContainerEntry() {}
00791
00797 void* operator new(size_t s)
00798 { return AAAMemoryManager_S::instance()->malloc(s); }
00799
00805 void operator delete(void *p)
00806 { AAAMemoryManager_S::instance()->free(p); }
00807
00808 AAA_AVPDataType type;
00810 void* data;
00811 };
00812
00813
00816 template <class T>
00817 class AAATypeSpecificAvpContainerEntry
00818 : public AAAAvpContainerEntry
00819 {
00820 public:
00826 AAATypeSpecificAvpContainerEntry(int type) : AAAAvpContainerEntry(type)
00827 {
00828 data = (new(AAAMemoryManager_S::instance()->malloc(sizeof(T))) T);
00829 }
00830
00834 ~AAATypeSpecificAvpContainerEntry()
00835 {
00836 AAAMemoryManager_S::instance()->free(data);
00837 }
00838
00842 inline T* dataPtr() const { return (T*)data; }
00843
00847 inline T& dataRef() const { return *((T*)data); }
00848
00854 void* operator new(size_t s)
00855 { return AAAMemoryManager_S::instance()->malloc(s); }
00856
00863 void operator delete(void *p)
00864 { AAAMemoryManager_S::instance()->free(p); }
00865
00866 };
00867
00868
00869 enum AAAAvpParseType {
00870 PARSE_TYPE_FIXED_HEAD = 0,
00871 PARSE_TYPE_FIXED_TAIL,
00872 PARSE_TYPE_REQUIRED,
00873 PARSE_TYPE_OPTIONAL
00874 };
00875
00880 class DIAMETERPARSER_EXPORT AAAAvpContainer
00881 : public std::vector<AAAAvpContainerEntry*>
00882 {
00883 friend class AAAAvpContainerList;
00885 public:
00889 AAAAvpContainer() : flag(false), parseType(PARSE_TYPE_OPTIONAL)
00890 {}
00891
00895 ~AAAAvpContainer() {}
00896
00903 inline void releaseEntries()
00904 { for (unsigned i=0; i<size(); i++) { delete (*this)[i]; }}
00905
00912 inline void add(AAAAvpContainerEntry* e) { resize(size()+1, e); }
00913
00920 void remove(AAAAvpContainerEntry* e);
00921
00926 inline const char* getAvpName() const { return avpName.c_str(); }
00927
00933 inline void setAvpName(const char* name)
00934 {
00935 avpName = std::string(name);
00936 }
00937
00943 void* operator new(size_t s)
00944 { return AAAMemoryManager_S::instance()->malloc(s); }
00945
00951 void operator delete(void *p)
00952 { AAAMemoryManager_S::instance()->free(p); }
00953
00957 inline AAAAvpParseType& ParseType() { return parseType; }
00958
00959 protected:
00960 std::string avpName;
00962 bool flag;
00964 AAAAvpParseType parseType;
00965 };
00966
00967
00973 class DIAMETERPARSER_EXPORT AAAAvpContainerList
00974 : public std::list<AAAAvpContainer*>
00975
00976 {
00977 public:
00981 AAAAvpContainerList() {}
00982
00986 ~AAAAvpContainerList();
00987
00993 inline void add(AAAAvpContainer* c) { push_back(c); }
00994
01000 inline void prepend(AAAAvpContainer* c) { push_front(c); }
01001
01007 void releaseContainers();
01008
01015 AAAAvpContainer* search(const char* name);
01016
01022 void* operator new(size_t s)
01023 { return AAAMemoryManager_S::instance()->malloc(s); }
01024
01031 void* operator new(size_t s, void*p)
01032 { return p; }
01033
01039 void operator delete(void *p)
01040 { AAAMemoryManager_S::instance()->free(p); }
01041
01042 void operator delete(void *p, void*q)
01043 { AAAMemoryManager_S::instance()->free(p); }
01044
01045 AAAAvpContainer* search(AAADictionaryEntry*);
01047 void reset();
01049 private:
01050 AAAAvpContainer* search(const char*, bool);
01051 AAAAvpContainer* search(bool);
01052 };
01053
01062 class DIAMETERPARSER_EXPORT AAAAvpContainerEntryManager
01063 {
01064 public:
01071 AAAAvpContainerEntry *acquire(AAA_AVPDataType type);
01072
01078 inline void release(AAAAvpContainerEntry* entry) { delete entry; }
01079 };
01080
01089 class DIAMETERPARSER_EXPORT AAAAvpContainerManager
01090 {
01091 public:
01098 AAAAvpContainer *acquire(const char* name);
01099
01105 void release(AAAAvpContainer* entry);
01106 };
01107
01111 struct hdr_flag {
01112 AAA_UINT8 r:1;
01113 AAA_UINT8 p:1;
01114 AAA_UINT8 e:1;
01115 AAA_UINT8 t:1;
01116 AAA_UINT8 rsvd:4;
01117 };
01118
01122 typedef bool boolean_t;
01123
01125 class AAADictionaryHandle {};
01126
01131 class DIAMETERPARSER_EXPORT AAADictionaryManager
01132 {
01133 public:
01139 AAADictionaryManager(int p=-1) : protocol(p) { }
01140
01148 void init(char *dictFile);
01149
01158 boolean_t getCommandCode(char *commandName,
01159 AAACommandCode *commandCode,
01160 AAAApplicationId *appId);
01161
01170 AAADictionaryHandle *getDictHandle(AAACommandCode code,
01171 AAAApplicationId id,
01172 int rflag);
01173
01180 AAADictionaryHandle *getDictHandle(char *cmdName);
01181
01182 private:
01183 int protocol;
01184 };
01185
01186 class AAAEmptyClass {};
01187
01192 template <class RAWDATA, class APPDATA, class DICTDATA = AAAEmptyClass>
01193 class DIAMETERPARSER_EXPORT_ONLY AAAParser
01194 {
01195 public:
01199 AAAParser() {}
01200
01204 virtual ~AAAParser() {};
01205
01209 virtual void parseRawToApp();
01210
01214 virtual void parseAppToRaw();
01215
01221 void setRawData(RAWDATA data) { rawData = data; }
01222
01228 void setAppData(APPDATA data) { appData = data; }
01229
01235 void setDictData(DICTDATA data) { dictData = data; }
01236
01240 RAWDATA getRawData() { return rawData; }
01241
01245 template <class T> void getRawData(T*& data) { data = (T*)rawData; }
01246
01250 APPDATA getAppData() { return appData; }
01251
01255 template <class T> void getAppData(T*& data) { data = (T*)appData; }
01256
01260 DICTDATA getDictData() { return dictData; }
01261
01265 template <class T> void getDictData(T*& data) { data = (T*)dictData; }
01266
01267 private:
01268 RAWDATA rawData;
01270 APPDATA appData;
01272 DICTDATA dictData;
01273 };
01274
01276 enum DiameterParserError {
01277 DictionaryError,
01278 HeaderError,
01279 PayloadError
01280 };
01281
01290 class AAAMessageBlock: public ACE_Message_Block
01291 {
01292 public:
01300 static AAAMessageBlock* Acquire(char *buf, ACE_UINT32 s) { return new AAAMessageBlock(buf,s); }
01301
01308 static AAAMessageBlock* Acquire(ACE_UINT32 s) { return new AAAMessageBlock(s); }
01309
01316 static AAAMessageBlock* Acquire(AAAMessageBlock* b) { return new AAAMessageBlock(b); }
01317
01324 void Release() { release(); }
01325
01326 protected:
01334 AAAMessageBlock(char *buf, ACE_UINT32 s) { init(buf, s); }
01335
01342 AAAMessageBlock(ACE_UINT32 s) { init(s); }
01343
01350 AAAMessageBlock(AAAMessageBlock *b) : ACE_Message_Block((const ACE_Message_Block&)*b,0) {}
01351
01357 void* operator new(size_t s)
01358 { return AAAMemoryManager_S::instance()->malloc(s); }
01359
01365 void operator delete(void *p)
01366 { AAAMemoryManager_S::instance()->free(p); }
01367
01368 private:
01372 ~AAAMessageBlock() {}
01373 };
01374
01380 class AAA_MessageBlockScope
01381 {
01382 public:
01383 AAA_MessageBlockScope(ACE_Message_Block *&mb) : mb_(mb) { }
01384 ~AAA_MessageBlockScope(void) { mb_->release(); }
01385
01386 protected:
01387 ACE_Message_Block *&mb_;
01388 };
01389
01393 enum ParseOption {
01394 PARSE_LOOSE = 0,
01395 PARSE_STRICT = 1,
01396 };
01397
01401 class AAADictionaryOption
01402 {
01403 public:
01404 AAADictionaryOption() {}
01405 AAADictionaryOption(ParseOption opt, int id) :
01406 option(opt), protocolId(id) {}
01407
01408 ParseOption option;
01409 int protocolId;
01410 };
01411
01412 class AAADiameterHeader;
01413 class AAAMessage;
01414
01416 typedef AAAParser<AAAMessageBlock*, AAADiameterHeader*, ParseOption>
01417 HeaderParser;
01418
01420 typedef AAAParser<AAAMessageBlock*, AAADiameterHeader*, AAADictionaryOption*>
01421 HeaderParserWithProtocol;
01422
01424 typedef AAAParser<AAAMessageBlock*, AAAAvpContainerList*, AAADictionaryHandle*>
01425 PayloadParser;
01426
01436 void DIAMETERPARSER_EXPORT_ONLY HeaderParser::parseRawToApp();
01437
01448 void DIAMETERPARSER_EXPORT_ONLY HeaderParser::parseAppToRaw();
01449
01462 void DIAMETERPARSER_EXPORT_ONLY PayloadParser::parseRawToApp();
01463
01476 void DIAMETERPARSER_EXPORT_ONLY PayloadParser::parseAppToRaw();
01477
01481 #define HEADER_SIZE 20
01482
01487 class DIAMETERPARSER_EXPORT AAADiameterHeader
01488 {
01489 friend class AAAParser<AAAMessageBlock*, AAADiameterHeader*, ParseOption>;
01490 friend class AAAParser<AAAMessageBlock*, AAADiameterHeader*, AAADictionaryOption*>;
01492 public:
01504 AAADiameterHeader(AAA_UINT8 ver,
01505 ACE_UINT32 length,
01506 struct hdr_flag flags,
01507 AAACommandCode code,
01508 AAAApplicationId appId,
01509 ACE_UINT32 hh,
01510 ACE_UINT32 ee) {
01511 this->ver = ver;
01512 this->length = length;
01513 this->flags = flags;
01514 this->code = code;
01515 this->appId = appId;
01516 this->hh = hh;
01517 this->ee = ee;
01518 }
01519
01523 AAADiameterHeader() {}
01524
01528 inline AAADictionaryHandle *getDictHandle() { return dictHandle; }
01529
01533 const char* getCommandName();
01534
01535 AAA_UINT8 ver;
01537 ACE_UINT32 length:24;
01539 struct hdr_flag flags;
01541 AAACommandCode code:24;
01543 AAAApplicationId appId;
01545 ACE_UINT32 hh;
01547 ACE_UINT32 ee;
01549 private:
01550 AAADictionaryHandle* dictHandle;
01551 };
01552
01561 class DIAMETERPARSER_EXPORT AAAMessage
01562 {
01563 public:
01564 AAADiameterHeader hdr;
01566 AAAAvpContainerList acl;
01568 AAAErrorStatus status;
01570 IP_ADDR originator;
01572 IP_ADDR sender;
01574 time_t secondsTillExpire;
01576 time_t startTime;
01577 };
01578
01579 typedef AAAParser<AAAMessageBlock*, AAAAvpContainerEntry*,
01580 AAADictionaryEntry*> AvpValueParser;
01581
01588 void DIAMETERPARSER_EXPORT_ONLY AvpValueParser::parseRawToApp();
01589
01596 void DIAMETERPARSER_EXPORT_ONLY AvpValueParser::parseAppToRaw();
01597
01602 typedef boost::function1<AAAAvpContainerEntry*, int> AvpContainerEntryFunctor;
01603
01608 typedef boost::function0<AvpValueParser*> AvpValueParserFunctor;
01609
01612 template <class T>
01613 class AvpContainerEntryCreator
01614 {
01615 public:
01621 AAAAvpContainerEntry* operator()(int type) { return new T(type); }
01622 };
01623
01625 template <class T>
01626 class AvpValueParserCreator
01627 {
01628 public:
01632 AvpValueParser* operator()() { return new T(); }
01633 };
01634
01640 class DIAMETERPARSER_EXPORT AvpType {
01641 public:
01642
01652 AvpType(char* name, AAA_AVPDataType type, ACE_UINT32 size,
01653 AvpValueParserFunctor parserCreator,
01654 AvpContainerEntryFunctor containerEntryCreator) :
01655 name(name), type(type), size(size),
01656 parserCreator(parserCreator),
01657 containerEntryCreator(containerEntryCreator) {}
01658
01662 char* getName(void) { return name; };
01663
01667 AAA_AVPDataType getType(void) { return type; };
01668
01672 ACE_UINT32 getMinSize(void) { return size; };
01673
01678 AvpValueParser* createParser() { return parserCreator(); }
01679
01684 AAAAvpContainerEntry* createContainerEntry(int type)
01685 { return containerEntryCreator(type); }
01686
01687 private:
01688
01689 char *name;
01691 AAA_AVPDataType type;
01693 ACE_UINT32 size;
01695 AvpValueParserFunctor parserCreator;
01697 AvpContainerEntryFunctor containerEntryCreator;
01698 };
01699
01719 class DIAMETERPARSER_EXPORT AvpTypeList_S : public std::list<AvpType*>
01720 {
01721 friend class ACE_Singleton<AvpTypeList_S, ACE_Recursive_Thread_Mutex>;
01722 public:
01728 AvpType* search(ACE_UINT32 type)
01729 {
01730 for (iterator i = begin(); i != end(); i++)
01731 {
01732 if ((ACE_UINT32)((*i)->getType()) == type)
01733 return *i;
01734 }
01735 return NULL;
01736 }
01742 AvpType* search(const char* name)
01743 {
01744 for (iterator i = begin(); i != end(); i++)
01745 {
01746 if (ACE_OS::strcmp((*i)->getName(), name) == 0)
01747 return *i;
01748 }
01749 return NULL;
01750 }
01756 void add(AvpType* avpType) {
01757 mutex.acquire();
01758 push_back(avpType);
01759 mutex.release();
01760 }
01761 private:
01765 AvpTypeList_S(void) { registerDefaultTypes(); };
01766
01770 ~AvpTypeList_S(void) {};
01771
01775 void registerDefaultTypes();
01776
01777 ACE_Thread_Mutex mutex;
01778 };
01779
01780 typedef ACE_Singleton<AvpTypeList_S, ACE_Recursive_Thread_Mutex> AvpTypeList;
01781 DIAMETERPARSER_SINGLETON_DECLARE(ACE_Singleton, AvpTypeList_S, ACE_Recursive_Thread_Mutex);
01782
01787 class UTF8Checker
01788 {
01789 public:
01793 UTF8Checker() {}
01794
01798 int operator()(const char *data, unsigned length, bool nullCheck=false)
01799 {
01800 unsigned i = 0;
01801 while (i<length)
01802 {
01803 unsigned char b = data[i++];
01804
01805
01806
01807 if (b == 0x00) { if (nullCheck) return -1; }
01808
01809 else if ((b >> 7) == 0x00) { continue; }
01810
01811
01812 if ((b >> 1) == 0x76) { return -1; }
01813
01814 b <<= 1;
01815
01816 int count=0;
01817 for (count=0; count<5; count++) { if ((b && 0x40) == 0) break; }
01818
01819
01820 if (count==0) { return -1; }
01821
01822
01823 for (int j=0; j<count; j++) {
01824 if (i >= length) { return -1; }
01825 if ((data[i++] >> 6) != 0x02) { return -1; }
01826 }
01827 }
01828 return 0;
01829 }
01830 };
01831
01835 class AAALogMsg : public ACE_Log_Msg
01836 {
01837 friend class ACE_Singleton<AAALogMsg, ACE_Recursive_Thread_Mutex>;
01839 private:
01840
01841
01842
01843 AAALogMsg() { }
01844 ~AAALogMsg() { }
01845 };
01846
01847 typedef ACE_Singleton<AAALogMsg, ACE_Recursive_Thread_Mutex> AAALogMsg_S;
01848 DIAMETERPARSER_SINGLETON_DECLARE(ACE_Singleton, AAALogMsg, ACE_Recursive_Thread_Mutex);
01849
01850 #define AAA_LOG AAALogMsg_S::instance()->log
01851
01853 template <typename T>
01854 class AAA_ScholarAttribute
01855 {
01856 public:
01857 AAA_ScholarAttribute() : isSet(false) {}
01858 AAA_ScholarAttribute(T &val) : value(val), isSet(true) {}
01859 virtual ~AAA_ScholarAttribute() {}
01860 inline void Clear() { isSet = false; }
01861 inline void Set(T val) { value=val; isSet=true; }
01862 virtual void CopyFrom(AAAAvpContainer &c)
01863 {
01864 value=c[0]->dataRef(Type2Type<T>()); isSet=true;
01865 }
01866 void CopyTo(AAAAvpContainer &c, AAA_AVPDataType t)
01867 {
01868 AAAAvpContainerEntryManager em;
01869 AAAAvpContainerEntry *e = em.acquire(t);
01870 e->dataRef(Type2Type<T>()) = value;
01871 c.add(e);
01872 }
01873 inline bool operator==(const T& v) const { return value == v; }
01874 inline T& operator()() { return value; }
01875 inline T& operator=(T v) { isSet = true; value=v; return value; }
01876 inline bool& IsSet() { return isSet; }
01877
01878 protected:
01879 T value;
01880 bool isSet;
01881 };
01882
01884 template <typename T>
01885 class AAA_GroupedScholarAttribute : public AAA_ScholarAttribute<T>
01886 {
01887 public:
01888 void CopyFrom(AAAAvpContainer &c)
01889 {
01890 AAAAvpContainerList& cl =
01891 c[0]->dataRef(Type2Type<AAAAvpContainerList>());
01892 value.CopyFrom(cl);
01893 isSet = true;
01894 }
01895 void CopyTo(AAAAvpContainer &c)
01896 {
01897 AAAAvpContainerEntryManager em;
01898 AAAAvpContainerEntry *e = em.acquire(AAA_AVP_GROUPED_TYPE);
01899 value.CopyTo(e->dataRef(Type2Type<AAAAvpContainerList>()));
01900 c.add(e);
01901 }
01902 inline T& operator=(T v) { isSet = true; value=v; return value; }
01903 };
01904
01905 template <typename T>
01906 class AAA_VectorAttribute : public std::vector<T>
01907 {
01908 public:
01909 AAA_VectorAttribute() : isSet(false) {}
01910 virtual ~AAA_VectorAttribute() {}
01911 inline void Clear() { isSet = false; std::vector<T>::clear(); }
01912 inline std::vector<T>& operator()() { return *this; }
01913 inline std::vector<T>& operator=(std::vector<T>& value)
01914 {
01915 isSet = true;
01916 (std::vector<T>&)(*this)=value;
01917 return *this;
01918 }
01919 virtual void CopyFrom(AAAAvpContainer &c)
01920 {
01921 isSet = true;
01922 if (size() < c.size())
01923 resize(c.size());
01924 for (unsigned i=0; i<c.size(); i++)
01925 (*this)[i] = c[i]->dataRef(Type2Type<T>());
01926 }
01927 void CopyTo(AAAAvpContainer &c, AAA_AVPDataType t)
01928 {
01929 AAAAvpContainerEntryManager em;
01930 AAAAvpContainerEntry *e = em.acquire(t);
01931 for (unsigned i=0; i<size(); i++)
01932 {
01933 e = em.acquire(t);
01934 e->dataRef(Type2Type<T>()) = (*this)[i];
01935 c.add(e);
01936 }
01937 }
01938 inline bool& IsSet() { return isSet; }
01939
01940 protected:
01941 bool isSet;
01942 };
01943
01944 template <typename T>
01945 class AAA_GroupedVectorAttribute : public AAA_VectorAttribute<T>
01946 {
01947 public:
01948 void CopyFrom(AAAAvpContainer &c)
01949 {
01950 isSet = true;
01951 if (size() < c.size())
01952 resize(c.size());
01953 for (unsigned i=0; i<c.size(); i++)
01954 {
01955 AAAAvpContainerList& cl =
01956 c[i]->dataRef(Type2Type<AAAAvpContainerList>());
01957 (*this)[i].CopyFrom(cl);
01958 isSet = true;
01959 }
01960 }
01961 void CopyTo(AAAAvpContainer &c)
01962 {
01963 AAAAvpContainerEntryManager em;
01964 AAAAvpContainerEntry *e;
01965 for (unsigned i=0; i<size(); i++)
01966 {
01967 e = em.acquire(AAA_AVP_GROUPED_TYPE);
01968 (*this)[i].CopyTo(e->dataRef(Type2Type<AAAAvpContainerList>()));
01969 e->dataRef(Type2Type<T>()) = (*this)[i];
01970 c.add(e);
01971 }
01972 }
01973 inline T& operator=(T v) { isSet = true; value=v; return value; }
01974 };
01975
01976 template<class D, AAA_AVPDataType t>
01977 class AAA_AvpWidget {
01978 public:
01979 AAA_AvpWidget(char *name) {
01980 AAAAvpContainerManager cm;
01981 m_cAvp = cm.acquire(name);
01982 }
01983 AAA_AvpWidget(char *name, D &value) {
01984 AAAAvpContainerManager cm;
01985 m_cAvp = cm.acquire(name);
01986 Get() = value;
01987 }
01988 AAA_AvpWidget(AAAAvpContainer *avp) :
01989 m_cAvp(avp) {
01990 }
01991 ~AAA_AvpWidget() {
01992 if (! m_cAvp->size()) {
01993 delete m_cAvp;
01994 }
01995 }
01996 D &Get() {
01997 AAAAvpContainerEntryManager em;
01998 AAAAvpContainerEntry *e = em.acquire(t);
01999 m_cAvp->add(e);
02000 return e->dataRef(Type2Type<D>());
02001 }
02002 AAAAvpContainer *operator()() {
02003 return m_cAvp;
02004 }
02005 bool empty() {
02006 return (m_cAvp->size() == 0);
02007 }
02008 private:
02009 AAAAvpContainer *m_cAvp;
02010 };
02011
02012 typedef AAA_AvpWidget<diameter_identity_t,
02013 AAA_AVP_DIAMID_TYPE> AAA_IdentityAvpWidget;
02014 typedef AAA_AvpWidget<diameter_address_t,
02015 AAA_AVP_ADDRESS_TYPE> AAA_AddressAvpWidget;
02016 typedef AAA_AvpWidget<diameter_integer32_t,
02017 AAA_AVP_INTEGER32_TYPE> AAA_Int32AvpWidget;
02018 typedef AAA_AvpWidget<diameter_unsigned32_t,
02019 AAA_AVP_UINTEGER32_TYPE> AAA_UInt32AvpWidget;
02020 typedef AAA_AvpWidget<diameter_utf8string_t,
02021 AAA_AVP_UTF8_STRING_TYPE> AAA_Utf8AvpWidget;
02022 typedef AAA_AvpWidget<diameter_grouped_t,
02023 AAA_AVP_GROUPED_TYPE> AAA_GroupedAvpWidget;
02024 typedef AAA_AvpWidget<diameter_octetstring_t,
02025 AAA_AVP_STRING_TYPE> AAA_StringAvpWidget;
02026 typedef AAA_AvpWidget<diameter_uri_t,
02027 AAA_AVP_DIAMURI_TYPE> AAA_DiamUriAvpWidget;
02028 typedef AAA_AvpWidget<diameter_enumerated_t,
02029 AAA_AVP_ENUM_TYPE> AAA_EnumAvpWidget;
02030
02031 template<class D, AAA_AVPDataType t>
02032 class AAA_AvpContainerWidget
02033 {
02034 public:
02035 AAA_AvpContainerWidget(AAAAvpContainerList &lst) :
02036 m_List(lst) {
02037 }
02038 D *GetAvp(char *name, unsigned int index=0) {
02039 AAAAvpContainer* c = m_List.search(name);
02040 if (c && (index < c->size())) {
02041 AAAAvpContainerEntry *e = (*c)[index];
02042 return e->dataPtr(Type2Type<D>());
02043 }
02044 return (0);
02045 }
02046 D &AddAvp(char *name) {
02047 AAAAvpContainer* c = m_List.search(name);
02048 if (! c) {
02049 AAA_AvpWidget<D, t> avpWidget(name);
02050 m_List.add(avpWidget());
02051 return avpWidget.Get();
02052 }
02053 else if (c->size() == 0) {
02054 AAA_AvpWidget<D, t> avpWidget(c);
02055 m_List.add(avpWidget());
02056 return avpWidget.Get();
02057 }
02058 else {
02059 return (*c)[0]->dataRef(Type2Type<D>());
02060 }
02061 }
02062 void AddAvp(AAA_AvpContainerWidget<D, t> &avp) {
02063 m_List.add(avp());
02064 }
02065 void DelAvp(char *name) {
02066 std::list<AAAAvpContainer*>::iterator i;
02067 for (i=m_List.begin(); i!=m_List.end();i++) {
02068 AAAAvpContainer *c = *i;
02069 if (ACE_OS::strcmp(c->getAvpName(), name) == 0) {
02070 m_List.erase(i);
02071 AAAAvpContainerManager cm;
02072 cm.release(c);
02073 }
02074 }
02075 }
02076
02077 private:
02078 AAAAvpContainerList &m_List;
02079 };
02080
02081 typedef AAA_AvpContainerWidget<diameter_identity_t, AAA_AVP_DIAMID_TYPE>
02082 AAA_IdentityAvpContainerWidget;
02083 typedef AAA_AvpContainerWidget<diameter_address_t, AAA_AVP_ADDRESS_TYPE>
02084 AAA_AddressAvpContainerWidget;
02085 typedef AAA_AvpContainerWidget<diameter_integer32_t, AAA_AVP_INTEGER32_TYPE>
02086 AAA_Int32AvpContainerWidget;
02087 typedef AAA_AvpContainerWidget<diameter_unsigned32_t, AAA_AVP_UINTEGER32_TYPE>
02088 AAA_UInt32AvpContainerWidget;
02089 typedef AAA_AvpContainerWidget<diameter_utf8string_t, AAA_AVP_UTF8_STRING_TYPE>
02090 AAA_Utf8AvpContainerWidget;
02091 typedef AAA_AvpContainerWidget<diameter_grouped_t, AAA_AVP_GROUPED_TYPE>
02092 AAA_GroupedAvpContainerWidget;
02093 typedef AAA_AvpContainerWidget<diameter_octetstring_t, AAA_AVP_STRING_TYPE>
02094 AAA_StringAvpContainerWidget;
02095 typedef AAA_AvpContainerWidget<diameter_uri_t, AAA_AVP_DIAMURI_TYPE>
02096 AAA_DiamUriAvpContainerWidget;
02097 typedef AAA_AvpContainerWidget<diameter_enumerated_t, AAA_AVP_ENUM_TYPE>
02098 AAA_EnumAvpContainerWidget;
02099
02100 class AAA_MsgResultCode
02101 {
02102 public:
02103 typedef enum {
02104 RCODE_NON_PRESENT,
02105 RCODE_INFORMATIONAL,
02106 RCODE_SUCCESS,
02107 RCODE_PROTOCOL_ERROR,
02108 RCODE_TRANSIENT_FAILURE,
02109 RCODE_PERMANENT_FAILURE
02110 } RCODE;
02111
02112 public:
02113 AAA_MsgResultCode(AAAMessage &msg) :
02114 m_Msg(msg) {
02115 }
02116 diameter_unsigned32_t ResultCode() {
02117 AAA_UInt32AvpContainerWidget resultCode(m_Msg.acl);
02118 diameter_unsigned32_t *rc = resultCode.GetAvp("Result-Code");
02119 return (rc) ? *rc : 0;
02120 }
02121 void ResultCode(diameter_unsigned32_t c) {
02122 AAA_UInt32AvpContainerWidget resultCode(m_Msg.acl);
02123 resultCode.AddAvp("Result-Code") = c;
02124 }
02125 RCODE InterpretedResultCode() {
02126 diameter_unsigned32_t code = ResultCode();
02127 if (code) {
02128 for (int i=RCODE_INFORMATIONAL;
02129 i<=RCODE_PERMANENT_FAILURE;
02130 i++) {
02131 code -= 1000;
02132 if ((code/1000) == 0) {
02133 return RCODE(i);
02134 }
02135 }
02136 return (RCODE_PERMANENT_FAILURE);
02137 }
02138 return (RCODE_NON_PRESENT);
02139 }
02140
02141 private:
02142 AAAMessage &m_Msg;
02143 };
02144
02145 #endif
02146
02147
02148