sip-options.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
Pekka Pessi's avatar
Pekka Pessi committed
22
 *
23 24 25 26 27
 */

/**@page sip-options Query SIP OPTIONS
 * 
 * @section synopsis Synopsis
28
 * <tt>sip-options [OPTIONS] target-uri </tt>
Pekka Pessi's avatar
Pekka Pessi committed
29
 *
30
 * @section description Description
31 32
 * The @em sip-options utility sends a SIP OPTIONS request (or any other SIP
 * request) to a SIP server.
Pekka Pessi's avatar
Pekka Pessi committed
33
 *
34
 * The @em sip-options tool will print out status line and interesting headers
Pekka Pessi's avatar
Pekka Pessi committed
35 36 37
 * from the response, excluding From, Via, Call-ID, and CSeq. The message
 * body is also printed.
 *
38
 * @section options Command Line Options
Pekka Pessi's avatar
Pekka Pessi committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 * 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
56
 *     as @b From header as well.
Pekka Pessi's avatar
Pekka Pessi committed
57 58 59 60 61 62 63
 * </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>
64 65 66 67
 * <dt>--extra | -x/dt>
 * <dd>Read extra headers (and optionally a message body) from the standard
 *     input
 * </dd>
Pekka Pessi's avatar
Pekka Pessi committed
68 69
 * </dl>
 *
70
 * @section return Return Codes
Pekka Pessi's avatar
Pekka Pessi committed
71 72 73 74 75 76
 * <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>
 *
77
 * @section examples Examples
Pekka Pessi's avatar
Pekka Pessi committed
78 79 80 81 82
 * You want to query supported features of sip:essip00net.nokia.com:
 * @code
 * $ options sip:essip00net.nokia.com
 * @endcode
 *
83 84
 * @section environment Environment
 * #SIPADDRESS, #sip_proxy, #NTA_DEBUG, #TPORT_DEBUG, #TPORT_LOG.
Pekka Pessi's avatar
Pekka Pessi committed
85
 * 
86 87
 * @section bugs Reporting Bugs
 * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
Pekka Pessi's avatar
Pekka Pessi committed
88
 *
89
 * @section author Author
Pekka Pessi's avatar
Pekka Pessi committed
90 91
 * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
 *
92 93
 * @section copyright Copyright
 * Copyright (C) 2005 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
94
 *
95 96 97
 * This program is free software; see the source for copying conditions.
 * There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.
Pekka Pessi's avatar
Pekka Pessi committed
98 99 100 101 102 103 104 105 106 107 108 109 110
 */

#include "config.h"

#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

111 112 113 114 115 116
#include <sofia-sip/nta.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_tag.h>
#include <sofia-sip/sl_utils.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/auth_client.h>
Pekka Pessi's avatar
Pekka Pessi committed
117
#include <sofia-sip/tport_tag.h>
Pekka Pessi's avatar
Pekka Pessi committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

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;
};

137
char const name[] = "sip-options";
Pekka Pessi's avatar
Pekka Pessi committed
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

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"),
Pekka Pessi's avatar
Pekka Pessi committed
171
    *o_http_proxy = NULL,
Pekka Pessi's avatar
Pekka Pessi committed
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
    *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; }
Pekka Pessi's avatar
Pekka Pessi committed
200
    else if (MATCH2(s, "--http-proxy"))     { o_http_proxy = v; }
Pekka Pessi's avatar
Pekka Pessi committed
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
    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 = 
Pekka Pessi's avatar
Pekka Pessi committed
229 230
      nta_agent_create(context->c_root,
		       URL_STRING_MAKE(o_bind),
Pekka Pessi's avatar
Pekka Pessi committed
231
		       NULL, NULL, /* Ignore incoming messages */
Pekka Pessi's avatar
Pekka Pessi committed
232
		       TPTAG_HTTP_CONNECT(o_http_proxy),
233
		       TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    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 {
256
	isize_t len;
Pekka Pessi's avatar
Pekka Pessi committed
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
	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,
303
			NULL, NULL,      /* ignore incoming requests */
Pekka Pessi's avatar
Pekka Pessi committed
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
			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;
}

338
/** Handle responses to OPTIONS request */
Pekka Pessi's avatar
Pekka Pessi committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
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;
      }
362 363 364 365 366 367

      if (h->sh_class->hc_name == NULL) {
	sl_header_print(stdout, NULL, h);
      } 
      else if (h->sh_class->hc_name[0] == '\0') {
	sl_header_print(stdout, "%s\n", h); 
Pekka Pessi's avatar
Pekka Pessi committed
368 369
      }
      else {
370 371
	snprintf(hname, sizeof hname, "%s: %%s\n", h->sh_class->hc_name);
	sl_header_print(stdout, hname, h); 
Pekka Pessi's avatar
Pekka Pessi committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
      }
    }
  }
  
  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;
389
  size_t len;
Pekka Pessi's avatar
Pekka Pessi committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

  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;
}