diameter_api.c File Reference


Detailed Description

CDiameterPeer - Diameter API interface.

Author:
Dragos Vingarzan vingarzan -at- fokus dot fraunhofer dot de

Definition in file diameter_api.c.

#include "diameter_api.h"
#include "peer.h"
#include "peermanager.h"
#include "receiver.h"
#include "transaction.h"
#include "api_process.h"
#include "routing.h"

Go to the source code of this file.

Functions

AAATransactionAAACreateTransaction (AAAApplicationId app_id, AAACommandCode cmd_code)
 Create a AAATransaction for the given request.
int AAADropTransaction (AAATransaction *trans)
 Free the memory allocated for the AAATransaction.
int AAAAddRequestHandler (AAARequestHandler_f *f, void *param)
 Add a handler function for incoming requests.
int AAAAddResponseHandler (AAAResponseHandler_f *f, void *param)
 Add a handler function for incoming responses.
AAAReturnCode AAASendMessage (AAAMessage *message, AAATransactionCallback_f *callback_f, void *callback_param)
 Send a AAAMessage asynchronously.
AAAReturnCode AAASendMessageToPeer (AAAMessage *message, str *peer_id, AAATransactionCallback_f *callback_f, void *callback_param)
 Send a AAAMessage asynchronously.
void sendrecv_cb (int is_timeout, void *param, AAAMessage *ans)
 Generic callback used by AAASendRecvMessage() to block until a transactional response is received.
AAAMessageAAASendRecvMessage (AAAMessage *message)
 Send a AAAMessage synchronously.
AAAMessageAAASendRecvMessageToPeer (AAAMessage *message, str *peer_id)
 Send a AAAMessage synchronously.

Variables

handler_listhandlers
 list of handlers
gen_lock_t * handlers_lock
 lock for list of handlers


Function Documentation

AAATransaction* AAACreateTransaction ( AAAApplicationId  app_id,
AAACommandCode  cmd_code 
)

Create a AAATransaction for the given request.

Parameters:
app_id - id of the request's application
cmd_code - request's code
Returns:
the AAATransaction*

Definition at line 73 of file diameter_api.c.

References _AAATransaction::application_id, and _AAATransaction::command_code.

Referenced by load_cdp().

00074 {
00075     AAATransaction *t;
00076     t = shm_malloc(sizeof(AAATransaction));
00077     if (!t) return 0;
00078     memset(t,0,sizeof(AAATransaction)); 
00079     t->application_id=app_id;
00080     t->command_code=cmd_code;           
00081     return t;
00082 }

int AAADropTransaction ( AAATransaction trans  ) 

Free the memory allocated for the AAATransaction.

Parameters:
trans - the AAATransaction to be deallocated
Returns:
1 on success, 0 on failure

Definition at line 89 of file diameter_api.c.

Referenced by load_cdp().

00090 {
00091     if (!trans) return 0;
00092 //  LOG(L_ERR,"\nCALLED HERE %d %d\n",trans->done,trans->with_callback);
00093     shm_free(trans);
00094     return 1;
00095 }

int AAAAddRequestHandler ( AAARequestHandler_f f,
void *  param 
)

Add a handler function for incoming requests.

Parameters:
f - the callback function
param - generic parameter to be used when calling the callback functions
Returns:
1 on success, 0 on failure

Definition at line 109 of file diameter_api.c.

References handler_t::handler, handlers, handlers_lock, handler_list_t::head, handler_t::next, handler_t::param, handler_t::prev, REQUEST_HANDLER, handler_t::requestHandler, handler_list_t::tail, and handler_t::type.

Referenced by load_cdp().

00110 {
00111     handler *h = shm_malloc(sizeof(handler));
00112     if (!h) {
00113         LOG(L_ERR,"ERR:AAAAddRequestHandler: error allocating %d bytes in shm\n",
00114             sizeof(handler));
00115         return 0;
00116     }
00117     h->type = REQUEST_HANDLER;
00118     h->handler.requestHandler = f;
00119     h->param = param;
00120     h->next = 0;
00121     lock_get(handlers_lock);
00122     h->prev = handlers->tail;
00123     if (handlers->tail) handlers->tail->next = h;
00124     handlers->tail = h;
00125     if (!handlers->head) handlers->head = h;
00126     lock_release(handlers_lock);
00127     return 1;
00128 }

int AAAAddResponseHandler ( AAAResponseHandler_f f,
void *  param 
)

Add a handler function for incoming responses.

Parameters:
f - the callback function
param - generic parameter to be used when calling the callback functions
Returns:
1 on success, 0 on failure

Definition at line 136 of file diameter_api.c.

References handler_t::handler, handlers, handlers_lock, handler_list_t::head, handler_t::next, handler_t::param, handler_t::prev, RESPONSE_HANDLER, handler_t::responseHandler, handler_list_t::tail, and handler_t::type.

Referenced by load_cdp().

