Browse Source

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 16 years ago
parent
commit
1dfc197d38
2 changed files with 42 additions and 1 deletions
  1. 39 1
      forward.c
  2. 3 0
      forward.h

+ 39 - 1
forward.c

@@ -301,7 +301,44 @@ not_forced:
 	return send_sock;
 }
 
+static struct _check_self_func {
+	check_self_f fself;
+	struct _check_self_func *next;
+} *_check_self_func_list = NULL;
+
+/* register a function to be called when matching for myself
+ * - return 0 on success, -1 on error
+ * - f must have same prototype as check_self() and return same kind of values
+ */
+int register_check_self_func(check_self_f f)
+{
+	struct _check_self_func *nf = 0;
+	nf=(struct _check_self_func*)pkg_malloc(sizeof(struct _check_self_func));
+	if(nf==0)
+	{
+		LM_ERR("no more pkg\n");
+		return -1;
+	}
+	nf->fself = f;
+	nf->next = _check_self_func_list;
+	_check_self_func_list = nf;
+	return 0;
+}
 
+/* run registered check self functions
+ * returns 1 if true, 0 if false
+ */
+int run_check_self_func(str* host, unsigned short port, unsigned short proto)
+{
+	struct _check_self_func *sf = 0;
+
+	if(_check_self_func_list==NULL)
+		return 0;
+	for(sf=_check_self_func_list; sf; sf=sf->next)
+		if(sf->fself(host, port, proto)==1)
+			return 1;
+	return 0;
+}
 
 /* checks if the proto: host:port is one of the address we listen on;
  * if port==0, the  port number is ignored
@@ -316,7 +353,8 @@ int check_self(str* host, unsigned short port, unsigned short proto)
 	/* try to look into the aliases*/
 	if (grep_aliases(host->s, host->len, port, proto)==0){
 		DBG("check_self: host != me\n");
-		return 0;
+		return (_check_self_func_list==NULL)?0:run_check_self_func(host,
+														port, proto);
 	}
 found:
 	return 1;

+ 3 - 0
forward.h

@@ -83,6 +83,9 @@ inline static struct socket_info* get_send_socket(struct sip_msg* msg,
 
 
 struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
+typedef int (*check_self_f)(str* host, unsigned short port,
+		unsigned short proto);
+int register_check_self_func(check_self_f f);
 int check_self(str* host, unsigned short port, unsigned short proto);
 int check_self_port(unsigned short port, unsigned short proto);
 int forward_request( struct sip_msg* msg, str* dst,  unsigned short port,