Browse Source

registrar(k): exported inter-module API

Daniel-Constantin Mierla 15 years ago
parent
commit
5befde4838

+ 79 - 0
modules_k/registrar/api.c

@@ -0,0 +1,79 @@
+/*
+ * $Id$
+ *
+ * Functions that process REGISTER message 
+ * and store data in usrloc
+ *
+ * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <stdio.h>
+
+#include "../../dprint.h"
+
+#include "reg_mod.h"
+#include "lookup.h"
+#include "save.h"
+#include "api.h"
+
+/**
+ *
+ */
+int regapi_save(struct sip_msg *msg, char *table, int flags)
+{
+	udomain_t* d;
+
+	if(ul.get_udomain(table, &d)<0)
+	{
+		LM_ERR("usrloc domain [%s] not found\n", table);
+		return -1;
+	}
+	return save(msg, d, flags);
+}
+
+/**
+ *
+ */
+int regapi_lookup(struct sip_msg *msg, char *table)
+{
+	udomain_t* d;
+
+	if(ul.get_udomain(table, &d)<0)
+	{
+		LM_ERR("usrloc domain [%s] not found\n", table);
+		return -1;
+	}
+	return lookup(msg, d);
+}
+
+/**
+ *
+ */
+int bind_registrar(registrar_api_t* api)
+{
+	if (!api) {
+		ERR("Invalid parameter value\n");
+		return -1;
+	}
+	api->save   = regapi_save;
+	api->lookup = regapi_lookup;
+
+	return 0;
+}

+ 72 - 0
modules_k/registrar/api.h

@@ -0,0 +1,72 @@
+/*
+ * $Id$
+ *
+ * Functions that process REGISTER message 
+ * and store data in usrloc
+ *
+ * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+
+#ifndef _REGISTRAR_API_H_
+#define _REGISTRAR_API_H_
+
+#include "../../sr_module.h"
+#include "../../parser/msg_parser.h"
+
+typedef int (*regapi_save_f)(struct sip_msg *msg, char *table, int flags);
+int regapi_save(struct sip_msg *msg, char *table, int flags);
+
+typedef int (*regapi_lookup_f)(struct sip_msg *msg, char *table);
+int regapi_lookup(struct sip_msg *msg, char *table);
+
+/**
+ * @brief REGISTRAR API structure
+ */
+typedef struct registrar_api {
+	regapi_save_f    save;
+	regapi_lookup_f  lookup;
+} registrar_api_t;
+
+typedef int (*bind_registrar_f)(registrar_api_t* api);
+int bind_registrar(registrar_api_t* api);
+
+/**
+ * @brief Load the REGISTRAR API
+ */
+static inline int registrar_load_api(registrar_api_t *api)
+{
+	bind_registrar_f bindregistrar;
+
+	bindregistrar = (bind_registrar_f)find_export("bind_registrar", 0, 0);
+	if(bindregistrar == 0) {
+		LM_ERR("cannot find bind_registrar\n");
+		return -1;
+	}
+	if (bindregistrar(api) < 0)
+	{
+		LM_ERR("cannot bind registrar api\n");
+		return -1;
+	}
+	return 0;
+}
+
+
+#endif

+ 5 - 5
modules_k/registrar/lookup.c

@@ -57,7 +57,7 @@
  *          -2 : found but method not allowed
  *          -3 : error
  */
-int lookup(struct sip_msg* _m, char* _t, char* _s)
+int lookup(struct sip_msg* _m, udomain_t* _d)
 {
 	urecord_t* r;
 	str aor, uri;
@@ -78,11 +78,11 @@ int lookup(struct sip_msg* _m, char* _t, char* _s)
 	
 	get_act_time();
 
-	ul.lock_udomain((udomain_t*)_t, &aor);
-	res = ul.get_urecord((udomain_t*)_t, &aor, &r);
+	ul.lock_udomain(_d, &aor);
+	res = ul.get_urecord(_d, &aor, &r);
 	if (res > 0) {
 		LM_DBG("'%.*s' Not found in usrloc\n", aor.len, ZSW(aor.s));
-		ul.unlock_udomain((udomain_t*)_t, &aor);
+		ul.unlock_udomain(_d, &aor);
 		return -1;
 	}
 
@@ -171,7 +171,7 @@ int lookup(struct sip_msg* _m, char* _t, char* _s)
 
 done:
 	ul.release_urecord(r);
-	ul.unlock_udomain((udomain_t*)_t, &aor);
+	ul.unlock_udomain(_d, &aor);
 	return ret;
 }
 

+ 2 - 1
modules_k/registrar/lookup.h

@@ -33,12 +33,13 @@
 #define LOOKUP_H
 
 #include "../../parser/msg_parser.h"
