Merge "res_pjsip/res_pjsip_callerid: NULL check on caller id name string"
[asterisk/asterisk.git] / res / res_pjsip_empty_info.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2016, Digium, Inc.
5  *
6  * Bradley Latus <brad.latus@gmail.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <depend>res_pjsip_session</depend>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_ua.h>
30
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_session.h"
33 #include "asterisk/module.h"
34
35 static void send_response(struct ast_sip_session *session,
36                 struct pjsip_rx_data *rdata, int code)
37 {
38         pjsip_tx_data *tdata;
39         pjsip_dialog *dlg = session->inv_session->dlg;
40
41         if (pjsip_dlg_create_response(dlg, rdata, code, NULL, &tdata) == PJ_SUCCESS) {
42                 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
43                 pjsip_dlg_send_response(dlg, tsx, tdata);
44         }
45 }
46
47 static int empty_info_incoming_request(struct ast_sip_session *session,
48                 struct pjsip_rx_data *rdata)
49 {
50         if (!rdata->msg_info.ctype) {
51                 /* Need to return 200 OK on empty body */
52                 /* Some SBCs use empty INFO as a KEEPALIVE */
53                 send_response(session, rdata, 200);
54                 return 1;
55         }
56
57         /* Let another module respond */
58         return 0;
59
60 }
61
62 static struct ast_sip_session_supplement empty_info_supplement = {
63         .method = "INFO",
64         .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST,
65         .incoming_request = empty_info_incoming_request,
66 };
67
68 static int load_module(void)
69 {
70         CHECK_PJSIP_SESSION_MODULE_LOADED();
71
72         ast_sip_session_register_supplement(&empty_info_supplement);
73         return AST_MODULE_LOAD_SUCCESS;
74 }
75
76 static int unload_module(void)
77 {
78         ast_sip_session_unregister_supplement(&empty_info_supplement);
79         return 0;
80 }
81
82 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Empty INFO Support",
83         .support_level = AST_MODULE_SUPPORT_CORE,
84         .load = load_module,
85         .unload = unload_module,
86         .load_pri = AST_MODPRI_APP_DEPEND,
87 );