Browse Source

lcr: added ser RPC support

- copied & adapted lcr_rpc.[ch] from the ser version of lcr.
- register the rpc array from mod_init if RPC_SUPPORT is defined
 (defined by default in lcr_rpc.h unless lcr is compiled with
  -DNO_RPC_SUPPORT).
- init db for all the children if RPC_SUPPORT is defined
 (needed for xmlrpc which does not run in a separate process)
- moved some defines, structs and typedefs from lcr_mod.c to lcr_mod.h.
Andrei Pelinescu-Onciul 16 years ago
parent
commit
826d4a56f2
4 changed files with 265 additions and 25 deletions
  1. 37 25
      modules/lcr/lcr_mod.c
  2. 27 0
      modules/lcr/lcr_mod.h
  3. 158 0
      modules/lcr/lcr_rpc.c
  4. 43 0
      modules/lcr/lcr_rpc.h

+ 37 - 25
modules/lcr/lcr_mod.c

@@ -36,6 +36,7 @@
  *  2008-10-10: Database values are now checked and from/to_gw functions
  *              execute in O(logN) time.
  *  2008-11-26: Added timer based check of gateways (shurik)
+ *  2009-05-12  added RPC support (andrei)
  */
 
 #include <stdio.h>
@@ -53,7 +54,6 @@
 #include "../../lib/srdb1/db.h"
 #include "../../lib/kcore/km_ut.h"
 #include "../../usr_avp.h"
-#include "../../parser/parse_uri.h"
 #include "../../parser/parse_from.h"
 #include "../../parser/msg_parser.h"
 #include "../../action.h"
@@ -69,6 +69,12 @@
 #include "../../mod_fix.h"
 #include "hash.h"
 #include "mi.h"
+#include "lcr_rpc.h" /* defines RPC_SUPPORT */
+#ifdef RPC_SUPPORT
+#include "../../rpc_lookup.h"
+#endif /* RPC_SUPPORT */
+
+
 
 MODULE_VERSION
 
@@ -86,6 +92,7 @@ MODULE_VERSION
 static void destroy(void);       /* Module destroy function */
 static int mi_child_init(void);
 static int mod_init(void);       /* Module initialization function */
+static int child_init(int rank); /* Per-child initialization function */
 static void free_shared_memory(void);
 static int fixstringloadgws(void **param, int param_count);
 
@@ -123,10 +130,6 @@ static int fixstringloadgws(void **param, int param_count);
 
 #define PRIORITY_COL "priority"
 
-#define MAX_NO_OF_GWS 128
-#define MAX_TAG_LEN 16
-#define MAX_HOST_LEN 64
-#define MAX_USER_LEN 64
 
 /* Default module parameter values */
 #define DEF_LCR_HASH_SIZE 128
@@ -141,25 +144,6 @@ static int fixstringloadgws(void **param, int param_count);
 /* TMB Structure */
 struct tm_binds tmb;
 
-typedef enum sip_protos uri_transport;
-
-struct gw_info {
-    unsigned int ip_addr;
-    char hostname[MAX_HOST_LEN];
-    unsigned short hostname_len;
-    unsigned int port;
-    unsigned int grp_id;
-    uri_type scheme;
-    uri_transport transport;
-    unsigned int strip;
-    char tag[MAX_TAG_LEN + 1];
-    unsigned short tag_len;
-    unsigned short weight;
-    unsigned int flags;
-    unsigned short ping;
-    unsigned int next;  /* index of next gw in the same group */
-};
-
 struct gw_grp {
     unsigned int grp_id;
     unsigned int first;   /* index to first gw of group in gw table */
@@ -362,7 +346,7 @@ struct module_exports exports = {
 	mod_init,  /* module initialization function */
 	0,         /* response function */
 	destroy,   /* destroy function */
-	0          /* child initialization function */
+	child_init /* child initialization function */
 };
 
 
@@ -429,6 +413,14 @@ static int mod_init(void)
 		LM_ERR("failed to register MI commands\n");
 		return -1;
 	}