00137 {
00138     handler *h = shm_malloc(sizeof(handler));
00139     if (!h) {
00140         LOG(L_ERR,"ERR:AAAAddResponseHandler: error allocating %d bytes in shm\n",
00141             sizeof(handler));
00142         return 0;
00143     }
00144     h->type = RESPONSE_HANDLER;
00145     h->handler.responseHandler = f;
00146     h->param = param;
00147     h->next = 0;
00148     lock_get(handlers_lock);
00149     h->prev = handlers->tail;
00150     if (handlers->tail) handlers->tail->next = h;
00151     handlers->tail = h;
00152     if (!handlers->head) handlers->head = h;
00153     lock_release(handlers_lock);
00154     return 1;
00155 }

AAAReturnCode AAASendMessage ( AAAMessage message,
AAATransactionCallback_f callback_f,
void *  callback_param 
)

Send a AAAMessage asynchronously.

When the response is received, the callback_f(callback_param,...) is called.

Parameters:
message - the request to be sent
peer_id - FQDN of the peer to send
callback_f - callback to be called on transactional response or transaction timeout
callback_param - generic parameter to call the transactional callback function with
Returns:
1 on success, 0 on failure
Todo:
remove peer_id and add Realm routing

Definition at line 170 of file diameter_api.c.

References AAAFreeMessage(), add_trans(), DP_TRANS_TIMEOUT, _peer_t::fqdn, get_routing_peer(), I_Open, is_req, peer_send_msg(), R_Open, and _peer_t::state.

Referenced by load_cdp().

00174 {
00175     peer *p;
00176     p = get_routing_peer(message);
00177     if (!p) {
00178         LOG(L_ERR,"ERROR:AAASendMessage(): Can't find a suitable connected peer in the routing table.\n");
00179         goto error;
00180     }
00181     if (p->state!=I_Open && p->state!=R_Open){
00182         LOG(L_ERR,"ERROR:AAASendMessage(): Peer not connected to %.*s\n",p->fqdn.len,p->fqdn.s);
00183         goto error;
00184     }
00185     /* only add transaction following when required */
00186     if (callback_f){
00187         if (is_req(message))
00188             add_trans(message,callback_f,callback_param,DP_TRANS_TIMEOUT,1);
00189         else
00190             LOG(L_ERR,"ERROR:AAASendMessage(): can't add transaction callback for answer.\n");
00191     }
00192         
00193     if (!peer_send_msg(p,message))
00194         goto error;
00195         
00196     return 1;
00197 error:  
00198     AAAFreeMessage(&message);
00199     return 0;
00200 }

AAAReturnCode AAASendMessageToPeer ( AAAMessage message,
str *  peer_id,
AAATransactionCallback_f callback_f,
void *  callback_param 
)

Send a AAAMessage asynchronously.

When the response is received, the callback_f(callback_param,...) is called.

Parameters:
message - the request to be sent
peer_id - FQDN of the peer to send
callback_f - callback to be called on transactional response or transaction timeout
callback_param - generic parameter to call the transactional callback function with
Returns:
1 on success, 0 on failure
Todo:
remove peer_id and add Realm routing

Definition at line 212 of file diameter_api.c.

References AAAFreeMessage(), add_trans(), DP_TRANS_TIMEOUT, get_peer_by_fqdn(), I_Open, is_req, peer_send_msg(), R_Open, and _peer_t::state.

Referenced by load_cdp().

00217 {
00218     peer *p;
00219     p = get_peer_by_fqdn(peer_id);
00220     if (!p) {
00221         LOG(L_ERR,"ERROR:AAASendMessageToPeer(): Peer unknown %.*s\n",peer_id->len,peer_id->s);
00222         goto error;
00223     }
00224     if (p->state!=I_Open && p->state!=R_Open){
00225         LOG(L_ERR,"ERROR:AAASendMessageToPeer(): Peer not connected to %.*s\n",peer_id->len,peer_id->s);
00226         goto error;
00227     }
00228     /* only add transaction following when required */
00229     if (callback_f){
00230         if (is_req(message))
00231             add_trans(message,callback_f,callback_param,DP_TRANS_TIMEOUT,1);
00232         else
00233             LOG(L_ERR,"ERROR:AAASendMessageToPeer(): can't add transaction callback for answer.\n");
00234     }
00235         
00236     if (!peer_send_msg(p,message))
00237         goto error;
00238         
00239     return 1;
00240 error:  
00241     AAAFreeMessage(&message);
00242     return 0;
00243 }

void sendrecv_cb ( int  is_timeout,
void *  param,
AAAMessage ans 
)

Generic callback used by AAASendRecvMessage() to block until a transactional response is received.

The AAASendRecvMessage() is basically a AAASendMessage() that has a callback (this function) that blocks until a transactional response or timeout is received and then it returns that.

Parameters:
is_timeout - if this is a time-out or response event
param - generic parameter to call the transactional callback function with
ans - the answer for the callback

Definition at line 257 of file diameter_api.c.

Referenced by AAASendRecvMessage(), and AAASendRecvMessageToPeer().

00258 {
00259     lock_release((gen_lock_t*)param);
00260 }

AAAMessage* AAASendRecvMessage ( AAAMessage message  ) 

Send a AAAMessage synchronously.

