Browse code

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 authored on 13/05/2009 11:52:51
Showing 4 changed files
... ...
@@ -36,6 +36,7 @@
36 36
  *  2008-10-10: Database values are now checked and from/to_gw functions
37 37
  *              execute in O(logN) time.
38 38
  *  2008-11-26: Added timer based check of gateways (shurik)
39
+ *  2009-05-12  added RPC support (andrei)
39 40
  */
40 41
 
41 42
 #include <stdio.h>
... ...
@@ -53,7 +54,6 @@
53 53
 #include "../../lib/srdb1/db.h"
54 54
 #include "../../lib/kcore/km_ut.h"
55 55
 #include "../../usr_avp.h"
56
-#include "../../parser/parse_uri.h"
57 56
 #include "../../parser/parse_from.h"
58 57
 #include "../../parser/msg_parser.h"
59 58
 #include "../../action.h"
... ...
@@ -69,6 +69,12 @@
69 69
 #include "../../mod_fix.h"
70 70
 #include "hash.h"
71 71
 #include "mi.h"
72
+#include "lcr_rpc.h" /* defines RPC_SUPPORT */
73
+#ifdef RPC_SUPPORT
74
+#include "../../rpc_lookup.h"
75
+#endif /* RPC_SUPPORT */
76
+
77
+
72 78
 
73 79
 MODULE_VERSION
74 80
 
... ...
@@ -86,6 +92,7 @@ MODULE_VERSION
86 86
 static void destroy(void);       /* Module destroy function */
87 87
 static int mi_child_init(void);
88 88
 static int mod_init(void);       /* Module initialization function */
89
+static int child_init(int rank); /* Per-child initialization function */
89 90
 static void free_shared_memory(void);
90 91
 static int fixstringloadgws(void **param, int param_count);
91 92
 
... ...
@@ -123,10 +130,6 @@ static int fixstringloadgws(void **param, int param_count);
123 123
 
124 124
 #define PRIORITY_COL "priority"
125 125
 
126
-#define MAX_NO_OF_GWS 128
127
-#define MAX_TAG_LEN 16
128
-#define MAX_HOST_LEN 64
129
-#define MAX_USER_LEN 64
130 126
 
131 127
 /* Default module parameter values */
132 128
 #define DEF_LCR_HASH_SIZE 128
... ...
@@ -141,25 +144,6 @@ static int fixstringloadgws(void **param, int param_count);
141 141
 /* TMB Structure */
142 142
 struct tm_binds tmb;
143 143
 
144
-typedef enum sip_protos uri_transport;
145
-
146
-struct gw_info {
147
-    unsigned int ip_addr;
148
-    char hostname[MAX_HOST_LEN];
149
-    unsigned short hostname_len;
150
-    unsigned int port;
151
-    unsigned int grp_id;
152
-    uri_type scheme;
153
-    uri_transport transport;
154
-    unsigned int strip;
155
-    char tag[MAX_TAG_LEN + 1];
156
-    unsigned short tag_len;
157
-    unsigned short weight;
158
-    unsigned int flags;
159
-    unsigned short ping;
160
-    unsigned int next;  /* index of next gw in the same group */
161
-};
162
-
163 144
 struct gw_grp {
164 145
     unsigned int grp_id;
165 146
     unsigned int first;   /* index to first gw of group in gw table */
... ...
@@ -362,7 +346,7 @@ struct module_exports exports = {
362 362
 	mod_init,  /* module initialization function */
363 363
 	0,         /* response function */
364 364
 	destroy,   /* destroy function */
365
-	0          /* child initialization function */
365
+	child_init /* child initialization function */
366 366
 };
367 367
 
368 368
 
... ...
@@ -429,6 +413,14 @@ static int mod_init(void)
429 429
 		LM_ERR("failed to register MI commands\n");
430 430
 		return -1;
431 431
 	}
432
+#ifdef RPC_SUPPORT
433
+	if (rpc_register_array(lcr_rpc)!=0)
434
+	{
435
+		LM_ERR("failed to register RPC commands\n");
436
+		return -1;
437
+	}
438
+#endif /* RPC_SUPPORT */
439
+
432 440
 
433 441
     /* Update length of module variables */
434 442
     db_url.len = strlen(db_url.s);
... ...
@@ -675,6 +667,26 @@ err:
675 675
 }
676 676
 
677 677
 
