| 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 |
#include "asterisk.h" |
| 30 |
#include "asterisk/module.h" |
| 31 |
#include "asterisk/xml.h" |
| 32 |
#include "asterisk/res_geolocation.h" |
| 33 |
|
| 34 |
#include <pjsip_ua.h> |
| 35 |
#include "asterisk/res_pjsip.h" |
| 36 |
#include "asterisk/res_pjsip_session.h" |
| 37 |
|
| 38 |
static pj_str_t GEOLOCATION_HDR; |
| 39 |
static pj_str_t GEOLOCATION_ROUTING_HDR; |
| 40 |
|
| 41 |
static int find_pidf(const char *session_name, struct pjsip_rx_data *rdata, char *geoloc_uri, |
| 42 |
char **pidf_body, unsigned int *pidf_len) |
| 43 |
{ |
| 44 |
char *local_uri = ast_strdupa(geoloc_uri); |
| 45 |
char *ra = NULL; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
if (!rdata->msg_info.msg->body) { |
| 51 |
ast_log(LOG_WARNING, "%s: There's no message body in which to search for '%s'. Skipping\n", |
| 52 |
session_name, geoloc_uri); |
| 53 |
return -1; |
| 54 |
} |
| 55 |
|
| 56 |
if (local_uri[0] == '<') { |
| 57 |
local_uri++; |
| 58 |
} |
| 59 |
ra = strchr(local_uri, '>'); |
| 60 |
if (ra) { |
| 61 |
*ra = '\0'; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
if (ast_sip_are_media_types_equal(&rdata->msg_info.ctype->media, |
| 71 |
&pjsip_media_type_application_pidf_xml)) { |
| 72 |
*pidf_body = rdata->msg_info.msg->body->data; |
| 73 |
*pidf_len = rdata->msg_info.msg->body->len; |
| 74 |
} else if (ast_sip_are_media_types_equal(&rdata->msg_info.ctype->media, |
| 75 |
&pjsip_media_type_multipart_mixed)) { |
| 76 |
pj_str_t cid = pj_str(local_uri); |
| 77 |
pjsip_multipart_part *mp = pjsip_multipart_find_part_by_cid_str( |
| 78 |
rdata->tp_info.pool, rdata->msg_info.msg->body, &cid); |
| 79 |
|
| 80 |
if (!mp) { |
| 81 |
ast_log(LOG_WARNING, "%s: A Geolocation header was found with URI '%s'" |
| 82 |
" but the associated multipart part was not found in the message body. Skipping URI", |
| 83 |
session_name, geoloc_uri); |
| 84 |
return -1; |
| 85 |
} |
| 86 |
*pidf_body = mp->body->data; |
| 87 |
*pidf_len = mp->body->len; |
| 88 |
} else { |
| 89 |
ast_log(LOG_WARNING, "%s: A Geolocation header was found with URI '%s'" |
| 90 |
" but no pidf document with that content id was found. Skipping URI", |
| 91 |
session_name, geoloc_uri); |
| 92 |
return -1; |
| 93 |
} |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
static int add_eprofile_to_channel(struct ast_sip_session *session, |
| 99 |
struct ast_geoloc_eprofile *eprofile, struct ast_str * buf) |
| 100 |
{ |
| 101 |
const char *session_name = (session ? ast_sip_session_get_name(session) : "NULL_SESSION"); |
| 102 |
struct ast_datastore *ds = NULL; |
| 103 |
int rc = 0; |
| 104 |
SCOPE_ENTER(4, "%s\n", session_name); |
| 105 |
|
| 106 |
ds = ast_geoloc_datastore_create(session_name); |
| 107 |
if (!ds) { |
| 108 |
SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, |
| 109 |
"%s: Couldn't allocate a geoloc datastore\n", session_name); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
ast_geoloc_datastore_set_inheritance(ds, 1); |
| 117 |
|
| 118 |
rc = ast_geoloc_datastore_add_eprofile(ds, eprofile); |
| 119 |
if (rc <= 0) { |
| 120 |
ast_datastore_free(ds); |
| 121 |
SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_WARNING, |
| 122 |
"%s: Couldn't add eprofile '%s' to datastore\n", session_name, |
| 123 |
eprofile->id); |
| 124 |
} |
| 125 |
|
| 126 |
ast_channel_lock(session->channel); |
| 127 |
ast_channel_datastore_add(session->channel, ds); |
| 128 |
ast_channel_unlock(session->channel); |
| 129 |
|
| 130 |
SCOPE_EXIT_RTN_VALUE(0, "%s: eprofile: '%s' EffectiveLoc: %s\n", |
| 131 |
session_name, eprofile->id, ast_str_buffer( |
| 132 |
ast_variable_list_join(eprofile->effective_location, ",", "=", NULL, &buf))); |
| 133 |
} |
| 134 |
|
| 135 |
static int handle_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata) |
| 136 |
{ |
| 137 |
const char *session_name = (session ? ast_sip_session_get_name(session) : "NULL_SESSION"); |
| 138 |
struct ast_sip_endpoint *endpoint = (session ? session->endpoint : NULL); |
| 139 |
struct ast_channel *channel = (session ? session->channel : NULL); |
| 140 |
RAII_VAR(struct ast_geoloc_profile *, config_profile, NULL, ao2_cleanup); |
| 141 |
RAII_VAR(struct ast_geoloc_eprofile *, config_eprofile, NULL, ao2_cleanup); |
| 142 |
RAII_VAR(struct ast_datastore *, ds, NULL, ast_datastore_free); |
| 143 |
RAII_VAR(struct ast_geoloc_eprofile *, incoming_eprofile, NULL, ao2_cleanup); |
| 144 |
char *geoloc_hdr_value = NULL; |
| 145 |
char *geoloc_routing_hdr_value = NULL; |
| 146 |
char *geoloc_uri = NULL; |
| 147 |
int rc = 0; |
| 148 |
RAII_VAR(struct ast_str *, buf, NULL, ast_free); |
| 149 |
pjsip_generic_string_hdr *geoloc_hdr = NULL; |
| 150 |
pjsip_generic_string_hdr *geoloc_routing_hdr = NULL; |
| 151 |
SCOPE_ENTER(3, "%s\n", session_name); |
| 152 |
|
| 153 |
if (!session) { |
| 154 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_WARNING, "%s: session is NULL!!!. Skipping.\n", |
| 155 |
session_name); |
| 156 |
} |
| 157 |
if (!endpoint) { |
| 158 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_WARNING, "%s: Session has no endpoint. Skipping.\n", |
| 159 |
session_name); |
| 160 |
} |
| 161 |
|
| 162 |
if (!channel) { |
| 163 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_WARNING, "%s: Session has no channel. Skipping.\n", |
| 164 |
session_name); |
| 165 |
} |
| 166 |
|
| 167 |
if (!rdata) { |
| 168 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_WARNING, "%s: Session has no rdata. Skipping.\n", |
| 169 |
session_name); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
geoloc_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &GEOLOCATION_HDR, NULL); |
| 177 |
geoloc_routing_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, |
| 178 |
&GEOLOCATION_ROUTING_HDR, NULL); |
| 179 |
|
| 180 |
if (!geoloc_hdr) { |
| 181 |
ast_trace(4, "%s: Message has no Geolocation header\n", session_name); |
| 182 |
} else { |
| 183 |
ast_trace(4, "%s: Geolocation: " PJSTR_PRINTF_SPEC "\n", session_name, |
| 184 |
PJSTR_PRINTF_VAR(geoloc_hdr->hvalue)); |
| 185 |
} |
| 186 |
|
| 187 |
if (ast_strlen_zero(endpoint->geoloc_incoming_call_profile)) { |
| 188 |
if (geoloc_hdr) { |
| 189 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_NOTICE, "%s: Message has Geolocation header '" |
| 190 |
PJSTR_PRINTF_SPEC "' but endpoint has no geoloc_incoming_call_profile. " |
| 191 |
"Done.\n", session_name, |
| 192 |
PJSTR_PRINTF_VAR(geoloc_hdr->hvalue)); |
| 193 |
} else { |
| 194 |
SCOPE_EXIT_RTN_VALUE(0, "%s: Endpoint has no geoloc_incoming_call_profile. " |
| 195 |
"Done.\n", session_name); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
config_profile = ast_geoloc_get_profile(endpoint->geoloc_incoming_call_profile); |
| 200 |
if (!config_profile) { |
| 201 |
if (geoloc_hdr) { |
| 202 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_NOTICE, "%s: Message has Geolocation header '" |
| 203 |
PJSTR_PRINTF_SPEC "' but endpoint's geoloc_incoming_call_profile doesn't exist. " |
| 204 |
"Done.\n", session_name, |
| 205 |
PJSTR_PRINTF_VAR(geoloc_hdr->hvalue)); |
| 206 |
} else { |
| 207 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_NOTICE, "%s: Message has no Geolocation header and endpoint has " |
| 208 |
" an invalid geoloc_incoming_call_profile. Done.\n", session_name); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
buf = ast_str_create(1024); |
| 213 |
if (!buf) { |
| 214 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_WARNING, "%s: Unable to allocate buf\n", session_name); |
| 215 |
} |
| 216 |
|
| 217 |
if (config_profile->precedence != AST_GEOLOC_PRECED_DISCARD_CONFIG) { |
| 218 |
config_eprofile = ast_geoloc_eprofile_create_from_profile(config_profile); |
| 219 |
if (!config_eprofile) { |
| 220 |
ast_log(LOG_WARNING, "%s: Unable to create config_eprofile from " |
| 221 |
"profile '%s'\n", session_name, ast_sorcery_object_get_id(config_profile)); |
| 222 |
} |
| 223 |
|
| 224 |
if (config_eprofile && config_eprofile->effective_location) { |
| 225 |
ast_trace(4, "%s: config eprofile '%s' has effective location\n", |
| 226 |
session_name, config_eprofile->id); |
| 227 |
|
| 228 |
if (!geoloc_hdr || config_profile->precedence == AST_GEOLOC_PRECED_DISCARD_INCOMING || |
| 229 |
config_profile->precedence == AST_GEOLOC_PRECED_PREFER_CONFIG) { |
| 230 |
|
| 231 |
ast_trace(4, "%s: config eprofile '%s' is being used\n", |
| 232 |
session_name, config_eprofile->id); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
rc = add_eprofile_to_channel(session, config_eprofile, buf); |
| 242 |
if (rc != 0) { |
| 243 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_WARNING, |
| 244 |
"%s: Couldn't add config eprofile '%s' to datastore. Fail.\n", session_name, |
| 245 |
config_eprofile->id); |
| 246 |
} |
| 247 |
|
| 248 |
SCOPE_EXIT_RTN_VALUE(0, "%s: Added geoloc datastore with eprofile from config. Done.\n", |
| 249 |
session_name); |
| 250 |
} |
| 251 |
} else { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
ast_trace(4, "%s: Either config_eprofile didn't exist or it had no effective location\n", |
| 257 |
session_name); |
| 258 |
|
| 259 |
ao2_cleanup(config_eprofile); |
| 260 |
config_eprofile = NULL; |
| 261 |
if (config_profile->precedence == AST_GEOLOC_PRECED_DISCARD_INCOMING) { |
| 262 |
SCOPE_EXIT_RTN_VALUE(0, "%s: DISCARD_INCOMING set and no config eprofile. Done.\n", |
| 263 |
session_name); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if (geoloc_hdr && config_profile->precedence != AST_GEOLOC_PRECED_DISCARD_INCOMING) { |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
geoloc_hdr_value = ast_alloca(geoloc_hdr->hvalue.slen + 1); |
| 289 |
ast_copy_pj_str(geoloc_hdr_value, &geoloc_hdr->hvalue, geoloc_hdr->hvalue.slen + 1); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
while (geoloc_hdr_value && !incoming_eprofile) { |
| 297 |
char *pidf_body = NULL; |
| 298 |
unsigned int pidf_len = 0; |
| 299 |
struct ast_xml_doc *incoming_doc = NULL; |
| 300 |
int rc = 0; |
| 301 |
|
| 302 |
|
| 303 |
geoloc_uri = ast_strsep(&geoloc_hdr_value, ',', AST_STRSEP_TRIM); |
| 304 |
if (ast_strlen_zero(geoloc_uri) || geoloc_uri[0] != '<' || strchr(geoloc_uri, '>') == NULL) { |
| 305 |
ast_log(LOG_WARNING, "%s: Geolocation header has no or bad URI '%s'. Skipping\n", session_name, |
| 306 |
S_OR(geoloc_uri, "<empty>")); |
| 307 |
continue; |
| 308 |
} |
| 309 |
|
| 310 |
ast_trace(4, "Processing URI '%s'\n", geoloc_uri); |
| 311 |
|
| 312 |
if (!ast_begins_with(geoloc_uri, "<cid:")) { |
| 313 |
ast_trace(4, "Processing URI '%s'\n", geoloc_uri); |
| 314 |
|
| 315 |
incoming_eprofile = ast_geoloc_eprofile_create_from_uri(geoloc_uri, session_name); |
| 316 |
if (!incoming_eprofile) { |
| 317 |
ast_log(LOG_WARNING, "%s: Unable to create effective profile for URI '%s'. Skipping\n", |
| 318 |
session_name, geoloc_uri); |
| 319 |
continue; |
| 320 |
} |
| 321 |
} else { |
| 322 |
ast_trace(4, "Processing PIDF-LO '%s'\n", geoloc_uri); |
| 323 |
|
| 324 |
rc = find_pidf(session_name, rdata, geoloc_uri, &pidf_body, &pidf_len); |
| 325 |
if (rc != 0 || !pidf_body || pidf_len == 0) { |
| 326 |
continue; |
| 327 |
} |
| 328 |
ast_trace(5, "Processing PIDF-LO "PJSTR_PRINTF_SPEC "\n", (int)pidf_len, pidf_body); |
| 329 |
|
| 330 |
incoming_doc = ast_xml_read_memory(pidf_body, pidf_len); |
| 331 |
if (!incoming_doc) { |
| 332 |
ast_log(LOG_WARNING, "%s: Unable to parse pidf document for URI '%s'\n", |
| 333 |
session_name, geoloc_uri); |
| 334 |
continue; |
| 335 |
} |
| 336 |
|
| 337 |
incoming_eprofile = ast_geoloc_eprofile_create_from_pidf(incoming_doc, geoloc_uri, session_name); |
| 338 |
ast_xml_close(incoming_doc); |
| 339 |
|
| 340 |
if (!incoming_eprofile) { |
| 341 |
ast_log(LOG_WARNING, |
| 342 |
"%s: Couldn't create incoming_eprofile from pidf\n", session_name); |
| 343 |
continue; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if (!incoming_eprofile) { |
| 350 |
|
| 351 |
incoming_eprofile = config_eprofile; |
| 352 |
} else { |
| 353 |
ao2_cleanup(config_eprofile); |
| 354 |
config_eprofile = NULL; |
| 355 |
if (geoloc_routing_hdr) { |
| 356 |
geoloc_routing_hdr_value = ast_alloca(geoloc_routing_hdr->hvalue.slen + 1); |
| 357 |
ast_copy_pj_str(geoloc_routing_hdr_value, &geoloc_routing_hdr->hvalue, |
| 358 |
geoloc_routing_hdr->hvalue.slen + 1); |
| 359 |
incoming_eprofile->allow_routing_use = ast_true(geoloc_routing_hdr_value); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
if (incoming_eprofile) { |
| 364 |
rc = add_eprofile_to_channel(session, incoming_eprofile, buf); |
| 365 |
if (rc != 0) { |
| 366 |
SCOPE_EXIT_LOG_RTN_VALUE(0, LOG_WARNING, |
| 367 |
"%s: Couldn't add eprofile '%s' to channel. Fail.\n", session_name, |
| 368 |
incoming_eprofile->id); |
| 369 |
} |
| 370 |
|
| 371 |
SCOPE_EXIT_RTN_VALUE(0, "%s: Added eprofile '%s' to channel. Done.\n", |
| 372 |
session_name, incoming_eprofile->id); |
| 373 |
} |
| 374 |
|
| 375 |
SCOPE_EXIT_RTN_VALUE(0, "%s: No eprofiles to add to channel. Done.\n", session_name); |
| 376 |
} |
| 377 |
|
| 378 |
static const char *add_eprofile_to_tdata(struct ast_geoloc_eprofile *eprofile, struct ast_channel *channel, |
| 379 |
struct pjsip_tx_data *tdata, struct ast_str **buf, const char *session_name) |
| 380 |
{ |
| 381 |
static const pj_str_t from_name = { "From", 4}; |
| 382 |
static const pj_str_t cid_name = { "Content-ID", 10 }; |
| 383 |
|
| 384 |
pjsip_sip_uri *sip_uri; |
| 385 |
pjsip_generic_string_hdr *cid; |
| 386 |
pj_str_t cid_value; |
| 387 |
pjsip_from_hdr *from = pjsip_msg_find_hdr_by_name(tdata->msg, &from_name, NULL); |
| 388 |
pjsip_sdp_info *tdata_sdp_info; |
| 389 |
pjsip_msg_body *multipart_body = NULL; |
| 390 |
pjsip_multipart_part *pidf_part; |
| 391 |
pj_str_t pidf_body_text; |
| 392 |
char id[6]; |
| 393 |
size_t alloc_size; |
| 394 |
RAII_VAR(char *, base_cid, NULL, ast_free); |
| 395 |
const char *final_doc; |
| 396 |
int rc = 0; |
| 397 |
SCOPE_ENTER(3, "%s\n", session_name); |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
final_doc = ast_geoloc_eprofile_to_pidf(eprofile, channel, buf, session_name); |
| 405 |
ast_trace(5, "Final pidf: \n%s\n", final_doc); |
| 406 |
|
| 407 |
if (!final_doc) { |
| 408 |
SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR, "%s: Unable to create pidf document from" |
| 409 |
" eprofile '%s'\n\n", session_name, eprofile->id); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
tdata_sdp_info = pjsip_tdata_get_sdp_info(tdata); |
| 427 |
if (tdata_sdp_info->sdp) { |
| 428 |
ast_trace(4, "body: %p %u\n", tdata_sdp_info->sdp, (unsigned)tdata_sdp_info->sdp_err); |
| 429 |
|
| 430 |
rc = pjsip_create_multipart_sdp_body(tdata->pool, tdata_sdp_info->sdp, &multipart_body); |
| 431 |
if (rc != PJ_SUCCESS) { |
| 432 |
SCOPE_EXIT_LOG_RTN_VALUE(NULL, LOG_ERROR, "%s: Unable to create sdp multipart body\n", |
| 433 |
session_name); |
| 434 |
} |
| 435 |
} else { |
| 436 |
multipart_body = pjsip_multipart_create(tdata->pool, &pjsip_media_type_multipart_mixed, NULL); |
| 437 |
} |
| 438 |
|
| 439 |
pidf_part = pjsip_multipart_create_part(tdata->pool); |
| 440 |
pj_cstr(&pidf_body_text, final_doc); |
| 441 |
pidf_part->body = pjsip_msg_body_create(tdata->pool, &pjsip_media_type_application_pidf_xml.type, |
| 442 |
&pjsip_media_type_application_pidf_xml.subtype, &pidf_body_text); |
| 443 |
|
| 444 |
pjsip_multipart_add_part(tdata->pool, multipart_body, pidf_part); |
| 445 |
|
| 446 |
sip_uri = (pjsip_sip_uri *)pjsip_uri_get_uri(from->uri); |
| 447 |
alloc_size = sizeof(id) + pj_strlen(&sip_uri->host) + 2; |
| 448 |
base_cid = ast_malloc(alloc_size); |
| 449 |
sprintf(base_cid, "%s@%.*s", |
| 450 |
ast_generate_random_string(id, sizeof(id)), |
| 451 |
(int) pj_strlen(&sip_uri->host), pj_strbuf(&sip_uri->host)); |
| 452 |
|
| 453 |
ast_str_set(buf, 0, "cid:%s", base_cid); |
| 454 |
ast_trace(4, "cid: '%s' uri: '%s'\n", base_cid, ast_str_buffer(*buf)); |
| 455 |
|
| 456 |
cid_value.ptr = pj_pool_alloc(tdata->pool, alloc_size); |
| 457 |
cid_value.slen = sprintf(cid_value.ptr, "<%s>", base_cid); |
| 458 |
|
| 459 |
cid = pjsip_generic_string_hdr_create(tdata->pool, &cid_name, &cid_value); |
| 460 |
|
| 461 |
pj_list_insert_after(&pidf_part->hdr, cid); |
| 462 |
|
| 463 |
tdata->msg->body = multipart_body; |
| 464 |
|
| 465 |
SCOPE_EXIT_RTN_VALUE(ast_str_buffer(*buf), "%s: PIDF-LO added with cid '%s'\n", session_name, base_cid); |
| 466 |
} |
| 467 |
|
| 468 |
static void handle_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata) |
| 469 |
{ |
| 470 |
const char *session_name = ast_sip_session_get_name(session); |
| 471 |
struct ast_sip_endpoint *endpoint = session->endpoint; |
| 472 |
struct ast_channel *channel = session->channel; |
| 473 |
RAII_VAR(struct ast_geoloc_profile *, config_profile, NULL, ao2_cleanup); |
| 474 |
RAII_VAR(struct ast_geoloc_eprofile *, config_eprofile, NULL, ao2_cleanup); |
| 475 |
RAII_VAR(struct ast_geoloc_eprofile *, incoming_eprofile, NULL, ao2_cleanup); |
| 476 |
struct ast_geoloc_eprofile *final_eprofile = NULL; |
| 477 |
RAII_VAR(struct ast_str *, buf, NULL, ast_free); |
| 478 |
struct ast_datastore *ds = NULL; |
| 479 |
pjsip_msg_body *orig_body = NULL; |
| 480 |
pjsip_generic_string_hdr *geoloc_hdr = NULL; |
| 481 |
int eprofile_count = 0; |
| 482 |
int rc = 0; |
| 483 |
const char *uri; |
| 484 |
SCOPE_ENTER(3, "%s\n", session_name); |
| 485 |
|
| 486 |
if (!endpoint) { |
| 487 |
SCOPE_EXIT_LOG_RTN(LOG_WARNING, "%s: Session has no endpoint. Skipping.\n", |
| 488 |
session_name); |
| 489 |
} |
| 490 |
|
| 491 |
if (!channel) { |
| 492 |
SCOPE_EXIT_LOG_RTN(LOG_WARNING, "%s: Session has no channel. Skipping.\n", |
| 493 |
session_name); |
| 494 |
} |
| 495 |
|
| 496 |
if (ast_strlen_zero(endpoint->geoloc_outgoing_call_profile)) { |
| 497 |
SCOPE_EXIT_RTN("%s: Endpoint has no geoloc_outgoing_call_profile. Skipping.\n", |
| 498 |
session_name); |
| 499 |
} |
| 500 |
|
| 501 |
config_profile = ast_geoloc_get_profile(endpoint->geoloc_outgoing_call_profile); |
| 502 |
if (!config_profile) { |
| 503 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Endpoint's geoloc_outgoing_call_profile doesn't exist. " |
| 504 |
"Geolocation info discarded.\n", session_name); |
| 505 |
} |
| 506 |
|
| 507 |
config_eprofile = ast_geoloc_eprofile_create_from_profile(config_profile); |
| 508 |
if (!config_eprofile) { |
| 509 |
SCOPE_EXIT_LOG_RTN(LOG_WARNING, "%s: Unable to create eprofile from " |
| 510 |
"profile '%s'\n", session_name, ast_sorcery_object_get_id(config_profile)); |
| 511 |
} |
| 512 |
|
| 513 |
if (!config_eprofile->effective_location) { |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
ast_trace(4, "%s: There was no effective location for config profile '%s'\n", |
| 519 |
session_name, ast_sorcery_object_get_id(config_profile)); |
| 520 |
ao2_ref(config_eprofile, -1); |
| 521 |
config_eprofile = NULL; |
| 522 |
} |
| 523 |
|
| 524 |
ds = ast_geoloc_datastore_find(channel); |
| 525 |
if (!ds) { |
| 526 |
ast_trace(4, "%s: There was no geoloc datastore on the channel\n", session_name); |
| 527 |
} else { |
| 528 |
eprofile_count = ast_geoloc_datastore_size(ds); |
| 529 |
ast_trace(4, "%s: There are %d geoloc profiles on this channel\n", session_name, |
| 530 |
eprofile_count); |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
incoming_eprofile = ast_geoloc_datastore_get_eprofile(ds, 0); |
| 536 |
} |
| 537 |
|
| 538 |
ast_trace(4, "%s: Profile precedence: %s\n\n", session_name, |
| 539 |
ast_geoloc_precedence_to_name(config_profile->precedence)); |
| 540 |
|
| 541 |
switch (config_profile->precedence) { |
| 542 |
case AST_GEOLOC_PRECED_DISCARD_INCOMING: |
| 543 |
final_eprofile = config_eprofile; |
| 544 |
ao2_cleanup(incoming_eprofile); |
| 545 |
incoming_eprofile = NULL; |
| 546 |
break; |
| 547 |
case AST_GEOLOC_PRECED_PREFER_INCOMING: |
| 548 |
if (incoming_eprofile) { |
| 549 |
final_eprofile = incoming_eprofile; |
| 550 |
ao2_cleanup(config_eprofile); |
| 551 |
config_eprofile = NULL; |
| 552 |
} else { |
| 553 |
final_eprofile = config_eprofile; |
| 554 |
} |
| 555 |
break; |
| 556 |
case AST_GEOLOC_PRECED_DISCARD_CONFIG: |
| 557 |
final_eprofile = incoming_eprofile; |
| 558 |
ao2_cleanup(config_eprofile); |
| 559 |
config_eprofile = NULL; |
| 560 |
break; |
| 561 |
case AST_GEOLOC_PRECED_PREFER_CONFIG: |
| 562 |
if (config_eprofile) { |
| 563 |
final_eprofile = config_eprofile; |
| 564 |
ao2_cleanup(incoming_eprofile); |
| 565 |
incoming_eprofile = NULL; |
| 566 |
} else { |
| 567 |
final_eprofile = incoming_eprofile; |
| 568 |
} |
| 569 |
break; |
| 570 |
} |
| 571 |
|
| 572 |
if (!final_eprofile) { |
| 573 |
SCOPE_EXIT_RTN("%s: No eprofiles to send. Done.\n", |
| 574 |
session_name); |
| 575 |
} |
| 576 |
|
| 577 |
if (!final_eprofile->effective_location) { |
| 578 |
ast_geoloc_eprofile_refresh_location(final_eprofile); |
| 579 |
} |
| 580 |
|
| 581 |
buf = ast_str_create(1024); |
| 582 |
if (!buf) { |
| 583 |
SCOPE_EXIT_LOG_RTN(LOG_WARNING, "%s: Unable to allocate buf\n", session_name); |
| 584 |
} |
| 585 |
|
| 586 |
if (final_eprofile->format == AST_GEOLOC_FORMAT_URI) { |
| 587 |
uri = ast_geoloc_eprofile_to_uri(final_eprofile, channel, &buf, session_name); |
| 588 |
if (!uri) { |
| 589 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Unable to create URI from eprofile '%s'\n", |
| 590 |
session_name, final_eprofile->id); |
| 591 |
} |
| 592 |
} else { |
| 593 |
orig_body = tdata->msg->body; |
| 594 |
uri = add_eprofile_to_tdata(final_eprofile, channel, tdata, &buf, session_name); |
| 595 |
if (!uri) { |
| 596 |
tdata->msg->body = orig_body; |
| 597 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Unable to add eprofile '%s' to tdata\n", |
| 598 |
session_name, final_eprofile->id); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
uri = ast_strdupa(ast_str_buffer(buf)); |
| 603 |
ast_str_reset(buf); |
| 604 |
ast_str_set(&buf, 0, "<%s>", uri); |
| 605 |
if (!ast_strlen_zero(final_eprofile->location_source)) { |
| 606 |
ast_str_append(&buf, 0, ";loc-src=%s", final_eprofile->location_source); |
| 607 |
} |
| 608 |
uri = ast_strdupa(ast_str_buffer(buf)); |
| 609 |
ast_trace(4, "%s: Using URI '%s'\n", session_name, uri); |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
geoloc_hdr = ast_sip_add_header2(tdata, "Geolocation", uri); |
| 614 |
if (geoloc_hdr == NULL) { |
| 615 |
if (orig_body) { |
| 616 |
tdata->msg->body = orig_body; |
| 617 |
} |
| 618 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Unable to add Geolocation header\n", session_name); |
| 619 |
} |
| 620 |
rc = ast_sip_add_header(tdata, "Geolocation-Routing", final_eprofile->allow_routing_use ? "yes" : "no"); |
| 621 |
if (rc != 0) { |
| 622 |
if (orig_body) { |
| 623 |
tdata->msg->body = orig_body; |
| 624 |
} |
| 625 |
pj_list_erase(geoloc_hdr); |
| 626 |
SCOPE_EXIT_LOG_RTN(LOG_ERROR, "%s: Unable to add Geolocation-Routing header\n", session_name); |
| 627 |
} |
| 628 |
SCOPE_EXIT_RTN("%s: Geolocation: %s\n", session_name, uri); |
| 629 |
} |
| 630 |
|
| 631 |
static struct ast_sip_session_supplement geolocation_supplement = { |
| 632 |
.method = "INVITE", |
| 633 |
.priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL + 10, |
| 634 |
.incoming_request = handle_incoming_request, |
| 635 |
.outgoing_request = handle_outgoing_request, |
| 636 |
}; |
| 637 |
|
| 638 |
static int reload_module(void) |
| 639 |
{ |
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
static int unload_module(void) |
| 644 |
{ |
| 645 |
int res = 0; |
| 646 |
ast_sip_session_unregister_supplement(&geolocation_supplement); |
| 647 |
|
| 648 |
return res; |
| 649 |
} |
| 650 |
|
| 651 |
static int load_module(void) |
| 652 |
{ |
| 653 |
int res = 0; |
| 654 |
GEOLOCATION_HDR = pj_str("Geolocation"); |
| 655 |
GEOLOCATION_ROUTING_HDR = pj_str("Geolocation-Routing"); |
| 656 |
|
| 657 |
ast_sip_session_register_supplement(&geolocation_supplement); |
| 658 |
|
| 659 |
return res; |
| 660 |
} |
| 661 |
|
| 662 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "res_pjsip_geolocation Module for Asterisk", |
| 663 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 664 |
.load = load_module, |
| 665 |
.unload = unload_module, |
| 666 |
.reload = reload_module, |
| 667 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND - 1, |
| 668 |
.requires = "res_geolocation,res_pjsip,res_pjsip_session,chan_pjsip", |
| 669 |
); |