blob: 6111915604763684d60b773c775889340dec910d [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36#include "fdproto-internal.h"
37
38/* The dispatch module in the library is quite simple: callbacks are saved in a global list
39 * in no particular order. In addition, they are also linked from the dictionary objects they
40 * refer to. */
41
42/* Protection for the lists managed in this module. */
43pthread_rwlock_t fd_disp_lock = PTHREAD_RWLOCK_INITIALIZER;
44
45/* List of all registered handlers -- useful if we want to cleanup properly at some point... */
46static struct fd_list all_handlers = FD_LIST_INITIALIZER( all_handlers );
47
48/* List of handlers registered for DISP_HOW_ANY. Other handlers are stored in the dictionary */
49static struct fd_list any_handlers = FD_LIST_INITIALIZER( any_handlers );
50
51/* The structure to store a callback */
52struct disp_hdl {
53 int eyec; /* Eye catcher, DISP_EYEC */
54 struct fd_list all; /* link in the all_handlers list */
55 struct fd_list parent;/* link in dictionary cb_list or in any_handlers */
56 enum disp_how how; /* Copy of registration parameter */
57 struct disp_when when; /* Copy of registration parameter */
58 int (*cb)( struct msg **, struct avp *, struct session *, void *, enum disp_action *); /* The callback itself */
59 void *opaque; /* opaque data passed back to the callback */
60};
61
62#define DISP_EYEC 0xD15241C1
63#define VALIDATE_HDL( _hdl ) \
64 ( ( ( _hdl ) != NULL ) && ( ((struct disp_hdl *)( _hdl ))->eyec == DISP_EYEC ) )
65
66/**************************************************************************************/
67
68/* Call CBs from a given list (any_handlers if cb_list is NULL) -- must have locked fd_disp_lock before */
69int fd_disp_call_cb_int( struct fd_list * cb_list, struct msg ** msg, struct avp *avp, struct session *sess, enum disp_action *action,
70 struct dict_object * obj_app, struct dict_object * obj_cmd, struct dict_object * obj_avp, struct dict_object * obj_enu,
71 char ** drop_reason, struct msg ** drop_msg)
72{
73 struct fd_list * senti, *li;
74 int r;
75 TRACE_ENTRY("%p %p %p %p %p %p %p %p %p", cb_list, msg, avp, sess, action, obj_app, obj_cmd, obj_avp, obj_enu);
76 CHECK_PARAMS(msg && action);
77
78 senti = cb_list;
79 if (!senti)
80 senti = &any_handlers;
81
82 for (li = senti->next; li != senti; li = li->next) {
83 struct disp_hdl * hdl = (struct disp_hdl *)(li->o);
84
85 TRACE_DEBUG(ANNOYING, "when: %p %p %p %p", hdl->when.app, hdl->when.command, hdl->when.avp, hdl->when.value);
86
87 /* Check this handler matches this message / avp */
88 if (hdl->when.app && (hdl->when.app != obj_app))
89 continue;
90 if (hdl->when.command && (hdl->when.command != obj_cmd))
91 continue;
92 if (hdl->when.avp && (hdl->when.avp != obj_avp))
93 continue;
94 if (hdl->when.value && (hdl->when.value != obj_enu))
95 continue;
96
97 /* We have a match, the cb must be called. */
98 CHECK_FCT_DO( (r = (*hdl->cb)(msg, avp, sess, hdl->opaque, action)),
99 {
100 *drop_reason = "Internal error: a DISPATCH callback returned an error";
101 *drop_msg = *msg;
102 *msg = NULL;
103 }
104 );
105
106 if (*action != DISP_ACT_CONT)
107 break;
108
109 if ( *msg == NULL )
110 break;
111 }
112
113 /* We're done on this list */
114 return 0;
115}
116
117/**************************************************************************************/
118
119/* Create a new handler and link it */
120int fd_disp_register ( int (*cb)( struct msg **, struct avp *, struct session *, void *, enum disp_action *),
121 enum disp_how how, struct disp_when * when, void * opaque, struct disp_hdl ** handle )
122{
123 struct fd_list * cb_list = NULL;
124 struct disp_hdl * new;
125 struct dict_object * type_enum = NULL, * type_avp;
126 struct dictionary * dict = NULL;
127
128 TRACE_ENTRY("%p %d %p %p", cb, how, when, handle);
129 CHECK_PARAMS( cb && ( (how == DISP_HOW_ANY) || when ));
130
131 switch (how) {
132 case DISP_HOW_ANY:
133 cb_list = &any_handlers;
134 break;
135
136 case DISP_HOW_APPID:
137 CHECK_FCT( fd_dict_disp_cb(DICT_APPLICATION, when->app, &cb_list) );
138 break;
139
140 case DISP_HOW_CC:
141 CHECK_FCT( fd_dict_disp_cb(DICT_COMMAND, when->command, &cb_list) );
142 break;
143
144 case DISP_HOW_AVP_ENUMVAL:
145 CHECK_FCT( fd_dict_disp_cb(DICT_ENUMVAL, when->value, &cb_list) ); /* cb_list is then overwritten */
146 CHECK_FCT( fd_dict_getdict(when->value, &dict) );
147 CHECK_FCT( fd_dict_search(dict, DICT_TYPE, TYPE_OF_ENUMVAL, when->value, &type_enum, EINVAL) );
148 case DISP_HOW_AVP:
149 CHECK_FCT( fd_dict_disp_cb(DICT_AVP, when->avp, &cb_list) );
150 if (dict) {
151 CHECK_FCT( fd_dict_search(dict, DICT_TYPE, TYPE_OF_AVP, when->avp, &type_avp, EINVAL) );
152 if (type_enum) {
153 CHECK_PARAMS( type_enum == type_avp );
154 }
155 }
156 break;
157
158 default:
159 CHECK_PARAMS(how = 0);
160 }
161 /* We might further check optional fields, but we trust the caller ^^ */
162
163 /* Create the new handler */
164 CHECK_MALLOC( new = malloc( sizeof(struct disp_hdl) ) );
165 memset(new, 0, sizeof(struct disp_hdl));
166 new->eyec = DISP_EYEC;
167 fd_list_init(&new->all, new);
168 fd_list_init(&new->parent, new);
169 new->how = how;
170 switch (how) {
171 case DISP_HOW_ANY:
172 /* there is no "when" in that case */
173 break;
174 case DISP_HOW_AVP_ENUMVAL:
175 new->when.value = when->value;
176 case DISP_HOW_AVP:
177 new->when.avp = when->avp;
178 case DISP_HOW_CC:
179 new->when.command = when->command;
180 case DISP_HOW_APPID:
181 new->when.app = when->app;
182 }
183 new->cb = cb;
184 new->opaque = opaque;
185
186 /* Now, link this new element in the appropriate lists */
187 CHECK_POSIX( pthread_rwlock_wrlock(&fd_disp_lock) );
188 fd_list_insert_before(&all_handlers, &new->all);
189 fd_list_insert_before(cb_list, &new->parent);
190 CHECK_POSIX( pthread_rwlock_unlock(&fd_disp_lock) );
191
192 /* We're done */
193 if (handle)
194 *handle = new;
195
196 return 0;
197}
198
199/* Delete a handler */
200int fd_disp_unregister ( struct disp_hdl ** handle, void ** opaque )
201{
202 struct disp_hdl * del;
203 TRACE_ENTRY("%p", handle);
204 CHECK_PARAMS( handle && VALIDATE_HDL(*handle) );
205 del = *handle;
206 *handle = NULL;
207
208 CHECK_POSIX( pthread_rwlock_wrlock(&fd_disp_lock) );
209 fd_list_unlink(&del->all);
210 fd_list_unlink(&del->parent);
211 CHECK_POSIX( pthread_rwlock_unlock(&fd_disp_lock) );
212
213 if (opaque)
214 *opaque = del->opaque;
215
216 free(del);
217 return 0;
218}
219
220/* Delete all handlers */
221void fd_disp_unregister_all ( void )
222{
223 TRACE_ENTRY("");
224 while (!FD_IS_LIST_EMPTY(&all_handlers)) {
225 CHECK_FCT_DO( fd_disp_unregister((void *)&(all_handlers.next->o), NULL), /* continue */ );
226 }
227 return;
228}