/* frees a message allocated through AAANewMessage() */ AAAReturnCode AAAFreeMessage(AAAMessage **msg) { AAA_AVP *avp_t; AAA_AVP *avp; /* param check */ if (!msg || !(*msg)) goto done; /* free the avp list */ avp = (*msg)->avpList.head; while (avp) { avp_t = avp; avp = avp->next; /*free the avp*/ AAAFreeAVP(&avp_t); } /* free the buffer (if any) */ if ( (*msg)->buf.s ) ad_free( (*msg)->buf.s ); /* free the AAA msg */ ad_free(*msg); msg = 0; done: return AAA_ERR_SUCCESS; }
void ad_free(ARRAY_DINAMICO ad) { if (ad != NULL) { free(ad->elementos); ad_free(ad->garbage_collector); } free(ad); }
/* The function frees an AVP */ AAAReturnCode AAAFreeAVP(AAA_AVP **avp) { /* some checks */ if (!avp || !(*avp)) { LM_ERR("param avp cannot be null!!\n"); return AAA_ERR_PARAMETER; } /* free all the mem */ if ( (*avp)->free_it && (*avp)->data.s ) ad_free((*avp)->data.s); ad_free( *avp ); *avp = 0; return AAA_ERR_SUCCESS; }
void ad_clean_gc(ARRAY_DINAMICO ad, ad_elimina_elems *f_eliminacao) { int i; for (i = 0; i < ad->garbage_collector->posicao; i++) f_eliminacao(ad->garbage_collector->elementos[i]); ad_free(ad->garbage_collector); ad->garbage_collector = (ARRAY_DINAMICO) ad_inicializa_gc(8); }
void ad_deep_free(ARRAY_DINAMICO ad, ad_elimina_elems *f_eliminacao) { int i; if (ad != NULL) { for (i = 0; i < ad->posicao; i++) f_eliminacao(ad->elementos[i]); for (i = 0; i < ad->garbage_collector->posicao; i++) f_eliminacao(ad->garbage_collector->elementos[i]); ad_free(ad->garbage_collector); free(ad->elementos); } free(ad); }
AAA_AVP* AAACloneAVP( AAA_AVP *avp , unsigned char clone_data) { AAA_AVP *n_avp; if (!avp || !(avp->data.s) || !(avp->data.len) ) goto error; /* clone the avp structure */ n_avp = (AAA_AVP*)ad_malloc( sizeof(AAA_AVP) ); if (!n_avp) { LM_ERR(" cannot get free pkg memory!!\n"); goto error; } memcpy( n_avp, avp, sizeof(AAA_AVP)); n_avp->next = n_avp->prev = 0; if (clone_data) { /* clone the avp data */ n_avp->data.s = (char*)ad_malloc( avp->data.len ); if (!(n_avp->data.s)) { LM_ERR("cannot get free pkg memory!!\n"); ad_free( n_avp ); goto error; } memcpy( n_avp->data.s, avp->data.s, avp->data.len); n_avp->free_it = 1; } else { /* link the clone's data to the original's data */ n_avp->data.s = avp->data.s; n_avp->data.len = avp->data.len; n_avp->free_it = 0; } return n_avp; error: return 0; }
/* from a AAAMessage structure, a buffer to be send is build */ AAAReturnCode AAABuildMsgBuffer( AAAMessage *msg ) { unsigned char *p; AAA_AVP *avp; AAA_AVP *mem; // group member /* first let's compute the length of the buffer */ msg->buf.len = AAA_MSG_HDR_SIZE; /* AAA message header size */ /* count and add the avps */ for(avp=msg->avpList.head;avp;avp=avp->next) { msg->buf.len += AVP_HDR_SIZE(avp->flags)+ to_32x_len( avp->data.len ); } // DBG("xxxx len=%d\n",msg->buf.len); /* allocate some memory */ msg->buf.s = (char*)ad_malloc( msg->buf.len ); if (!msg->buf.s) { ERROR("ERROR:AAABuildMsgBuffer: no more free memory!\n"); goto error; } memset(msg->buf.s, 0, msg->buf.len); /* fill in the buffer */ p = (unsigned char*)msg->buf.s; /* DIAMETER HEADER */ /* message length */ ((unsigned int*)p)[0] =htonl(msg->buf.len); /* Diameter Version */ *p = 1; p += VER_SIZE + MESSAGE_LENGTH_SIZE; /* command code */ ((unsigned int*)p)[0] = htonl(msg->commandCode); /* flags */ *p = (unsigned char)msg->flags; p += FLAGS_SIZE + COMMAND_CODE_SIZE; /* application-ID */ ((unsigned int*)p)[0] = htonl(msg->applicationId); p += APPLICATION_ID_SIZE; /* hop by hop id */ ((unsigned int*)p)[0] = msg->hopbyhopId; p += HOP_BY_HOP_IDENTIFIER_SIZE; /* end to end id */ ((unsigned int*)p)[0] = msg->endtoendId; p += END_TO_END_IDENTIFIER_SIZE; /* AVPS */ for(avp=msg->avpList.head;avp;avp=avp->next) { /* AVP HEADER */ /* avp code */ set_4bytes(p,avp->code); p +=4; /* flags */ (*p++) = (unsigned char)avp->flags; /* avp length */ set_3bytes(p, (AVP_HDR_SIZE(avp->flags)+avp->data.len) ); p += 3; /* vendor id */ if ((avp->flags&0x80)!=0) { set_4bytes(p,avp->vendorId); p +=4; } /* data */ if (!avp->groupedHead) { memcpy( p, avp->data.s, avp->data.len); p += to_32x_len( avp->data.len ); } else { // group members for(mem=avp->groupedHead;mem;mem=mem->next) p+=AAAAVPBuildBuffer(mem, p); } } if ((char*)p-msg->buf.s!=msg->buf.len) { ERROR("BUG: build_buf_from_msg: mismatch between len and buf!\n"); ad_free( msg->buf.s ); msg->buf.s = 0; msg->buf.len = 0; goto error; } // DBG("Message: %.*s\n", msg->buf.len, msg->buf.s); return AAA_ERR_SUCCESS; error: return -1; }