sip-options.c 10.1 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
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
/**@page options options - Query SIP OPTIONS
 * 
 * @par Name
 * options - Query SIP OPTIONS
 *
 * @par Synopsis
 * <tt>options [--bind=url] [--from=url] [-a|--all] to </tt>
 *
 * @par Description
 * The @em options utility sends an OPTIONS request to a SIP server.
 *
 * @par 
 * The @em options tool will print out status line and interesting headers
 * from the response, excluding From, Via, Call-ID, and CSeq. The message
 * body is also printed.
 *
 * @par Command Line Options
 * The @e options utility accepts following command line options:
 * <dl>
 * <dt>-m url | --contact=url | --bind=url</dt>
 * <dd>Specifies the SIP URL to which the @em options utility binds.
 * </dd>
 * <dt>--1XX | -1</dt>
 * <dd>Print also preliminary responses. If this option is not present,
 *     preliminary responses are silently discarded.
 * </dd>
 * <dt>--all | -a</dt>
 * <dd>All SIP headers will be printed. If the --all option is given, 
 *     the @em options utility also prints @b From, @b Via, @b Call-ID or
 *     @b CSeq headers.
 * </dd>
 * <dt>--from=url</dt>
 * <dd>Specifies the @b From header. Unless this option is used or the
 *     environment variable @c SIPADDRESS is set, local Contact URL is used
 *     as b@b From header as well.
 * </dd>
 * <dt>--mf=n</dt>
 * <dd>Specify the initial Max-Forwards count (defaults to 70, stack default).
 * </dd>
 * <dt>--method=s</dt>
 * <dd>Specify the request method (defaults to OPTIONS).
 * </dd>
 * </dl>
 *
 * @par Return Codes
 * <table>
 * <tr><td>0<td>when successful (a 2XX-series response is received)
 * <tr><td>1<td>when unsuccessful (a 3XX..6XX-series response is received)
 * <tr><td>2<td>initialization failure
 * </table>
 *
 * @par Examples
 * You want to query supported features of sip:essip00net.nokia.com:
 * @code
 * $ options sip:essip00net.nokia.com
 * @endcode
 *
 * @par Environment
 * @c SIPADDRESS, @c NTA_DEBUG, @c TPORT_DEBUG, @c TPORT_LOG.
 * 
 * Copyright (C) 2005 Nokia Research Center.
 *
 * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
 *
 * @STARTLGPL@
 * 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
 * @ENDLGPL@
 */

#include "config.h"

const char _options_c_id[] = 
Pekka Pessi's avatar
Pekka Pessi committed
86
 "$Id: sip-options.c,v 1.1 2005/10/06 20:00:54 ppessi Exp $";
Pekka Pessi's avatar
Pekka Pessi committed
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

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

typedef struct context_s context_t;
#define NTA_OUTGOING_MAGIC_T context_t

#include <nta.h>
#include <sip_header.h>
#include <sip_tag.h>
#include <sl_utils.h>
#include <sip_util.h>
#include <auth_client.h>

struct context_s {
  su_home_t   	  c_home[1];
  su_root_t   	 *c_root;
  nta_agent_t 	 *c_agent;
  url_t          *c_proxy;
  char const     *c_username;
  char const     *c_password;
  nta_leg_t   	 *c_leg;
  nta_outgoing_t *c_orq;
  auth_client_t  *c_proxy_auth;
  auth_client_t  *c_auth;
  unsigned        c_proxy_auth_retries;
  unsigned        c_auth_retries;
  int             c_all;
  int             c_pre;
  int             c_retval;
};

char const name[] = "options";

static
void usage(int rc)
{
  fprintf(rc ? stderr : stdout, 
	  "usage: %s OPTIONS url [extra-file]\n"
	  "where OPTIONS are\n"
	  "    --mf=count | --max-forwards=count\n"
	  "    --contact=url | --bind=url | -m=url\n"
	  "    --from=url\n"
	  "    --ua=user-agent\n"
	  "    --all | -a\n"
	  "    --1XX | -1\n"
	  "    --x | --extra \n",
	  name);
  exit(rc);
}

