2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <assert.h>
2N/A#include <errno.h>
2N/A#include <pthread.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <sip.h>
2N/A
2N/A#include "sip_msg.h"
2N/A#include "sip_miscdefs.h"
2N/A#include "sip_parse_generic.h"
2N/A
2N/A/*
2N/A * Response consists of SIP version, response code, response phrase and CRLF.
2N/A */
2N/A#define SIP_RESPONSE "%s %d %s%s"
2N/A
2N/Avoid sip_free_content(_sip_msg_t *);
2N/A
2N/A/*
2N/A * Allocate a new sip msg struct.
2N/A */
2N/Asip_msg_t
2N/Asip_new_msg(void)
2N/A{
2N/A _sip_msg_t *sip_msg;
2N/A
2N/A sip_msg = calloc(1, sizeof (_sip_msg_t));
2N/A if (sip_msg != NULL) {
2N/A sip_msg->sip_msg_ref_cnt = 1;
2N/A (void) pthread_mutex_init(&sip_msg->sip_msg_mutex, NULL);
2N/A }
2N/A return ((sip_msg_t)sip_msg);
2N/A}
2N/A
2N/A/*
2N/A * Free all resources. The lock is taken by SIP_MSG_REFCNT_DECR. The
2N/A * thread that decrements the last refcount should take care that
2N/A * the message is not accessible to other threads before doing so.
2N/A * Else, if the message is still accessible to others, it is
2N/A * possible that the other thread could be waiting to take the
2N/A * lock when we proceed to destroy it.
2N/A */
2N/Avoid
2N/Asip_destroy_msg(_sip_msg_t *_sip_msg)
2N/A{
2N/A#ifdef __solaris__
2N/A assert(mutex_held(&_sip_msg->sip_msg_mutex));
2N/A#endif
2N/A (void) sip_delete_start_line_locked(_sip_msg);
2N/A assert(_sip_msg->sip_msg_ref_cnt == 0);
2N/A sip_delete_all_headers((sip_msg_t)_sip_msg);
2N/A sip_free_content(_sip_msg);
2N/A if (_sip_msg->sip_msg_buf != NULL)
2N/A free(_sip_msg->sip_msg_buf);
2N/A
2N/A if (_sip_msg->sip_msg_old_buf != NULL)
2N/A free(_sip_msg->sip_msg_old_buf);
2N/A
2N/A while (_sip_msg->sip_msg_req_res != NULL) {
2N/A sip_message_type_t *sip_msg_type_ptr;
2N/A
2N/A sip_msg_type_ptr = _sip_msg->sip_msg_req_res->sip_next;
2N/A if (_sip_msg->sip_msg_req_res->is_request) {
2N/A sip_request_t *reqline;
2N/A
2N/A reqline = &_sip_msg->sip_msg_req_res->U.sip_request;
2N/A if (reqline->sip_parse_uri != NULL) {
2N/A sip_free_parsed_uri(reqline->sip_parse_uri);
2N/A reqline->sip_parse_uri = NULL;
2N/A }
2N/A }
2N/A free(_sip_msg->sip_msg_req_res);
2N/A _sip_msg->sip_msg_req_res = sip_msg_type_ptr;
2N/A }
2N/A (void) pthread_mutex_destroy(&_sip_msg->sip_msg_mutex);
2N/A free(_sip_msg);
2N/A}
2N/A
2N/A/*
2N/A * Free a sip msg struct.
2N/A */
2N/Avoid
2N/Asip_free_msg(sip_msg_t sip_msg)
2N/A{
2N/A if (sip_msg == NULL)
2N/A return;
2N/A
2N/A SIP_MSG_REFCNT_DECR((_sip_msg_t *)sip_msg);
2N/A}
2N/A
2N/A/*
2N/A * Hold a sip msg struct.
2N/A */
2N/Avoid
2N/Asip_hold_msg(sip_msg_t sip_msg)
2N/A{
2N/A
2N/A if (sip_msg == NULL)
2N/A return;
2N/A
2N/A SIP_MSG_REFCNT_INCR((_sip_msg_t *)sip_msg);
2N/A}
2N/A
2N/A/*
2N/A * Clone a message
2N/A */
2N/Asip_msg_t
2N/Asip_clone_msg(sip_msg_t sip_msg)
2N/A{
2N/A _sip_msg_t *new_msg;
2N/A _sip_msg_t *_sip_msg;
2N/A sip_content_t *sip_content;
2N/A sip_content_t *msg_content;
2N/A sip_content_t *new_content = NULL;
2N/A int len;
2N/A
2N/A if (sip_msg == NULL)
2N/A return (NULL);
2N/A new_msg = (_sip_msg_t *)sip_new_msg();
2N/A if (new_msg == NULL)
2N/A return (NULL);
2N/A _sip_msg = (_sip_msg_t *)sip_msg;
2N/A /*
2N/A * Get start line
2N/A */
2N/A if (sip_copy_start_line(_sip_msg, new_msg) != 0) {
2N/A sip_free_msg((sip_msg_t)new_msg);
2N/A return (NULL);
2N/A }
2N/A if (sip_copy_all_headers(_sip_msg, new_msg) != 0) {
2N/A sip_free_msg((sip_msg_t)new_msg);
2N/A return (NULL);
2N/A }
2N/A (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex);
2N/A sip_content = _sip_msg->sip_msg_content;
2N/A while (sip_content != NULL) {
2N/A msg_content = calloc(1, sizeof (sip_content_t));
2N/A if (msg_content == NULL) {
2N/A sip_free_msg((sip_msg_t)new_msg);
2N/A (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
2N/A return (NULL);
2N/A }
2N/A len = sip_content->sip_content_end -
2N/A sip_content->sip_content_start;
2N/A msg_content->sip_content_start = malloc(len + 1);
2N/A if (msg_content->sip_content_start == NULL) {
2N/A sip_free_msg((sip_msg_t)new_msg);
2N/A (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
2N/A return (NULL);
2N/A }
2N/A (void) strncpy(msg_content->sip_content_start,
2N/A sip_content->sip_content_start, len);
2N/A msg_content->sip_content_start[len] = '\0';
2N/A msg_content->sip_content_current =
2N/A msg_content->sip_content_start;
2N/A msg_content->sip_content_end = msg_content->sip_content_start +
2N/A len;
2N/A msg_content->sip_content_allocated = B_TRUE;
2N/A new_msg->sip_msg_content_len += len;
2N/A new_msg->sip_msg_len += len;
2N/A if (new_msg->sip_msg_content == NULL)
2N/A new_msg->sip_msg_content = msg_content;
2N/A else
2N/A new_content->sip_content_next = msg_content;
2N/A new_content = msg_content;
2N/A sip_content = sip_content->sip_content_next;
2N/A }
2N/A (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
2N/A /*
2N/A * Since this is a new message, no threads should be referring
2N/A * to this, so it is not necessary to take the lock, however,
2N/A * since sip_msg_to_msgbuf() expects the lock to be held, we'll
2N/A * take it here.
2N/A */
2N/A (void) pthread_mutex_lock(&new_msg->sip_msg_mutex);
2N/A new_msg->sip_msg_buf = sip_msg_to_msgbuf((sip_msg_t)new_msg, NULL);
2N/A if (new_msg->sip_msg_buf == NULL) {
2N/A (void) pthread_mutex_unlock(&new_msg->sip_msg_mutex);
2N/A sip_free_msg((sip_msg_t)new_msg);
2N/A return (NULL);
2N/A }
2N/A new_msg->sip_msg_cannot_be_modified = B_TRUE;
2N/A (void) pthread_mutex_unlock(&new_msg->sip_msg_mutex);
2N/A
2N/A return ((sip_msg_t)new_msg);
2N/A}
2N/A
2N/A/*
2N/A * Return the SIP message as a string. Caller frees the string
2N/A */
2N/Achar *
2N/Asip_msg_to_str(sip_msg_t sip_msg, int *error)
2N/A{
2N/A _sip_msg_t *msg;
2N/A char *msgstr;
2N/A
2N/A if (sip_msg == NULL) {
2N/A if (error != NULL)
2N/A *error = EINVAL;
2N/A return (NULL);
2N/A }
2N/A msg = (_sip_msg_t *)sip_msg;
2N/A (void) pthread_mutex_lock(&msg->sip_msg_mutex);
2N/A msgstr = sip_msg_to_msgbuf(msg, error);
2N/A (void) pthread_mutex_unlock(&msg->sip_msg_mutex);
2N/A return (msgstr);
2N/A}
2N/A
2N/A/*
2N/A * Given a message generate a string that includes all the headers and the
2N/A * content.
2N/A */
2N/Achar *
2N/Asip_msg_to_msgbuf(_sip_msg_t *msg, int *error)
2N/A{
2N/A _sip_header_t *header;
2N/A int len = 0;
2N/A char *p;
2N/A char *e;
2N/A sip_content_t *sip_content;
2N/A#ifdef _DEBUG
2N/A int tlen = 0;
2N/A int clen = 0;
2N/A#endif
2N/A
2N/A if (error != NULL)
2N/A *error = 0;
2N/A
2N/A if (msg == NULL) {
2N/A if (error != NULL)
2N/A *error = EINVAL;
2N/A return (NULL);
2N/A }
2N/A#ifdef __solaris__
2N/A assert(mutex_held(&msg->sip_msg_mutex));
2N/A#endif
2N/A
2N/A p = (char *)malloc(msg->sip_msg_len + 1);
2N/A if (p == NULL) {
2N/A if (error != 0)
2N/A *error = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A e = p;
2N/A
2N/A /*
2N/A * Get the start line
2N/A */
2N/A if (msg->sip_msg_start_line != NULL) {
2N/A len = msg->sip_msg_start_line->sip_hdr_end -
2N/A msg->sip_msg_start_line->sip_hdr_start;
2N/A (void) strncpy(e, msg->sip_msg_start_line->sip_hdr_start, len);
2N/A e += len;
2N/A#ifdef _DEBUG
2N/A tlen += len;
2N/A#endif
2N/A }
2N/A header = sip_search_for_header(msg, NULL, NULL);
2N/A while (header != NULL) {
2N/A if (header->sip_header_state != SIP_HEADER_DELETED) {
2N/A if (header->sip_header_state ==
2N/A SIP_HEADER_DELETED_VAL) {
2N/A len = sip_copy_values(e, header);
2N/A } else {
2N/A len = header->sip_hdr_end -
2N/A header->sip_hdr_start;
2N/A (void) strncpy(e, header->sip_hdr_start, len);
2N/A }
2N/A#ifdef _DEBUG
2N/A tlen += len;
2N/A assert(tlen <= msg->sip_msg_len);
2N/A#endif
2N/A }
2N/A header = sip_search_for_header(msg, NULL, header);
2N/A e += len;
2N/A }
2N/A sip_content = msg->sip_msg_content;
2N/A while (sip_content != NULL) {
2N/A len = sip_content->sip_content_end -
2N/A sip_content->sip_content_start;
2N/A#ifdef _DEBUG
2N/A clen += len;
2N/A assert(clen <= msg->sip_msg_content_len);
2N/A tlen += len;
2N/A assert(tlen <= msg->sip_msg_len);
2N/A#endif
2N/A (void) strncpy(e, sip_content->sip_content_start, len);
2N/A e += len;
2N/A sip_content = sip_content->sip_content_next;
2N/A }
2N/A p[msg->sip_msg_len] = '\0';
2N/A return (p);
2N/A}
2N/A
2N/A/*
2N/A * This is called just before sending the message to the transport. It
2N/A * creates the sip_msg_buf from the SIP headers.
2N/A */
2N/Aint
2N/Asip_adjust_msgbuf(_sip_msg_t *msg)
2N/A{
2N/A _sip_header_t *header;
2N/A int ret;
2N/A#ifdef _DEBUG
2N/A int tlen = 0;
2N/A int clen = 0;
2N/A#endif
2N/A
2N/A if (msg == NULL)
2N/A return (EINVAL);
2N/A
2N/A (void) pthread_mutex_lock(&msg->sip_msg_mutex);
2N/A if ((msg->sip_msg_buf != NULL) && (!msg->sip_msg_modified)) {
2N/A /*
2N/A * We could just be forwarding the message we
2N/A * received.
2N/A */
2N/A (void) pthread_mutex_unlock(&msg->sip_msg_mutex);
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * We are sending a new message or a message that we received
2N/A * but have modified it. We keep the old
2N/A * msgbuf till the message is freed as some
2N/A * headers still point to it.
2N/A */
2N/A
2N/A assert(msg->sip_msg_old_buf == NULL);
2N/A msg->sip_msg_old_buf = msg->sip_msg_buf;
2N/A /*
2N/A * We add the content-length header here, if it has not
2N/A * already been added.
2N/A */
2N/A header = sip_search_for_header(msg, SIP_CONTENT_LENGTH, NULL);
2N/A if (header != NULL) {
2N/A /*
2N/A * Mark the previous header as deleted.
2N/A */
2N/A header->sip_header_state = SIP_HEADER_DELETED;
2N/A header->sip_hdr_sipmsg->sip_msg_len -= header->sip_hdr_end -
2N/A header->sip_hdr_start;
2N/A }
2N/A (void) pthread_mutex_unlock(&msg->sip_msg_mutex);
2N/A ret = sip_add_content_length(msg, msg->sip_msg_content_len);
2N/A if (ret != 0) {
2N/A (void) pthread_mutex_unlock(&msg->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A (void) pthread_mutex_lock(&msg->sip_msg_mutex);
2N/A msg->sip_msg_modified = B_FALSE;
2N/A
2N/A msg->sip_msg_buf = sip_msg_to_msgbuf((sip_msg_t)msg, &ret);
2N/A if (msg->sip_msg_buf == NULL) {
2N/A (void) pthread_mutex_unlock(&msg->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A /*
2N/A * Once the message has been sent it can not be modified
2N/A * any furthur as we keep a pointer to it for retransmission
2N/A */
2N/A msg->sip_msg_cannot_be_modified = B_TRUE;
2N/A
2N/A (void) pthread_mutex_unlock(&msg->sip_msg_mutex);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Copy header values into ptr
2N/A */
2N/Aint
2N/Asip_copy_values(char *ptr, _sip_header_t *header)
2N/A{
2N/A sip_header_value_t value;
2N/A int tlen = 0;
2N/A int len = 0;
2N/A boolean_t first = B_TRUE;
2N/A char *p = ptr;
2N/A char *s;
2N/A boolean_t crlf_present = B_FALSE;
2N/A
2N/A if (sip_parse_goto_values(header) != 0)
2N/A return (0);
2N/A
2N/A len = header->sip_hdr_current - header->sip_hdr_start;
2N/A (void) strncpy(p, header->sip_hdr_start, len);
2N/A tlen += len;
2N/A p += len;
2N/A value = header->sip_hdr_parsed->value;
2N/A while (value != NULL) {
2N/A if (value->value_state != SIP_VALUE_DELETED) {
2N/A crlf_present = B_FALSE;
2N/A len = value->value_end - value->value_start;
2N/A if (first) {
2N/A (void) strncpy(p, value->value_start, len);
2N/A first = B_FALSE;
2N/A } else {
2N/A s = value->value_start;
2N/A while (*s != SIP_COMMA)
2N/A s--;
2N/A len += value->value_start - s;
2N/A (void) strncpy(p, s, len);
2N/A }
2N/A tlen += len;
2N/A p += len;
2N/A s = value->value_end;
2N/A while (s != value->value_start) {
2N/A if (*s == '\r' && strncmp(s, SIP_CRLF,
2N/A strlen(SIP_CRLF)) == 0) {
2N/A crlf_present = B_TRUE;
2N/A break;
2N/A }
2N/A s--;
2N/A }
2N/A } else {
2N/A if (value->next == NULL && !first && !crlf_present) {
2N/A s = value->value_end;
2N/A while (*s != '\r')
2N/A s--;
2N/A len = value->value_end - s;
2N/A (void) strncpy(p, s, len);
2N/A tlen += len;
2N/A p += len;
2N/A }
2N/A }
2N/A value = value->next;
2N/A }
2N/A return (tlen);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Add content (message body) to sip_msg
2N/A */
2N/Aint
2N/Asip_add_content(sip_msg_t sip_msg, char *content)
2N/A{
2N/A size_t len;
2N/A sip_content_t **loc;
2N/A sip_content_t *msg_content;
2N/A _sip_msg_t *_sip_msg;
2N/A
2N/A if (sip_msg == NULL || content == NULL || strlen(content) == 0)
2N/A return (EINVAL);
2N/A len = strlen(content);
2N/A _sip_msg = (_sip_msg_t *)sip_msg;
2N/A (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex);
2N/A
2N/A if (_sip_msg->sip_msg_cannot_be_modified) {
2N/A (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
2N/A return (ENOTSUP);
2N/A }
2N/A
2N/A msg_content = calloc(1, sizeof (sip_content_t));
2N/A if (msg_content == NULL) {
2N/A (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
2N/A return (ENOMEM);
2N/A }
2N/A msg_content->sip_content_start = malloc(strlen(content) + 1);
2N/A if (msg_content->sip_content_start == NULL) {
2N/A (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
2N/A free(msg_content);
2N/A return (ENOMEM);
2N/A }
2N/A (void) strncpy(msg_content->sip_content_start, content,
2N/A strlen(content));
2N/A msg_content->sip_content_start[strlen(content)] = '\0';
2N/A msg_content->sip_content_current = msg_content->sip_content_start;
2N/A msg_content->sip_content_end = msg_content->sip_content_start +
2N/A strlen(msg_content->sip_content_start);
2N/A msg_content->sip_content_allocated = B_TRUE;
2N/A
2N/A loc = &_sip_msg->sip_msg_content;
2N/A while (*loc != NULL)
2N/A loc = &((*loc)->sip_content_next);
2N/A *loc = msg_content;
2N/A
2N/A _sip_msg->sip_msg_content_len += len;
2N/A _sip_msg->sip_msg_len += len;
2N/A if (_sip_msg->sip_msg_buf != NULL)
2N/A _sip_msg->sip_msg_modified = B_TRUE;
2N/A (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Free the message content
2N/A */
2N/Avoid
2N/Asip_free_content(_sip_msg_t *sip_msg)
2N/A{
2N/A sip_content_t *content;
2N/A
2N/A if (sip_msg == NULL)
2N/A return;
2N/A content = sip_msg->sip_msg_content;
2N/A while (content != NULL) {
2N/A sip_content_t *content_tmp;
2N/A
2N/A content_tmp = content;
2N/A content = content->sip_content_next;
2N/A if (content_tmp->sip_content_allocated)
2N/A free(content_tmp->sip_content_start);
2N/A free(content_tmp);
2N/A }
2N/A sip_msg->sip_msg_content = NULL;
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Add a response line to sip_response
2N/A */
2N/Aint
2N/Asip_add_response_line(sip_msg_t sip_response, int response, char *response_code)
2N/A{
2N/A _sip_header_t *new_header;
2N/A int header_size;
2N/A _sip_msg_t *_sip_response;
2N/A int ret;
2N/A
2N/A if (sip_response == NULL || response < 0 || response_code == NULL)
2N/A return (EINVAL);
2N/A _sip_response = (_sip_msg_t *)sip_response;
2N/A (void) pthread_mutex_lock(&_sip_response->sip_msg_mutex);
2N/A if (_sip_response->sip_msg_cannot_be_modified) {
2N/A (void) pthread_mutex_unlock(&_sip_response->sip_msg_mutex);
2N/A return (ENOTSUP);
2N/A }
2N/A header_size = strlen(SIP_VERSION) + SIP_SPACE_LEN +
2N/A SIP_SIZE_OF_STATUS_CODE + SIP_SPACE_LEN + strlen(response_code) +
2N/A strlen(SIP_CRLF);
2N/A
2N/A new_header = sip_new_header(header_size);
2N/A if (new_header == NULL) {
2N/A (void) pthread_mutex_unlock(&_sip_response->sip_msg_mutex);
2N/A return (ENOMEM);
2N/A }
2N/A new_header->sip_hdr_sipmsg = _sip_response;
2N/A
2N/A (void) snprintf(new_header->sip_hdr_start, header_size + 1,
2N/A SIP_RESPONSE, SIP_VERSION, response, response_code, SIP_CRLF);
2N/A
2N/A new_header->sip_hdr_next = _sip_response->sip_msg_start_line;
2N/A _sip_response->sip_msg_start_line = new_header;
2N/A _sip_response->sip_msg_len += header_size;
2N/A ret = sip_parse_first_line(_sip_response->sip_msg_start_line,
2N/A &_sip_response->sip_msg_req_res);
2N/A if (_sip_response->sip_msg_buf != NULL)
2N/A _sip_response->sip_msg_modified = B_TRUE;
2N/A (void) pthread_mutex_unlock(&_sip_response->sip_msg_mutex);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * create a response based on the sip_request.
2N/A * Copies Call-ID, CSeq, From, To and Via headers from the request.
2N/A */
2N/Asip_msg_t
2N/Asip_create_response(sip_msg_t sip_request, int response, char *response_code,
2N/A char *totag, char *mycontact)
2N/A{
2N/A _sip_msg_t *new_msg;
2N/A _sip_msg_t *_sip_request;
2N/A boolean_t ttag_present;
2N/A
2N/A if (sip_request == NULL || response_code == NULL)
2N/A return (NULL);
2N/A
2N/A ttag_present = sip_get_to_tag(sip_request, NULL) != NULL;
2N/A
2N/A new_msg = (_sip_msg_t *)sip_new_msg();
2N/A if (new_msg == NULL)
2N/A return (NULL);
2N/A _sip_request = (_sip_msg_t *)sip_request;
2N/A
2N/A (void) pthread_mutex_lock(&_sip_request->sip_msg_mutex);
2N/A
2N/A /*
2N/A * Add response line.
2N/A */
2N/A if (sip_add_response_line(new_msg, response, response_code) != 0)
2N/A goto error;
2N/A
2N/A /*
2N/A * Copy Via headers
2N/A */
2N/A if (_sip_find_and_copy_all_header(_sip_request, new_msg, SIP_VIA) != 0)
2N/A goto error;
2N/A
2N/A /*
2N/A * Copy From header.
2N/A */
2N/A if (_sip_find_and_copy_header(_sip_request, new_msg, SIP_FROM,
2N/A NULL, B_FALSE)) {
2N/A goto error;
2N/A }
2N/A /*
2N/A * Copy To header. If To tag is present, copy it, if not then
2N/A * add one if the repsonse is not provisional.
2N/A */
2N/A if (ttag_present || (totag == NULL && response == SIP_TRYING)) {
2N/A if (_sip_find_and_copy_header(_sip_request, new_msg, SIP_TO,
2N/A NULL, B_FALSE)) {
2N/A goto error;
2N/A }
2N/A } else {
2N/A char *xtra_param;
2N/A boolean_t tag_alloc = B_FALSE;
2N/A int taglen;
2N/A
2N/A if (totag == NULL) {
2N/A totag = sip_guid();
2N/A if (totag == NULL)
2N/A goto error;
2N/A tag_alloc = B_TRUE;
2N/A }
2N/A taglen = strlen(SIP_TAG) + strlen(totag) + 1;
2N/A xtra_param = (char *)malloc(taglen);
2N/A if (xtra_param == NULL) {
2N/A if (tag_alloc)
2N/A free(totag);
2N/A goto error;
2N/A }
2N/A (void) snprintf(xtra_param, taglen, "%s%s", SIP_TAG, totag);
2N/A if (tag_alloc)
2N/A free(totag);
2N/A if (_sip_find_and_copy_header(_sip_request, new_msg,
2N/A SIP_TO, xtra_param, B_FALSE)) {
2N/A free(xtra_param);
2N/A goto error;
2N/A }
2N/A free(xtra_param);
2N/A }
2N/A
2N/A /*
2N/A * Copy Call-ID header.
2N/A */
2N/A if (_sip_find_and_copy_header(_sip_request, new_msg, SIP_CALL_ID, NULL,
2N/A B_FALSE)) {
2N/A goto error;
2N/A }
2N/A /*
2N/A * Copy CSEQ header
2N/A */
2N/A if (_sip_find_and_copy_header(_sip_request, new_msg, SIP_CSEQ, NULL,
2N/A B_FALSE)) {
2N/A goto error;
2N/A }
2N/A /*
2N/A * Copy RECORD-ROUTE header, if present.
2N/A */
2N/A if (sip_search_for_header(_sip_request, SIP_RECORD_ROUTE, NULL) !=
2N/A NULL) {
2N/A if (_sip_find_and_copy_all_header(_sip_request, new_msg,
2N/A SIP_RECORD_ROUTE) != 0) {
2N/A goto error;
2N/A }
2N/A }
2N/A if (mycontact != NULL) {
2N/A if (sip_add_contact(new_msg, NULL, mycontact, B_FALSE,
2N/A NULL) != 0) {
2N/A goto error;
2N/A }
2N/A }
2N/A (void) pthread_mutex_unlock(&_sip_request->sip_msg_mutex);
2N/A return ((sip_msg_t)new_msg);
2N/Aerror:
2N/A sip_free_msg((sip_msg_t)new_msg);
2N/A (void) pthread_mutex_unlock(&_sip_request->sip_msg_mutex);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * NON OK ACK : MUST contain values for the Call-ID, From, and Request-URI
2N/A * that are equal to the values of those header fields in the orig request
2N/A * passed to the transport. The To header field in the ACK MUST equal the To
2N/A * header field in the response being acknowledged. The ACK MUST contain the
2N/A * top Via header field of the original request. The CSeq header field in
2N/A * the ACK MUST contain the same value for the sequence number as was
2N/A * present in the original request, but the method parameter MUST be equal
2N/A * to "ACK".
2N/A */
2N/Aint
2N/Asip_create_nonOKack(sip_msg_t request, sip_msg_t response, sip_msg_t ack_msg)
2N/A{
2N/A int seqno;
2N/A char *uri;
2N/A _sip_msg_t *_request;
2N/A _sip_msg_t *_response;
2N/A _sip_msg_t *_ack_msg;
2N/A int ret;
2N/A
2N/A if (request == NULL || response == NULL || ack_msg == NULL ||
2N/A request == ack_msg) {
2N/A return (EINVAL);
2N/A }
2N/A _request = (_sip_msg_t *)request;
2N/A _response = (_sip_msg_t *)response;
2N/A _ack_msg = (_sip_msg_t *)ack_msg;
2N/A
2N/A (void) pthread_mutex_lock(&_request->sip_msg_mutex);
2N/A if (_request->sip_msg_req_res == NULL) {
2N/A if ((ret = sip_parse_first_line(_request->sip_msg_start_line,
2N/A &_request->sip_msg_req_res)) != 0) {
2N/A (void) pthread_mutex_unlock(&_request->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A }
2N/A if (_request->sip_msg_req_res->U.sip_request.sip_request_uri.
2N/A sip_str_ptr == NULL) {
2N/A (void) pthread_mutex_unlock(&_request->sip_msg_mutex);
2N/A return (EINVAL);
2N/A }
2N/A uri = (char *)malloc(_request->sip_msg_req_res->U.sip_request.
2N/A sip_request_uri.sip_str_len + 1);
2N/A if (uri == NULL) {
2N/A (void) pthread_mutex_unlock(&_request->sip_msg_mutex);
2N/A return (EINVAL);
2N/A }
2N/A (void) strncpy(uri,
2N/A _request->sip_msg_req_res->U.sip_request.sip_request_uri.
2N/A sip_str_ptr, _request->sip_msg_req_res->U.sip_request.
2N/A sip_request_uri.sip_str_len);
2N/A uri[_request->sip_msg_req_res->U.sip_request.
2N/A sip_request_uri.sip_str_len] = '\0';
2N/A if ((ret = sip_add_request_line(_ack_msg, ACK, uri)) != 0) {
2N/A (void) pthread_mutex_unlock(&_request->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A free(uri);
2N/A if ((ret = _sip_find_and_copy_header(_request, _ack_msg, SIP_VIA,
2N/A NULL, B_TRUE)) != 0) {
2N/A (void) pthread_mutex_unlock(&_request->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A (void) _sip_find_and_copy_header(_request, _ack_msg,
2N/A SIP_MAX_FORWARDS, NULL, B_TRUE);
2N/A
2N/A (void) pthread_mutex_lock(&_response->sip_msg_mutex);
2N/A if ((ret = _sip_find_and_copy_header(_response, _ack_msg, SIP_TO,
2N/A NULL, B_TRUE)) != 0) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A if ((ret = _sip_find_and_copy_header(_request, _ack_msg, SIP_FROM,
2N/A NULL, B_TRUE)) != 0) {
2N/A (void) pthread_mutex_unlock(&_request->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A if ((ret = _sip_find_and_copy_header(_request, _ack_msg, SIP_CALL_ID,
2N/A NULL, B_TRUE)) != 0) {
2N/A (void) pthread_mutex_unlock(&_request->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A (void) pthread_mutex_unlock(&_request->sip_msg_mutex);
2N/A seqno = sip_get_callseq_num(_request, &ret);
2N/A if (ret != 0)
2N/A return (ret);
2N/A if ((ret = sip_add_cseq(_ack_msg, ACK, seqno)) != 0)
2N/A return (ret);
2N/A if ((ret = sip_adjust_msgbuf(_ack_msg)) != 0)
2N/A return (ret);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * This is a 2XX ACK, for others ACK is constructed differently,
2N/A * esp. the branch id is retained.
2N/A */
2N/Aint
2N/Asip_create_OKack(sip_msg_t response, sip_msg_t ack_msg, char *transport,
2N/A char *sent_by, int sent_by_port, char *via_params)
2N/A{
2N/A int seqno;
2N/A char *uri;
2N/A sip_parsed_header_t *parsed_header;
2N/A sip_hdr_value_t *contact_value;
2N/A _sip_header_t *header;
2N/A _sip_msg_t *_response;
2N/A _sip_msg_t *_ack_msg;
2N/A int ret;
2N/A
2N/A if (response == NULL || response == NULL || transport == NULL)
2N/A return (EINVAL);
2N/A _response = (_sip_msg_t *)response;
2N/A _ack_msg = (_sip_msg_t *)ack_msg;
2N/A
2N/A /*
2N/A * Get URI from the response, Contact field
2N/A */
2N/A (void) pthread_mutex_lock(&_response->sip_msg_mutex);
2N/A if ((header = sip_search_for_header(_response, SIP_CONTACT,
2N/A NULL)) == NULL) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (EINVAL);
2N/A }
2N/A if ((ret = sip_parse_cftr_header(header, (void *)&parsed_header)) !=
2N/A 0) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A contact_value = (sip_hdr_value_t *)parsed_header->value;
2N/A if (contact_value->cftr_uri.sip_str_ptr == NULL) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (EINVAL);
2N/A }
2N/A uri = (char *)malloc(contact_value->cftr_uri.sip_str_len + 1);
2N/A if (uri == NULL) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ENOMEM);
2N/A }
2N/A (void) strncpy(uri, contact_value->cftr_uri.sip_str_ptr,
2N/A contact_value->cftr_uri.sip_str_len);
2N/A uri[contact_value->cftr_uri.sip_str_len] = '\0';
2N/A if ((ret = sip_add_request_line(_ack_msg, ACK, uri)) != 0) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A free(uri);
2N/A if ((ret = sip_add_via(_ack_msg, transport, sent_by, sent_by_port,
2N/A via_params)) != 0) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A
2N/A if ((ret = _sip_find_and_copy_header(_response, _ack_msg, SIP_TO,
2N/A NULL, B_TRUE)) != 0) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A if ((ret = _sip_find_and_copy_header(_response, _ack_msg, SIP_FROM,
2N/A NULL, B_TRUE)) != 0) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A if ((ret = _sip_find_and_copy_header(_response, _ack_msg, SIP_CALL_ID,
2N/A NULL, B_TRUE)) != 0) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A /*
2N/A * Copy Max-Forward if present
2N/A */
2N/A if (sip_search_for_header(_response, SIP_MAX_FORWARDS, NULL) != NULL) {
2N/A if ((ret = _sip_find_and_copy_header(_response, _ack_msg,
2N/A SIP_MAX_FORWARDS, NULL, B_TRUE)) != 0) {
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A return (ret);
2N/A }
2N/A }
2N/A (void) pthread_mutex_unlock(&_response->sip_msg_mutex);
2N/A seqno = sip_get_callseq_num(_response, &ret);
2N/A if (ret != 0)
2N/A return (ret);
2N/A if ((ret = sip_add_cseq(_ack_msg, ACK, seqno)) != 0)
2N/A return (ret);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Request-Line = Method SP Request-URI SP SIP-Version CRLF
2N/A */
2N/Aint
2N/Asip_add_request_line(sip_msg_t sip_request, sip_method_t method,
2N/A char *request_uri)
2N/A{
2N/A _sip_header_t *new_header;
2N/A int header_size;
2N/A _sip_msg_t *_sip_request;
2N/A
2N/A if (method < INVITE || method >= MAX_SIP_METHODS ||
2N/A request_uri == NULL || sip_request == NULL) {
2N/A return (EINVAL);
2N/A }
2N/A
2N/A _sip_request = (_sip_msg_t *)sip_request;
2N/A (void) pthread_mutex_lock(&_sip_request->sip_msg_mutex);
2N/A if (_sip_request->sip_msg_cannot_be_modified) {
2N/A (void) pthread_mutex_unlock(&_sip_request->sip_msg_mutex);
2N/A return (ENOTSUP);
2N/A }
2N/A
2N/A header_size = strlen(sip_methods[method].name) + SIP_SPACE_LEN +
2N/A strlen(request_uri) + SIP_SPACE_LEN + strlen(SIP_VERSION) +
2N/A strlen(SIP_CRLF);
2N/A
2N/A new_header = sip_new_header(header_size);
2N/A if (new_header == NULL) {
2N/A (void) pthread_mutex_unlock(&_sip_request->sip_msg_mutex);
2N/A return (ENOMEM);
2N/A }
2N/A new_header->sip_hdr_sipmsg = _sip_request;
2N/A
2N/A (void) snprintf(new_header->sip_hdr_start, header_size + 1,
2N/A "%s %s %s%s", sip_methods[method].name, request_uri,
2N/A SIP_VERSION, SIP_CRLF);
2N/A
2N/A new_header->sip_hdr_next = _sip_request->sip_msg_start_line;
2N/A _sip_request->sip_msg_start_line = new_header;
2N/A _sip_request->sip_msg_len += header_size;
2N/A (void) sip_parse_first_line(_sip_request->sip_msg_start_line,
2N/A &_sip_request->sip_msg_req_res);
2N/A if (_sip_request->sip_msg_buf != NULL)
2N/A _sip_request->sip_msg_modified = B_TRUE;
2N/A (void) pthread_mutex_unlock(&_sip_request->sip_msg_mutex);
2N/A return (0);
2N/A}