678
+/* Module initialization function called in each child separately */
679
+static int child_init(int rank)
680
+{
681
+#ifdef RPC_SUPPORT
682
+	/* do nothing for the main process, tcp main process or timer */
683
+	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN ||
684
+		rank==PROC_TIMER)
685
+		return 0;
686
+	/* init db for the rest of the processes:
687
+	   - we need it for PROC_RPC and PROC_FIFO if we want db access from
688
+	     RPC accessed via the ctl module
689
+	   - we need it from all the ser tcp or tls processes if we want
690
+	     db access from RPC via the xmlrpc module */
691
+	return lcr_db_init(&db_url);
692
+#else
693
+	return 0;
694
+#endif /* RPC_SUPPORT */
695
+}
696
+
697
+
678 698
 static void destroy(void)
679 699
 {
680 700
     lcr_db_close();
... ...
@@ -34,9 +34,14 @@
34 34
 #include <pcre.h>
35 35
 #include "../../lib/kmi/mi.h"
36 36
 #include "../../locking.h"
37
+#include "../../parser/parse_uri.h"
37 38
 
38 39
 #define MAX_PREFIX_LEN 32
39 40
 #define MAX_URI_LEN 256
41
+#define MAX_HOST_LEN 64
42
+#define MAX_NO_OF_GWS 128
43
+#define MAX_TAG_LEN 16
44
+#define MAX_USER_LEN 64
40 45
 
41 46
 struct lcr_info {
42 47
     char prefix[MAX_PREFIX_LEN + 1];
... ...
@@ -50,10 +55,32 @@ struct lcr_info {
50 50
     struct lcr_info *next;
51 51
 };
52 52
 
53
+typedef enum sip_protos uri_transport;
54
+
55
+struct gw_info {
56
+    unsigned int ip_addr;
57
+    char hostname[MAX_HOST_LEN];
58
+    unsigned short hostname_len;
59
+    unsigned int port;
60
+    unsigned int grp_id;
61
+    uri_type scheme;
62
+    uri_transport transport;
63
+    unsigned int strip;
64
+    char tag[MAX_TAG_LEN + 1];
65
+    unsigned short tag_len;
66
+    unsigned short weight;
67
+    unsigned int flags;
68
+    unsigned short ping;
69
+    unsigned int next;  /* index of next gw in the same group */
70
+};
71
+
53 72
 extern unsigned int lcr_hash_size_param;
54 73
 
55 74
 extern gen_lock_t *reload_lock;
56 75
 
76
+extern struct gw_info **gws;
77
+extern struct lcr_info ***lcrs;
78
+
57 79
 int  mi_print_gws(struct mi_node* rpl);
58 80
 int  mi_print_lcrs(struct mi_node* rpl);
59 81
 int  reload_gws_and_lcrs(void);
60 82
new file mode 100644
... ...
@@ -0,0 +1,158 @@
0
+/*
1
+ * Various lcr related functions
2
+ *
3
+ * Copyright (C) 2005 Juha Heinanen
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+
27
+#include "lcr_rpc.h"
28
+
29
+#ifdef RPC_SUPPORT
30
+
31
+#include "lcr_mod.h"
32
+#include "../../ip_addr.h"
33
+
34
+
35
+static const char* reload_doc[2] = {
36
+	"Reload gateway table from database.",
37
+	0
38
+};
39
+
40
+
41
+static void reload(rpc_t* rpc, void* c)
42
+{
43
+	lock_get(reload_lock);
44
+	if (reload_gws_and_lcrs() !=1)
45
+		rpc->fault(c, 500, "LCR Gateway Reload Failed");
46
+	lock_release(reload_lock);
47
+}
48
+
49
+
50
+static const char* dump_gws_doc[2] = {
51
+	"Dump the contents of the gateway table.",
52
+	0
53
+};
54
+
55
+
56
+static void dump_gws(rpc_t* rpc, void* c)
57
+{
58
+	void* st;
59
+	unsigned int i;
60
+	enum sip_protos transport;
61
+	str hostname;
62
+	str tag;
63
+	
64
+	for (i = 0; i <= (*gws)[0].ip_addr; i++) {
65
+		if (rpc->add(c, "{", &st) < 0) return;
66
+		rpc->struct_add(st, "d", "grp_id", (*gws)[i].grp_id);
67
+		rpc->struct_printf(st,   "ip_addr", "%d.%d.%d.%d",
68
+								((*gws)[i].ip_addr << 24) >> 24,
69
+								(((*gws)[i].ip_addr >> 8) << 24) >> 24,
70
+								(((*gws)[i].ip_addr >> 16) << 24) >> 24,
71
+								(*gws)[i].ip_addr >> 24);
72
+		hostname.s=(*gws)[i].hostname;
73
+		hostname.len=(*gws)[i].hostname_len;
74
+		rpc->struct_add(st, "S", "hostname", &hostname);
75
+		if  ((*gws)[i].port > 0)
76
+			rpc->struct_add(st, "d", "port", (*gws)[i].port);
77
+		if ((*gws)[i].scheme == SIP_URI_T) {
78
+		    rpc->struct_add(st, "s", "scheme", "sip");
79
+		} else {
80
+		    rpc->struct_add(st, "s", "scheme", "sips");
81
+		}
82
+		transport = (*gws)[i].transport;
83
+		switch(transport){
84
+			case PROTO_UDP:
85
+				rpc->struct_add(st, "s", "transport", "UDP");
86
+				break;
87
+			case PROTO_TCP:
88
+				rpc->struct_add(st, "s", "transport", "TCP");
89
+				break;
90
+			case PROTO_TLS:
91
+				rpc->struct_add(st, "s", "transport", "TLS");
92
+				break;
93
+			case PROTO_SCTP:
94
+				rpc->struct_add(st, "s", "transport", "SCTP");
95
+				break;
96
+			case PROTO_NONE:
97
+				break;
98
+		}
99
+		tag.s=(*gws)[i].tag;
100
+		tag.len=(*gws)[i].tag_len;
101
+		rpc->struct_add(st, "dSddd", "strip",  (*gws)[i].strip,
102
+									 "tag",    (*gws)[i].tag, /* FIXME */
103
+									 "weight", (*gws)[i].weight,
104
+									 "flags",  &tag,
105
+									 "ping",   (*gws)[i].ping
106
+									 );
107
+	}
108
+}
109
+
110
+
111
+
112
+static const char* dump_lcr_doc[2] = {
113
+	"Dump the contents of the lcr table.",
114
+	0
115
+};
116
+
117
+
118
+static void dump_lcr(rpc_t* rpc, void* c)
119
+{
120
+	int i;
121
+	struct lcr_info* lcr_rec;
122
+	void* st;
123
+	str prefix, from_uri;
124
+	
125
+	for (i=0; i < lcr_hash_size_param; i++){
126
+		lcr_rec=(*lcrs)[i];
127
+		while(lcr_rec){
128
+			if (rpc->add(c, "{", &st) < 0) return;
129
+			prefix.s=lcr_rec->prefix;
130
+			prefix.len=lcr_rec->prefix_len;
131
+			from_uri.s=lcr_rec->from_uri;
132
+			from_uri.len=lcr_rec->from_uri_len;
133
+			rpc->struct_add(st, "SSdd",	"prefix",	&prefix,
134
+										"from_uri",	&from_uri,
135
+										"grp_id",	lcr_rec->grp_id,
136
+										"priority",	lcr_rec->priority
137
+							);
138
+			lcr_rec=lcr_rec->next;
139
+		}
140
+	}
141
+	lcr_rec=(*lcrs)[lcr_hash_size_param];
142
+	while(lcr_rec){
143
+		rpc->add(c, "d", lcr_rec->prefix_len);
144
+		lcr_rec=lcr_rec->next;
145
+	}
146
+}
147
+
148
+
149
+
150
+rpc_export_t lcr_rpc[] = {
151
+	{"lcr.reload", reload, reload_doc, 0},
152
+	{"lcr.dump_gws",   dump_gws,   dump_gws_doc,   0},
153
+	{"lcr.dump_lcr",   dump_lcr,   dump_lcr_doc,   0},
154
+	{0, 0, 0, 0}
155
+};
156
+
157
+#endif /* RPC_SUPPORT */
0 158
new file mode 100644
... ...
@@ -0,0 +1,43 @@
0
+/*
1
+ * Various lcr related functions
2
+ *
3
+ * Copyright (C) 2005 Juha Heinanen
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+
27
+#ifndef _LCR_RPC_H
28
+#define _LCR_RPC_H
29
+
30
+#ifndef NO_RPC_SUPPORT
31
+#define RPC_SUPPORT /* support ser RPCs by default */
32
+#endif
33
+
34
+#ifdef RPC_SUPPORT
35
+
36
+#include "../../rpc.h"
37
+
38
+extern rpc_export_t lcr_rpc[];
39
+
40
+#endif /* RPC_SUPPORT */
41
+
42
+#endif /* _LCR_RPC_H */