+#ifdef RPC_SUPPORT
+	if (rpc_register_array(lcr_rpc)!=0)
+	{
+		LM_ERR("failed to register RPC commands\n");
+		return -1;
+	}
+#endif /* RPC_SUPPORT */
+
 
     /* Update length of module variables */
     db_url.len = strlen(db_url.s);
@@ -675,6 +667,26 @@ err:
 }
 
 
+/* Module initialization function called in each child separately */
+static int child_init(int rank)
+{
+#ifdef RPC_SUPPORT
+	/* do nothing for the main process, tcp main process or timer */
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN ||
+		rank==PROC_TIMER)
+		return 0;
+	/* init db for the rest of the processes:
+	   - we need it for PROC_RPC and PROC_FIFO if we want db access from
+	     RPC accessed via the ctl module
+	   - we need it from all the ser tcp or tls processes if we want
+	     db access from RPC via the xmlrpc module */
+	return lcr_db_init(&db_url);
+#else
+	return 0;
+#endif /* RPC_SUPPORT */
+}
+
+
 static void destroy(void)
 {
     lcr_db_close();

+ 27 - 0
modules/lcr/lcr_mod.h

@@ -34,9 +34,14 @@
 #include <pcre.h>
 #include "../../lib/kmi/mi.h"
 #include "../../locking.h"
+#include "../../parser/parse_uri.h"
 
 #define MAX_PREFIX_LEN 32
 #define MAX_URI_LEN 256
+#define MAX_HOST_LEN 64
+#define MAX_NO_OF_GWS 128
+#define MAX_TAG_LEN 16
+#define MAX_USER_LEN 64
 
 struct lcr_info {
     char prefix[MAX_PREFIX_LEN + 1];
@@ -50,10 +55,32 @@ struct lcr_info {
     struct lcr_info *next;
 };
 
+typedef enum sip_protos uri_transport;
+
+struct gw_info {
+    unsigned int ip_addr;
+    char hostname[MAX_HOST_LEN];
+    unsigned short hostname_len;
+    unsigned int port;
+    unsigned int grp_id;
+    uri_type scheme;
+    uri_transport transport;
+    unsigned int strip;
+    char tag[MAX_TAG_LEN + 1];
+    unsigned short tag_len;
+    unsigned short weight;
+    unsigned int flags;
+    unsigned short ping;
+    unsigned int next;  /* index of next gw in the same group */
+};
+
 extern unsigned int lcr_hash_size_param;
 
 extern gen_lock_t *reload_lock;
 
+extern struct gw_info **gws;
+extern struct lcr_info ***lcrs;
+
 int  mi_print_gws(struct mi_node* rpl);
 int  mi_print_lcrs(struct mi_node* rpl);
 int  reload_gws_and_lcrs(void);

+ 158 - 0
modules/lcr/lcr_rpc.c

@@ -0,0 +1,158 @@
+/*
+ * Various lcr related functions
+ *
+ * Copyright (C) 2005 Juha Heinanen
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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 "lcr_rpc.h"
+
+#ifdef RPC_SUPPORT
+
+#include "lcr_mod.h"
+#include "../../ip_addr.h"
+
+
+static const char* reload_doc[2] = {
+	"Reload gateway table from database.",
+	0
+};
+
+
+static void reload(rpc_t* rpc, void* c)
+{
+	lock_get(reload_lock);
+	if (reload_gws_and_lcrs() !=1)
+		rpc->fault(c, 500, "LCR Gateway Reload Failed");
+	lock_release(reload_lock);
+}
+
+
+static const char* dump_gws_doc[2] = {
+	"Dump the contents of the gateway table.",
+	0
+};
+
+
+static void dump_gws(rpc_t* rpc, void* c)
+{
+	void* st;
+	unsigned int i;
+	enum sip_protos transport;
+	str hostname;
+	str tag;
+	
+	for (i = 0; i <= (*gws)[0].ip_addr; i++) {
+		if (rpc->add(c, "{", &st) < 0) return;
+		rpc->struct_add(st, "d", "grp_id", (*gws)[i].grp_id);
+		rpc->struct_printf(st,   "ip_addr", "%d.%d.%d.%d",
+								((*gws)[i].ip_addr << 24) >> 24,
+								(((*gws)[i].ip_addr >> 8) << 24) >> 24,
+								(((*gws)[i].ip_addr >> 16) << 24) >> 24,
+								(*gws)[i].ip_addr >> 24);
+		hostname.s=(*gws)[i].hostname;
+		hostname.len=(*gws)[i].hostname_len;
+		rpc->struct_add(st, "S", "hostname", &hostname);
+		if  ((*gws)[i].port > 0)
+			rpc->struct_add(st, "d", "port", (*gws)[i].port);
+		if ((*gws)[i].scheme == SIP_URI_T) {
+		    rpc->struct_add(st, "s", "scheme", "sip");
+		} else {
+		    rpc->struct_add(st, "s", "scheme", "sips");
+		}
+		transport = (*gws)[i].transport;
+		switch(transport){
+			case PROTO_UDP:
+				rpc->struct_add(st, "s", "transport", "UDP");
+				break;
+			case PROTO_TCP:
+				rpc->struct_add(st, "s", "transport", "TCP");
+				break;
+			case PROTO_TLS:
+				rpc->struct_add(st, "s", "transport", "TLS");
+				break;
+			case PROTO_SCTP:
+				rpc->struct_add(st, "s", "transport", "SCTP");
+				break;
+			case PROTO_NONE:
+				break;
+		}
+		tag.s=(*gws)[i].tag;
+		tag.len=(*gws)[i].tag_len;
+		rpc->struct_add(st, "dSddd", "strip",  (*gws)[i].strip,
+									 "tag",    (*gws)[i].tag, /* FIXME */
+									 "weight", (*gws)[i].weight,
+									 "flags",  &tag,
+									 "ping",   (*gws)[i].ping
+									 );
+	}
+}
+
+
+
+static const char* dump_lcr_doc[2] = {
+	"Dump the contents of the lcr table.",
+	0
+};
+
+
+static void dump_lcr(rpc_t* rpc, void* c)
+{
+	int i;
+	struct lcr_info* lcr_rec;
+	void* st;
+	str prefix, from_uri;
+	
+	for (i=0; i < lcr_hash_size_param; i++){
+		lcr_rec=(*lcrs)[i];
+		while(lcr_rec){
+			if (rpc->add(c, "{", &st) < 0) return;
+			prefix.s=lcr_rec->prefix;
+			prefix.len=lcr_rec->prefix_len;
+			from_uri.s=lcr_rec->from_uri;
+			from_uri.len=lcr_rec->from_uri_len;
+			rpc->struct_add(st, "SSdd",	"prefix",	&prefix,
+										"from_uri",	&from_uri,
+										"grp_id",	lcr_rec->grp_id,
+										"priority",	lcr_rec->priority
+							);
+			lcr_rec=lcr_rec->next;
+		}
+	}
+	lcr_rec=(*lcrs)[lcr_hash_size_param];
+	while(lcr_rec){
+		rpc->add(c, "d", lcr_rec->prefix_len);
+		lcr_rec=lcr_rec->next;
+	}
+}
+
+
+
+rpc_export_t lcr_rpc[] = {
+	{"lcr.reload", reload, reload_doc, 0},
+	{"lcr.dump_gws",   dump_gws,   dump_gws_doc,   0},
+	{"lcr.dump_lcr",   dump_lcr,   dump_lcr_doc,   0},
+	{0, 0, 0, 0}
+};
+
+#endif /* RPC_SUPPORT */

+ 43 - 0
modules/lcr/lcr_rpc.h

@@ -0,0 +1,43 @@
+/*
+ * Various lcr related functions
+ *
+ * Copyright (C) 2005 Juha Heinanen
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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 _LCR_RPC_H
+#define _LCR_RPC_H
+
+#ifndef NO_RPC_SUPPORT
+#define RPC_SUPPORT /* support ser RPCs by default */
+#endif
+
+#ifdef RPC_SUPPORT
+
+#include "../../rpc.h"
+
+extern rpc_export_t lcr_rpc[];
+
+#endif /* RPC_SUPPORT */
+
+#endif /* _LCR_RPC_H */