nta_check.c 11.4 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) 2005 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
/**@CFILE nta_check.c
26
 * @brief Checks for features, MIME types, session timer.
27
 *
28
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
29
 *
30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * @date Created: Wed Mar  8 16:35:05 EET 2006 ppessi
 */

#include "config.h"

#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/nta.h>

/* ======================================================================== */
/* Request validation */

44 45 46 47 48
/**Check that we support all features which UAC requires.
 *
 * The list of supported features is compared with the list of features
 * required by the UAC. If some features are not listed as supported, return
 * 420. If @a irq is non-NULL, the 420 response message is sent to the
49
 * client along with list of unsupported features in the @Unsupported
50 51 52 53 54 55 56 57 58 59 60
 * header, too.
 *
 * @param   irq incoming transaction object (may be NULL).
 * @param   sip contents of the SIP message
 * @param supported   list of protocol features supported
 * @param tag, value, ... optional list of tagged arguments used
 *                        when responding to the transaction
 *
 * @return 0 if successful.
 * 420 if any of the required features is not supported.
 */
61 62 63 64 65
int nta_check_required(nta_incoming_t *irq,
		       sip_t const *sip,
		       sip_supported_t const *supported,
		       tag_type_t tag, tag_value_t value, ...)
{
66 67
  int status = 0;

68
  if (sip->sip_require) {
69
    su_home_t home[SU_HOME_AUTO_SIZE(512)];
70 71
    sip_unsupported_t *us;

72 73
    su_home_auto(home, sizeof home);

74 75 76
    us = sip_has_unsupported(home, supported, sip->sip_require);

    if (us) {
77 78 79 80 81 82 83 84 85 86 87
      status = 420;
      if (irq) {
	ta_list ta;
	ta_start(ta, tag, value);
	nta_incoming_treply(irq,
			    SIP_420_BAD_EXTENSION,
			    SIPTAG_UNSUPPORTED(us),
			    SIPTAG_SUPPORTED(supported),
			    ta_tags(ta));
	ta_end(ta);
      }
88
    }
89 90

    su_home_deinit(home);
91
  }
92 93

  return status;
94 95
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/** Check that UAC supports all the required features.
 *
 * The list of required features is compared with the features supported by
 * the UAC. If some features are not supported, return 421. If @a irq is
 * non-NULL, the 421 response message is sent to the client, too.
 *
 * @param irq incoming transaction object (may be NULL).
 * @param sip contents of the SIP message
 * @param require   list of required protocol features
 * @param tag, value, ... optional list of tagged arguments used
 *                        when responding to the transaction
 *
 * @return 0 if successful.
 * 421 if any of the required features is not supported.
 */
111 112 113 114 115
int nta_check_supported(nta_incoming_t *irq,
			sip_t const *sip,
			sip_require_t *require,
			tag_type_t tag, tag_value_t value, ...)
{
116 117
  if (!sip_has_unsupported(NULL, sip->sip_supported, require))
    return 0;
118

119
  if (irq) {
120 121 122 123 124 125 126 127 128
    ta_list ta;
    ta_start(ta, tag, value);
    nta_incoming_treply(irq,
			SIP_421_EXTENSION_REQUIRED,
			SIPTAG_REQUIRE(require),
			ta_tags(ta));
    ta_end(ta);
  }

129
  return 421;
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/** Check that we allow the request method.
 *
 * The request-method is compared with the list of supported methods in @a
 * allow. If match is found, 0 is is returned. Otherwise, if the
 * request-method is well-known, 405 is returned. If the request-method is
 * unknown, 501 is returned. If @a irq is non-NULL, the 405 or 501 response
 * message is sent to the client, too.
 *
 * @param irq 	incoming transaction object (may be NULL).
 * @param sip 	contents of the SIP message
 * @param allow   list of allowed methods
 * @param tag, value, ...   optional list of tagged arguments used
 *                          when responding to the transaction
 *
 * @return 0 if successful, 405 is request-method is not allowed, 501 if
 * request-method is unknown.
 */
149 150 151 152 153 154
int nta_check_method(nta_incoming_t *irq,
		     sip_t const *sip,
		     sip_allow_t const *allow,
		     tag_type_t tag, tag_value_t value, ...)
{
  /* Check extensions */
155
  sip_method_t method = sip->sip_request->rq_method;
156 157
  char const *name = sip->sip_request->rq_method_name;

158 159
  if (sip_is_allowed(allow, method, name))
    return 0;
160

161 162 163
  if (irq) {
    ta_list ta;
    ta_start(ta, tag, value);
164 165 166

    if (method != sip_method_unknown)
      /* Well-known method */
167 168 169 170 171
      nta_incoming_treply(irq,
			  SIP_405_METHOD_NOT_ALLOWED,
			  SIPTAG_ALLOW(allow),
			  ta_tags(ta));
    else
172
      /* Completeley unknown method */
173 174 175 176 177 178 179
      nta_incoming_treply(irq,
			  SIP_501_NOT_IMPLEMENTED,
			  SIPTAG_ALLOW(allow),
			  ta_tags(ta));
    ta_end(ta);
  }

180
  return method != sip_method_unknown ? 405 : 501;
181 182 183 184
}

static char const application_sdp[] = "application/sdp";

185 186
/** Check that we understand session content in the request.
 *
187
 * If there is no @ContentDisposition header or the @ContentDisposition
188 189 190 191 192 193
 * header indicated "session", the message body and content-type is compared
 * with the acceptable session content-types listed in @a session_accepts.
 * (typically, @c "application/sdp"). If no match is found, a 415 is
 * returned. If @a irq is non-NULL, the 415 response message is sent to the
 * client, too.
 *
194
 * If the @ContentDisposition header indicates something else but "session",
195 196 197
 * and it does not contain "handling=optional" parameter, a 415 response is
 * returned, too.
 *
198
 * Also, the @ContentEncoding header is checked. If it is not empty
199 200 201 202 203 204 205 206 207 208 209 210
 * (indicating no content-encoding), a 415 response is returned, too.
 *
 * @param irq 	incoming (server) transaction object (may be NULL).
 * @param sip 	contents of the SIP message
 * @param session_accepts   list of acceptable content-types for "session"
 *                          content disposition
 * @param tag, value, ...   optional list of tagged arguments used
 *                          when responding to the transaction
 *
 * @return 0 if successful, 415 if content-type is not acceptable.
 */
int nta_check_session_content(nta_incoming_t *irq,
211 212 213 214 215 216 217 218 219 220 221
			      sip_t const *sip,
			      sip_accept_t const *session_accepts,
			      tag_type_t tag, tag_value_t value, ...)
{
  sip_content_type_t const *c = sip->sip_content_type;
  sip_content_disposition_t const *cd = sip->sip_content_disposition;
  int acceptable_type = 0, acceptable_encoding = 0;

  if (sip->sip_payload == NULL)
    return 0;

222
  if (cd == NULL || su_casematch(cd->cd_type, "session")) {
223 224 225 226 227 228
    sip_accept_t const *ab = session_accepts;
    char const *c_type;

    if (c)
      c_type = c->c_type;
    else if (sip->sip_payload->pl_len > 3 &&
229
	     su_casenmatch(sip->sip_payload->pl_data, "v=0", 3))
230 231 232 233
      /* Missing Content-Type, but it looks like SDP  */
      c_type = application_sdp;
    else
      /* No chance */
234
      ab = NULL, c_type = NULL;
235 236

    for (; ab; ab = ab->ac_next) {
237
      if (su_casematch(c_type, ab->ac_type))
238 239 240 241 242 243
	break;
    }

    if (ab)
      acceptable_type = 1;
  }
244
  else if (cd->cd_optional)
245 246 247 248
    acceptable_type = 1;

  /* Empty or missing Content-Encoding */
  if (!sip->sip_content_encoding ||
249
      !sip->sip_content_encoding->k_items ||
250 251 252
      !sip->sip_content_encoding->k_items[0] ||
      !sip->sip_content_encoding->k_items[0][0])
    acceptable_encoding = 1;
253

254 255 256
  if (acceptable_type && acceptable_encoding)
    return 0;

257 258 259 260 261 262 263 264 265
  if (irq) {
    ta_list ta;
    ta_start(ta, tag, value);
    nta_incoming_treply(irq,
			SIP_415_UNSUPPORTED_MEDIA,
			SIPTAG_ACCEPT(session_accepts),
			ta_tags(ta));
    ta_end(ta);
  }
266 267 268 269

  return 415;
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

/**Check that UAC accepts one of listed MIME content-types.
 *
 * The list of acceptable content-types are compared with the acceptable
 * content-types. If match is found, it is returned in @a return_acceptable.
 * If no match is found, a 406 is returned. If @a irq is non-NULL, the 406
 * response message is sent to the client, too.
 *
 * @param irq incoming transaction object (may be NULL).
 * @param sip contents of the SIP message
 * @param acceptable list of acceptable content types
 * @param return_acceptable optional return-value parameter for
 *                          matched content-type
 * @param tag, value, ... optional list of tagged arguments used
 *                        when responding to the transaction
 *
 * @return 406 if no content-type is acceptable by client, 0 if successful.
 */
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
int nta_check_accept(nta_incoming_t *irq,
		     sip_t const *sip,
		     sip_accept_t const *acceptable,
		     sip_accept_t const **return_acceptable,
		     tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  sip_accept_t const *ac, *ab;
  sip_method_t method;

  if (!acceptable)
    return 0;

  if (sip->sip_request)
    method = sip->sip_request->rq_method;
  else /* if (sip->sip_cseq) */
    method = sip->sip_cseq->cs_method;

  /* Missing Accept header implies support for SDP in INVITE and OPTIONS
307
   * (and PRACK and UPDATE?)
308
   */
309
  if (!sip->sip_accept && (method == sip_method_invite ||
310 311 312 313
			   method == sip_method_options ||
			   method == sip_method_prack ||
			   method == sip_method_update)) {
    for (ab = acceptable; ab; ab = ab->ac_next)
314
      if (su_casematch(application_sdp, ab->ac_type)) {
315 316 317
	if (return_acceptable) *return_acceptable = ab;
	return 0;
      }
318
  }
319 320 321 322 323 324

  for (ac = sip->sip_accept; ac; ac = ac->ac_next) {
    if (sip_q_value(ac->ac_q) == 0 || !ac->ac_type)
      continue;

    for (ab = acceptable; ab; ab = ab->ac_next)
325
      if (su_casematch(ac->ac_type, ab->ac_type)) {
326 327 328 329 330
	if (return_acceptable) *return_acceptable = ab;
	return 0;
      }
  }

331 332 333 334 335 336 337 338
  if (irq) {
    ta_start(ta, tag, value);
    nta_incoming_treply(irq,
			SIP_406_NOT_ACCEPTABLE,
			SIPTAG_ACCEPT(acceptable),
			ta_tags(ta));
    ta_end(ta);
  }
339 340 341 342

  return 406;
}

343
/**Check @SessionExpires header.
344
 *
345
 * If the proposed session-expiration time is smaller than @MinSE or our
346 347
 * minimal session expiration time, respond with 422 containing shortest
 * acceptable session expiration time in @MinSE header.
348 349 350 351 352 353 354 355
 *
 * @param irq 	incoming transaction object (may be NULL).
 * @param sip 	contents of the SIP message
 * @param my_min_se   minimal session expiration time in seconds
 * @param tag, value, ...   optional list of tagged arguments used
 *                          when responding to the transaction
 *
 * @return 422 if session expiration time is too small, 0 when successful.
356 357 358 359 360 361
 */
int nta_check_session_expires(nta_incoming_t *irq,
			      sip_t const *sip,
			      sip_time_t my_min_se,
			      tag_type_t tag, tag_value_t value, ...)
{
362
  unsigned long min_se = my_min_se;
363

364 365
  if (sip->sip_min_se && min_se < sip->sip_min_se->min_delta)
    min_se = sip->sip_min_se->min_delta;
366

367 368
  if (sip->sip_session_expires->x_delta >= min_se)
    return 0;
369

370 371 372
  if (irq) {
    ta_list ta;
    sip_min_se_t min_se0[1];
373

374 375 376 377 378 379 380 381 382
    ta_start(ta, tag, value);

    sip_min_se_init(min_se0)->min_delta = min_se;

    nta_incoming_treply(irq,
			SIP_422_SESSION_TIMER_TOO_SMALL,
			SIPTAG_MIN_SE(min_se0),
			ta_tags(ta));
    ta_end(ta);
383 384
  }

385
  return 422;
386
}