File: sms_report.c

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; 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 (382 lines) | stat: -rw-r--r-- 10,026 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * $Id$
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of ser, a free SIP server.
 *
 * ser 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
 *
 * For a license to use the ser software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact iptel.org by e-mail at the following addresses:
 *    info@iptel.org
 *
 * ser 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 <time.h>
#include <unistd.h>
#include "../../mem/shm_mem.h"
#include "../../timer.h"
#include "sms_report.h"
#include "sms_funcs.h"

#define REPORT_TIMEOUT     1*60*60   // one hour
#define START_ERR_MSG      "Your message (or part of it) couldn't be "\
                           "delivered. The SMS Center said: "
#define START_ERR_MSG_LEN  (strlen(START_ERR_MSG))
#define END_ERR_MSG        ". The message was: "
#define END_ERR_MSG_LEN    (strlen( END_ERR_MSG))

struct report_cell {
	int             status;
	time_t          timeout;
	char            *text;
	unsigned int    text_len;
	struct sms_msg  *sms;
};

struct report_cell *report_queue=0;
typedef time_t (get_time_func)(void);
get_time_func  *get_time;




/*-------------- Function to set time - from ser or system ------------------*/
/*
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of ser, a free SIP server.
 *
 * ser 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
 *
 * For a license to use the ser software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact iptel.org by e-mail at the following addresses:
 *    info@iptel.org
 *
 * ser 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
 */

/* gets the time from ser */
static time_t get_time_ser(void)
{
	return  get_ticks();
}
/* gets the time from system */
static time_t get_time_sys(void)
{
	return time(0);
}
/* detects if the ser time function get_ticks works, and depending of that
   sets the correct time function to be used */
void set_gettime_function(void)
{
	unsigned int t1,t2;

	t1 = get_ticks();
	sleep(2);
	t2 = get_ticks();
	if (!t1 && !t2) {
		get_time = get_time_sys;
		LM_INFO("using system time func.\n");
	} else {
		get_time = get_time_ser;
		LM_INFO("using ser time func.\n");
	}
}




static inline void free_report_cell(struct report_cell *cell)
{
	if (!cell)
		return;
	if (cell->sms && !(--(cell->sms->ref)))
		shm_free(cell->sms);
	cell->sms = 0;
	cell->status = 0;
	cell->timeout = 0;
	cell->text = 0;
	cell->text_len = 0;
}




int init_report_queue(void)
{
	report_queue = (struct report_cell*)
		shm_malloc(NR_CELLS*sizeof(struct report_cell));
	if (!report_queue) {
		LM_ERR("no more free pkg_mem!\n");
		return -1;
	}
	memset( report_queue , 0 , NR_CELLS*sizeof(struct report_cell) );
	return 1;
}




void destroy_report_queue(void)
{
	int i;

	if (report_queue){
		for(i=0;i<NR_CELLS;i++)
			if (report_queue[i].sms)
				free_report_cell(&(report_queue[i]));
		shm_free(report_queue);
		report_queue = 0;
	}
}




void add_sms_into_report_queue(int id, struct sms_msg *sms, char *p, int l)
{
	if (report_queue[id].sms){
		LM_INFO("old message still waiting for report at location %d"
				" -> discarding\n",id);
		free_report_cell(&(report_queue[id]));
	}

	sms->ref++;
	report_queue[id].status = -1;
	report_queue[id].sms = sms;
	report_queue[id].text = p;
	report_queue[id].text_len = l;
	report_queue[id].timeout = get_time() + REPORT_TIMEOUT;
}




int  relay_report_to_queue(int id, char *phone, int status, int *old_status)
{
	struct report_cell *cell;
	int    ret_code;

	cell = &(report_queue[id]);
	ret_code = 0;

	/* first, do we have a match into the sms queue? */
	if (!cell->sms) {
		LM_INFO("report received for cell %d,"
				" but the sms was already trashed from queue!\n",id);
		goto done;
	}
	if (strlen(phone)!=cell->sms->to.len ||
	strncmp(phone,cell->sms->to.s,cell->sms->to.len)) {
		LM_INFO("report received for cell %d, but the phone nr is different"
				"->old report->ignored\n",id);
		goto done;
	}

	if (old_status)
		*old_status = cell->status;
	cell->status = status;
	if (status>=0 && status<32) {
		LM_DBG("sms %d confirmed with code %d\n", id, status);
		ret_code = 2; /* success */
	} else if (status<64) {
		/* provisional report */
		LM_DBG("sms %d received prov. report with"
			" code %d\n",id, status);
		ret_code = 1; /* provisional */
	} else {
		LM_DBG("sms %d received error report with code %d\n",id, status);
		ret_code = 3; /* error */
	}

done:
	return ret_code;
}




