Browse code

ims_charging: fix missing line-breaks for log messages

Henning Westerholt authored on 10/06/2020 11:50:51
Showing 1 changed files
... ...
@@ -125,7 +125,7 @@ dberror:
125 125
 }
126 126
 
127 127
 int load_ro_info_from_db(int hash_size, int fetch_num_rows) {
128
-    LM_WARN("not supported yet");
128
+    LM_WARN("not supported yet\n");
129 129
     return 0;
130 130
 }
131 131
 
Browse code

ims_charging: 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)

Henning Westerholt authored on 30/12/2018 23:45:52
Showing 1 changed files
... ...
@@ -98,8 +98,8 @@ int init_ro_db(const str *db_url, int dlg_hash_size, int db_update_period, int f
98 98
     }
99 99
 
100 100
     if (db_check_table_version(&ro_dbf, ro_db_handle, &ro_session_table_name, RO_TABLE_VERSION) < 0) {
101
-	LM_ERR("error during dialog-table version check.\n");
102
-	return -1;
101
+	DB_TABLE_VERSION_ERROR(ro_session_table_name);
102
+	goto dberror;
103 103
     }
104 104
 
105 105
     //	if( (dlg_db_mode==DB_MODE_DELAYED) && 
... ...
@@ -110,13 +110,18 @@ int init_ro_db(const str *db_url, int dlg_hash_size, int db_update_period, int f
110 110
 
111 111
     if ((load_ro_info_from_db(dlg_hash_size, fetch_num_rows)) != 0) {
112 112
 	LM_ERR("unable to load the dialog data\n");
113
-	return -1;
113
+	goto dberror;
114 114
     }
115 115
 
116 116
     ro_dbf.close(ro_db_handle);
117 117
     ro_db_handle = 0;
118 118
 
119 119
     return 0;
120
+
121
+dberror:
122
+    ro_dbf.close(ro_db_handle);
123
+    ro_db_handle = 0;
124
+    return -1;
120 125
 }
121 126
 
