osip_message.c 12 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
/*
  The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com

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

  This library 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
  Lesser General Public License for more details.

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

#include <osipparser2/internal.h>

#include <osipparser2/osip_port.h>
#include <osipparser2/osip_message.h>

/* enable logging of memory accesses */

const char *osip_protocol_version = "SIP/2.0";

int osip_message_init(osip_message_t **sip) {
  *sip = (osip_message_t *) osip_malloc(sizeof(osip_message_t));

  if (*sip == NULL)
    return OSIP_NOMEM;

  memset(*sip, 0, sizeof(osip_message_t));

#ifndef MINISIZE
  osip_list_init(&(*sip)->accepts);
  osip_list_init(&(*sip)->accept_encodings);

  osip_list_init(&(*sip)->accept_languages);
  osip_list_init(&(*sip)->alert_infos);
  osip_list_init(&(*sip)->allows);
  osip_list_init(&(*sip)->authentication_infos);
#endif
  osip_list_init(&(*sip)->authorizations);
  (*sip)->call_id = NULL;
  osip_list_init(&(*sip)->call_infos);
  osip_list_init(&(*sip)->contacts);

#ifndef MINISIZE
  osip_list_init(&(*sip)->content_encodings);
#endif
  (*sip)->content_length = NULL;
  (*sip)->content_type = NULL;
  (*sip)->cseq = NULL;
#ifndef MINISIZE
  osip_list_init(&(*sip)->error_infos);
#endif
  (*sip)->from = NULL;
  (*sip)->mime_version = NULL;
  osip_list_init(&(*sip)->proxy_authenticates);
#ifndef MINISIZE
  osip_list_init(&(*sip)->proxy_authentication_infos);
#endif
  osip_list_init(&(*sip)->proxy_authorizations);
  osip_list_init(&(*sip)->record_routes);
  osip_list_init(&(*sip)->routes);
  (*sip)->to = NULL;
  osip_list_init(&(*sip)->vias);
  osip_list_init(&(*sip)->www_authenticates);

  osip_list_init(&(*sip)->bodies);

  osip_list_init(&(*sip)->headers);

  (*sip)->message_property = 3;
  (*sip)->message = NULL; /* buffer to avoid calling osip_message_to_str many times (for retransmission) */
  (*sip)->message_length = 0;

  (*sip)->application_data = NULL;
  return OSIP_SUCCESS; /* ok */
}

void osip_message_set_reason_phrase(osip_message_t *sip, char *reason) {
  sip->reason_phrase = reason;
}

void osip_message_set_status_code(osip_message_t *sip, int status_code) {
  sip->status_code = status_code;
}

void osip_message_set_method(osip_message_t *sip, char *sip_method) {
  sip->sip_method = sip_method;
}

void osip_message_set_version(osip_message_t *sip, char *sip_version) {
  sip->sip_version = sip_version;
}

void osip_message_set_uri(osip_message_t *sip, osip_uri_t *url) {
  sip->req_uri = url;
}

