res_pjsip: Fix warning by deferring implicit type cast.
[asterisk/asterisk.git] / res / res_pjsip / pjsip_session.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2017, CFWare, LLC
5  *
6  * Corey Farrell <git@cfware.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 #include "asterisk.h"
20
21 #include <pjsip.h>
22 #include <pjsip_ua.h>
23 #include <pjlib.h>
24
25 #include "asterisk/res_pjsip.h"
26 #include "asterisk/res_pjsip_session.h"
27 #include "include/res_pjsip_private.h"
28 #include "asterisk/linkedlists.h"
29 #include "asterisk/lock.h"
30 #include "asterisk/module.h"
31
32
33 AST_RWLIST_HEAD_STATIC(session_supplements, ast_sip_session_supplement);
34
35 void internal_sip_session_register_supplement(struct ast_sip_session_supplement *supplement)
36 {
37         struct ast_sip_session_supplement *iter;
38         int inserted = 0;
39         SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
40
41         if (!supplement->response_priority) {
42                 supplement->response_priority = AST_SIP_SESSION_BEFORE_MEDIA;
43         }
44
45         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&session_supplements, iter, next) {
46                 if (iter->priority > supplement->priority) {
47                         AST_RWLIST_INSERT_BEFORE_CURRENT(supplement, next);
48                         inserted = 1;
49                         break;
50                 }
51         }
52         AST_RWLIST_TRAVERSE_SAFE_END;
53
54         if (!inserted) {
55                 AST_RWLIST_INSERT_TAIL(&session_supplements, supplement, next);
56         }
57 }
58
59 int __ast_sip_session_register_supplement(struct ast_sip_session_supplement *supplement,
60         const char *file, int line, const char *func)
61 {
62         internal_sip_session_register_supplement(supplement);
63         __ast_module_ref(AST_MODULE_SELF, file, line, func);
64
65         return 0;
66 }
67
68 int internal_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
69 {
70         struct ast_sip_session_supplement *iter;
71         int res = -1;
72         SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
73
74         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&session_supplements, iter, next) {
75                 if (supplement == iter) {
76                         AST_RWLIST_REMOVE_CURRENT(next);
77                         res = 0;
78                         break;
79                 }
80         }
81         AST_RWLIST_TRAVERSE_SAFE_END;
82
83         return res;
84 }
85
86 void __ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement,
87         const char *file, int line, const char *func)
88 {
89         if (!internal_sip_session_unregister_supplement(supplement)) {
90                 __ast_module_unref(AST_MODULE_SELF, file, line, func);
91         }
92 }
93
94 static struct ast_sip_session_supplement *supplement_dup(const struct ast_sip_session_supplement *src)
95 {
96         struct ast_sip_session_supplement *dst = ast_calloc(1, sizeof(*dst));
97
98         if (!dst) {
99                 return NULL;
100         }
101         /* Will need to revisit if shallow copy becomes an issue */
102         *dst = *src;
103
104         return dst;
105 }
106
107 int ast_sip_session_add_supplements(struct ast_sip_session *session)
108 {
109         struct ast_sip_session_supplement *iter;
110         SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
111
112         AST_RWLIST_TRAVERSE(&session_supplements, iter, next) {
113                 struct ast_sip_session_supplement *copy = supplement_dup(iter);
114
115                 if (!copy) {
116                         return -1;
117                 }
118                 AST_LIST_INSERT_TAIL(&session->supplements, copy, next);
119         }
120
121         return 0;
122 }
123