122 127
 int load_ro_info_from_db(int hash_size, int fetch_num_rows) {
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,269 @@
1
+#include "ro_db_handler.h"
2
+#include "../../lib/srdb1/db.h"
3
+
4
+static db1_con_t* ro_db_handle = 0; /* database connection handle */
5
+static db_func_t ro_dbf;
6
+
7
+str ro_session_table_name = str_init(RO_SESSION_TABLE_NAME);
8
+str id_column = str_init(ID_COL);
9
+str h_entry_column = str_init(HASH_ENTRY_COL);
10
+str h_id_column = str_init(HASH_ID_COL);
11
+str session_id_column = str_init(SESSION_ID_COL);
12
+str dlg_h_entry_column = str_init(DLG_HASH_ENTRY_COL);
13
+str dlg_h_id_column = str_init(DLG_HASH_ID_COL);
14
+str direction_column = str_init(DIRECTION_COL);
15
+str asserted_column = str_init(ASSERTED_ID_COL);
16
+str callee_column = str_init(CALLEE_COL);
17
+str start_time_col = str_init(START_TIME_COL);
18
+str last_event_ts_column = str_init(LAST_EVENT_TS_COL);
19
+str reserved_sec_column = str_init(RESERVED_SECS_COL);
20
+str valid_for_column = str_init(VALID_FOR_COL);
21
+str state_column = str_init(STATE_COL);
22
+str incoming_trunk_id_column = str_init(INCOMING_TRUNK_ID_COL);
23
+str outgoing_trunk_id_column = str_init(OUTGOING_TRUNK_ID_COL);
24
+str rating_group_column = str_init(RATING_GROUP_COL);
25
+str service_identifier_column = str_init(SERVICE_IDENTIFIER_COL);
26
+
27
+typedef enum ro_session_field_idx {
28
+    ID_COL_IDX = 0,
29
+    HASH_ENTRY_COL_IDX,
30
+    HASH_ID_COL_IDX,
31
+    SESSION_ID_COL_IDX,
32
+    DLG_HASH_ENTRY_COL_IDX,
33
+    DLG_HASH_ID_COL_IDX,
34
+    DIRECTION_COL_IDX,
35
+    ASSERTED_ID_COL_IDX,
36
+    CALLEE_COL_IDX,
37
+    START_TIME_COL_IDX,
38
+    LAST_EVENT_TS_COL_IDX,
39
+    RESERVED_SECS_COL_IDX,
40
+    VALID_FOR_COL_IDX,
41
+    STATE_COL_IDX,
42
+    INCOMING_TRUNK_ID_COL_IDX,
43
+    OUTGOING_TRUNK_ID_COL_IDX,
44
+    RATING_GROUP_COL_IDX,
45
+    SERVICE_IDENTIFIER_COL_IDX
46
+	    
47
+} ro_session_field_idx_t;
48
+
49
+#define GET_FIELD_IDX(_val, _idx)\
50
+		(_val + _idx)
51
+
52
+#define SET_STR_VALUE(_val, _str)\
53
+	do{\
54
+			VAL_STR((_val)).s = (_str).s;\
55
+			VAL_STR((_val)).len = (_str).len;\
56
+	}while(0);
57
+
58
+#define SET_NULL_FLAG(_vals, _i, _max, _flag)\
59
+	do{\
60
+		for((_i) = 0;(_i)<(_max); (_i)++)\
61
+			VAL_NULL((_vals)+(_i)) = (_flag);\
62
+	}while(0);
63
+
64
+#define SET_PROPER_NULL_FLAG(_str, _vals, _index)\
65
+	do{\
66
+		if( (_str).len == 0)\
67
+			VAL_NULL( (_vals)+(_index) ) = 1;\
68
+		else\
69
+			VAL_NULL( (_vals)+(_index) ) = 0;\
70
+	}while(0);
71
+
72
+#define GET_STR_VALUE(_res, _values, _index, _not_null, _unref)\
73
+	do{\
74
+		if (VAL_NULL((_values)+ (_index))) { \
75
+			if (_not_null) {\
76
+				if (_unref) unref_dlg(dlg,1);\
77
+				goto next_dialog; \
78
+			} else { \
79
+				(_res).s = 0; \
80
+				(_res).len = 0; \
81
+			}\
82
+		} else { \
83
+			(_res).s = VAL_STR((_values)+ (_index)).s;\
84
+			(_res).len = strlen(VAL_STR((_values)+ (_index)).s);\
85
+		} \
86
+	}while(0);
87
+
88
+int init_ro_db(const str *db_url, int dlg_hash_size, int db_update_period, int fetch_num_rows) {
89
+    /* Find a database module */
90
+    if (db_bind_mod(db_url, &ro_dbf) < 0) {
91
+	LM_ERR("Unable to bind to a database driver\n");
92
+	return -1;
93
+    }
94
+
95
+    if (ro_connect_db(db_url) != 0) {
96
+	LM_ERR("unable to connect to the database\n");
97
+	return -1;
98
+    }
99
+
100
+    if (db_check_table_version(&ro_dbf, ro_db_handle, &ro_session_table_name, RO_TABLE_VERSION) < 0) {
101
+	LM_ERR("error during dialog-table version check.\n");
102
+	return -1;
103
+    }
104
+
105
+    //	if( (dlg_db_mode==DB_MODE_DELAYED) && 
106
+    //	(register_timer( dialog_update_db, 0, db_update_period)<0 )) {
107
+    //		LM_ERR("failed to register update db\n");
108
+    //		return -1;
109
+    //	}
110
+
111
+    if ((load_ro_info_from_db(dlg_hash_size, fetch_num_rows)) != 0) {
112
+	LM_ERR("unable to load the dialog data\n");
113
+	return -1;
114
+    }
115
+
116
+    ro_dbf.close(ro_db_handle);
117
+    ro_db_handle = 0;
118
+
119
+    return 0;
120
+}
121
+
122
+int load_ro_info_from_db(int hash_size, int fetch_num_rows) {
123
+    LM_WARN("not supported yet");
124
+    return 0;
125
+}
126
+
127
+int ro_connect_db(const str *db_url) {
128
+    if (ro_db_handle) {
129
+	LM_CRIT("BUG - db connection found already open\n");
130
+	return -1;
131
+    }
132
+    if ((ro_db_handle = ro_dbf.init(db_url)) == 0)
133
+	return -1;
134
+
135
+    /* use default table */
136
+    if (ro_dbf.use_table(ro_db_handle, &ro_session_table_name) != 0) {
137
+	LM_ERR("Error in use table for table name [%.*s]\n", ro_session_table_name.len, ro_session_table_name.s);
138
+	return -1;
139
+    }
140
+
141
+    return 0;
142
+}
143
+
144
+void db_set_int_val(db_val_t* values, int index, int val) {
145
+    VAL_TYPE(GET_FIELD_IDX(values, index)) = DB1_INT;
146
+    VAL_NULL(GET_FIELD_IDX(values, index)) = 0;
147
+    VAL_INT(GET_FIELD_IDX(values, index)) = val;
148
+}
149
+
150
+void db_set_str_val(db_val_t* values, int index, str* val) {
151
+    VAL_TYPE(GET_FIELD_IDX(values, index)) = DB1_STR;
152
+    VAL_NULL(GET_FIELD_IDX(values, index)) = 0;
153
+    SET_STR_VALUE(GET_FIELD_IDX(values, index), *val);
154
+}
155
+
156
+void db_set_datetime_val(db_val_t* values, int index, time_t val) {
157
+    VAL_TYPE(GET_FIELD_IDX(values, index)) = DB1_DATETIME;
158
+    VAL_NULL(GET_FIELD_IDX(values, index)) = 0;
159
+    VAL_TIME(GET_FIELD_IDX(values, index)) = val;
160
+}
161
+
162
+int update_ro_dbinfo_unsafe(struct ro_session* ro_session) {
163
+    if ((ro_session->flags & RO_SESSION_FLAG_NEW) != 0 && (ro_session->flags & RO_SESSION_FLAG_INSERTED) == 0) {
164
+
165
+	db_val_t values[RO_SESSION_TABLE_COL_NUM];
166
+	db_key_t insert_keys[RO_SESSION_TABLE_COL_NUM] = {
167
+	    &id_column, &h_entry_column, &h_id_column, &session_id_column, &dlg_h_entry_column, &dlg_h_id_column,
168
+	    &direction_column, &asserted_column, &callee_column, &start_time_col, &last_event_ts_column,
169
+	    &reserved_sec_column, &valid_for_column, &state_column, &incoming_trunk_id_column, &outgoing_trunk_id_column, &rating_group_column, &service_identifier_column
170
+	};
171
+
172
+	VAL_TYPE(GET_FIELD_IDX(values, ID_COL_IDX)) = DB1_INT;
173
+	VAL_NULL(GET_FIELD_IDX(values, ID_COL_IDX)) = 1;
174
+
175
+	db_set_int_val(values, HASH_ENTRY_COL_IDX, ro_session->h_entry);
176
+	db_set_int_val(values, HASH_ID_COL_IDX, ro_session->h_id);
177
+	db_set_str_val(values, SESSION_ID_COL_IDX, &ro_session->ro_session_id);
178
+	db_set_int_val(values, DLG_HASH_ENTRY_COL_IDX, ro_session->dlg_h_entry);
179
+	db_set_int_val(values, DLG_HASH_ID_COL_IDX, ro_session->dlg_h_id);
180
+	db_set_int_val(values, DIRECTION_COL_IDX, ro_session->direction);
181
+	db_set_str_val(values, ASSERTED_ID_COL_IDX, &ro_session->asserted_identity);
182
+	db_set_str_val(values, CALLEE_COL_IDX, &ro_session->called_asserted_identity);
183
+	db_set_datetime_val(values, START_TIME_COL_IDX, ro_session->start_time/1000000);
184
+	db_set_datetime_val(values, LAST_EVENT_TS_COL_IDX, ro_session->last_event_timestamp/1000000);
185
+	db_set_int_val(values, RESERVED_SECS_COL_IDX, ro_session->reserved_secs);
186
+	db_set_int_val(values, VALID_FOR_COL_IDX, ro_session->valid_for);
187
+	db_set_int_val(values, STATE_COL_IDX, ro_session->active);
188
+	db_set_str_val(values, INCOMING_TRUNK_ID_COL_IDX, &ro_session->incoming_trunk_id);
189
+	db_set_str_val(values, OUTGOING_TRUNK_ID_COL_IDX, &ro_session->outgoing_trunk_id);
190
+	db_set_int_val(values, RATING_GROUP_COL_IDX, ro_session->rating_group);
191
+	db_set_int_val(values, SERVICE_IDENTIFIER_COL_IDX, ro_session->service_identifier);
192
+	
193
+
194
+	LM_DBG("Inserting ro_session into database\n");
195
+	if ((ro_dbf.insert(ro_db_handle, insert_keys, values, RO_SESSION_TABLE_COL_NUM)) != 0) {
196
+	    LM_ERR("could not add new Ro session into DB.... continuing\n");
197
+	    goto error;
198
+	}
199
+	ro_session->flags &= ~(RO_SESSION_FLAG_NEW | RO_SESSION_FLAG_CHANGED);
200
+	ro_session->flags |= RO_SESSION_FLAG_INSERTED;
201
+
202
+    } else if ((ro_session->flags & RO_SESSION_FLAG_CHANGED) != 0 && (ro_session->flags & RO_SESSION_FLAG_INSERTED) != 0) {
203
+
204
+	db_val_t values[RO_SESSION_TABLE_COL_NUM-1];
205
+	db_key_t update_keys[RO_SESSION_TABLE_COL_NUM-1] = {
206
+	    &h_entry_column, &h_id_column, &session_id_column, &dlg_h_entry_column, &dlg_h_id_column,
207
+	    &direction_column, &asserted_column, &callee_column, &start_time_col, &last_event_ts_column,
208
+	    &reserved_sec_column, &valid_for_column, &state_column, &incoming_trunk_id_column, &outgoing_trunk_id_column, &rating_group_column, &service_identifier_column
209
+	};
210
+
211
+	db_set_int_val(values, HASH_ENTRY_COL_IDX - 1, ro_session->h_entry);
212
+	db_set_int_val(values, HASH_ID_COL_IDX - 1, ro_session->h_id);
213
+	db_set_str_val(values, SESSION_ID_COL_IDX - 1, &ro_session->ro_session_id);
214
+	db_set_int_val(values, DLG_HASH_ENTRY_COL_IDX - 1, ro_session->dlg_h_entry);
215
+	db_set_int_val(values, DLG_HASH_ID_COL_IDX - 1, ro_session->dlg_h_id);
216
+	db_set_int_val(values, DIRECTION_COL_IDX - 1, ro_session->direction);
217
+	db_set_str_val(values, ASSERTED_ID_COL_IDX - 1, &ro_session->asserted_identity);
218
+	db_set_str_val(values, CALLEE_COL_IDX - 1, &ro_session->called_asserted_identity);
219
+	db_set_datetime_val(values, START_TIME_COL_IDX - 1, ro_session->start_time/1000000);
220
+	db_set_datetime_val(values, LAST_EVENT_TS_COL_IDX - 1, ro_session->last_event_timestamp/1000000);
221
+	db_set_int_val(values, RESERVED_SECS_COL_IDX - 1, ro_session->reserved_secs);
222
+	db_set_int_val(values, VALID_FOR_COL_IDX - 1, ro_session->valid_for);
223
+	db_set_int_val(values, STATE_COL_IDX - 1, ro_session->active);
224
+	db_set_str_val(values, INCOMING_TRUNK_ID_COL_IDX - 1, &ro_session->incoming_trunk_id);
225
+	db_set_str_val(values, OUTGOING_TRUNK_ID_COL_IDX - 1, &ro_session->outgoing_trunk_id);
226
+	db_set_int_val(values, RATING_GROUP_COL_IDX - 1, ro_session->rating_group);
227
+	db_set_int_val(values, SERVICE_IDENTIFIER_COL_IDX - 1, ro_session->service_identifier);
228
+
229
+	LM_DBG("Updating ro_session in database\n");
230
+	if ((ro_dbf.update(ro_db_handle, update_keys/*match*/, 0/*match*/, values/*match*/, update_keys/*update*/, values/*update*/, 3/*match*/, 13/*update*/)) != 0) {
231
+	    LM_ERR("could not update Ro session information in DB... continuing\n");
232
+	    goto error;
233
+	}
234
+	ro_session->flags &= ~RO_SESSION_FLAG_CHANGED;
235
+    } else if ((ro_session->flags & RO_SESSION_FLAG_DELETED) != 0) {
236
+	db_val_t values[3];
237
+	db_key_t match_keys[3] = {&h_entry_column, &h_id_column, &session_id_column};
238
+	
239
+	db_set_int_val(values, HASH_ENTRY_COL_IDX - 1, ro_session->h_entry);
240
+	db_set_int_val(values, HASH_ID_COL_IDX - 1, ro_session->h_id);
241
+	db_set_str_val(values, SESSION_ID_COL_IDX - 1, &ro_session->ro_session_id);
242
+	
243
+	if(ro_dbf.delete(ro_db_handle, match_keys, 0, values, 3) < 0) {
244
+		LM_ERR("failed to delete ro session database information... continuing\n");
245
+		return -1;
246
+	}
247
+    } else {
248
+	LM_WARN("Asked to update Ro session in strange state [%d]\n", ro_session->flags);
249
+    }
250
+
251
+    return 0;
252
+
253
+error:
254
+    return -1;
255
+}
256
+
257
+int update_ro_dbinfo(struct ro_session* ro_session) {
258
+    struct ro_session_entry entry;
259
+    /* lock the entry */
260
+    entry = (ro_session_table->entries)[ro_session->h_entry];
261
+    ro_session_lock(ro_session_table, &entry);
262
+    if (update_ro_dbinfo_unsafe(ro_session) != 0) {
263
+	LM_ERR("failed to update ro_session in DB\n");
264
+	ro_session_unlock(ro_session_table, &entry);
265
+	return -1;
266
+    }
267
+    ro_session_unlock(ro_session_table, &entry);
268
+    return 0;
269
+}