void osip_message_free(osip_message_t *sip) {
  if (sip == NULL)
    return;

  osip_free(sip->sip_method);
  osip_free(sip->sip_version);

  if (sip->req_uri != NULL)
    osip_uri_free(sip->req_uri);

  osip_free(sip->reason_phrase);

#ifndef MINISIZE
  osip_list_special_free(&sip->accepts, (void (*)(void *)) & osip_accept_free);
#endif
  osip_list_special_free(&sip->authorizations, (void (*)(void *)) & osip_authorization_free);

  if (sip->call_id != NULL)
    osip_call_id_free(sip->call_id);

#ifndef MINISIZE
  osip_list_special_free(&sip->accept_encodings, (void (*)(void *)) & osip_accept_encoding_free);
  osip_list_special_free(&sip->accept_languages, (void (*)(void *)) & osip_accept_language_free);
  osip_list_special_free(&sip->alert_infos, (void (*)(void *)) & osip_alert_info_free);
  osip_list_special_free(&sip->allows, (void (*)(void *)) & osip_allow_free);
  osip_list_special_free(&sip->authentication_infos, (void (*)(void *)) & osip_authentication_info_free);
  osip_list_special_free(&sip->content_encodings, (void (*)(void *)) & osip_content_encoding_free);
  osip_list_special_free(&sip->error_infos, (void (*)(void *)) & osip_error_info_free);
  osip_list_special_free(&sip->proxy_authentication_infos, (void (*)(void *)) & osip_proxy_authentication_info_free);
#endif
  osip_list_special_free(&sip->call_infos, (void (*)(void *)) & osip_call_info_free);
  osip_list_special_free(&sip->contacts, (void (*)(void *)) & osip_contact_free);

  if (sip->content_length != NULL)
    osip_content_length_free(sip->content_length);

  if (sip->content_type != NULL)
    osip_content_type_free(sip->content_type);

  if (sip->cseq != NULL)
    osip_cseq_free(sip->cseq);

  if (sip->from != NULL)
    osip_from_free(sip->from);

  if (sip->mime_version != NULL)
    osip_mime_version_free(sip->mime_version);

  osip_list_special_free(&sip->proxy_authenticates, (void (*)(void *)) & osip_proxy_authenticate_free);
  osip_list_special_free(&sip->proxy_authorizations, (void (*)(void *)) & osip_proxy_authorization_free);
  osip_list_special_free(&sip->record_routes, (void (*)(void *)) & osip_record_route_free);
  osip_list_special_free(&sip->routes, (void (*)(void *)) & osip_route_free);

  if (sip->to != NULL)
    osip_to_free(sip->to);

  osip_list_special_free(&sip->vias, (void (*)(void *)) & osip_via_free);
  osip_list_special_free(&sip->www_authenticates, (void (*)(void *)) & osip_www_authenticate_free);
  osip_list_special_free(&sip->headers, (void (*)(void *)) & osip_header_free);
  osip_list_special_free(&sip->bodies, (void (*)(void *)) & osip_body_free);
  osip_free(sip->message);
  osip_free(sip);
}

