modules/dialog/dlg_cb.c
5b3d7218
 /*
  * Copyright (C) 2006 Voice Sistem SRL
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
5b3d7218
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
5b3d7218
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
5b3d7218
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
5b3d7218
  *
c3291cfe
  */
 
 
 /*!
  * \file
  * \brief Callbacks
  * \ingroup dialog
  * Module: \ref dialog
5b3d7218
  */
 
 
 #include "../../mem/shm_mem.h"
 #include "../../dprint.h"
 #include "dlg_hash.h"
 #include "dlg_cb.h"
 
aa7edc2b
 
5b3d7218
 static struct dlg_head_cbl* create_cbs = 0;
 
aa7edc2b
 static struct dlg_head_cbl* load_cbs = 0;
 
c021559e
 static struct dlg_cb_params params = {NULL, NULL, DLG_DIR_NONE, NULL, NULL};
ea694f02
 
 
0723496c
 #define POINTER_CLOSED_MARKER  ((void *)(-1))
 
aa7edc2b
 
 static void run_load_callback(struct dlg_callback *cb);
 
 
 
 static struct dlg_head_cbl* init_dlg_callback(void)
5b3d7218
 {
aa7edc2b
 	struct dlg_head_cbl *new_cbs;
 
 	new_cbs = (struct dlg_head_cbl*)shm_malloc(sizeof(struct dlg_head_cbl));
 	if (new_cbs==0) {
b9bb99d2
 		LM_ERR("no more shm mem\n");
aa7edc2b
 		return 0;
5b3d7218
 	}
aa7edc2b
 	new_cbs->first = 0;
 	new_cbs->types = 0;
 
 	return new_cbs;
5b3d7218
 }
 
 
29b0bf53
 void destroy_dlg_callbacks_list(struct dlg_callback *cb)
5b3d7218
 {
 	struct dlg_callback *cb_t;
 
 	while(cb) {
 		cb_t = cb;
 		cb = cb->next;
ea694f02
 		if (cb_t->callback_param_free && cb_t->param) {
 			cb_t->callback_param_free(cb_t->param);
 			cb_t->param = NULL;
 		}
5b3d7218
 		shm_free(cb_t);
 	}
29b0bf53
 }
 
 
aa7edc2b
 void destroy_dlg_callbacks(unsigned int types)
29b0bf53
 {
aa7edc2b
 	if (types&DLGCB_CREATED) {
 		if (create_cbs && create_cbs!=POINTER_CLOSED_MARKER) {
 			destroy_dlg_callbacks_list(create_cbs->first);
 			shm_free(create_cbs);
 			create_cbs = POINTER_CLOSED_MARKER;
 		}
 	}
 	if (types&DLGCB_LOADED) {
 		if (load_cbs && load_cbs!=POINTER_CLOSED_MARKER) {
 			destroy_dlg_callbacks_list(load_cbs->first);
 			shm_free(load_cbs);
 			load_cbs = POINTER_CLOSED_MARKER;
 		}
 	}
5b3d7218
 }
 
 
00cca1f4
 int register_dlgcb(struct dlg_cell *dlg, int types, dialog_cb f,
 										void *param, param_free_cb ff )