#include "apps_utils.h"

static int response_to_options(context_t *context,
			       nta_outgoing_t *oreq,
			       sip_t const *sip);
static char *readfile(FILE *f);

int main(int argc, char *argv[])
{
  su_home_t *home;
  context_t context[1] = {{{SU_HOME_INIT(context)}}};
  char 
    *extra = NULL,
    *o_bind = "sip:*:*", 
    *o_from = getenv("SIPADDRESS"),
    *o_max_forwards = NULL,
    *o_method = NULL,
    *o_to = NULL;
   
  char *s, *v;

  sip_method_t method = sip_method_options;

#define MATCH(s, o) \
      ((strncmp(s, o, strlen(o)) == 0))
#define MATCH1(s, o) \
      ((strncmp(s, o, strlen(o)) == 0) && \
       (v = (s[strlen(o)] ? s + strlen(o) : argv++[1])))
#define MATCH2(s, o) \
      ((strncmp(s, o, strlen(o)) == 0) && \
       (s[strlen(o)] == '=' || s[strlen(o)] == '\0') && \
       (v = s[strlen(o)] ? s + strlen(o) + 1 : argv++[1]))

  while ((s = argv++[1])) {
    if (!MATCH(s, "-"))             { o_to = s;           break; }
    else if (strcmp(s, "") == 0)    { o_to = argv++[1];   break; }
    else if (MATCH(s, "-a") || MATCH(s, "--all")) 
                                    { context->c_all = 1; }
    else if (MATCH(s, "-x") || MATCH(s, "--extra")) 
                                    { extra = "-"; }
    else if (MATCH(s, "-1") || MATCH(s, "--1XX")) 
                                    { context->c_pre = 1; }
    else if (MATCH2(s, "--mf"))     { o_max_forwards = v; }
    else if (MATCH2(s, "--max-forwards"))     { o_max_forwards = v; }
    else if (MATCH2(s, "--bind"))   { o_bind = v; }
    else if (MATCH1(s, "-m"))       { o_bind = v; }
    else if (MATCH2(s, "--contact")){ o_bind = v; }
    else if (MATCH2(s, "--from"))   { o_from = v; }
    else if (MATCH2(s, "--method")) { o_method = v; }
    else if (MATCH(s, "--help"))    { usage(0); }
    else 
      usage(2);
  }

  if (!o_to)
    usage(2);

  if (argv[1])
    extra = argv++[1];

  su_init();
  
  su_home_init(home = context->c_home);

  context->c_root = su_root_create(context);
  context->c_retval = 2;

  if (context->c_root) {
    url_string_t *r_uri;

    context->c_agent = 
      nta_agent_create(context->c_root, URL_STRING_MAKE(o_bind), 
		       NULL, NULL, /* Ignore incoming messages */
		       NULL);

    if (context->c_agent) {
      sip_addr_t *from, *to;
      sip_contact_t const *m = nta_agent_contact(context->c_agent);

      to = sip_to_create(home, (url_string_t *)o_to);

      if (o_from)
	from = sip_from_make(home, o_from);
      else
	from = sip_from_create(home, (url_string_t const *)m->m_url);

      if (!from) {
	fprintf(stderr, "%s: no valid From address\n", name);
	exit(2);
      }

      tag_from_header(context->c_agent, context->c_home, from);

      if (o_method) {
	method = sip_method_code(o_method); 
      } else {
	int len;
	char const *params = to->a_url->url_params;

	len = url_param(params, "method", NULL, 0);

	if (len > 0) {
	  o_method = su_alloc(home, len + 1);
	  if (o_method == 0 ||
	      url_param(params, "method", o_method, len + 1) != len) {
	    fprintf(stderr, "%s: %s\n", name, 
		    o_method ? "internal error" : strerror(errno));
	    exit(2);
	  }
	  method = sip_method_code(o_method);
	}
      }

      r_uri = (url_string_t *)url_hdup(home, to->a_url);

      sip_aor_strip(to->a_url);
      sip_aor_strip(from->a_url);

      context->c_username = from->a_url->url_user;
      context->c_password = from->a_url->url_password;
      from->a_url->url_password = NULL;

      if (extra) {
	FILE *hf;
	
	if (strcmp(extra, "-"))
	  hf = fopen(extra, "rb");
	else
	  hf = stdin;

	extra = readfile(hf);
      }

      context->c_proxy = url_hdup(context->c_home, 
				  (url_t *)getenv("sip_proxy"));

      nta_agent_set_params(context->c_agent,
			   NTATAG_SIPFLAGS(MSG_FLG_EXTRACT_COPY),
			   NTATAG_DEFAULT_PROXY(context->c_proxy),
			   TAG_END());

      context->c_leg = 
	nta_leg_tcreate(context->c_agent,
			NULL, NULL,      /* ignore incoming messages */
			SIPTAG_FROM(from), /* who is sending OPTIONS? */
			SIPTAG_TO(to), /* whom we are sending OPTIONS? */
			TAG_END());
				      
      if (context->c_leg) {
	context->c_orq = 
	  nta_outgoing_tcreate(context->c_leg,
			       response_to_options, context, 
			       NULL,
			       method, o_method, r_uri,
			       SIPTAG_USER_AGENT_STR("options"),
			       SIPTAG_MAX_FORWARDS_STR(o_max_forwards),
			       SIPTAG_HEADER_STR(extra),
			       TAG_END());

	if (context->c_orq) {
	  su_root_run(context->c_root); 
	  nta_outgoing_destroy(context->c_orq), context->c_orq = NULL;
	}

	nta_leg_destroy(context->c_leg), context->c_leg = NULL;
      }

      nta_agent_destroy(context->c_agent), context->c_agent = NULL;
    }
    
    su_root_destroy(context->c_root);
  }

  su_deinit();

  return context->c_retval;
}

