File: rtpengine_db.c

package info (click to toggle)
kamailio 4.2.0-2%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (166 lines) | stat: -rw-r--r-- 3,774 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * rtpproxy module
 *
 * Copyright (c) 2013 Crocodile RCS Ltd
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "../../lib/srdb1/db.h"
#include "../../lib/srdb1/db_res.h"

#include "rtpengine.h"

#define RTPP_TABLE_VERSION 1

static db_func_t rtpp_dbf;
static db1_con_t *rtpp_db_handle = NULL;

str rtpp_db_url = {NULL, 0};
str rtpp_table_name = str_init("rtpproxy");
str rtpp_set_id_col = str_init("set_id");
str rtpp_url_col = str_init("url");

static int rtpp_connect_db(void)
{
	if ((rtpp_db_url.s == NULL) || (rtpp_db_url.len == 0))
		return -1;
	if ((rtpp_db_handle = rtpp_dbf.init(&rtpp_db_url)) == NULL)
	{
		LM_ERR("Cannot initialize db connection\n");
		return -1;
	}
	return 0;
}

static void rtpp_disconnect_db(void)
{
	if (rtpp_db_handle)
	{
		rtpp_dbf.close(rtpp_db_handle);
		rtpp_db_handle = NULL;
	}
}

static int rtpp_load_db(void)
{
	int i;
	struct rtpp_set *rtpp_list = NULL;
	db1_res_t *res = NULL;
	db_val_t *values = NULL;
	db_row_t *rows = NULL;
	db_key_t query_cols[] = {&rtpp_set_id_col, &rtpp_url_col};

	str url;
	int set_id;
	/* int weight, flags; */
	int n_rows = 0;
	int n_cols = 2;

	if (rtpp_db_handle == NULL)
	{
		LM_ERR("invalid db handle\n");
		return -1;
	}
	if (rtpp_dbf.use_table(rtpp_db_handle, &rtpp_table_name) < 0)
	{
		LM_ERR("unable to use table '%.*s'\n", rtpp_table_name.len, rtpp_table_name.s);
		return -1;
	}
	if (rtpp_dbf.query(rtpp_db_handle, 0, 0, 0, query_cols, 0, n_cols, 0, &res) < 0)
	{
		LM_ERR("error while running db query\n");
		return -1;
	}

	n_rows = RES_ROW_N(res);
	rows = RES_ROWS(res);
	if (n_rows == 0)
	{
		LM_WARN("No rtpproxy instances in database\n");
		return 0;
	}
	for (i=0; i<n_rows; i++)
	{
		values = ROW_VALUES(rows + i);

		set_id = VAL_INT(values);
		url.s = VAL_STR(values+1).s;
		url.len = strlen(url.s);
		/*
		weight = VAL_INT(values+2);
		flags = VAL_INT(values+3);
		*/
		if ((rtpp_list = get_rtpp_set(set_id)) == NULL)
		{
			LM_ERR("error getting rtpp_list for set %d\n", set_id);
			continue;
		}

		if (add_rtpengine_socks(rtpp_list, url.s) !=  0)
		{
			LM_ERR("error inserting '%.*s' into set %d\n", url.len, url.s, set_id);
		}
	}

	rtpp_dbf.free_result(rtpp_db_handle, res);
	return 0;
}

int init_rtpproxy_db(void)
{
	int ret;
	int rtpp_table_version;
	if (rtpp_db_url.s == NULL)
		/* Database not configured */
		return 0;

	if (db_bind_mod(&rtpp_db_url, &rtpp_dbf) < 0)
	{
		LM_ERR("Unable to bind to db driver - %.*s\n", rtpp_db_url.len, rtpp_db_url.s);
		return -1;
	}
	if (rtpp_connect_db() != 0)
	{
		LM_ERR("Unable to connect to db\n");
		return -1;
	}

	rtpp_table_version = db_table_version(&rtpp_dbf, rtpp_db_handle, &rtpp_table_name);
	if (rtpp_table_version < 0)
	{
		LM_ERR("failed to get rtpp table version\n");
		ret = -1;
		goto done;
	}
	switch (rtpp_table_version) {
		case RTPP_TABLE_VERSION:
			break;
		default:
			LM_ERR("invalid table version (found %d, require %d)\n",
			  rtpp_table_version, RTPP_TABLE_VERSION);
			ret = -1;
			goto done;
	}
	ret = rtpp_load_db();

done:
	rtpp_disconnect_db();

	return ret;
}