00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdlib.h>
00036 #include "script_cb.h"
00037 #include "dprint.h"
00038 #include "error.h"
00039 #include "mem/mem.h"
00040
00041
00042 static struct script_cb *pre_req_cb=0;
00043
00044 static struct script_cb *post_req_cb=0;
00045
00046
00047 static struct script_cb *pre_rpl_cb=0;
00048
00049 static struct script_cb *post_rpl_cb=0;
00050
00051
00052 static unsigned int cb_id=0;
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 static inline int add_callback( struct script_cb **list,
00063 cb_function f, void *param)
00064 {
00065 struct script_cb *new_cb;
00066
00067 new_cb=pkg_malloc(sizeof(struct script_cb));
00068 if (new_cb==0) {
00069 LM_ERR("out of pkg memory\n");
00070 return -1;
00071 }
00072 new_cb->cbf = f;
00073 new_cb->id = cb_id++;
00074 new_cb->param = param;
00075
00076 new_cb->next = *list;
00077 *list = new_cb;
00078 return 0;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 int register_script_cb( cb_function f, int type, void *param )
00090 {
00091
00092 if ( (type&(REQ_TYPE_CB|RPL_TYPE_CB))==0 ) {
00093 LM_CRIT("invalid callback, no request or reply type specified\n");
00094 return -1;
00095 }
00096 if ( (type&(PRE_SCRIPT_CB|POST_SCRIPT_CB))==0 ||
00097 (type&PRE_SCRIPT_CB && type&POST_SCRIPT_CB) ) {
00098 LM_CRIT("invalid callback, please set either the POST or PRE type\n");
00099 return -1;
00100 }
00101
00102 if (type&REQ_TYPE_CB) {
00103
00104 if (type&PRE_SCRIPT_CB) {
00105 if (add_callback( &pre_req_cb, f, param)<0)
00106 goto error;
00107 } else if (type&POST_SCRIPT_CB) {
00108 if (add_callback( &post_req_cb, f, param)<0)
00109 goto error;
00110 }
00111 }
00112 if (type&RPL_TYPE_CB) {
00113
00114 if (type&PRE_SCRIPT_CB) {
00115 if (add_callback( &pre_rpl_cb, f, param)<0)
00116 goto error;
00117 } else if (type&POST_SCRIPT_CB) {
00118 if (add_callback( &post_rpl_cb, f, param)<0)
00119 goto error;
00120 }
00121 }
00122
00123 return 0;
00124 error:
00125 LM_ERR("failed to add callback\n");
00126 return -1;
00127 }
00128
00129
00130
00131
00132
00133
00134 static inline void destroy_cb_list(struct script_cb **list)
00135 {
00136 struct script_cb *foo;
00137
00138 while( *list ) {
00139 foo = *list;
00140 *list = (*list)->next;
00141 pkg_free( foo );
00142 }
00143 }
00144
00145
00146
00147
00148
00149 void destroy_script_cb(void)
00150 {
00151 destroy_cb_list( &pre_req_cb );
00152 destroy_cb_list( &post_req_cb );
00153 destroy_cb_list( &pre_rpl_cb );
00154 destroy_cb_list( &post_req_cb );
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164 static inline int exec_pre_cb( struct sip_msg *msg, struct script_cb *cb)
00165 {
00166 for ( ; cb ; cb=cb->next ) {
00167
00168 if (cb->cbf(msg, cb->param)==0)
00169 return 0;
00170 }
00171 return 1;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181 static inline int exec_post_cb( struct sip_msg *msg, struct script_cb *cb)
00182 {
00183 for ( ; cb ; cb=cb->next){
00184 cb->cbf( msg, cb->param);
00185 }
00186 return 1;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 int exec_pre_req_cb( struct sip_msg *msg)
00196 {
00197 return exec_pre_cb( msg, pre_req_cb);
00198 }
00199
00200
00201
00202
00203
00204
00205
00206 int exec_pre_rpl_cb( struct sip_msg *msg)
00207 {
00208 return exec_pre_cb( msg, pre_rpl_cb);
00209 }
00210
00211
00212
00213
00214
00215
00216
00217 int exec_post_req_cb( struct sip_msg *msg)
00218 {
00219 return exec_post_cb( msg, post_req_cb);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 int exec_post_rpl_cb( struct sip_msg *msg)
00229 {
00230 return exec_post_cb( msg, post_rpl_cb);
00231 }