jresponse.c 13.5 KB
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
/*
  eXosip - This is the eXtended osip library.
  Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com

  eXosip is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  eXosip is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  In addition, as a special exception, the copyright holders give
  permission to link the code of portions of this program with the
  OpenSSL library under certain conditions as described in each
  individual source file, and distribute linked combinations
  including the two.
  You must obey the GNU General Public License in all respects
  for all of the code used other than OpenSSL.  If you modify
  file(s) with this exception, you may extend this exception to your
  version of the file(s), but you are not obligated to do so.  If you
  do not wish to do so, delete this exception statement from your
  version.  If you delete this exception statement from all source
  files in the program, then also delete it here.
*/

#include "eXosip2.h"

int _eXosip_build_response_default(struct eXosip_t *excontext, osip_message_t **dest, osip_dialog_t *dialog, int status, osip_message_t *request) {
  osip_generic_param_t *tag;
  osip_message_t *response;
  int i;

  *dest = NULL;

  if (request == NULL)
    return OSIP_BADPARAMETER;

  i = osip_message_init(&response);

  if (i != 0)
    return i;

  /* initialise osip_message_t structure */
  /* yet done... */

  response->sip_version = (char *) osip_malloc(8 * sizeof(char));

  if (response->sip_version == NULL) {
    osip_message_free(response);
    return OSIP_NOMEM;
  }

  sprintf(response->sip_version, "SIP/2.0");
  osip_message_set_status_code(response, status);

#ifndef MINISIZE

  /* handle some internal reason definitions. */
  if (MSG_IS_NOTIFY(request) && status == 481) {
    response->reason_phrase = osip_strdup("Subscription Does Not Exist");

  } else if (MSG_IS_SUBSCRIBE(request) && status == 202) {
    response->reason_phrase = osip_strdup("Accepted subscription");

  } else {
    response->reason_phrase = osip_strdup(osip_message_get_reason(status));

    if (response->reason_phrase == NULL) {
      if (response->status_code == 101)
        response->reason_phrase = osip_strdup("Dialog Establishement");

      else
        response->reason_phrase = osip_strdup("Unknown code");
    }

    response->req_uri = NULL;
    response->sip_method = NULL;
  }

#else
  response->reason_phrase = osip_strdup(osip_message_get_reason(status));

  if (response->reason_phrase == NULL) {
    if (response->status_code == 101)
      response->reason_phrase = osip_strdup("Dialog Establishement");

    else
      response->reason_phrase = osip_strdup("Unknown code");
  }

  response->req_uri = NULL;
  response->sip_method = NULL;
#endif

  if (response->reason_phrase == NULL) {
    osip_message_free(response);
    return OSIP_NOMEM;
  }

  i = osip_to_clone(request->to, &(response->to));

  if (i != 0) {
    osip_message_free(response);
    return i;
  }

  i = osip_to_get_tag(response->to, &tag);

  if (i != 0) { /* we only add a tag if it does not already contains one! */
    if ((dialog != NULL) && (dialog->local_tag != NULL))
    /* it should contain the local TAG we created */
    {
      osip_to_set_tag(response->to, osip_strdup(dialog->local_tag));

    } else {
      if (status != 100)
        osip_to_set_tag(response->to, _eXosip_malloc_new_random());
    }
  }

  i = osip_from_clone(request->from, &(response->from));

  if (i != 0) {
    osip_message_free(response);
    return i;
  }

  {
    osip_list_iterator_t it;
    osip_via_t *via = (osip_via_t *) osip_list_get_first(&request->vias, &it);

    while (via != NULL) {
      osip_via_t *via2;

      i = osip_via_clone(via, &via2);

      if (i != 0) {
        osip_message_free(response);
        return i;
      }

      osip_list_add(&response->vias, via2, -1);
      via = (osip_via_t *) osip_list_get_next(&it);
    }
  }

  i = osip_call_id_clone(request->call_id, &(response->call_id));

  if (i != 0) {
    osip_message_free(response);
    return i;
  }

  i = osip_cseq_clone(request->cseq, &(response->cseq));

  if (i != 0) {
    osip_message_free(response);
    return i;
  }

#ifndef MINISIZE

  if (MSG_IS_SUBSCRIBE(request)) {
    osip_header_t *exp;
    osip_header_t *evt_hdr;

    osip_message_header_get_byname(request, "event", 0, &evt_hdr);

    if (evt_hdr != NULL && evt_hdr->hvalue != NULL)
      osip_message_set_header(response, "Event", evt_hdr->hvalue);

    else
      osip_message_set_header(response, "Event", "presence");

    i = osip_message_get_expires(request, 0, &exp);

    if (exp == NULL) {
      osip_header_t *cp;

      i = osip_header_clone(exp, &cp);

      if (cp != NULL)
        osip_list_add(&response->headers, cp, 0);
    }
  }

#endif

  osip_message_set_user_agent(response, excontext->user_agent);

  *dest = response;
  return OSIP_SUCCESS;
}