int osip_message_clone(const osip_message_t *sip, osip_message_t **dest) {
  osip_message_t *copy;
  int i;

  *dest = NULL;

  if (sip == NULL)
    return OSIP_BADPARAMETER;

  i = osip_message_init(&copy);

  if (i != 0)
    return i;

  copy->sip_method = osip_strdup(sip->sip_method);

  if (sip->sip_method != NULL && copy->sip_method == NULL) {
    osip_message_free(copy);
    return OSIP_NOMEM;
  }

  copy->sip_version = osip_strdup(sip->sip_version);

  if (sip->sip_version != NULL && copy->sip_version == NULL) {
    osip_message_free(copy);
    return OSIP_NOMEM;
  }

  copy->status_code = sip->status_code;
  copy->reason_phrase = osip_strdup(sip->reason_phrase);

  if (sip->reason_phrase != NULL && copy->reason_phrase == NULL) {
    osip_message_free(copy);
    return OSIP_NOMEM;
  }

  if (sip->req_uri != NULL) {
    i = osip_uri_clone(sip->req_uri, &(copy->req_uri));

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

#ifndef MINISIZE
  i = osip_list_clone(&sip->accepts, &copy->accepts, (int (*)(void *, void **)) & osip_accept_clone);

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

  i = osip_list_clone(&sip->accept_encodings, &copy->accept_encodings, (int (*)(void *, void **)) & osip_accept_encoding_clone);

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

  i = osip_list_clone(&sip->accept_languages, &copy->accept_languages, (int (*)(void *, void **)) & osip_accept_language_clone);

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

  i = osip_list_clone(&sip->alert_infos, &copy->alert_infos, (int (*)(void *, void **)) & osip_alert_info_clone);

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

  i = osip_list_clone(&sip->allows, &copy->allows, (int (*)(void *, void **)) & osip_allow_clone);

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

  i = osip_list_clone(&sip->authentication_infos, &copy->authentication_infos, (int (*)(void *, void **)) & osip_authentication_info_clone);

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

  i = osip_list_clone(&sip->content_encodings, &copy->content_encodings, (int (*)(void *, void **)) & osip_content_encoding_clone);

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

  i = osip_list_clone(&sip->error_infos, &copy->error_infos, (int (*)(void *, void **)) & osip_error_info_clone);

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

  i = osip_list_clone(&sip->proxy_authentication_infos, &copy->proxy_authentication_infos, (int (*)(void *, void **)) & osip_proxy_authentication_info_clone);

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

#endif
  i = osip_list_clone(&sip->call_infos, &copy->call_infos, (int (*)(void *, void **)) & osip_call_info_clone);

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

  i = osip_list_clone(&sip->authorizations, &copy->authorizations, (int (*)(void *, void **)) & osip_authorization_clone);

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

  if (sip->call_id != NULL) {
    i = osip_call_id_clone(sip->call_id, &(copy->call_id));

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

  i = osip_list_clone(&sip->contacts, &copy->contacts, (int (*)(void *, void **)) & osip_contact_clone);

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

  if (sip->content_length != NULL) {
    i = osip_content_length_clone(sip->content_length, &(copy->content_length));

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

  if (sip->content_type != NULL) {
    i = osip_content_type_clone(sip->content_type, &(copy->content_type));

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

  if (sip->cseq != NULL) {
    i = osip_cseq_clone(sip->cseq, &(copy->cseq));

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

  if (sip->from != NULL) {
    i = osip_from_clone(sip->from, &(copy->from));

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

  if (sip->mime_version != NULL) {
    i = osip_mime_version_clone(sip->mime_version, &(copy->mime_version));

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

  i = osip_list_clone(&sip->proxy_authenticates, &copy->proxy_authenticates, (int (*)(void *, void **)) & osip_proxy_authenticate_clone);

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

  i = osip_list_clone(&sip->proxy_authorizations, &copy->proxy_authorizations, (int (*)(void *, void **)) & osip_proxy_authorization_clone);

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

  i = osip_list_clone(&sip->record_routes, &copy->record_routes, (int (*)(void *, void **)) & osip_record_route_clone);

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

  i = osip_list_clone(&sip->routes, &copy->routes, (int (*)(void *, void **)) & osip_route_clone);

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

  if (sip->to != NULL) {
    i = osip_to_clone(sip->to, &(copy->to));

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

  i = osip_list_clone(&sip->vias, &copy->vias, (int (*)(void *, void **)) & osip_via_clone);

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

  i = osip_list_clone(&sip->www_authenticates, &copy->www_authenticates, (int (*)(void *, void **)) & osip_www_authenticate_clone);

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

  i = osip_list_clone(&sip->headers, &copy->headers, (int (*)(void *, void **)) & osip_header_clone);

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

  i = osip_list_clone(&sip->bodies, &copy->bodies, (int (*)(void *, void **)) & osip_body_clone);

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

  copy->message_length = sip->message_length;
  copy->message = osip_strdup(sip->message);

  if (copy->message == NULL && sip->message != NULL) {
    osip_message_free(copy);
    return OSIP_NOMEM;
  }

  copy->message_property = sip->message_property;
  copy->application_data = sip->application_data;

  *dest = copy;
  return OSIP_SUCCESS;
}

int osip_message_get_knownheaderlist(osip_list_t *header_list, int pos, void **dest) {
  *dest = NULL;

  if (osip_list_size(header_list) <= pos)
    return OSIP_UNDEFINED_ERROR; /* does not exist */

  *dest = (void *) osip_list_get(header_list, pos);
  return pos;
}