sip-dig.c 20.6 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 27 28 29 30 31 32
 * This is an example program for @b sresolv library in synchronous mode.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Original Created: Tue Jul 16 18:50:14 2002 ppessi
 */

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/**@page sip-dig Resolve SIP URIs.
 * 
 * @section sip_dig_synopsis Synopsis
 * <tt>sip-dig [OPTIONS] uri...</tt>
 *
 * @section sip_dig_description Description
 * The @em sip-dig utility resolves SIP URIs as described in @RFC3263. It
 * queries NAPTR, SRV and A/AAAA records and prints out the resulting
 * transport addresses.
 *
 * The default transports are: UDP, TCP, SCTP, TLS and TLS-SCTP. The SIPS
 * URIs are resolved using only TLS transports, TLS and TLS-SCTP. If not
 * otherwise indicated by NAPTR or SRV records, the sip-dig uses UDP and TCP
 * as transports for SIP and TLS for SIPS URIs.
 *
 * The results are printed intended, with a preference followed by weight, 
 * then protocol name, port number and IP address in numeric format.
 *
 * @section sip_dig_options Command Line Options
 * The @e sip-dig utility accepts following command line options:
 * <dl>
54 55 56 57 58
 * <dt>-p <em>protoname</em></dt>

 * <dd>Use named transport protocol. The <em>protoname</em> can be either
 * well-known, e.g., "udp", or it can specify NAPTR service and SRV
 * identifier, e.g., "tls-udp/SIPS+D2U/_sips._udp.".
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
 * </dd>
 * <dt>--udp</dt>
 * <dd>Use UDP transport protocol.
 * </dd>
 * <dt>--tcp</dt>
 * <dd>Use TCP transport protocol.
 * </dd>
 * <dt>--tls</dt>
 * <dd>Use TLS over TCP transport protocol.
 * </dd>
 * <dt>--sctp</dt>
 * <dd>Use SCTP transport protocol.
 * </dd>
 * <dt>--tls-sctp</dt>
 * <dd>Use TLS over SCTP transport protocol.
 * </dd>
 * <dt>--no-sctp</dt>
 * <dd>Ignore SCTP or TLS-SCTP records in the list of default transports. 
 * This option has no effect if transport protocols has been explicitly
 * listed.
 * </dd>
 * <dt>-4</dt>
 * <dd>Query IP4 addresses (A records)
 * </dd>
 * <dt>-6</dt>
 * <dd>Query IP6 addresses (AAAA records).
 * </dd>
 * <dt>-v</dt>
 * <dd>Be verbatim.
 * </dd>
 * <dt></dt>
 * <dd>
 * </dd>
 * </dl>
 *
 * @section sip_dig_return 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>
 *
 * @section sip_dig_examples Examples
 *
103 104 105
 * Resolve sip:openlaboratory.net, prefer TLS over TCP, TCP over UDP:
 * @code
 * $ sip-dig --tls --tcp --udp sip:openlaboratory.net
106 107 108
 *	1 0.333 tls 5061 212.213.221.127
 *	2 0.333 tcp 5060 212.213.221.127
 *	3 0.333 udp 5060 212.213.221.127
109
 * @endcode
110
 *
111 112 113
 * Resolve sips:example.net with TLS over SCTP (TLS-SCTP) and TLS:
 * @code
 * $ sip-dig -p tls-sctp --tls sips:example.net
114 115
 *	1 0.500 tls-udp 5061 172.21.55.26
 *	2 0.500 tls 5061 172.21.55.26
116
 * @endcode
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
 *
 * @section sip_dig_environment Environment
 * #SRESOLV_DEBUG, SRESOLV_CONF
 * 
 * @section sip_dig_bugs Reporting Bugs
 * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
 *
 * @section sip_dig_author Author
 * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
 *
 * @section sip_dig_copyright Copyright
 * Copyright (C) 2006 Nokia Corporation.
 *
 * 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.
 */

#include "config.h"

137
#if !defined(HAVE_WIN32)
138 139 140 141
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
142 143 144
#endif

#include "sofia-sip/su.h"
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

#include "sofia-resolv/sres.h"
#include "sofia-resolv/sres_record.h"

#include "sofia-sip/url.h"
#include "sofia-sip/su_alloc.h"
#include "sofia-sip/hostdomain.h"

char const name[] = "sip-dig";

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

