core: Added WS support/events to Kamailio core
[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:
100                                 if(_sr_events_list.tcp_ws_frame==0)
101                                         _sr_events_list.tcp_ws_frame = f;
102                                 else return -1;
103                         break;
104                 default:
105                         return -1;
106         }
107         return 0;
108 }
109
110 /**
111  *
112  */
113 int sr_event_exec(int type, void *data)
114 {
115         int ret;
116 #ifdef EXTRA_DEBUG
117         str *p;
118 #endif /* EXTRA_DEBUG */
119         switch(type) {
120                 case SREV_NET_DATA_IN:
121                                 if(unlikely(_sr_events_list.net_data_in!=0))
122                                 {
123 #ifdef EXTRA_DEBUG
124                                         p = (str*)data;
125                                         LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
126                                                         "%.*s\n+++++\n", p->len, p->s);
127 #endif /* EXTRA_DEBUG */
128                                         ret = _sr_events_list.net_data_in(data);
129 #ifdef EXTRA_DEBUG
130                                         LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
131                                                         "%.*s\n+++++\n", p->len, p->s);
132 #endif /* EXTRA_DEBUG */
133                                         return ret;
134                                 } else return 1;
135                         break;
136                 case SREV_NET_DATA_OUT:
137                                 if(unlikely(_sr_events_list.net_data_out!=0))
138                                 {
139 #ifdef EXTRA_DEBUG
140                                         p = (str*)data;
141                                         LM_DBG("PRE-OUT ++++++++++++++++++++\n"
142                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
143 #endif /* EXTRA_DEBUG */
144                                         ret = _sr_events_list.net_data_out(data);
145 #ifdef EXTRA_DEBUG
146                                         LM_DBG("POST-OUT ++++++++++++++++++++\n"
147                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
148 #endif /* EXTRA_DEBUG */
149                                         return ret;
150                                 } else return 1;
151                         break;
152                 case SREV_CORE_STATS:
153                                 if(unlikely(_sr_events_list.core_stats!=0))
154                                 {
155                                         ret = _sr_events_list.core_stats(data);
156                                         return ret;
157                                 } else return 1;
158                         break;
159                 case SREV_CFG_RUN_ACTION:
160                                 if(unlikely(_sr_events_list.run_action!=0))
161                                 {
162                                         ret = _sr_events_list.run_action(data);
163                                         return ret;
164                                 } else return 1;
165                 case SREV_PKG_SET_USED:
166                                 if(unlikely(_sr_events_list.pkg_set_used!=0))
167                                 {
168                                         ret = _sr_events_list.pkg_set_used(data);
169                                         return ret;
170                                 } else return 1;
171                 case SREV_PKG_SET_REAL_USED:
172                                 if(unlikely(_sr_events_list.pkg_set_real_used!=0))
173                                 {
174                                         ret = _sr_events_list.pkg_set_real_used(data);
175                                         return ret;
176                                 } else return 1;
177                 case SREV_NET_DGRAM_IN:
178                                 if(unlikely(_sr_events_list.net_dgram_in!=0))
179                                 {
180                                         ret = _sr_events_list.net_dgram_in(data);
181                                         return ret;
182                                 } else return 1;
183                 case SREV_TCP_HTTP_100C:
184                                 if(unlikely(_sr_events_list.tcp_http_100c!=0))
185                                 {
186                                         ret = _sr_events_list.tcp_http_100c(data);
187                                         return ret;
188                                 } else return 1;
189                 case SREV_TCP_MSRP_FRAME:
190                                 if(unlikely(_sr_events_list.tcp_msrp_frame!=0))
191                                 {
192                                         ret = _sr_events_list.tcp_msrp_frame(data);
193                                         return ret;
194                                 } else return 1;
195                 case SREV_TCP_WS_FRAME:
196                                 if(unlikely(_sr_events_list.tcp_ws_frame!=0))
197                                 {
198                                         ret = _sr_events_list.tcp_ws_frame(data);
199                                         return ret;
200                                 } else return 1;
201                 default:
202                         return -1;
203         }
204 }
205
206 /**
207  *
208  */
209 int sr_event_enabled(int type)
210 {
211         switch(type) {
212                 case SREV_NET_DATA_IN:
213                                 return (_sr_events_list.net_data_in!=0)?1:0;
214                 case SREV_NET_DATA_OUT:
215                                 return (_sr_events_list.net_data_out!=0)?1:0;
216                 case SREV_CORE_STATS:
217                                 return (_sr_events_list.core_stats!=0)?1:0;
218                 case SREV_CFG_RUN_ACTION:
219                                 return (_sr_events_list.run_action!=0)?1:0;
220                 case SREV_PKG_SET_USED:
221                                 return (_sr_events_list.pkg_set_used!=0)?1:0;
222                 case SREV_PKG_SET_REAL_USED:
223                                 return (_sr_events_list.pkg_set_real_used!=0)?1:0;
224                 case SREV_NET_DGRAM_IN:
225                                 return (_sr_events_list.net_dgram_in!=0)?1:0;
226                 case SREV_TCP_HTTP_100C:
227                                 return (_sr_events_list.tcp_http_100c!=0)?1:0;
228                 case SREV_TCP_MSRP_FRAME:
229                                 return (_sr_events_list.tcp_msrp_frame!=0)?1:0;
230                 case SREV_TCP_WS_FRAME:
231                                 return (_sr_events_list.tcp_ws_frame!=0)?1:0;
232         }
233         return 0;
234 }
235