+#include "../../modules_k/usrloc/usrloc.h"
 
 
 /*! \brief
  * Lookup a contact in usrloc and rewrite R-URI if found
  */
-int lookup(struct sip_msg* _m, char* _t, char* _s);
+int lookup(struct sip_msg* _m, udomain_t* _d);
 
 
 /*! \brief

+ 25 - 4
modules_k/registrar/reg_mod.c

@@ -67,11 +67,12 @@
 #include "../../socket_info.h"
 #include "../../pvar.h"
 #include "../../lib/kcore/km_ut.h"
-#include "../usrloc/ul_mod.h"
+#include "../../modules_k/usrloc/usrloc.h"
 #include "../../modules/sl/sl.h"
 #include "../../mod_fix.h"
 
 #include "save.h"
+#include "api.h"
 #include "lookup.h"
 #include "regpv.h"
 #include "reply.h"
@@ -86,6 +87,9 @@ usrloc_api_t ul;/*!< Structure containing pointers to usrloc functions*/
 static int  mod_init(void);
 static int  child_init(int);
 static void mod_destroy(void);
+static int w_save(struct sip_msg* _m, char* _d, char* _cflags);
+static int w_lookup(struct sip_msg* _m, char* _d, char* _p2);
+
 /*! \brief Fixup functions */
 static int domain_fixup(void** param, int param_no);
 static int save_fixup(void** param, int param_no);
@@ -150,11 +154,11 @@ static pv_export_t mod_pvs[] = {
  * Exported functions
  */
 static cmd_export_t cmds[] = {
-	{"save",         (cmd_function)save,         1,    save_fixup, 0,
+	{"save",         (cmd_function)w_save,       1,    save_fixup, 0,
 			REQUEST_ROUTE | ONREPLY_ROUTE },
-	{"save",         (cmd_function)save,         2,    save_fixup, 0,
+	{"save",         (cmd_function)w_save,       2,    save_fixup, 0,
 			REQUEST_ROUTE | ONREPLY_ROUTE },
-	{"lookup",       (cmd_function)lookup,       1,  domain_fixup, 0,
+	{"lookup",       (cmd_function)w_lookup,     1,  domain_fixup, 0,
 			REQUEST_ROUTE | FAILURE_ROUTE },
 	{"registered",   (cmd_function)registered,   1,  domain_fixup, 0,
 			REQUEST_ROUTE | FAILURE_ROUTE },
@@ -168,6 +172,8 @@ static cmd_export_t cmds[] = {
 	{"reg_free_contacts", (cmd_function)pv_free_contacts,   1,
 			fixup_str_null, 0,
 			REQUEST_ROUTE| FAILURE_ROUTE },
+	{"bind_registrar",  (cmd_function)bind_registrar,  0,
+		0, 0, 0},
 	{0, 0, 0, 0, 0, 0}
 };
 
@@ -383,6 +389,21 @@ static int child_init(int rank)
 	return 0;
 }
 
+/*! \brief
+ * Wrapper to save(location)
+ */
+static int w_save(struct sip_msg* _m, char* _d, char* _cflags)
+{
+	return save(_m, (udomain_t*)_d, ((int)(unsigned long)_cflags));
+}
+
+/*! \brief
+ * Wrapper to lookup(location)
+ */
+static int w_lookup(struct sip_msg* _m, char* _d, char* _p2)
+{
+	return lookup(_m, (udomain_t*)_d);
+}
 
 /*! \brief
  * Convert char* parameter to udomain_t* pointer

+ 2 - 2
modules_k/registrar/save.c

@@ -741,8 +741,8 @@ static inline int add_contacts(struct sip_msg* _m, contact_t* _c,
 /*!\brief
  * Process REGISTER request and save it's contacts
  */
-#define is_cflag_set(_name) (((unsigned int)(unsigned long)_cflags)&(_name))
-int save(struct sip_msg* _m, char* _d, char* _cflags)
+#define is_cflag_set(_name) (((unsigned int)_cflags)&(_name))
+int save(struct sip_msg* _m, udomain_t* _d, int _cflags)
 {
 	contact_t* c;
 	int st, mode;

+ 2 - 1
modules_k/registrar/save.h

@@ -40,12 +40,13 @@
 
 
 #include "../../parser/msg_parser.h"
+#include "../../modules_k/usrloc/usrloc.h"
 
 
 /*! \brief
  * Process REGISTER request and save it's contacts
  */
-int save(struct sip_msg* _m, char* _d, char* _cflags);
+int save(struct sip_msg* _m, udomain_t* _d, int _cflags);
 int unregister(struct sip_msg* _m, char* _d, char* _uri);