00001
00057 #include "diameter_api.h"
00058 #include "peer.h"
00059 #include "peermanager.h"
00060 #include "receiver.h"
00061 #include "transaction.h"
00062 #include "api_process.h"
00063 #include "routing.h"
00064
00065
00066
00073 AAATransaction *AAACreateTransaction(AAAApplicationId app_id,AAACommandCode cmd_code)
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 }
00083
00089 int AAADropTransaction(AAATransaction *trans)
00090 {
00091 if (!trans) return 0;
00092
00093 shm_free(trans);
00094 return 1;
00095 }
00096
00097
00098
00099
00100 extern handler_list *handlers;
00101 extern gen_lock_t *handlers_lock;
00109 int AAAAddRequestHandler(AAARequestHandler_f *f,void *param)
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 }
00129
00136 int AAAAddResponseHandler(AAAResponseHandler_f *f,void *param)
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 }
00156
00157
00158
00159
00170 AAAReturnCode AAASendMessage(
00171 AAAMessage *message,
00172 AAATransactionCallback_f *callback_f,
00173 void *callback_param)
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
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 }
00201
00212 AAAReturnCode AAASendMessageToPeer(
00213 AAAMessage *message,
00214 str *peer_id,
00215 AAATransactionCallback_f *callback_f,
00216 void *callback_param)
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
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 }
00244
00245
00257 void sendrecv_cb(int is_timeout,void *param,AAAMessage *ans)
00258 {
00259 lock_release((gen_lock_t*)param);
00260 }
00261
00271 AAAMessage* AAASendRecvMessage(AAAMessage *message)
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
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 }
00320
00330 AAAMessage* AAASendRecvMessageToPeer(AAAMessage *message, str *peer_id)
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
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 }
00379
00380
00381