Browse code

rtpproxy: convert module to use DB_TABLE_VERSION_ERROR helper

- convert module to use DB_TABLE_VERSION_ERROR helper
- unify error handling (properly close database on errors, null db handle)
- use abstract db_table_version_error function

Henning Westerholt authored on 01/01/2019 21:01:47
Showing 1 changed files
... ...
@@ -129,7 +129,6 @@ static int rtpp_load_db(void)
129 129
 int init_rtpproxy_db(void)
130 130
 {
131 131
 	int ret;
132
-	int rtpp_table_version;
133 132
 	if (rtpp_db_url.s == NULL)
134 133
 		/* Database not configured */
135 134
 		return 0;
... ...
@@ -146,22 +145,12 @@ int init_rtpproxy_db(void)
146 145
 		return -1;
147 146
 	}
148 147
 
149
-	rtpp_table_version = db_table_version(&rtpp_dbf, rtpp_db_handle, &rtpp_table_name);
150
-	if (rtpp_table_version < 0)
148
+	if (db_check_table_version(&rtpp_dbf, rtpp_db_handle, &rtpp_table_name, RTPP_TABLE_VERSION) < 0)
151 149
 	{
152
-		LM_ERR("failed to get rtpp table version\n");
150
+		DB_TABLE_VERSION_ERROR(rtpp_table_name);
153 151
 		ret = -1;
154 152
 		goto done;
155 153
 	}
156
-	switch (rtpp_table_version) {
157
-		case RTPP_TABLE_VERSION:
158
-			break;
159
-		default:
160
-			LM_ERR("invalid table version (found %d, require %d)\n",
161
-					rtpp_table_version, RTPP_TABLE_VERSION);
162
-			ret = -1;
163
-			goto done;
164
-	}
165 154
 	ret = rtpp_load_db();
166 155
 
167 156
 done:
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -24,7 +24,7 @@
24 24
 #include "../../lib/srdb1/db.h"
25 25
 #include "../../lib/srdb1/db_res.h"
26 26
 
27
-#include "../../parser/msg_parser.h"
27
+#include "../../core/parser/msg_parser.h"
28 28
 #include "rtpproxy.h"
29 29
 
30 30
 #define RTPP_TABLE_VERSION 1
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,171 @@
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 "../../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
+
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
+
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
+
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
+
129
+int init_rtpproxy_db(void)
130
+{
131
+	int ret;
132
+	int rtpp_table_version;
133
+	if (rtpp_db_url.s == NULL)
134
+		/* Database not configured */
135
+		return 0;
136
+
137
+	if (db_bind_mod(&rtpp_db_url, &rtpp_dbf) < 0)
138
+	{
139
+		LM_ERR("Unable to bind to db driver - %.*s\n",
140
+				rtpp_db_url.len, rtpp_db_url.s);
141
+		return -1;
142
+	}
143
+	if (rtpp_connect_db() != 0)
144
+	{
145
+		LM_ERR("Unable to connect to db\n");
146
+		return -1;
147
+	}
148
+
149
+	rtpp_table_version = db_table_version(&rtpp_dbf, rtpp_db_handle, &rtpp_table_name);
150
+	if (rtpp_table_version < 0)
151
+	{
152
+		LM_ERR("failed to get rtpp table version\n");
153
+		ret = -1;
154
+		goto done;
155
+	}
156
+	switch (rtpp_table_version) {
157
+		case RTPP_TABLE_VERSION:
158
+			break;
159
+		default:
160
+			LM_ERR("invalid table version (found %d, require %d)\n",
161
+					rtpp_table_version, RTPP_TABLE_VERSION);
162
+			ret = -1;
163
+			goto done;
164
+	}
165
+	ret = rtpp_load_db();
166
+
167
+done:
168
+	rtpp_disconnect_db();
169
+
170
+	return ret;
171
+}