5b3d7218
 {
 	struct dlg_callback *cb;
 
aa7edc2b
 	if ( types&DLGCB_LOADED ) {
 		if (types!=DLGCB_LOADED) {
 			LM_CRIT("DLGCB_LOADED type must be register alone!\n");
 			return -1;
 		}
 	} else if ( types&DLGCB_CREATED ) {
b5637251
 		if (types!=DLGCB_CREATED) {
b9bb99d2
 			LM_CRIT("DLGCB_CREATED type must be register alone!\n");
5b3d7218
 			return -1;
 		}
 	} else {
 		if (dlg==0) {
b9bb99d2
 			LM_CRIT("non-DLGCB_CREATED type "
b5637251
 				"must be register to a dialog (dlg missing)!\n");
5b3d7218
 			return -1;
 		}
 	}
 	cb = (struct dlg_callback*)shm_malloc(sizeof(struct dlg_callback));
 	if (cb==0) {
b9bb99d2
 		LM_ERR("no more shm mem\n");
5b3d7218
 		return -1;
 	}
 
 	cb->types = types;
 	cb->callback = f;
 	cb->param = param;
ea694f02
 	cb->callback_param_free = ff;
5b3d7218
 
aa7edc2b
 	if ( types==DLGCB_CREATED ) {
 		if (load_cbs==POINTER_CLOSED_MARKER) {
 			LM_CRIT("DLGCB_CREATED type registered after shutdown!?!\n");
 			goto error;
 		}
 		if (create_cbs==0) {
 			/* not initialized yet */
 			if ( (create_cbs=init_dlg_callback())==NULL ) {
 				LM_ERR("no more shm mem\n");
 				goto error;
 			}
 		}
5b3d7218
 		cb->next = create_cbs->first;
 		create_cbs->first = cb;
 		create_cbs->types |= types;
aa7edc2b
 	} else if (types==DLGCB_LOADED) {
 		if (load_cbs==POINTER_CLOSED_MARKER) {
 			/* run the callback on the spot */
 			run_load_callback(cb);
 			destroy_dlg_callbacks_list(cb);
 			return 0;
 		}
 		if (load_cbs==0) {
 			/* not initialized yet */
 			if ( (load_cbs=init_dlg_callback())==NULL ) {
 				LM_ERR("no more shm mem\n");
 				goto error;
 			}
 		}
 		cb->next = load_cbs->first;
 		load_cbs->first = cb;
 		load_cbs->types |= types;
5b3d7218
 	} else {
 		cb->next = dlg->cbs.first;
 		dlg->cbs.first = cb;
 		dlg->cbs.types |= types;
 	}
 
 	return 0;
aa7edc2b
 error:
 	shm_free(cb);
 	return -1;
 }
 
 
 static void run_load_callback(struct dlg_callback *cb)
 {
 	struct dlg_cell *dlg;
 	unsigned int i;
 
c021559e
 	params.req = NULL;
 	params.rpl = NULL;
aa7edc2b
 	params.direction = DLG_DIR_NONE;
 	params.param = &cb->param;
 
 	for( i=0 ; i<d_table->size ; i++ ) {
 		for( dlg=d_table->entries[i].first ; dlg ; dlg=dlg->next )
 			cb->callback( dlg, DLGCB_LOADED, &params );
 	}
 
 	return;
 }
 
 
 void run_load_callbacks( void )
 {
 	struct dlg_callback *cb;
 
fe350637
 	if (load_cbs && load_cbs!=POINTER_CLOSED_MARKER) {
 		for ( cb=load_cbs->first; cb; cb=cb->next )
 			run_load_callback( cb );
 	}
aa7edc2b
 
 	return;
5b3d7218
 }
 
 
 void run_create_callbacks(struct dlg_cell *dlg, struct sip_msg *msg)
 {
 	struct dlg_callback *cb;
 
d470fdeb
 	if (create_cbs==NULL || create_cbs->first==NULL)
5b3d7218
 		return;
 
c021559e
 	params.req = msg;
 	params.rpl = NULL;
00cca1f4
 	/* initial request goes DOWNSTREAM all the time */
 	params.direction = DLG_DIR_DOWNSTREAM;
 	/* avoid garbage due static structure */
 	params.param = NULL;
594cfbaa
 	params.dlg_data = NULL;
ea694f02
 
5b3d7218
 	for ( cb=create_cbs->first; cb; cb=cb->next)  {
b9bb99d2
 		LM_DBG("dialog=%p\n",dlg);
ea694f02
 		params.param = &cb->param;
 		cb->callback( dlg, DLGCB_CREATED, &params );
5b3d7218
 	}
 	return;
 }
 
 
c021559e
 void run_dlg_callbacks( int type ,
 						struct dlg_cell *dlg,
 						struct sip_msg *req,
 						struct sip_msg *rpl,
 						unsigned int dir, void *dlg_data)
5b3d7218
 {
 	struct dlg_callback *cb;
 
c021559e
 	params.req = req;
 	params.rpl = rpl;
00cca1f4
 	params.direction = dir;
594cfbaa
 	params.dlg_data = dlg_data;
ea694f02
 
5b3d7218
 	if (dlg->cbs.first==0 || ((dlg->cbs.types)&type)==0 )
 		return;
 
 	for ( cb=dlg->cbs.first; cb; cb=cb->next)  {
 		if ( (cb->types)&type ) {
b9bb99d2
 			LM_DBG("dialog=%p, type=%d\n", dlg, type);
ea694f02
 			params.param = &cb->param;
 			cb->callback( dlg, type, &params );
5b3d7218
 		}
 	}
 	return;
 }