register(k): pack contact info based on header for searching existing ul records
[sip-router] / events.c
1 /**
2  * $Id$
3  *
4  * Copyright (C) 2009 SIP-Router.org
5  *
6  * This file is part of Extensible SIP Router, a free SIP server.
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20
21 /*!
22  * \file
23  * \brief SIP-router core :: 
24  * \ingroup core
25  * Module: \ref core
26  */
27
28 #include "dprint.h"
29 #include "mem/mem.h"
30 #include "events.h"
31
32 static sr_event_cb_t _sr_events_list;
33 static int _sr_events_inited = 0;
34
35 /**
36  *
37  */
38 void sr_event_cb_init(void)
39 {
40         if(_sr_events_inited == 0)
41         {
42                 memset(&_sr_events_list, 0, sizeof(sr_event_cb_t));
43                 _sr_events_inited = 1;
44         }
45 }
46
47 /**
48  *
49  */
50 int sr_event_register_cb(int type, sr_event_cb_f f)
51 {
52         sr_event_cb_init();
53         switch(type) {
54                 case SREV_NET_DATA_IN:
55                                 if(_sr_events_list.net_data_in==0)
56                                         _sr_events_list.net_data_in = f;
57                                 else return -1;
58                         break;
59                 case SREV_NET_DATA_OUT:
60                                 if(_sr_events_list.net_data_out==0)
61                                         _sr_events_list.net_data_out = f;
62                                 else return -1;
63                         break;
64                 case SREV_CORE_STATS:
65                                 if(_sr_events_list.core_stats==0)
66                                         _sr_events_list.core_stats = f;
67                                 else return -1;
68                         break;
69                 case SREV_CFG_RUN_ACTION:
70                                 if(_sr_events_list.run_action==0)
71                                         _sr_events_list.run_action = f;
72                                 else return -1;
73                         break;
74                 case SREV_PKG_SET_USED:
75                                 if(_sr_events_list.pkg_set_used==0)
76                                         _sr_events_list.pkg_set_used = f;
77                                 else return -1;
78                         break;
79                 case SREV_PKG_SET_REAL_USED:
80                                 if(_sr_events_list.pkg_set_real_used==0)
81                                         _sr_events_list.pkg_set_real_used = f;
82                                 else return -1;
83                         break;
84                 case SREV_NET_DGRAM_IN:
85                                 if(_sr_events_list.net_dgram_in==0)
86                                         _sr_events_list.net_dgram_in = f;
87                                 else return -1;
88                         break;
89                 case SREV_TCP_HTTP_100C:
90                                 if(_sr_events_list.tcp_http_100c==0)
91                                         _sr_events_list.tcp_http_100c = f;
92                                 else return -1;
93                         break;
94                 case SREV_TCP_MSRP_FRAME:
95                                 if(_sr_events_list.tcp_msrp_frame==0)
96                                         _sr_events_list.tcp_msrp_frame = f;
97                                 else return -1;
98                         break;
99                 case SREV_TCP_WS_FRAME_IN:
100                                 if(_sr_events_list.tcp_ws_frame_in==0)
101                                         _sr_events_list.tcp_ws_frame_in = f;
102                                 else return -1;
103                         break;
104                 case SREV_TCP_WS_FRAME_OUT:
105                                 if(_sr_events_list.tcp_ws_frame_out==0)
106                                         _sr_events_list.tcp_ws_frame_out = f;
107                                 else return -1;
108                         break;
109                 default:
110                         return -1;
111         }
112         return 0;
113 }
114
115 /**
116  *
117  */
118 int sr_event_exec(int type, void *data)
119 {
120         int ret;
121 #ifdef EXTRA_DEBUG
122         str *p;
123 #endif /* EXTRA_DEBUG */
124         switch(type) {
125                 case SREV_NET_DATA_IN:
126                                 if(unlikely(_sr_events_list.net_data_in!=0))
127                                 {
128 #ifdef EXTRA_DEBUG
129                                         p = (str*)data;
130                                         LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
131                                                         "%.*s\n+++++\n", p->len, p->s);
132 #endif /* EXTRA_DEBUG */
133                                         ret = _sr_events_list.net_data_in(data);
134 #ifdef EXTRA_DEBUG
135                                         LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
136                                                         "%.*s\n+++++\n", p->len, p->s);
137 #endif /* EXTRA_DEBUG */
138                                         return ret;
139                                 } else return 1;
140                         break;
141                 case SREV_NET_DATA_OUT:
142                                 if(unlikely(_sr_events_list.net_data_out!=0))
143                                 {
144 #ifdef EXTRA_DEBUG
145                                         p = (str*)data;
146                                         LM_DBG("PRE-OUT ++++++++++++++++++++\n"
147                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
148 #endif /* EXTRA_DEBUG */
149                                         ret = _sr_events_list.net_data_out(data);
150 #ifdef EXTRA_DEBUG
151                                         LM_DBG("POST-OUT ++++++++++++++++++++\n"
152                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
153 #endif /* EXTRA_DEBUG */
154                                         return ret;
155                                 } else return 1;
156                         break;
157                 case SREV_CORE_STATS:
158                                 if(unlikely(_sr_events_list.core_stats!=0))
159                                 {
160                                         ret = _sr_events_list.core_stats(data);
161                                         return ret;
162                                 } else return 1;
163                         break;
164                 case SREV_CFG_RUN_ACTION:
165                                 if(unlikely(_sr_events_list.run_action!=0))
166                                 {
167                                         ret = _sr_events_list.run_action(data);
168                                         return ret;
169                                 } else return 1;
170                 case SREV_PKG_SET_USED:
171                                 if(unlikely(_sr_events_list.pkg_set_used!=0))
172                                 {
173                                         ret = _sr_events_list.pkg_set_used(data);
174                                         return ret;
175                                 } else return 1;
176                 case SREV_PKG_SET_REAL_USED:
177                                 if(unlikely(_sr_events_list.pkg_set_real_used!=0))
178                                 {
179                                         ret = _sr_events_list.pkg_set_real_used(data);
180                                         return ret;
181                                 } else return 1;
182                 case SREV_NET_DGRAM_IN:
183                                 if(unlikely(_sr_events_list.net_dgram_in!=0))
184                                 {
185                                         ret = _sr_events_list.net_dgram_in(data);
186                                         return ret;
187                                 } else return 1;
188                 case SREV_TCP_HTTP_100C:
189                                 if(unlikely(_sr_events_list.tcp_http_100c!=0))
190                                 {
191                                         ret = _sr_events_list.tcp_http_100c(data);
192                                         return ret;
193                                 } else return 1;
194                 case SREV_TCP_MSRP_FRAME:
195                                 if(unlikely(_sr_events_list.tcp_msrp_frame!=0))
196                                 {
197                                         ret = _sr_events_list.tcp_msrp_frame(data);
198                                         return ret;
199                                 } else return 1;
200                 case SREV_TCP_WS_FRAME_IN:
201                                 if(unlikely(_sr_events_list.tcp_ws_frame_in!=0))
202                                 {
203                                         ret = _sr_events_list.tcp_ws_frame_in(data);
204                                         return ret;
205                                 } else return 1;
206                 case SREV_TCP_WS_FRAME_OUT:
207                                 if(unlikely(_sr_events_list.tcp_ws_frame_out!=0))
208                                 {
209                                         ret = _sr_events_list.tcp_ws_frame_out(data);
210                                         return ret;
211                                 } else return 1;
212                 default:
213                         return -1;
214         }
215 }
216
217 /**
218  *
219  */
220 int sr_event_enabled(int type)
221 {
222         switch(type) {
223                 case SREV_NET_DATA_IN:
224                                 return (_sr_events_list.net_data_in!=0)?1:0;
225                 case SREV_NET_DATA_OUT:
226                                 return (_sr_events_list.net_data_out!=0)?1:0;
227                 case SREV_CORE_STATS:
228                                 return (_sr_events_list.core_stats!=0)?1:0;
229                 case SREV_CFG_RUN_ACTION:
230                                 return (_sr_events_list.run_action!=0)?1:0;
231                 case SREV_PKG_SET_USED:
232                                 return (_sr_events_list.pkg_set_used!=0)?1:0;
233                 case SREV_PKG_SET_REAL_USED:
234                                 return (_sr_events_list.pkg_set_real_used!=0)?1:0;
235                 case SREV_NET_DGRAM_IN:
236                                 return (_sr_events_list.net_dgram_in!=0)?1:0;
237                 case SREV_TCP_HTTP_100C:
238                                 return (_sr_events_list.tcp_http_100c!=0)?1:0;
239                 case SREV_TCP_MSRP_FRAME:
240                                 return (_sr_events_list.tcp_msrp_frame!=0)?1:0;
241                 case SREV_TCP_WS_FRAME_IN:
242                                 return (_sr_events_list.tcp_ws_frame_in!=0)?1:0;
243                 case SREV_TCP_WS_FRAME_OUT:
244                                 return (_sr_events_list.tcp_ws_frame_out!=0)?1:0;
245         }
246         return 0;
247 }
248