This blocks until a response is received or a transactional time-out happens.

Parameters:
message - the request to be sent
peer_id - FQDN of the peer to send
Returns:
1 on success, 0 on failure
Todo:
remove peer_id and add Realm routing
Todo:
replace the busy-waiting lock in here with one that does not consume CPU

Definition at line 271 of file diameter_api.c.

References AAAFreeMessage(), add_trans(), _cdp_trans_t::ans, DP_TRANS_TIMEOUT, _peer_t::fqdn, free_trans(), get_routing_peer(), I_Open, is_req, peer_send_msg(), R_Open, sendrecv_cb(), and _peer_t::state.

Referenced by load_cdp().

00272 {
00273     peer *p;
00274     gen_lock_t *lock;
00275     cdp_trans_t *t;
00276     AAAMessage *ans;
00277     
00278     p = get_routing_peer(message);
00279     if (!p) {
00280         LOG(L_ERR,"ERROR:AAASendRecvMessage(): Can't find a suitable connected peer in the routing table.\n");
00281         goto error;
00282     }
00283     if (p->state!=I_Open && p->state!=R_Open){
00284         LOG(L_ERR,"ERROR:AAASendRecvMessage(): Peer not connected to %.*s\n",p->fqdn.len,p->fqdn.s);
00285         goto error;
00286     }
00287     
00288     
00289     if (is_req(message)){
00290         lock = lock_alloc();
00291         lock = lock_init(lock);
00292         lock_get(lock);
00293         t = add_trans(message,sendrecv_cb,(void*)lock,DP_TRANS_TIMEOUT,0);
00294 
00295         if (!peer_send_msg(p,message)) {
00296             lock_destroy(lock);
00297             lock_dealloc((void*)lock);  
00298             goto error;
00299         }
00300 
00301         /* block until callback is executed */
00302         lock_get(lock);     
00303         lock_destroy(lock);
00304         lock_dealloc((void*)lock);
00305         ans = t->ans;
00306         free_trans(t);
00307         return ans;
00308     }
00309     else
00310     {
00311         LOG(L_ERR,"ERROR:AAASendRecvMessage(): can't add wait for answer to answer.\n");
00312         goto error;
00313     }
00314 
00315         
00316 error:  
00317     AAAFreeMessage(&message);
00318     return 0;
00319 }

AAAMessage* AAASendRecvMessageToPeer ( AAAMessage message,
str *  peer_id 
)

Send a AAAMessage synchronously.

This blocks until a response is received or a transactional time-out happens.

Parameters:
message - the request to be sent
peer_id - FQDN of the peer to send
Returns:
1 on success, 0 on failure
Todo:
remove peer_id and add Realm routing
Todo:
replace the busy-waiting lock in here with one that does not consume CPU

Definition at line 330 of file diameter_api.c.

References AAAFreeMessage(), add_trans(), _cdp_trans_t::ans, DP_TRANS_TIMEOUT, free_trans(), get_peer_by_fqdn(), I_Open, is_req, peer_send_msg(), R_Open, sendrecv_cb(), and _peer_t::state.

Referenced by load_cdp().

00331 {
00332     peer *p;
00333     gen_lock_t *lock;
00334     cdp_trans_t *t;
00335     AAAMessage *ans;
00336     
00337     p = get_peer_by_fqdn(peer_id);
00338     if (!p) {
00339         LOG(L_ERR,"ERROR:AAASendRecvMessageToPeer(): Peer unknown %.*s\n",peer_id->len,peer_id->s);
00340         goto error;
00341     }
00342     if (p->state!=I_Open && p->state!=R_Open){
00343         LOG(L_ERR,"ERROR:AAASendRecvMessageToPeer(): Peer not connected to %.*s\n",peer_id->len,peer_id->s);
00344         goto error;
00345     }
00346     
00347     
00348     if (is_req(message)){
00349         lock = lock_alloc();
00350         lock = lock_init(lock);
00351         lock_get(lock);
00352         t = add_trans(message,sendrecv_cb,(void*)lock,DP_TRANS_TIMEOUT,0);
00353 
00354         if (!peer_send_msg(p,message)) {
00355             lock_destroy(lock);
00356             lock_dealloc((void*)lock);  
00357             goto error;
00358         }
00359 
00360         /* block until callback is executed */
00361         lock_get(lock);     
00362         lock_destroy(lock);
00363         lock_dealloc((void*)lock);
00364         ans = t->ans;
00365         free_trans(t);
00366         return ans;
00367     }
00368     else
00369     {
00370         LOG(L_ERR,"ERROR:AAASendRecvMessageToPeer(): can't add wait for answer to answer.\n");
00371         goto error;
00372     }
00373 
00374         
00375 error:  
00376     AAAFreeMessage(&message);
00377     return 0;
00378 }


Variable Documentation

handler_list* handlers

list of handlers

Definition at line 60 of file api_process.c.

gen_lock_t* handlers_lock

lock for list of handlers

Definition at line 61 of file api_process.c.


Generated on Tue Jul 29 04:19:13 2008 for Open IMS Core CSCFs by  doxygen 1.5.2