| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsip-ua/sip_siprec.h> |
| 20 |
#include <pjsip/print_util.h> |
| 21 |
#include <pjsip/sip_endpoint.h> |
| 22 |
#include <pjsip/sip_event.h> |
| 23 |
#include <pjsip/sip_module.h> |
| 24 |
#include <pjsip/sip_transaction.h> |
| 25 |
#include <pjsip/sip_multipart.h> |
| 26 |
#include <pj/log.h> |
| 27 |
#include <pj/math.h> |
| 28 |
#include <pj/os.h> |
| 29 |
#include <pj/pool.h> |
| 30 |
|
| 31 |
|
| 32 |
#define THIS_FILE "sip_siprec.c" |
| 33 |
|
| 34 |
|
| 35 |
static const pj_str_t STR_SIPREC = {"siprec", 6}; |
| 36 |
static const pj_str_t STR_LABEL = {"label", 5}; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static void pjsip_siprec_deinit_module(pjsip_endpoint *endpt) |
| 41 |
{ |
| 42 |
PJ_TODO(provide_initialized_flag_for_each_endpoint); |
| 43 |
PJ_UNUSED_ARG(endpt); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static pj_status_t pjsip_siprec_check_request(pjsip_rx_data *rdata) |
| 51 |
{ |
| 52 |
const pj_str_t str_require = {"Require", 7}; |
| 53 |
const pj_str_t str_src = {"+sip.src", 8}; |
| 54 |
pjsip_require_hdr *req_hdr; |
| 55 |
pjsip_contact_hdr *contact_hdr; |
| 56 |
|
| 57 |
|
| 58 |
req_hdr = (pjsip_require_hdr*) |
| 59 |
pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_require, NULL); |
| 60 |
|
| 61 |
if(!req_hdr || (pjsip_siprec_verify_require_hdr(req_hdr) == PJ_FALSE)){ |
| 62 |
return PJ_FALSE; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
contact_hdr = (pjsip_contact_hdr*) |
| 67 |
pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL); |
| 68 |
|
| 69 |
if(!contact_hdr || !contact_hdr->uri){ |
| 70 |
return PJ_FALSE; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
if(!pjsip_param_find(&contact_hdr->other_param, &str_src)){ |
| 75 |
return PJ_FALSE; |
| 76 |
} |
| 77 |
return PJ_TRUE; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
PJ_DEF(pj_status_t) pjsip_siprec_init_module(pjsip_endpoint *endpt) |
| 85 |
{ |
| 86 |
pj_status_t status; |
| 87 |
|
| 88 |
PJ_ASSERT_RETURN(endpt, PJ_EINVAL); |
| 89 |
|
| 90 |
|
| 91 |
status = pjsip_endpt_add_capability(endpt, NULL, PJSIP_H_SUPPORTED, |
| 92 |
NULL, 1, &STR_SIPREC); |
| 93 |
|
| 94 |
if (status != PJ_SUCCESS) |
| 95 |
return status; |
| 96 |
|
| 97 |
|
| 98 |
if (pjsip_endpt_atexit(endpt, &pjsip_siprec_deinit_module) != PJ_SUCCESS) |
| 99 |
{ |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
pj_assert(!"Failed to register Siprec deinit."); |
| 105 |
PJ_LOG(1, (THIS_FILE, "Failed to register Siprec deinit.")); |
| 106 |
} |
| 107 |
|
| 108 |
return PJ_SUCCESS; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
PJ_DEF(pj_status_t) pjsip_siprec_verify_require_hdr(pjsip_require_hdr *req_hdr) |
| 116 |
{ |
| 117 |
unsigned i; |
| 118 |
for (i=0; i<req_hdr->count; ++i) { |
| 119 |
|
| 120 |
if (pj_stricmp(&req_hdr->values[i], &STR_SIPREC)==0) |
| 121 |
{ |
| 122 |
return PJ_TRUE; |
| 123 |
} |
| 124 |
} |
| 125 |
return PJ_FALSE; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
PJ_DEF(pj_status_t) pjsip_siprec_verify_request(pjsip_rx_data *rdata, |
| 133 |
pj_str_t *metadata, |
| 134 |
pjmedia_sdp_session *sdp_offer, |
| 135 |
unsigned *options, |
| 136 |
pjsip_dialog *dlg, |
| 137 |
pjsip_endpoint *endpt, |
| 138 |
pjsip_tx_data **p_tdata) |
| 139 |
{ |
| 140 |
int code = 200; |
| 141 |
pj_status_t status = PJ_SUCCESS; |
| 142 |
const char *warn_text = NULL; |
| 143 |
pjsip_hdr res_hdr_list; |
| 144 |
unsigned mi; |
| 145 |
|
| 146 |
|
| 147 |
if (p_tdata) *p_tdata = NULL; |
| 148 |
|
| 149 |
|
| 150 |
pj_list_init(&res_hdr_list); |
| 151 |
|
| 152 |
|
| 153 |
if (!(*options & PJSIP_INV_SUPPORT_SIPREC)){ |
| 154 |
return PJ_SUCCESS; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
if (pjsip_siprec_check_request(rdata) == PJ_FALSE){ |
| 159 |
|
| 160 |
if (*options & PJSIP_INV_REQUIRE_SIPREC){ |
| 161 |
code = PJSIP_SC_BAD_REQUEST; |
| 162 |
warn_text = "The INVITE request must be SIPREC"; |
| 163 |
goto on_return; |
| 164 |
}else { |
| 165 |
|
| 166 |
return PJ_SUCCESS; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
if (!rdata->msg_info.msg->body) { |
| 172 |
code = PJSIP_SC_BAD_REQUEST; |
| 173 |
warn_text = "SIPREC INVITE must have a body"; |
| 174 |
goto on_return; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
if(!sdp_offer){ |
| 179 |
code = PJSIP_SC_BAD_REQUEST; |
| 180 |
warn_text = "SIPREC INVITE without SDP is not supported"; |
| 181 |
goto on_return; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
for (mi=0; mi<sdp_offer->media_count; ++mi) { |
| 186 |
if (!pjmedia_sdp_media_find_attr(sdp_offer->media[mi], |
| 187 |
&STR_LABEL, NULL)){ |
| 188 |
code = PJSIP_SC_BAD_REQUEST; |
| 189 |
warn_text = "SDP must have label media attribute"; |
| 190 |
goto on_return; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
status = pjsip_siprec_get_metadata(rdata->tp_info.pool, |
| 195 |
rdata->msg_info.msg->body, |
| 196 |
metadata); |
| 197 |
|
| 198 |
if(status != PJ_SUCCESS) { |
| 199 |
code = PJSIP_SC_BAD_REQUEST; |
| 200 |
warn_text = "SIPREC INVITE must have a 'rs-metadata' Content-Type"; |
| 201 |
goto on_return; |
| 202 |
} |
| 203 |
|
| 204 |
*options |= PJSIP_INV_REQUIRE_SIPREC; |
| 205 |
|
| 206 |
return status; |
| 207 |
|
| 208 |
on_return: |
| 209 |
|
| 210 |
|
| 211 |
if (code != 200) { |
| 212 |
pjsip_tx_data *tdata; |
| 213 |
const pjsip_hdr *h; |
| 214 |
|
| 215 |
if (dlg) { |
| 216 |
status = pjsip_dlg_create_response(dlg, rdata, code, NULL, |
| 217 |
&tdata); |
| 218 |
} else { |
| 219 |
status = pjsip_endpt_create_response(endpt, rdata, code, NULL, |
| 220 |
&tdata); |
| 221 |
} |
| 222 |
|
| 223 |
if (status != PJ_SUCCESS) |
| 224 |
return status; |
| 225 |
|
| 226 |
|
| 227 |
h = res_hdr_list.next; |
| 228 |
while (h != &res_hdr_list) { |
| 229 |
pjsip_hdr *cloned; |
| 230 |
|
| 231 |
cloned = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h); |
| 232 |
PJ_ASSERT_RETURN(cloned, PJ_ENOMEM); |
| 233 |
|
| 234 |
pjsip_msg_add_hdr(tdata->msg, cloned); |
| 235 |
|
| 236 |
h = h->next; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if (warn_text) { |
| 241 |
pjsip_warning_hdr *warn_hdr; |
| 242 |
pj_str_t warn_value = pj_str((char*)warn_text); |
| 243 |
|
| 244 |
warn_hdr=pjsip_warning_hdr_create(tdata->pool, 399, |
| 245 |
pjsip_endpt_name(endpt), |
| 246 |
&warn_value); |
| 247 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)warn_hdr); |
| 248 |
} |
| 249 |
|
| 250 |
*p_tdata = tdata; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if (status == PJ_SUCCESS) |
| 256 |
status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
return status; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
PJ_DEF(pj_status_t) pjsip_siprec_get_metadata(pj_pool_t *pool, |
| 268 |
pjsip_msg_body *body, |
| 269 |
pj_str_t* metadata) |
| 270 |
{ |
| 271 |
pjsip_media_type app_metadata; |
| 272 |
pjsip_multipart_part *metadata_part; |
| 273 |
|
| 274 |
PJ_UNUSED_ARG(pool); |
| 275 |
pjsip_media_type_init2(&app_metadata, "application", "rs-metadata"); |
| 276 |
metadata_part = pjsip_multipart_find_part(body, &app_metadata, NULL); |
| 277 |
|
| 278 |
|
| 279 |
if (!metadata_part) { |
| 280 |
pjsip_media_type_init2(&app_metadata, "application", |
| 281 |
"rs-metadata+xml"); |
| 282 |
metadata_part = pjsip_multipart_find_part(body, &app_metadata, NULL); |
| 283 |
} |
| 284 |
|
| 285 |
if(!metadata_part) |
| 286 |
return PJ_ENOTFOUND; |
| 287 |
|
| 288 |
metadata->ptr = (char*)metadata_part->body->data; |
| 289 |
metadata->slen = metadata_part->body->len; |
| 290 |
|
| 291 |
return PJ_SUCCESS; |
| 292 |
} |