| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 ast_sip_session_register_supplement_with_module(struct ast_module *module, 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 |
ast_assert(supplement != NULL); |
| 42 |
|
| 43 |
supplement->module = module; |
| 44 |
|
| 45 |
if (!supplement->response_priority) { |
| 46 |
supplement->response_priority = AST_SIP_SESSION_BEFORE_MEDIA; |
| 47 |
} |
| 48 |
|
| 49 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&session_supplements, iter, next) { |
| 50 |
if (iter->priority > supplement->priority) { |
| 51 |
AST_RWLIST_INSERT_BEFORE_CURRENT(supplement, next); |
| 52 |
inserted = 1; |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 57 |
|
| 58 |
if (!inserted) { |
| 59 |
AST_RWLIST_INSERT_TAIL(&session_supplements, supplement, next); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement) |
| 64 |
{ |
| 65 |
struct ast_sip_session_supplement *iter; |
| 66 |
SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK); |
| 67 |
|
| 68 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&session_supplements, iter, next) { |
| 69 |
if (supplement == iter) { |
| 70 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 75 |
} |
| 76 |
|
| 77 |
static struct ast_sip_session_supplement *supplement_dup(const struct ast_sip_session_supplement *src) |
| 78 |
{ |
| 79 |
struct ast_sip_session_supplement *dst = ast_calloc(1, sizeof(*dst)); |
| 80 |
|
| 81 |
if (!dst) { |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
*dst = *src; |
| 86 |
|
| 87 |
return dst; |
| 88 |
} |
| 89 |
|
| 90 |
int ast_sip_session_add_supplements(struct ast_sip_session *session) |
| 91 |
{ |
| 92 |
struct ast_sip_session_supplement *iter; |
| 93 |
SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK); |
| 94 |
|
| 95 |
AST_RWLIST_TRAVERSE(&session_supplements, iter, next) { |
| 96 |
struct ast_sip_session_supplement *copy = supplement_dup(iter); |
| 97 |
|
| 98 |
if (!copy) { |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
ast_module_ref(copy->module); |
| 104 |
|
| 105 |
AST_LIST_INSERT_TAIL(&session->supplements, copy, next); |
| 106 |
} |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
int ast_sip_session_check_supplement_create(struct ast_sip_endpoint *endpoint, |
| 112 |
struct ast_sip_contact *contact, const char *location, |
| 113 |
const char *request_user, struct ast_stream_topology *req_topology) |
| 114 |
{ |
| 115 |
struct ast_sip_session_supplement *iter; |
| 116 |
SCOPED_LOCK(lock, &session_supplements, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK); |
| 117 |
|
| 118 |
AST_RWLIST_TRAVERSE(&session_supplements, iter, next) { |
| 119 |
if (iter->session_create && iter->session_create(endpoint, contact, location, |
| 120 |
request_user, req_topology)) { |
| 121 |
return -1; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
void ast_sip_session_remove_supplements(struct ast_sip_session *session) |
| 129 |
{ |
| 130 |
struct ast_sip_session_supplement *iter; |
| 131 |
|
| 132 |
if (!session) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
while ((iter = AST_LIST_REMOVE_HEAD(&session->supplements, next))) { |
| 138 |
if (iter->module) { |
| 139 |
|
| 140 |
ast_module_unref(iter->module); |
| 141 |
} |
| 142 |
|
| 143 |
ast_free(iter); |
| 144 |
} |
| 145 |
|
| 146 |
return; |
| 147 |
} |
| 148 |
|