Browse code

core: callback function support for myself check

- modules can register callback functions for myself check condition
- domain module is one candidate, resulting in coherent handling of
local domains via check myself operation
- callback functions must have same prototype and return codes as
check_self(...) function from core

Daniel-Constantin Mierla authored on 27/08/2009 08:22:10
Showing 2 changed files
... ...
@@ -301,7 +301,44 @@ not_forced:
301 301
 	return send_sock;
302 302
 }
303 303
 
304
+static struct _check_self_func {
305
+	check_self_f fself;
306
+	struct _check_self_func *next;
307
+} *_check_self_func_list = NULL;
308
+
309
+/* register a function to be called when matching for myself
310
+ * - return 0 on success, -1 on error
311
+ * - f must have same prototype as check_self() and return same kind of values
312
+ */
313
+int register_check_self_func(check_self_f f)
314
+{
315
+	struct _check_self_func *nf = 0;
316
+	nf=(struct _check_self_func*)pkg_malloc(sizeof(struct _check_self_func));
317
+	if(nf==0)
318
+	{
319
+		LM_ERR("no more pkg\n");
320
+		return -1;
321
+	}
322
+	nf->fself = f;
323
+	nf->next = _check_self_func_list;
324
+	_check_self_func_list = nf;
325
+	return 0;
326
+}
304 327
 
328
+/* run registered check self functions
329
+ * returns 1 if true, 0 if false
330
+ */
331
+int run_check_self_func(str* host, unsigned short port, unsigned short proto)
332
+{
333
+	struct _check_self_func *sf = 0;
334
+
335
+	if(_check_self_func_list==NULL)
336
+		return 0;
337
+	for(sf=_check_self_func_list; sf; sf=sf->next)
338
+		if(sf->fself(host, port, proto)==1)
339
+			return 1;
340
+	return 0;
341
+}
305 342
 
306 343
 /* checks if the proto: host:port is one of the address we listen on;
307 344
  * if port==0, the  port number is ignored
... ...
@@ -316,7 +353,8 @@ int check_self(str* host, unsigned short port, unsigned short proto)
316 316
 	/* try to look into the aliases*/
317 317
 	if (grep_aliases(host->s, host->len, port, proto)==0){
318 318
 		DBG("check_self: host != me\n");
319
-		return 0;
319
+		return (_check_self_func_list==NULL)?0:run_check_self_func(host,
320
+														port, proto);
320 321
 	}
321 322
 found:
322 323
 	return 1;
... ...
@@ -83,6 +83,9 @@ inline static struct socket_info* get_send_socket(struct sip_msg* msg,
83 83
 
84 84
 
85 85
 struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
86
+typedef int (*check_self_f)(str* host, unsigned short port,
87
+		unsigned short proto);
88
+int register_check_self_func(check_self_f f);
86 89
 int check_self(str* host, unsigned short port, unsigned short proto);
87 90
 int check_self_port(unsigned short port, unsigned short proto);
88 91
 int forward_request( struct sip_msg* msg, str* dst,  unsigned short port,