diam_message.h

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2002-2003 FhG Fokus
00005  *
00006  * This file is part of disc, a free diameter server/client.
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * This program 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
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * ---------
00024  *   2003-04-07 created by bogdan
00025  */
00026 
00034 #ifdef DIAM_ACC
00035 
00036 #ifndef _AAA_DIAMETER_MSG_H
00037 #define _AAA_DIAMETER_MSG_H
00038 
00039 #include "../../str.h"
00040 #include "../../mem/mem.h"
00041 
00042 #define ad_malloc       pkg_malloc
00043 #define ad_free         pkg_free
00044 
00045 /*********************************** AAA TYPES *******************************/
00046 
00047 #define AAA_NO_VENDOR_ID           0
00048 
00049 #define VER_SIZE                   1
00050 #define MESSAGE_LENGTH_SIZE        3
00051 #define FLAGS_SIZE                 1
00052 #define COMMAND_CODE_SIZE          3
00053 #define APPLICATION_ID_SIZE        4
00054 #define HOP_BY_HOP_IDENTIFIER_SIZE 4
00055 #define END_TO_END_IDENTIFIER_SIZE 4
00056 #define AVP_CODE_SIZE      4
00057 #define AVP_FLAGS_SIZE     1
00058 #define AVP_LENGTH_SIZE    3
00059 #define AVP_VENDOR_ID_SIZE 4
00060 
00061 #define AAA_MSG_HDR_SIZE  \
00062         (VER_SIZE + MESSAGE_LENGTH_SIZE + FLAGS_SIZE + COMMAND_CODE_SIZE +\
00063         APPLICATION_ID_SIZE+HOP_BY_HOP_IDENTIFIER_SIZE+END_TO_END_IDENTIFIER_SIZE)
00064 
00065 #define AVP_HDR_SIZE(_flags_)  \
00066         (AVP_CODE_SIZE+AVP_FLAGS_SIZE+AVP_LENGTH_SIZE+\
00067         AVP_VENDOR_ID_SIZE*(((_flags_)&AAA_AVP_FLAG_VENDOR_SPECIFIC)!=0) )
00068 
00069 /* message codes
00070  */
00071 #ifndef WORDS_BIGENDIAN
00072         #define AS_MSG_CODE      0x12010000
00073         #define AC_MSG_CODE      0x0f010000
00074         #define CE_MSG_CODE      0x01010000
00075         #define DW_MSG_CODE      0x18010000
00076         #define DP_MSG_CODE      0x1a010000
00077         #define RA_MSG_CODE      0x02010000
00078         #define ST_MSG_CODE      0x13010000
00079         #define MASK_MSG_CODE    0xffffff00
00080 #else
00081         #error BIG endian detected!!
00082         #define AS_MSG_CODE      0x00000112
00083         #define AC_MSG_CODE      0x0000010f
00084         #define CE_MSG_CODE      0x00000101
00085         #define DW_MSG_CODE      0x00000118
00086         #define DP_MSG_CODE      0x0000011a
00087         #define RA_MSG_CODE      0x00000102
00088         #define ST_MSG_CODE      0x00000113
00089         #define MASK_MSG_CODE    0x00ffffff
00090 #endif
00091 
00092 typedef unsigned int    AAACommandCode;
00093 typedef unsigned int    AAAVendorId;
00094 typedef unsigned int    AAAExtensionId;
00095 typedef unsigned int    AAA_AVPCode;
00096 typedef unsigned int    AAAValue;
00097 typedef unsigned int    AAAApplicationId;
00098 typedef void*           AAAApplicationRef;
00099 typedef str             AAASessionId;
00100 typedef unsigned int    AAAMsgIdentifier;
00101 typedef unsigned char   AAAMsgFlag;
00102 
00103 
00104 /* Status codes returned by functions in the AAA API */
00105 typedef enum {
00106         AAA_ERR_NOT_FOUND = -2,         /* handle or id not found */
00107         AAA_ERR_FAILURE   = -1,         /* unspecified failure during an AAA op. */
00108         AAA_ERR_SUCCESS   =  0,         /* AAA operation succeeded */
00109         AAA_ERR_NOMEM,                  /* op. caused memory to be exhausted */
00110         AAA_ERR_PROTO,                  /*  AAA protocol error */
00111         AAA_ERR_SECURITY,
00112         AAA_ERR_PARAMETER,
00113         AAA_ERR_CONFIG,
00114         AAA_ERR_UNKNOWN_CMD,
00115         AAA_ERR_MISSING_AVP,
00116         AAA_ERR_ALREADY_INIT,
00117         AAA_ERR_TIMED_OUT,
00118         AAA_ERR_CANNOT_SEND_MSG,
00119         AAA_ERR_ALREADY_REGISTERED,
00120         AAA_ERR_CANNOT_REGISTER,
00121         AAA_ERR_NOT_INITIALIZED,
00122         AAA_ERR_NETWORK_ERROR,
00123 } AAAReturnCode;
00124 
00125 
00126 /* The following are AVP data type codes. They correspond directly to
00127  * the AVP data types outline in the Diameter specification [1]: */
00128 typedef enum {
00129         AAA_AVP_DATA_TYPE,
00130         AAA_AVP_STRING_TYPE,
00131         AAA_AVP_ADDRESS_TYPE,
00132         AAA_AVP_INTEGER32_TYPE,
00133         AAA_AVP_INTEGER64_TYPE,
00134         AAA_AVP_TIME_TYPE,
00135 } AAA_AVPDataType;
00136 
00137 
00138 /* The following are used for AVP header flags and for flags in the AVP
00139  *  wrapper struct and AVP dictionary definitions. */
00140 typedef enum {
00141         AAA_AVP_FLAG_NONE               = 0x00,
00142         AAA_AVP_FLAG_MANDATORY          = 0x40,
00143         AAA_AVP_FLAG_RESERVED           = 0x1F,
00144         AAA_AVP_FLAG_VENDOR_SPECIFIC    = 0x80,
00145         AAA_AVP_FLAG_END_TO_END_ENCRYPT = 0x20,
00146 } AAA_AVPFlag;
00147 
00148 
00149 /* List with all known application identifiers */
00150 typedef enum {
00151         AAA_APP_DIAMETER_COMMON_MSG  = 0,
00152         AAA_APP_NASREQ               = 1,
00153         AAA_APP_MOBILE_IP            = 2,
00154         AAA_APP_DIAMETER_BASE_ACC    = 3,
00155         AAA_APP_RELAY                = 0xffffffff,
00156 }AAA_APP_IDS;
00157 
00158 
00159 /* The following are the result codes returned from remote servers as
00160  * part of messages */
00161 typedef enum {
00162         AAA_MUTI_ROUND_AUTH           = 1001,
00163         AAA_SUCCESS                   = 2001,
00164         AAA_COMMAND_UNSUPPORTED       = 3001,
00165         AAA_UNABLE_TO_DELIVER         = 3002,
00166         AAA_REALM_NOT_SERVED          = 3003,
00167         AAA_TOO_BUSY                  = 3004,
00168         AAA_LOOP_DETECTED             = 3005,
00169         AAA_REDIRECT_INDICATION       = 3006,
00170         AAA_APPLICATION_UNSUPPORTED   = 3007,
00171         AAA_INVALID_HDR_BITS          = 3008,
00172         AAA_INVALID_AVP_BITS          = 3009,
00173         AAA_UNKNOWN_PEER              = 3010,
00174         AAA_AUTHENTICATION_REJECTED   = 4001,
00175         AAA_OUT_OF_SPACE              = 4002,
00176         AAA_ELECTION_LOST             = 4003,
00177         AAA_AVP_UNSUPPORTED           = 5001,
00178         AAA_UNKNOWN_SESSION_ID        = 5002,
00179         AAA_AUTHORIZATION_REJECTED    = 5003,
00180         AAA_INVALID_AVP_VALUE         = 5004,
00181         AAA_MISSING_AVP               = 5005,
00182         AAA_RESOURCES_EXCEEDED        = 5006,
00183         AAA_CONTRADICTING_AVPS        = 5007,
00184         AAA_AVP_NOT_ALLOWED           = 5008,
00185         AAA_AVP_OCCURS_TOO_MANY_TIMES = 5009,
00186         AAA_NO_COMMON_APPLICATION     = 5010,
00187         AAA_UNSUPPORTED_VERSION       = 5011,
00188         AAA_UNABLE_TO_COMPLY          = 5012,
00189         AAA_INVALID_BIT_IN_HEADER     = 5013,
00190         AAA_INVALIS_AVP_LENGTH        = 5014,
00191         AAA_INVALID_MESSGE_LENGTH     = 5015,
00192         AAA_INVALID_AVP_BIT_COMBO     = 5016,
00193         AAA_NO_COMMON_SECURITY        = 5017,
00194 } AAAResultCode;
00195 
00196 
00197 typedef enum {
00198         AVP_User_Name                     =    1,
00199         AVP_Class                         =   25,
00200         AVP_Session_Timeout               =   27,
00201         AVP_Proxy_State                   =   33,
00202         AVP_Host_IP_Address               =  257,
00203         AVP_Auth_Application_Id            =  258,
00204         AVP_Vendor_Specific_Application_Id=  260,
00205         AVP_Redirect_Max_Cache_Time       =  262,
00206         AVP_Session_Id                    =  263,
00207         AVP_Origin_Host                   =  264,
00208         AVP_Supported_Vendor_Id           =  265,
00209         AVP_Vendor_Id                     =  266,
00210         AVP_Result_Code                   =  268,
00211         AVP_Product_Name                  =  269,
00212         AVP_Session_Binding               =  270,
00213         AVP_Disconnect_Cause              =  273,
00214         AVP_Auth_Request_Type             =  274,
00215         AVP_Auth_Grace_Period             =  276,
00216         AVP_Auth_Session_State            =  277,
00217         AVP_Origin_State_Id               =  278,
00218         AVP_Accounting_Record_Type                =  279,
00219         AVP_Proxy_Host                    =  280,
00220         AVP_Error_Message                 =  281,
00221         AVP_Record_Route                  =  282,
00222         AVP_Destination_Realm             =  283,
00223         AVP_Proxy_Info                    =  284,
00224         AVP_Re_Auth_Request_Type          =  285,
00225         AVP_Authorization_Lifetime        =  291,
00226         AVP_Redirect_Host                 =  292,
00227         AVP_Destination_Host              =  293,
00228         AVP_Termination_Cause             =  295,
00229         AVP_Origin_Realm                  =  296,
00230         AVP_Resource                                      =  400,
00231         AVP_Response                                      =  401,       
00232         AVP_Challenge                                     =  402,
00233         AVP_Method                                                =  403,
00234         AVP_Service_Type                                  =  404,
00235         AVP_User_Group                                    =  405,
00236         AVP_SIP_MSGID                                     =  406        
00237 
00238 }AAA_AVPCodeNr;
00239 
00240 
00241 /*   The following type allows the client to specify which direction to
00242  *   search for an AVP in the AVP list: */
00243 typedef enum {
00244         AAA_FORWARD_SEARCH = 0,
00245         AAA_BACKWARD_SEARCH
00246 } AAASearchType;
00247 
00248 
00249 
00250 typedef enum {
00251         AAA_ACCT_EVENT = 1,
00252         AAA_ACCT_START = 2,
00253         AAA_ACCT_INTERIM = 3,
00254         AAA_ACCT_STOP = 4
00255 } AAAAcctMessageType;
00256 
00257 
00258 typedef enum {
00259         AVP_DUPLICATE_DATA,
00260         AVP_DONT_FREE_DATA,
00261         AVP_FREE_DATA,
00262 } AVPDataStatus;
00263 
00264 /* The following structure contains a message AVP in parsed format */
00265 typedef struct avp {
00266         struct avp *next;
00267         struct avp *prev;
00268         enum {
00269                 AAA_RADIUS,
00270                 AAA_DIAMETER
00271         } packetType;
00272         AAA_AVPCode code;
00273         AAA_AVPFlag flags;
00274         AAA_AVPDataType type;
00275         AAAVendorId vendorId;
00276         str data;
00277         unsigned char free_it;
00278 } AAA_AVP;
00279 
00280 
00281 /* The following structure is used for representing lists of AVPs on the
00282  * message: */
00283 typedef struct _avp_list_t {
00284         AAA_AVP *head;
00285         AAA_AVP *tail;
00286 } AAA_AVP_LIST;
00287 
00288 
00289 /* The following structure contains the full AAA message: */
00290 typedef struct _message_t {
00291         AAAMsgFlag          flags;
00292         AAACommandCode      commandCode;
00293         AAAApplicationId    applicationId;
00294         AAAMsgIdentifier    endtoendId;
00295         AAAMsgIdentifier    hopbyhopId;
00296         AAASessionId        *sId;
00297         AAA_AVP             *sessionId;
00298         AAA_AVP             *orig_host;
00299         AAA_AVP             *orig_realm;
00300         AAA_AVP             *dest_host;
00301         AAA_AVP             *dest_realm;
00302         AAA_AVP             *res_code;
00303         AAA_AVP             *auth_ses_state;
00304         AAA_AVP_LIST        avpList;
00305         str                 buf;
00306         void                *in_peer;
00307 } AAAMessage;
00308 
00309 
00310 
00311 
00312 /**************************** AAA MESSAGE FUNCTIONS **************************/
00313 
00314 /* MESSAGES
00315  */
00316 
00317 #define is_req(_msg_) \
00318         (((_msg_)->flags)&0x80)
00319 
00320 AAAMessage* AAAInMessage(AAACommandCode commandCode, 
00321                                                         AAAApplicationId  applicationId);
00322 
00323 AAAReturnCode AAAFreeMessage(
00324                 AAAMessage **message);
00325 
00326 AAAResultCode AAASetMessageResultCode(
00327                 AAAMessage *message,
00328                 AAAResultCode resultCode);
00329 
00330 void AAAPrintMessage(
00331                 AAAMessage *msg);
00332 
00333 AAAReturnCode AAABuildMsgBuffer(
00334                 AAAMessage *msg );
00335 
00336 AAAMessage* AAATranslateMessage(
00337                 unsigned char* source,
00338                 unsigned int sourceLen,
00339                 int attach_buf );
00340 
00341 
00342 /* AVPS
00343  */
00344 
00345 #define AAACreateAndAddAVPToMessage(_msg_,_code_,_flags_,_vdr_,_data_,_len_) \
00346         ( AAAAddAVPToMessage(_msg_, \
00347         AAACreateAVP(_code_,_flags_,_vdr_,_data_,_len_, AVP_DUPLICATE_DATA),\
00348         (_msg_)->avpList.tail) )
00349 
00350 AAA_AVP* AAACreateAVP(
00351                 AAA_AVPCode code,
00352                 AAA_AVPFlag flags,
00353                 AAAVendorId vendorId,
00354                 char *data,
00355                 size_t length,
00356                 AVPDataStatus data_status);
00357 
00358 AAA_AVP* AAACloneAVP(
00359                 AAA_AVP *avp,
00360                 unsigned char duplicate_data );
00361 
00362 AAAReturnCode AAAAddAVPToMessage(
00363                 AAAMessage *msg,
00364                 AAA_AVP *avp,
00365                 AAA_AVP *position);
00366 
00367 AAA_AVP *AAAFindMatchingAVP(
00368                 AAAMessage *msg,
00369                 AAA_AVP *startAvp,
00370                 AAA_AVPCode avpCode,
00371                 AAAVendorId vendorId,
00372                 AAASearchType searchType);
00373 
00374 AAAReturnCode AAARemoveAVPFromMessage(
00375                 AAAMessage *msg,
00376                 AAA_AVP *avp);
00377 
00378 AAAReturnCode AAAFreeAVP(
00379                 AAA_AVP **avp);
00380 
00381 AAA_AVP* AAAGetFirstAVP(
00382                 AAA_AVP_LIST *avpList);
00383 
00384 AAA_AVP* AAAGetLastAVP(
00385                 AAA_AVP_LIST *avpList);
00386 
00387 AAA_AVP* AAAGetNextAVP(
00388                 AAA_AVP *avp);
00389 
00390 AAA_AVP* AAAGetPrevAVP(
00391                 AAA_AVP *avp);
00392 
00393 char *AAAConvertAVPToString(
00394                 AAA_AVP *avp,
00395                 char *dest,
00396                 unsigned int destLen);
00397 
00398 
00399 #endif
00400 
00401 
00402 #endif