nua_options.c 3.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2006 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

25 26
/**@CFILE nua_options.c
 * @brief Implementation of OPTIONS method
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
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Mar  8 17:02:19 EET 2006 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include <assert.h>

#include <sofia-sip/string0.h>
#include <sofia-sip/sip_protos.h>
#include <sofia-sip/sip_status.h>

#define NTA_LEG_MAGIC_T      struct nua_handle_s
#define NTA_OUTGOING_MAGIC_T struct nua_handle_s

#include "nua_stack.h"

/* ======================================================================== */
/* OPTIONS */

static int process_response_to_options(nua_handle_t *nh,
				       nta_outgoing_t *orq,
				       sip_t const *sip);

int
nua_stack_options(nua_t *nua, nua_handle_t *nh, nua_event_t e, tagi_t const *tags)
{
  struct nua_client_request *cr = nh->nh_cr;
  msg_t *msg;

  if (nh_is_special(nh)) {
65
    return UA_EVENT2(e, 900, "Invalid handle for OPTIONS");
66 67
  }
  else if (cr->cr_orq) {
68
    return UA_EVENT2(e, 900, "Request already in progress");
69 70 71 72 73 74 75 76 77 78 79 80 81 82
  }

  nua_stack_init_handle(nua, nh, nh_has_nothing, NULL, TAG_NEXT(tags));

  msg = nua_creq_msg(nua, nh, cr, cr->cr_retry_count,
			 SIP_METHOD_OPTIONS, 
			 TAG_NEXT(tags));

  cr->cr_orq = nta_outgoing_mcreate(nua->nua_nta,
				    process_response_to_options, nh, NULL,
				    msg,
				    SIPTAG_END(), TAG_NEXT(tags));
  if (!cr->cr_orq) {
    msg_destroy(msg);
83
    return UA_EVENT1(e, NUA_INTERNAL_ERROR);
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
  }

  return cr->cr_event = e;
}

void restart_options(nua_handle_t *nh, tagi_t *tags)
{
  nua_creq_restart(nh, nh->nh_cr, process_response_to_options, tags);
}

static int process_response_to_options(nua_handle_t *nh,
				       nta_outgoing_t *orq,
				       sip_t const *sip)
{
  if (nua_creq_check_restart(nh, nh->nh_cr, orq, sip, restart_options))
    return 0;
  return nua_stack_process_response(nh, nh->nh_cr, orq, sip, TAG_END());
}

int nua_stack_process_options(nua_t *nua,
			      nua_handle_t *nh,
			      nta_incoming_t *irq,
			      sip_t const *sip)
{
  msg_t *msg;

  int status = 200; char const *phrase = sip_200_OK;

112 113 114
  /* Hook to outbound */
  if (outbound_connect_check_accept(sip->sip_accept))
    return outbound_connect_process_options(nua->nua_registrations, irq, sip);
115

116 117 118 119 120 121 122 123
  if (nh == NULL)
    nh = nua->nua_dhandle;

  msg = nh_make_response(nua, nh, irq, status, phrase,
			 SIPTAG_ALLOW(NH_PGET(nh, allow)),
			 SIPTAG_SUPPORTED(NH_PGET(nh, supported)),
			 TAG_IF(NH_PGET(nh, path_enable),
				SIPTAG_SUPPORTED_STR("path")),
124
			 SIPTAG_ACCEPT_STR(SDP_MIME_TYPE),
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
			 TAG_END());

  if (msg) {
    su_home_t home[1] = { SU_HOME_INIT(home) };
#if 0				/* XXX */
    sdp_session_t *sdp;
    sip_t *sip = sip_object(msg);

    if ((sdp = nmedia_describe(nua, nh->nh_nm, nh, home))) {
      nh_sdp_insert(nh, home, msg, sip, sdp);
    }
#endif

    nta_incoming_mreply(irq, msg);

    su_home_deinit(home);
  }
  else
    SET_STATUS1(SIP_500_INTERNAL_SERVER_ERROR);

  msg = nta_incoming_getrequest(irq);

  nua_stack_event(nh->nh_nua, nh, msg, nua_i_options, status, phrase, TAG_END());

  return status;
}