enum { N_TPORT = 16 };

struct transport { char const *name, *service, *srv; };

struct dig {
  sres_resolver_t *sres;

  unsigned preference, ip4, ip6, sips, print;

  struct transport tports[N_TPORT];
};

int dig_naptr(struct dig *dig, char const *host, double weight);

int dig_all_srvs(struct dig *dig, char const *tport, char const *host,
		 double weight);

int dig_srv(struct dig *dig, char const *tport, char const *host,
	    double weight);

int dig_srv_at(struct dig *dig,
	       char const *tport, sres_record_t **answers, 
	       double weight, int pweight,
	       int priority);

int dig_addr(struct dig *dig, 
	     char const *tport, char const *host, char const *port,
	     double weight);

void print_addr_results(struct transport const *tports,
			char const *tport, char const *tport2,
			sres_record_t **answers, int type, int af,
			char const *port,
			double weight, int preference);

void print_result(char const *addr, char const *port, char const *tport,
		  double weight,
		  unsigned preference);

Pekka Pessi's avatar
Pekka Pessi committed
199
int prepare_transport(struct dig *dig, char const *tport);
200

Pekka Pessi's avatar
Pekka Pessi committed
201 202 203
int count_transports(struct dig *dig,
		     char const *tp1, 
		     char const *tp2);
204 205 206 207 208 209 210 211 212

void usage(int exitcode)
{
  fprintf(stderr, "usage: %s [OPTIONS] [@dnsserver] uri\n", name);
  exit(exitcode);
}