/** Handle responses to registration request */
static
int response_to_options(context_t *context,
			nta_outgoing_t *oreq,
			sip_t const *sip)
{
  if (proxy_authenticate(context, oreq, sip, response_to_options))
    return 0;
  if (server_authenticate(context, oreq, sip, response_to_options))
    return 0;
				   
  if (sip->sip_status->st_status >= 200 || context->c_pre) {
    sip_header_t *h = (sip_header_t *)sip->sip_status;
    char hname[64];

    for (; h; h = (sip_header_t *)h->sh_succ) {
      if (!context->c_all) {
	if (sip_is_from(h) ||
	    sip_is_via(h) ||
	    sip_is_call_id(h) ||
	    sip_is_cseq(h) ||
	    sip_is_content_length(h))
	  continue;
      }
      if (h->sh_class->hc_name) {
	snprintf(hname, sizeof hname, "%s: %%s\n", h->sh_class->hc_name);
	sl_header_print(stdout, hname, h); 
      }
      else {
	sl_header_print(stdout, NULL, h); 
      }
    }
  }
  
  if (sip->sip_status->st_status >= 200) {
    context->c_retval = sip->sip_status->st_status >= 300;
    su_root_break(context->c_root);
  }

  return 0;
}

/* Read in whole (binary!) file */
char *readfile(FILE *f)
{
  char *buffer = NULL;
  long size;
  int len;

  if (f == NULL)
    return NULL;

  for (size = 8192, buffer = NULL, len = 0; !feof(f); size += 8192) {
    buffer = realloc(buffer, size + 1);
    if (!buffer)
      exit(2);
    len += fread(buffer + len, 1, 8192, f);
  }

  buffer[len] = '\0';

  return buffer;
}