int _eXosip_complete_answer_that_establish_a_dialog(struct eXosip_t *excontext, osip_message_t *response, osip_message_t *request) {
  int i;
  int route_found = 0;
  char contact[1600];
  char scheme[10];
  osip_list_iterator_t it;
  osip_record_route_t *rr;

  snprintf(scheme, sizeof(scheme), "sip");

  /* 12.1.1:
     copy all record-route in response
     add a contact with global scope
   */
  rr = (osip_record_route_t *) osip_list_get_first(&request->record_routes, &it);

  while (rr != NULL) {
    osip_record_route_t *rr2;

    i = osip_record_route_clone(rr, &rr2);

    if (i != 0)
      return i;

    osip_list_add(&response->record_routes, rr2, -1);

    /* rfc3261: 12.1.1 UAS behavior (check sips in top most Record-Route) */
    if (it.pos == 0 && rr2 != NULL && rr2->url != NULL && rr2->url->scheme != NULL && osip_strcasecmp(rr2->url->scheme, "sips") == 0)
      snprintf(scheme, sizeof(scheme), "sips");

    rr = (osip_record_route_t *) osip_list_get_next(&it);
    route_found = 1;
  }

  if (MSG_IS_BYE(request)) {
    return OSIP_SUCCESS;
  }

  if (route_found == 0) {
    /* rfc3261: 12.1.1 UAS behavior (check sips in Contact if no Record-Route) */
    osip_contact_t *co = (osip_contact_t *) osip_list_get(&request->contacts, 0);

    if (co != NULL && co->url != NULL && co->url->scheme != NULL && osip_strcasecmp(co->url->scheme, "sips") == 0)
      snprintf(scheme, sizeof(scheme), "sips");
  }

  /* rfc3261: 12.1.1 UAS behavior (check sips in Request-URI) */
  if (request->req_uri->scheme != NULL && osip_strcasecmp(request->req_uri->scheme, "sips") == 0)
    snprintf(scheme, sizeof(scheme), "sips");

  /* special values to be replaced in transport layer (eXtl_*.c files) */
  if (request->to->url->username == NULL)
    snprintf(contact, 1000, "<%s:999.999.999.999:99999>", scheme);

  else {
    char *tmp2 = __osip_uri_escape_userinfo(request->to->url->username);

    snprintf(contact, 1000, "<%s:%s@999.999.999.999:99999>", scheme, tmp2);
    osip_free(tmp2);
  }

  {
    osip_via_t *via;

    via = (osip_via_t *) osip_list_get(&response->vias, 0);

    if (via == NULL || via->protocol == NULL)
      return OSIP_SYNTAXERROR;

    if (excontext->enable_outbound == 1) {
      contact[strlen(contact) - 1] = '\0';
      strcat(contact, ";ob");
      strcat(contact, ">");
    }

    if (strlen(contact) + strlen(via->protocol) + strlen(";transport=>") < 1024 && 0 != osip_strcasecmp(via->protocol, "UDP")) {
      contact[strlen(contact) - 1] = '\0';
      strcat(contact, ";transport=");
      strcat(contact, via->protocol);
      strcat(contact, ">");
    }

    if (excontext->sip_instance[0] != 0 && strlen(contact) + 285 < 1600) {
      strcat(contact, ";+sip.instance=\"<");
      if (strlen(excontext->sip_instance) == 36 && excontext->sip_instance[8] == '-') {
        strcat(contact, "urn:uuid:");
      }
      strcat(contact, excontext->sip_instance);
      strcat(contact, ">\"");
    }

    if (excontext->co_dialog_extra_params[0] != 0 && strlen(contact) + 542 < 1600) {
      strcat(contact, ";");
      strcat(contact, excontext->co_dialog_extra_params);
    }
  }

  osip_message_set_contact(response, contact);

  if (excontext->default_contact_displayname[0] != '\0') {
    osip_contact_t *new_contact;

    osip_message_get_contact(response, 0, &new_contact);

    if (new_contact != NULL) {
      new_contact->displayname = osip_strdup(excontext->default_contact_displayname);
    }
  }

  if (excontext->eXtl_transport._tl_update_contact != NULL)
    excontext->eXtl_transport._tl_update_contact(excontext, response);

  return OSIP_SUCCESS;
}