int main(int argc, char *argv[])
{
213
  int exitcode = 0;
214
  int o_sctp = 1, o_tls_sctp = 1, o_verbatim = 1;
215
  int family = 0, multiple = 0;
216 217 218 219 220 221 222 223 224 225
  char const *dnsserver = NULL;
  char const *string;
  url_t *uri = NULL;

  char const *host;
  char const *port;
  char *transport = NULL, tport[32];

  struct dig dig[1] = {{ NULL }};

226 227 228
  if (su_init() != 0) 
    return -1;

229 230 231 232 233 234 235
  while (argv[1] && argv[1][0] == '-') {
    if (strcmp(argv[1], "-v") == 0)
      o_verbatim++;
    else if (strcmp(argv[1], "-6") == 0)
      dig->ip6 = ++family;
    else if (strcmp(argv[1], "-4") == 0)
      dig->ip4 = ++family;
236 237 238 239 240 241 242 243 244 245 246
    else if (strncmp(argv[1], "-p", 2) == 0) {
      char const *proto;

      if (argv[1][2] == '=')
	proto = argv[1] + 3;
      else if (argv[1][2])
	proto = argv[1] + 2;
      else
	proto = argv++[2];

      if (proto == NULL)
247
	usage(2);
248

249 250
      if (prepare_transport(dig, proto) < 0)
	exit(2);
251
    }
252
    else if (strcmp(argv[1], "--udp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
253
      prepare_transport(dig, "udp");
254
    else if (strcmp(argv[1], "--tcp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
255
      prepare_transport(dig, "tcp");
256
    else if (strcmp(argv[1], "--tls") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
257
      prepare_transport(dig, "tls");
258
    else if (strcmp(argv[1], "--sctp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
259
      prepare_transport(dig, "sctp");
260
    else if (strcmp(argv[1], "--tls-sctp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
261
      prepare_transport(dig, "tls-sctp");
262
    else if (strcmp(argv[1], "--tls-udp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
263
      prepare_transport(dig, "tls-udp");
264 265 266 267 268 269 270 271 272 273 274
    else if (strcmp(argv[1], "--no-sctp") == 0)
      o_sctp = 0, o_tls_sctp = 0;
    else if (strcmp(argv[1], "--help") == 0)
      usage(0);
    else if (strcmp(argv[1], "-h") == 0)
      usage(0);
    else if (strcmp(argv[1], "-?") == 0)
      usage(0);
    else if (strcmp(argv++[1], "-") == 0)
      break;
    else
275
      usage(2);
276 277 278 279 280 281 282 283 284
    argv++;
  }

  if (!family)
    dig->ip4 = 1, dig->ip6 = 2;

  if (argv[1] && argv[1][0] == '@')
    dnsserver = argv++[1] + 1;

285
  if (!argv[1])
286
    usage(2);
287

288
  multiple = argv[1] && argv[2];
289

Pekka Pessi's avatar
Pekka Pessi committed
290 291 292
  if (!count_transports(dig, NULL, NULL)) {
    prepare_transport(dig, "udp");
    prepare_transport(dig, "tcp");
293
    if (o_sctp)
Pekka Pessi's avatar
Pekka Pessi committed
294 295
      prepare_transport(dig, "sctp");
    prepare_transport(dig, "tls");
296
    if (o_tls_sctp)
Pekka Pessi's avatar
Pekka Pessi committed
297
      prepare_transport(dig, "tls-sctp");
298 299
  }

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  dig->sres = sres_resolver_new(getenv("SRESOLV_CONF"));
  if (!dig->sres)
    perror("sres_resolver_new"), exit(1);

  for (; (string = argv[1]); argv++) {
    if (multiple)
      puts(string);

    uri = url_hdup(NULL, (void *)string);

    if (uri && uri->url_type == url_unknown)
      url_sanitize(uri);

    if (uri->url_type == url_any)
      continue;

    if (!uri || (uri->url_type != url_sip && uri->url_type != url_sips)) {
      fprintf(stderr, "%s: invalid uri\n", string);
      exitcode = 1;
      continue;
320
    }
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

    port = url_port(uri);
    if (port && !port[0]) port = NULL;
    if (url_param(uri->url_params, "transport=", tport, sizeof tport) > 0)
      transport = tport;

    host = uri->url_host;

    if (host_is_ip_address(host)) {
      if (transport) {
	print_result(host, port, transport, 1.0, 1);
      }
      else if (uri->url_type == url_sips) {
	print_result(host, port, "tls", 1.0, 1);
      }
      else {
	print_result(host, port, "udp", 1.0, 1);
	print_result(host, port, "tcp", 1.0, 2);
      }
      continue;
341 342
    }

343 344 345 346 347
    if (!host_is_domain(host)) {
      fprintf(stderr, "%s: invalid host\n", string);
      exitcode = 1;
      continue;
    }
348

349 350
    dig->sips = uri->url_type == url_sips;
    dig->preference = 1;
351

352 353 354 355 356 357
    if (!port && !transport && dig_naptr(dig, host, 1.0))
      continue /* resolved naptr */;
    else if (!port && dig_all_srvs(dig, transport, host, 1.0))
      continue /* resolved srv */;
    else if (dig_addr(dig, transport, host, port, 1.0))
      continue /* resolved a/aaaa */;
358 359

    fprintf(stderr, "%s: not found\n", string);
360 361
    exitcode = 1;
  }
362 363 364 365 366 367 368 369 370 371 372 373

  sres_resolver_unref(dig->sres);

  return exitcode;
}


int transport_is_secure(char const *tportname)
{
  return tportname && strncasecmp(tportname, "tls", 3) == 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
374
int prepare_transport(struct dig *dig, char const *tport)
375
{
Pekka Pessi's avatar
Pekka Pessi committed
376
  struct transport *tports = dig->tports;
377 378 379 380 381 382 383 384 385 386 387 388
  int j;

  for (j = 0; j < N_TPORT - 1; j++) {
    if (!tports[j].name)
      break;
    if (strcasecmp(tports[j].name, tport) == 0)
      return 1;
  }

  if (j == N_TPORT)
    return 0;

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  if (strchr(tport, '/')) {
    char *service = strchr(tport, '/');
    char *srv = strchr(service + 1, '/');

    if (!srv || srv[strlen(srv) - 1] != '.') {
      fprintf(stderr, "%s: invalid transport specifier \"%s\"\n", name, tport);
      fputs(
"\tspecifier should have name/service/srv-id\n"
"\twhere name is protocol name (e.g, \"tls-udp\")\n"
"\t      service specifies service as per RFC 2915 (e.g., \"SIPS+D2U\")\n"
"\t      srv-id is prefix for SRV lookup (e.g., \"_sips._udp.\")\n",
         stderr);
      if (srv)
	fputs("\t      and it should end with a dot \".\"\n", stderr);
      return -1;
    }

    *service++ = '\0', *srv++ = '\0';

    tports[j].name = tport,
    tports[j].service = service;
    tports[j].srv = srv;
  }
  else if (strcasecmp(tport, "udp") == 0) {
    tports[j].name = "udp";
414 415 416 417
    tports[j].service = "SIP+D2U";
    tports[j].srv = "_sip._udp.";
  }
  else if (strcasecmp(tport, "tcp") == 0) {
418
    tports[j].name = "tcp";
419 420 421 422
    tports[j].service = "SIP+D2T";
    tports[j].srv = "_sip._tcp.";
  }
  else if (strcasecmp(tport, "tls") == 0) {
423
    tports[j].name = "tls";
424 425 426 427
    tports[j].service = "SIPS+D2T";
    tports[j].srv = "_sips._tcp.";
  }
  else if (strcasecmp(tport, "sctp") == 0) {
428
    tports[j].name = "sctp";
429 430 431 432
    tports[j].service = "SIP+D2S";
    tports[j].srv = "_sip._sctp.";
  }
  else if (strcasecmp(tport, "tls-sctp") == 0) {
433
    tports[j].name = "tls-sctp";
434 435 436 437
    tports[j].service = "SIPS+D2S";
    tports[j].srv = "_sips._sctp.";
  }
  else {
438 439
    fprintf(stderr, "%s: unknown transport \"%s\"\n", name, tport);
    return -1;
440 441
  }

442 443
  j++;

444 445 446 447 448 449
  tports[j].service = tports[j].srv = tports[j].name = NULL;

  return 1;
}

int
Pekka Pessi's avatar
Pekka Pessi committed
450
count_transports(struct dig *dig,
451 452 453
		 char const *tport,
		 char const *tport2)
{
Pekka Pessi's avatar
Pekka Pessi committed
454
  
455
  int i, tcount = 0;
Pekka Pessi's avatar
Pekka Pessi committed
456
  struct transport const *tports = dig->tports;
457 458

  for (i = 0; tports[i].name; i++) {
Pekka Pessi's avatar
Pekka Pessi committed
459 460
    if (dig->sips && !transport_is_secure(tports[i].name))
      continue;
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    if (!tport || strcasecmp(tport, tports[i].name) == 0)
      tcount++;
    if (tport2 && strcasecmp(tport2, tports[i].name) == 0)
      tcount++;
  }

  return tcount;
}

struct transport const *
transport_by_service(struct transport const *tports, char const *s)
{
  int i;

  for (i = 0; tports[i].name; i++) {
    if (strcasecmp(tports[i].service, s) == 0)
      return tports + i;
  }

  return NULL;
}

int dig_naptr(struct dig *dig,
	      char const *host,
	      double weight)
{
  sres_record_t **answers = NULL;
  struct transport const *tp;
  int i, error;
  int order = 0, count = 0, nacount = 0, scount = 0;

Pekka Pessi's avatar
Pekka Pessi committed
492
  error = sres_blocking_query(dig->sres, sres_type_naptr, host, 0, &answers);
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
  if (error < 0)
    return 0;

  /* Sort by priority */
  sres_sort_answers(dig->sres, answers);

  /* Count number of matching naptrs */
  for (i = 0; answers[i]; i++) {
    sres_naptr_record_t const *na = answers[i]->sr_naptr;

    if (na->na_record->r_type != sres_type_naptr || na->na_record->r_status)
      continue;

    if (dig->print)
      printf("%s\n\t%d IN NAPTR %u %u \"%s\" \"%s\" \"%s\" %s\n",
	     na->na_record->r_name, na->na_record->r_ttl,
	     na->na_order, na->na_prefer,
	     na->na_flags, na->na_services,
	     na->na_regexp, na->na_replace);

    if (strcasecmp(na->na_flags, "s") && strcasecmp(na->na_flags, "a"))
      continue;

    if (nacount && order != na->na_order)
      continue;

    if (dig->sips && strncasecmp(na->na_services, "SIPS+", 5))
      continue;

    if (!transport_by_service(dig->tports, na->na_services))
      continue;

    order = na->na_order, nacount++;
  }

  if (nacount == 0) {
    sres_free_answers(dig->sres, answers);
    return 0;
  }

  for (i = 0; answers[i]; i++) {
    sres_naptr_record_t const *na = answers[i]->sr_naptr;

    if (na->na_record->r_type != sres_type_naptr || na->na_record->r_status)
      continue;
    if (order != na->na_order)
      continue;
    if (strcasecmp(na->na_flags, "s") && strcasecmp(na->na_flags, "a"))
      continue;
    if (dig->sips && strncasecmp(na->na_services, "SIPS+", 5))
      continue;

    tp = transport_by_service(dig->tports, na->na_services);
    if (!tp)
      continue;

    if (strcasecmp(na->na_flags, "s") == 0) {
      scount = dig_srv(dig, tp->name, na->na_replace, weight / nacount);
    }
    else if (strcasecmp(na->na_flags, "a") == 0) {
      scount = dig_addr(dig, tp->name, na->na_replace, NULL, weight / nacount);
    }
    else 
      scount = 0;

    count += scount;
  }

  return count;
}

int dig_all_srvs(struct dig *dig,
		 char const *tport,
		 char const *host,
		 double weight)
{
569
  int i, j, n;
570 571 572
  int tcount, count = 0, scount;
  char *domain;

573 574 575 576
  struct {
    char const *proto; sres_record_t **answers;
  } srvs[N_TPORT + 1] = {{ NULL }};

Pekka Pessi's avatar
Pekka Pessi committed
577
  tcount = count_transports(dig, tport, NULL);
578 579 580
  if (!tcount)
    return 0;

581
  for (i = 0, n = 0; dig->tports[i].name; i++) {
582 583 584
    if (tport && strcasecmp(dig->tports[i].name, tport))
      continue;

Pekka Pessi's avatar
Pekka Pessi committed
585 586 587
    if (dig->sips && !transport_is_secure(dig->tports[i].name))
      continue;

588 589
    domain = su_strcat(NULL, dig->tports[i].srv, host);

590
    if (domain) {
Pekka Pessi's avatar
Pekka Pessi committed
591
      if (sres_blocking_query(dig->sres, sres_type_srv, domain, 0,
592 593 594 595
			      &srvs[n].answers) >= 0) {
	srvs[n++].proto = dig->tports[i].name;
      }
      free(domain);
596
    }
597 598 599 600
  }

  if (n == 0)
    return 0;
601

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
  for (i = 0; i < n; i++) {
    unsigned priority = 0, pweight = 0, m = 0;
    sres_record_t **answers = srvs[i].answers;
    char const *tport = srvs[i].proto;

    for (j = 0; answers[j]; j++) {
      sres_srv_record_t const *srv = answers[j]->sr_srv;

      if (srv->srv_record->r_type != sres_type_srv)
	continue;
      if (srv->srv_record->r_status != 0)
	continue;
      
      if (srv->srv_priority != priority && pweight != 0) {
	scount = dig_srv_at(dig, tport, answers, weight / n, pweight,
			    priority);
	if (scount) dig->preference++;
	count += scount;
	pweight = 0, m = 0;
      }

      priority = srv->srv_priority, pweight += srv->srv_weight, m++;
    }

    if (m) {
      scount = dig_srv_at(dig, tport, answers, weight / n, pweight, priority);
      if (scount)
	dig->preference++;
      count += scount;
    }
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  }

  return count;
}

int dig_srv(struct dig *dig,
	    char const *tport,
	    char const *domain,
	    double weight)
{
  sres_record_t **answers = NULL;
  int j, n, error;
  int count = 0, scount = 0;

  uint32_t priority, pweight;

  assert(tport && domain);

Pekka Pessi's avatar
Pekka Pessi committed
650
  error = sres_blocking_query(dig->sres, sres_type_srv, domain, 0, &answers);
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
  if (error < 0)
    return 0;

  /* Sort by priority */
  sres_sort_answers(dig->sres, answers);

  priority = 0; pweight = 0; n = 0;

  for (j = 0; answers[j]; j++) {
    sres_srv_record_t const *srv = answers[j]->sr_srv;

    if (srv->srv_record->r_type != sres_type_srv)
      continue;
    if (srv->srv_record->r_status != 0)
      continue;

    if (srv->srv_priority != priority && pweight != 0) {
      scount = dig_srv_at(dig, tport, answers, weight, pweight,
			  priority);
      if (scount) dig->preference++;
      count += scount;
      pweight = 0, n = 0;
    }

    priority = srv->srv_priority, pweight += srv->srv_weight, n++;
  }

  if (n) {
679
    scount = dig_srv_at(dig, tport, answers, weight, pweight, priority);
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    if (scount) dig->preference++;
    count += scount;
  }

  sres_free_answers(dig->sres, answers);

  return count;
}

int dig_srv_at(struct dig *dig,
	       char const *tport,
	       sres_record_t **answers,
	       double weight, int pweight,
	       int priority)
{
  int count = 0;
  int i;
  char port[8];

  if (pweight == 0)
    pweight = 1;

  for (i = 0; answers[i]; i++) {
    sres_srv_record_t const *srv = answers[i]->sr_srv;
    if (srv->srv_record->r_type != sres_type_srv)
      continue;
    if (srv->srv_record->r_status != 0)
      continue;
    if (srv->srv_priority != priority)
      continue;
    snprintf(port, sizeof port, "%u", srv->srv_port);

    count += dig_addr(dig, tport, srv->srv_target, port,
		      weight * srv->srv_weight / pweight);
  }

  return count;
}

int dig_addr(struct dig *dig,
	     char const *tport,
	     char const *host,
	     char const *port,
	     double weight)
{
  int error, i;
  char const *tport2 = NULL;
  sres_record_t **answers1 = NULL, **answers2 = NULL;
  unsigned count1 = 0, count2 = 0, tcount = 0;
  int type1 = 0, type2 = 0, family1 = 0, family2 = 0;

  if (dig->ip6 > dig->ip4) {
    type1 = sres_type_aaaa, family1 = AF_INET6;
    if (dig->ip4)
      type2 = sres_type_a, family2 = AF_INET;
  }
  else {
    type1 = sres_type_a, family1 = AF_INET;
    if (dig->ip6)
      type2 = sres_type_aaaa, family2 = AF_INET6;
  }

  if (tport == NULL) {
    if (dig->sips)
      tport = "tls";
    else
      tport = "udp", tport2 = "tcp";
  }

Pekka Pessi's avatar
Pekka Pessi committed
749
  tcount = count_transports(dig, tport, tport2);
750 751 752 753
  if (!tcount)
    return 0;

  if (type1) {
Pekka Pessi's avatar
Pekka Pessi committed
754
    error = sres_blocking_query(dig->sres, type1, host, 0, &answers1);
755 756 757 758 759 760 761 762
    if (error >= 0)
      for (i = 0; answers1[i]; i++) {
	sres_common_t *r = answers1[i]->sr_record;
	count1 += r->r_type == type1 &&	r->r_status == 0;
      }
  }

  if (type2) {
Pekka Pessi's avatar
Pekka Pessi committed
763
    error = sres_blocking_query(dig->sres, type2, host, 0, &answers2);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
    if (error >= 0)
      for (i = 0; answers2[i]; i++) {
	sres_common_t *r = answers2[i]->sr_record;
	count2 += r->r_type == type2 &&	r->r_status == 0;
      }
  }

  if (count1 + count2) {
    double w = weight / (count1 + count2) / tcount;

    if (count1)
      print_addr_results(dig->tports, tport, tport2,
			 answers1, type1, family1, port,
			 w, dig->preference);
    if (count2)
      print_addr_results(dig->tports, tport, tport2,
			 answers2, type2, family2, port,
			 w, dig->preference);
  }

  sres_free_answers(dig->sres, answers1);
  sres_free_answers(dig->sres, answers2);

  return count1 + count2;
}

void
print_addr_results(struct transport const *tports,
		   char const *tport, char const *tport2,
		   sres_record_t **answers, int type, int af,
		   char const *port,
		   double weight, int preference)
{
  int i, j;
  char addr[64];

  for (i = 0; answers[i]; i++) {
    if (answers[i]->sr_record->r_type != type)
      continue;
    if (answers[i]->sr_record->r_status != 0)
      continue;

    inet_ntop(af, &answers[i]->sr_a->a_addr, addr, sizeof addr);

    for (j = 0; tports[j].name; j++) {
      if (strcasecmp(tport, tports[j].name) == 0)
	print_result(addr, port, tport, weight, preference);
      if (tport2 && strcasecmp(tport2, tports[j].name) == 0)
	print_result(addr, port, tport2, weight, preference);
    }
  }
}

void print_result(char const *addr,
		  char const *port,
		  char const *tport,
		  double weight,
		  unsigned preference)
{
  if (!port || !port[0])
    port = transport_is_secure(tport) ? "5061" : "5060";

  printf("\t%u %.3f %s %s %s\n", preference, weight, tport, port, addr);
}