1 /*
2  * rtpproxy module
3  *
4  * Copyright (c) 2013 Crocodile RCS Ltd
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 
24 #include "../../lib/srdb1/db.h"
25 #include "../../lib/srdb1/db_res.h"
26 
27 #include "../../core/parser/msg_parser.h"
28 #include "rtpproxy.h"
29 
30 #define RTPP_TABLE_VERSION 1
31 
32 static db_func_t rtpp_dbf;
33 static db1_con_t *rtpp_db_handle = NULL;
34 
35 str rtpp_db_url = {NULL, 0};
36 str rtpp_table_name = str_init("rtpproxy");
37 str rtpp_setid_col = str_init("setid");
38 str rtpp_url_col = str_init("url");
39 str rtpp_weight_col = str_init("weight");
40 str rtpp_flags_col = str_init("flags");
41 
rtpp_connect_db(void)42 static int rtpp_connect_db(void)
43 {
44 	if ((rtpp_db_url.s == NULL) || (rtpp_db_url.len == 0))
45 		return -1;
46 	if ((rtpp_db_handle = rtpp_dbf.init(&rtpp_db_url)) == NULL)
47 	{
48 		LM_ERR("Cannot initialize db connection\n");
49 		return -1;
50 	}
51 	return 0;
52 }
53 
rtpp_disconnect_db(void)54 static void rtpp_disconnect_db(void)
55 {
56 	if (rtpp_db_handle)
57 	{
58 		rtpp_dbf.close(rtpp_db_handle);
59 		rtpp_db_handle = NULL;
60 	}
61 }
62 
rtpp_load_db(void)63 static int rtpp_load_db(void)
64 {
65 	int i;
66 	struct rtpp_set *rtpp_list = NULL;
67 	db1_res_t *res = NULL;
68 	db_val_t *values = NULL;
69 	db_row_t *rows = NULL;
70 	db_key_t query_cols[] = {&rtpp_setid_col, &rtpp_url_col, &rtpp_weight_col,
71 		&rtpp_flags_col};
72 
73 	str set, url;
74 	int weight, flags;
75 	int n_rows = 0;
76 	int n_cols = 4;
77 
78 	if (rtpp_db_handle == NULL)
79 	{
80 		LM_ERR("invalid db handle\n");
81 		return -1;
82 	}
83 	if (rtpp_dbf.use_table(rtpp_db_handle, &rtpp_table_name) < 0)
84 	{
85 		LM_ERR("unable to use table '%.*s'\n", rtpp_table_name.len,
86 				rtpp_table_name.s);
87 		return -1;
88 	}
89 	if (rtpp_dbf.query(rtpp_db_handle, 0, 0, 0, query_cols, 0, n_cols, 0, &res) < 0)
90 	{
91 		LM_ERR("error while running db query\n");
92 		return -1;
93 	}
94 
95 	n_rows = RES_ROW_N(res);
96 	rows = RES_ROWS(res);
97 	if (n_rows == 0)
98 	{
99 		LM_WARN("No rtpproxy instances in database\n");
100 		return 0;
101 	}
102 	for (i=0; i<n_rows; i++)
103 	{
104 		values = ROW_VALUES(rows + i);
105 
106 		set.s = VAL_STR(values).s;
107 		set.len = strlen(set.s);
108 		url.s = VAL_STR(values+1).s;
109 		url.len = strlen(url.s);
110 		weight = VAL_INT(values+2);
111 		flags = VAL_INT(values+3);
112 
113 		if ((rtpp_list = get_rtpp_set(&set)) == NULL)
114 		{
115 			LM_ERR("error getting rtpp_list for set '%.*s'\n", set.len, set.s);
116 			continue;
117 		}
118 		if (insert_rtpp_node(rtpp_list, &url, weight, flags) < 0)
119 		{
120 			LM_ERR("error inserting '%.*s' into set '%.*s'\n", url.len, url.s,
121 					set.len, set.s);
122 		}
123 	}
124 
125 	rtpp_dbf.free_result(rtpp_db_handle, res);
126 	return 0;
127 }
128 
init_rtpproxy_db(void)129 int init_rtpproxy_db(void)
130 {
131 	int ret;
132 	if (rtpp_db_url.s == NULL)
133 		/* Database not configured */
134 		return 0;
135 
136 	if (db_bind_mod(&rtpp_db_url, &rtpp_dbf) < 0)
137 	{
138 		LM_ERR("Unable to bind to db driver - %.*s\n",
139 				rtpp_db_url.len, rtpp_db_url.s);
140 		return -1;
141 	}
142 	if (rtpp_connect_db() != 0)
143 	{
144 		LM_ERR("Unable to connect to db\n");
145 		return -1;
146 	}
147 
148 	if (db_check_table_version(&rtpp_dbf, rtpp_db_handle, &rtpp_table_name, RTPP_TABLE_VERSION) < 0)
149 	{
150 		DB_TABLE_VERSION_ERROR(rtpp_table_name);
151 		ret = -1;
152 		goto done;
153 	}
154 	ret = rtpp_load_db();
155 
156 done:
157 	rtpp_disconnect_db();
158 
159 	return ret;
160 }
161