int _eXosip_answer_invite_123456xx(struct eXosip_t *excontext, eXosip_call_t *jc, eXosip_dialog_t *jd, int code, osip_message_t **answer, int send) {
  int i;
  osip_transaction_t *tr;

  *answer = NULL;
  tr = _eXosip_find_last_inc_invite(jc, jd);

  if (tr == NULL || tr->orig_request == NULL) {
    OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL, "[eXosip] cannot find transaction to answer\n"));
    return OSIP_NOTFOUND;
  }

  if (code >= 200 && code < 300 && jd != NULL && jd->d_dialog == NULL) { /* element previously removed */
    OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL, "[eXosip] cannot answer this closed transaction\n"));
    return OSIP_WRONG_STATE;
  }

  /* is the transaction already answered? */
  if (tr->state == IST_COMPLETED || tr->state == IST_CONFIRMED || tr->state == IST_TERMINATED) {
    OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL, "[eXosip] transaction already answered\n"));
    return OSIP_WRONG_STATE;
  }

  if (jd == NULL)
    i = _eXosip_build_response_default(excontext, answer, NULL, code, tr->orig_request);

  else
    i = _eXosip_build_response_default(excontext, answer, jd->d_dialog, code, tr->orig_request);

  if (i != 0) {
    OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_INFO1, NULL, "[eXosip] cannot create response for invite\n"));
    *answer = NULL;
    return i;
  }

  /* request that estabish a dialog: */
  /* 12.1.1 UAS Behavior */
  if (code > 100 && code < 300) {
    i = _eXosip_complete_answer_that_establish_a_dialog(excontext, *answer, tr->orig_request);

    if (i != 0) {
      osip_message_free(*answer);
      *answer = NULL;
      return i;
    }
  }

  if (send == 1) {
    osip_event_t *evt_answer;

    if (code >= 200 && code < 300 && jd != NULL) {
      _eXosip_dialog_set_200ok(jd, *answer);
      /* wait for a ACK */
      osip_dialog_set_state(jd->d_dialog, DIALOG_CONFIRMED);
    }

    evt_answer = osip_new_outgoing_sipmessage(*answer);
    evt_answer->transactionid = tr->transactionid;

    osip_transaction_add_event(tr, evt_answer);
    _eXosip_wakeup(excontext);
    *answer = NULL;
  }

  return OSIP_SUCCESS;
}

#ifndef MINISIZE

int _eXosip_insubscription_answer_1xx(struct eXosip_t *excontext, eXosip_notify_t *jn, eXosip_dialog_t *jd, int code) {
  osip_event_t *evt_answer;
  osip_message_t *response;
  int i;
  osip_transaction_t *tr;

  tr = _eXosip_find_last_inc_subscribe(jn, jd);

  if (tr == NULL) {
    OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL, "[eXosip] cannot find transaction to answer"));
    return OSIP_NOTFOUND;
  }

  if (jd == NULL)
    i = _eXosip_build_response_default(excontext, &response, NULL, code, tr->orig_request);

  else
    i = _eXosip_build_response_default(excontext, &response, jd->d_dialog, code, tr->orig_request);

  if (i != 0) {
    OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL, "[eXosip] cannot create response for subscribe\n"));
    return i;
  }

  if (code > 100) {
    /* request that estabish a dialog: */
    /* 12.1.1 UAS Behavior */
    i = _eXosip_complete_answer_that_establish_a_dialog(excontext, response, tr->orig_request);

    if (i != 0) {
    }

    if (jd == NULL) {
      i = _eXosip_dialog_init_as_uas(&jd, tr->orig_request, response);

      if (i != 0) {
        OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL, "[eXosip] cannot create dialog\n"));

      } else
        ADD_ELEMENT(jn->n_dialogs, jd);
    }
  }

  evt_answer = osip_new_outgoing_sipmessage(response);
  evt_answer->transactionid = tr->transactionid;

  osip_transaction_add_event(tr, evt_answer);
  _eXosip_wakeup(excontext);
  return i;
}

int _eXosip_insubscription_answer_3456xx(struct eXosip_t *excontext, eXosip_notify_t *jn, eXosip_dialog_t *jd, int code) {
  osip_event_t *evt_answer;
  osip_message_t *response;
  int i;
  osip_transaction_t *tr;

  tr = _eXosip_find_last_inc_subscribe(jn, jd);

  if (tr == NULL) {
    OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_ERROR, NULL, "[eXosip] cannot find transaction to answer"));
    return OSIP_NOTFOUND;
  }

  if (jd == NULL)
    i = _eXosip_build_response_default(excontext, &response, NULL, code, tr->orig_request);

  else
    i = _eXosip_build_response_default(excontext, &response, jd->d_dialog, code, tr->orig_request);

  if (i != 0) {
    OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_INFO1, NULL, "[eXosip] cannot create response for subscribe\n"));
    return i;
  }

  if ((300 <= code) && (code <= 399)) {
    /* Should add contact fields */
    /* ... */
  }

  evt_answer = osip_new_outgoing_sipmessage(response);
  evt_answer->transactionid = tr->transactionid;

  osip_transaction_add_event(tr, evt_answer);
  _eXosip_wakeup(excontext);
  return OSIP_SUCCESS;
}

#endif