-
Notifications
You must be signed in to change notification settings - Fork 9
/
push_mod.c
624 lines (499 loc) · 16.2 KB
/
push_mod.c
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
* $Id$
*
* APNs support module
*
* Copyright (C) 2013 Volodymyr Tarasenko
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "../../core/sr_module.h"
#include "../../core/trim.h"
#include "../../core/dprint.h"
#include "../../core/mem/mem.h"
#include "../../core/parser/parse_to.h"
#include "../../core/parser/parse_uri.h"
#include "../../core/cfg/cfg_struct.h"
#include "push_mod.h"
#include "push.h"
#include "push_common.h"
#include "push_ssl_utils.h"
#include "apns_feedback.h"
MODULE_VERSION
static int mod_init(void);
static void destroy(void);
static int child_init(int rank);
//static void start_feedback_service();
static void feedback_service(int fd);
static void stop_feedback_service();
static int w_push_request(struct sip_msg *rq, const char *device_token);
static int w_push_message(struct sip_msg *rq, const char *device_token, const char *message);
static int w_push_custom_message(struct sip_msg *rq, const char *device_token, const char *message, const char* custom);
static int w_push_register(struct sip_msg *rq, const char *device_token);
static int w_push_msg(struct sip_msg *rq, const char *msg);
static int w_push_custom_msg(struct sip_msg *rq, const char *msg, const char* custom);
static int w_push_status(struct sip_msg *rq, const char* device_token, int code);
static int push_api_fixup(void** param, int param_no);
static int free_push_api_fixup(void** param, int param_no);
static void timer_cleanup_function(unsigned int ticks, void* param);
/* ----- PUSH variables ----------- */
/*@{*/
static char *apns_cert_file = 0;
static char *apns_cert_key = 0;
static char *apns_cert_ca = 0;
static char *apns_server = 0;
static char *apns_feedback_server = "feedback.sandbox.push.apple.com";
static char *apns_alert = "You have a call";
static int apns_badge = -1;
static char *apns_sound = 0;
static int apns_feedback_port = 2196;
static int apns_port;
static int push_flag = 0;
static int apns_read_timeout = 100000;
static int apns_feedback_read_timeout = 500000;
static char *push_db = 0;
static char *push_table = "push_apns";
///void *rh;
/*@}*/
static PushServer* apns = 0;
static cmd_export_t cmds[] = {
{"push_request", (cmd_function)w_push_request, 1,
push_api_fixup, free_push_api_fixup,
ANY_ROUTE},
{"push_request", (cmd_function)w_push_message, 2,
push_api_fixup, free_push_api_fixup,
ANY_ROUTE},
{"push_request", (cmd_function)w_push_custom_message, 3,
push_api_fixup, free_push_api_fixup,
ANY_ROUTE},
{"push_register", (cmd_function)w_push_register, 1,
push_api_fixup, free_push_api_fixup,
ANY_ROUTE},
{"push_message", (cmd_function)w_push_msg, 1,
push_api_fixup, free_push_api_fixup,
ANY_ROUTE},
{"push_message", (cmd_function)w_push_custom_msg, 2,
push_api_fixup, free_push_api_fixup,
ANY_ROUTE},
{0, 0, 0, 0, 0, 0}
};
static param_export_t params[] = {
{"push_db", STR_PARAM, &push_db },
{"push_table", STR_PARAM, &push_table },
{"push_flag", INT_PARAM, &push_flag },
{"push_apns_cert", STR_PARAM, &apns_cert_file },
{"push_apns_key", STR_PARAM, &apns_cert_key },
{"push_apns_cafile", STR_PARAM, &apns_cert_ca },
{"push_apns_server", STR_PARAM, &apns_server },
{"push_apns_port", INT_PARAM, &apns_port },
{"push_apns_alert", STR_PARAM, &apns_alert },
{"push_apns_sound", STR_PARAM, &apns_sound },
{"push_apns_badge", INT_PARAM, &apns_badge },
{"push_apns_rtimeout", INT_PARAM, &apns_read_timeout },
{"push_apns_feedback_server", STR_PARAM, &apns_feedback_server },
{"push_apns_feedback_port", INT_PARAM, &apns_feedback_port },
{"push_apns_feedback_rtimeout", INT_PARAM, &apns_feedback_read_timeout },
{0,0,0}
};
/* static proc_export_t procs[] = { */
/* {"Feedback service", 0, 0, feedback_service, 1 }, */
/* {0,0,0,0,0} */
/* }; */
struct module_exports exports= {
"push",
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* exported functions */
params, /* exported params */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* extra processes, depricated? */
mod_init, /* initialization module */
0, /* response function */
destroy, /* destroy function */
child_init /* per-child init function */
};
static int pipefd[2];
/************************** SIP helper functions ****************************/
#define USERNAME_MAX_SIZE 64
#define DOMAIN_MAX_SIZE 128
static int
get_callid(struct sip_msg* msg, str *cid)
{
if (msg->callid == NULL) {
if (parse_headers(msg, HDR_CALLID_F, 0) == -1) {
LM_ERR("cannot parse Call-ID header\n");
return -1;
}
if (msg->callid == NULL) {
LM_ERR("missing Call-ID header\n");
return -1;
}
}
*cid = msg->callid->body;
trim(cid);
return 0;
}
#define MAX_AOR_LEN 256
/*! \brief
* Extract Address of Record
*/
int extract_aor(str* _uri, str* _a, sip_uri_t *_pu)
{
static char aor_buf[MAX_AOR_LEN];
// str tmp;
sip_uri_t turi;
sip_uri_t *puri;
// int user_len;
str *uri;
// str realm_prefix = {0};
memset(aor_buf, 0, MAX_AOR_LEN);
uri=_uri;
if(_pu!=NULL)
puri = _pu;
else
puri = &turi;
if (parse_uri(uri->s, uri->len, puri) < 0) {
LM_ERR("failed to parse AoR [%.*s]\n", uri->len, uri->s);
return -1;
}
if ( (puri->user.len + puri->host.len + 1) > MAX_AOR_LEN
|| puri->user.len > USERNAME_MAX_SIZE
|| puri->host.len > DOMAIN_MAX_SIZE ) {
LM_ERR("Address Of Record too long\n");
return -2;
}
_a->s = aor_buf;
_a->len = puri->user.len;
if (un_escape(&puri->user, _a) < 0) {
LM_ERR("failed to unescape username\n");
return -3;
}
// user_len = _a->len;
/* if (reg_use_domain) { */
/* if (user_len) */
/* aor_buf[_a->len++] = '@'; */
/* /\* strip prefix (if defined) *\/ */
/* realm_prefix.len = cfg_get(registrar, registrar_cfg, realm_pref).len; */
/* if(realm_prefix.len>0) { */
/* realm_prefix.s = cfg_get(registrar, registrar_cfg, realm_pref).s; */
/* LM_DBG("realm prefix is [%.*s]\n", realm_prefix.len, */
/* (realm_prefix.len>0)?realm_prefix.s:""); */
/* } */
/* if (realm_prefix.len>0 */
/* && realm_prefix.len<puri->host.len */
/* && (memcmp(realm_prefix.s, puri->host.s, realm_prefix.len)==0)) */
/* { */
/* memcpy(aor_buf + _a->len, puri->host.s + realm_prefix.len, */
/* puri->host.len - realm_prefix.len); */
/* _a->len += puri->host.len - realm_prefix.len; */
/* } else { */
/* memcpy(aor_buf + _a->len, puri->host.s, puri->host.len); */
/* _a->len += puri->host.len; */
/* } */
/* } */
/* if (cfg_get(registrar, registrar_cfg, case_sensitive) && user_len) { */
/* tmp.s = _a->s + user_len + 1; */
/* tmp.len = _a->s + _a->len - tmp.s; */
/* strlower(&tmp); */
/* } else { */
strlower(_a);
/* } */
return 0;
}
/************************** INTERFACE functions ****************************/
static int mod_init( void )
{
LM_DBG("Init Push module\n");
apns = create_push_server(apns_cert_file,
apns_cert_key,
apns_cert_ca,
apns_server,
apns_port);
if (NULL == apns)
{
LM_ERR("Cannot create push structure, failed");
return -1;
}
apns->read_timeout = apns_read_timeout;
if ((push_db) && (-1 == push_check_db(apns, push_db, push_table)))
{
LM_ERR("Cannot connect database, failed");
return -1;
}
ssl_init();
register_timer(timer_cleanup_function,
apns,
2);
#ifdef ENABLE_FEEDBACK_SERVICE
register_procs(1);
#endif
/* do all staff in child init*/
return 0;
}
static int child_init(int rank)
{
LM_DBG("Child Init Push module\n");
#ifdef ENABLE_FEEDBACK_SERVICE
if (rank == PROC_MAIN)
{
pid_t pid;
if (-1 == pipe(pipefd))
{
LM_ERR("cannot create feedback command pipe\n");
return -1;
}
pid = fork_process(PROC_NOCHLDINIT, "MY PROC DESCRIPTION", 1);
if (pid < 0)
return -1; /* error */
if(pid == 0)
{
/* child */
close(pipefd[1]);
/* initialize the config framework */
if (cfg_child_init())
{
LM_ERR("cfg child init failed\n");
return -1;
}
LM_DBG("Start feedback server");
feedback_service(pipefd[0]);
exit(0);
}
close(pipefd[0]);
}
#endif
if ((push_db) && (-1 == push_connect_db(apns, push_db, push_table, rank)))
{
LM_ERR("Cannot connect database, failed");
return -1;
}
if (push_flag == ConnectEstablish)
return establish_ssl_connection(apns);
/* if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN) */
/* return 0; /\* do nothing for the main process *\/ */
return 0;
}
static void destroy(void)
{
LM_DBG("Push destroy\n");
#ifdef ENABLE_FEEDBACK_SERVICE
stop_feedback_service();
#endif
destroy_push_server(apns);
//ssl_shutdown();
}
static int push_api_fixup(void** param, int param_no)
{
char *p;
LM_DBG("Push push_api_fixup, param %d\n", param_no);
p = (char*)*param;
if (p==0 || p[0]==0) {
LM_ERR("first parameter is empty\n");
return E_SCRIPT;
}
return 0;
}
static int free_push_api_fixup(void** param, int param_no)
{
LM_DBG("Push free_push_api_fixup, param %d\n", param_no);
/* if(*param) */
/* { */
/* pkg_free(*param); */
/* *param = 0; */
/* } */
return 0;
}
static int w_push_request(struct sip_msg *rq, const char *device_token)
{
// str *ruri;
str callid;
size_t token_len = strlen(device_token);
LM_DBG("Push request started, token %s\n", device_token);
if (token_len != DEVICE_TOKEN_LEN_STR)
{
LM_ERR("Device token length wrong, reject push\n");
return -1;
}
// Working with sip message:
// ruri = GET_RURI(rq);
if (-1 == get_callid(rq, &callid))
{
LM_ERR("Geting CallID failed, reject push\n");
return -1;
}
if (-1 == push_send(apns, device_token, apns_alert, NULL, apns_badge))
{
LM_ERR("Push notification failed, call id %s, device token %s\n",
callid.s, device_token);
return -1;
}
LM_DBG("Success\n");
return 1;
}
static int w_push_message(struct sip_msg *rq, const char *device_token, const char *message)
{
return w_push_custom_message(rq, device_token, message, NULL);
}
static int w_push_custom_message(struct sip_msg *rq, const char *device_token, const char *message, const char* custom)
{
// str *ruri;
str callid;
size_t token_len = strlen(device_token);
LM_DBG("Push request started, token %s, message %s\n", device_token, message);
if (token_len != DEVICE_TOKEN_LEN_STR)
{
LM_ERR("Device token length wrong, reject push\n");
return -1;
}
// Working with sip message:
// ruri = GET_RURI(rq);
if (-1 == get_callid(rq, &callid))
{
LM_ERR("Geting CallID failed, reject push\n");
return -1;
}
if (-1 == push_send(apns, device_token, message, custom, apns_badge))
{
LM_ERR("Push notification failed, call id %s, device token %s, message %s\n",
callid.s, device_token, message);
return -1;
}
LM_DBG("Success\n");
return 1;
}
static int w_push_register(struct sip_msg *rq, const char *device_token)
{
str callid;
str uri, aor;
size_t token_len = strlen(device_token);
LM_DBG("Push request started, token %s\n", device_token);
if (token_len != DEVICE_TOKEN_LEN_STR)
{
LM_ERR("Device token length wrong, reject push\n");
return -1;
}
// Working with sip message:
if (-1 == get_callid(rq, &callid))
{
LM_ERR("Geting CallID failed, reject push\n");
return -1;
}
uri = get_to(rq)->uri; //rq->first_line.u.request.uri;
LM_DBG("Push request, URI %s, token %s\n", uri.s, device_token);
if (extract_aor(&uri, &aor, NULL) < 0) {
LM_ERR("failed to extract address of record\n");
return -1;
}
LM_DBG("Push request, AOR %s, token %s\n", aor.s, device_token);
if (-1 == push_register_device(apns, aor.s, device_token, &callid, push_table))
{
LM_ERR("Push device registration failed, call id %s, device token %s\n",
callid.s, device_token);
return -1;
}
LM_DBG("Success\n");
return 1;
}
static int w_push_msg(struct sip_msg *rq, const char* msg)
{
return w_push_custom_msg(rq, msg, NULL);
}
static int w_push_custom_msg(struct sip_msg *rq, const char* msg, const char* custom)
{
char* device_token = NULL;
to_body_t* to;
str /*uri,*/ aor;
str callid;
if (-1 == get_callid(rq, &callid))
{
LM_ERR("Geting CallID failed, reject push\n");
return -1;
}
if (0 != parse_to_header(rq))
{
LM_ERR("Parsing TO header failed, reject push\n");
return -1;
}
to = get_to(rq);
if (extract_aor(&to->uri, &aor, NULL) < 0)
{
LM_ERR("failed to extract address of record\n");
return -1;
}
LM_DBG("Send push message, aor [%s], getting token...\n", aor.s);
if (-1 == push_get_device(apns, aor.s, (const char **)&device_token, push_table))
{
LM_ERR("Push failed, cannot get device token, call id %s\n",
callid.s);
return -1;
}
LM_DBG("Sending push message, aor [%s], token [%s], msg [%s], badge [%d]...\n",
aor.s, device_token, msg, apns_badge);
if (-1 == push_send(apns, device_token, msg, custom, apns_badge))
{
LM_ERR("Push notification failed, call id %s, device token %s, message %s\n",
callid.s, device_token, msg);
free(device_token);
return -1;
}
free(device_token);
LM_DBG("Success\n");
return 1;
}
static int w_push_status(struct sip_msg *rq, const char* device_token, int code)
{
return -1;
}
static void feedback_service(int fd)
{
#define FEEDBACK_MSG_LEN 38
PushServer *feedback;
feedback = create_push_server(apns_cert_file,
apns_cert_key,
apns_cert_ca,
apns_feedback_server,
apns_feedback_port);
if (feedback == NULL)
{
LM_ERR("Cannot initiale feedback service");
return;
}
feedback->read_timeout = apns_feedback_read_timeout;
run_feedback(feedback, fd);
}
static void stop_feedback_service()
{
char cmd = 'q';
write(pipefd[1],&cmd, 1);
close(pipefd[1]);
}
static void timer_cleanup_function(unsigned int ticks, void* param)
{
PushServer *server = (PushServer*)param;
push_check_status(server);
}