void check_timeout_in_report_queue(void)
{
	int i;
	time_t current_time;

	current_time = get_time();
	for(i=0;i<NR_CELLS;i++)
		if (report_queue[i].sms && report_queue[i].timeout<=current_time) {
			LM_INFO("[%lu,%lu] record %d is discarded (timeout), having status"
				" %d\n", (long unsigned int)current_time,
				(long unsigned int)report_queue[i].timeout,
				i,report_queue[i].status);
			free_report_cell(&(report_queue[i]));
		}
}




void remove_sms_from_report_queue(int id)
{
	free_report_cell(&(report_queue[id]));
}




str* get_text_from_report_queue(int id)
{
	static str text;

	text.s = report_queue[id].text;
	text.len = report_queue[id].text_len;
	return &text;
}




struct sms_msg* get_sms_from_report_queue(int id)
{
	return ( report_queue[id].sms);
}




str* get_error_str(int status)
{
	static str err_str;

	switch (status) {
		/*
		case 0: strcat(sms->ascii,"Ok,short message received by the SME");
			break;
		case 1: strcat(sms->ascii,"Ok,short message forwarded by the SC to"
			" the SME but the SC is unable to confirm delivery");
			break;
		case 2: strcat(sms->ascii,"Ok,short message replaced by the SC");
			break;
		case 32: strcat(sms->ascii,"Still trying,congestion");
			break;
		case 33: strcat(sms->ascii,"Still trying,SME busy");
			break;
		case 34: strcat(sms->ascii,"Still trying,no response from SME");
			break;
		case 35: strcat(sms->ascii,"Still trying,service rejected");
			break;
		case 36: strcat(sms->ascii,"Still trying,quality of service not"
			" available");
			break;
		case 37: strcat(sms->ascii,"Still trying,error in SME");
			break;
		case 48:
			err_str.s =
			START_ERR_MSG"Delivery is not possible"END_ERR_MSG;
			err_str.len = 24 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		*/
		case 64:
			err_str.s =
			START_ERR_MSG"Error, remote procedure error"END_ERR_MSG;
			err_str.len = 29 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 65: 
			err_str.s =
			START_ERR_MSG"Error,incompatible destination"END_ERR_MSG;
			err_str.len = 30 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 66: 
			err_str.s =
			START_ERR_MSG"Error,connection rejected by SME"END_ERR_MSG;
			err_str.len = 32 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 67:
			err_str.s = START_ERR_MSG"Error,not obtainable"END_ERR_MSG;
			err_str.len = 20 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 68:
			err_str.s =
			START_ERR_MSG"Error,quality of service not available"END_ERR_MSG;
			err_str.len = 38 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 69:
			err_str.s =
			START_ERR_MSG"Error,no interworking available"END_ERR_MSG;
			err_str.len = 31 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 70:
			err_str.s =
			START_ERR_MSG"Error,SM validity period expired"END_ERR_MSG;
			err_str.len = 32 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 71:
			err_str.s =
			START_ERR_MSG"Error,SM deleted by originating SME"END_ERR_MSG;
			err_str.len = 35 + START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 72:
			err_str.s =
			START_ERR_MSG"Error,SM deleted by SC administration"END_ERR_MSG;
			err_str.len = 37+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 73:
			err_str.s = START_ERR_MSG"Error,SM does not exist"END_ERR_MSG;
			err_str.len = 29+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 96:
			err_str.s = START_ERR_MSG"Error,congestion"END_ERR_MSG;
			err_str.len = 23+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 97:
			err_str.s = START_ERR_MSG"Error,SME busy"END_ERR_MSG;
			err_str.len = 14+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 98:
			err_str.s = START_ERR_MSG"Error,no response from SME"END_ERR_MSG;
			err_str.len = 26+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 99:
			err_str.s = START_ERR_MSG"Error,service rejected"END_ERR_MSG;
			err_str.len = 22+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 100:
			err_str.s =
			START_ERR_MSG"Error,quality of service not available"END_ERR_MSG;
			err_str.len = 38+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		case 101:
			err_str.s = START_ERR_MSG"Error,error in SME"END_ERR_MSG;
			err_str.len = 18+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
			break;
		default:
			err_str.s = START_ERR_MSG"Unknown error code"END_ERR_MSG;
			err_str.len = 18+ START_ERR_MSG_LEN + END_ERR_MSG_LEN;
	}
	return &err_str;
}