sip-dig.c 19.8 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
/**@file sip-dig.c Use sresolv library to resolve a SIP or SIPS URI
26 27 28 29 30 31 32 33
 *
 * 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
 */

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 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 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
/**@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>
 * <dt>-p <e>protoname</e></dt>
 * <dd>Use named transport protocol.
 * </dd>
 * <dt>-s <e>protoname,service</e></dt>
 * <dd>Resolve named transport protocol with NAPTR @e service
 * </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
 *
 * Resolve sip:openlaboratory.net and sips:openlaboratory.net, prefer TLS
 * over TCP, TCP over UDP:
 * <code>
 * $ sip-dig --tls --tcp --udp sip:openlaboratory.net sips:openlaboratory.net
 *	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
 * </endcode>
 *
 * Resolve sips:example.net with DTLS (TLS-UDP) and TLS:
 * <code>
 * $ sip-dig -p tls-udp/SIPS+D2U --tls sips:example.net
 *	1 0.500 tls-udp 5061 172.21.55.26
 *	2 0.500 tls 5061 172.21.55.26
 * </endcode>
 *
 * @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"

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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <arpa/inet.h>

#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
198
int prepare_transport(struct dig *dig, char const *tport);
199

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

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

int main(int argc, char *argv[])
{
212
  int exitcode = 0;
213
  int o_sctp = 1, o_tls_sctp = 1, o_verbatim = 1;
214
  int family = 0, multiple = 0;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  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 }};

  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;
232 233 234 235 236 237 238 239 240 241 242 243 244
    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)
	usage(1);

Pekka Pessi's avatar
Pekka Pessi committed
245
      prepare_transport(dig, proto);
246
    }
247
    else if (strcmp(argv[1], "--udp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
248
      prepare_transport(dig, "udp");
249
    else if (strcmp(argv[1], "--tcp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
250
      prepare_transport(dig, "tcp");
251
    else if (strcmp(argv[1], "--tls") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
252
      prepare_transport(dig, "tls");
253
    else if (strcmp(argv[1], "--sctp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
254
      prepare_transport(dig, "sctp");
255
    else if (strcmp(argv[1], "--tls-sctp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
256
      prepare_transport(dig, "tls-sctp");
257
    else if (strcmp(argv[1], "--tls-udp") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
258
      prepare_transport(dig, "tls-udp");
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    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
      usage(1);
    argv++;
  }

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

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

280
  if (!argv[1])
281 282
    usage(1);

283
  multiple = argv[1] && argv[2];
284

Pekka Pessi's avatar
Pekka Pessi committed
285 286 287
  if (!count_transports(dig, NULL, NULL)) {
    prepare_transport(dig, "udp");
    prepare_transport(dig, "tcp");
288
    if (o_sctp)
Pekka Pessi's avatar
Pekka Pessi committed
289 290
      prepare_transport(dig, "sctp");
    prepare_transport(dig, "tls");
291
    if (o_tls_sctp)
Pekka Pessi's avatar
Pekka Pessi committed
292
      prepare_transport(dig, "tls-sctp");
293 294
  }

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  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;
315
    }
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

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

338 339 340 341 342
    if (!host_is_domain(host)) {
      fprintf(stderr, "%s: invalid host\n", string);
      exitcode = 1;
      continue;
    }
343

344 345
    dig->sips = uri->url_type == url_sips;
    dig->preference = 1;
346

347 348 349 350 351 352
    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 */;
353 354

    fprintf(stderr, "%s: not found\n", string);
355 356
    exitcode = 1;
  }
357 358 359 360 361 362 363 364 365 366 367 368

  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
369
int prepare_transport(struct dig *dig, char const *tport)
370
{
Pekka Pessi's avatar
Pekka Pessi committed
371
  struct transport *tports = dig->tports;
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 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 414 415 416 417 418 419 420
  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;

  if (strcasecmp(tport, "udp") == 0) {
    tports[j].service = "SIP+D2U";
    tports[j].srv = "_sip._udp.";
    tports[j++].name = "udp";
  }
  else if (strcasecmp(tport, "tcp") == 0) {
    tports[j].service = "SIP+D2T";
    tports[j].srv = "_sip._tcp.";
    tports[j++].name = "tcp";
  }
  else if (strcasecmp(tport, "tls") == 0) {
    tports[j].service = "SIPS+D2T";
    tports[j].srv = "_sips._tcp.";
    tports[j++].name = "tls";
  }
  else if (strcasecmp(tport, "sctp") == 0) {
    tports[j].service = "SIP+D2S";
    tports[j].srv = "_sip._sctp.";
    tports[j++].name = "sctp";
  }
  else if (strcasecmp(tport, "tls-sctp") == 0) {
    tports[j].service = "SIPS+D2S";
    tports[j].srv = "_sips._sctp.";
    tports[j++].name = "tls-sctp";
  }
  else {
    fprintf(stderr, "%s: ignoring unknown transport \"%s\" with NAPTR\n",
	    name, tport);
    return 0;
  }

  tports[j].service = tports[j].srv = tports[j].name = NULL;

  return 1;
}

int
Pekka Pessi's avatar
Pekka Pessi committed
421
count_transports(struct dig *dig,
422 423 424
		 char const *tport,
		 char const *tport2)
{
Pekka Pessi's avatar
Pekka Pessi committed
425
  
426
  int i, tcount = 0;
Pekka Pessi's avatar
Pekka Pessi committed
427
  struct transport const *tports = dig->tports;
428 429

  for (i = 0; tports[i].name; i++) {
Pekka Pessi's avatar
Pekka Pessi committed
430 431
    if (dig->sips && !transport_is_secure(tports[i].name))
      continue;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    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
463
  error = sres_blocking_query(dig->sres, sres_type_naptr, host, 0, &answers);
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 492 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
  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)
{
540
  int i, j, n;
541 542 543
  int tcount, count = 0, scount;
  char *domain;

544 545 546 547
  struct {
    char const *proto; sres_record_t **answers;
  } srvs[N_TPORT + 1] = {{ NULL }};

Pekka Pessi's avatar
Pekka Pessi committed
548
  tcount = count_transports(dig, tport, NULL);
549 550 551
  if (!tcount)
    return 0;

552
  for (i = 0, n = 0; dig->tports[i].name; i++) {
553 554 555
    if (tport && strcasecmp(dig->tports[i].name, tport))
      continue;

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

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

561
    if (domain) {
Pekka Pessi's avatar
Pekka Pessi committed
562
      if (sres_blocking_query(dig->sres, sres_type_srv, domain, 0,
563 564 565 566
			      &srvs[n].answers) >= 0) {
	srvs[n++].proto = dig->tports[i].name;
      }
      free(domain);
567
    }
568 569 570 571
  }

  if (n == 0)
    return 0;
572

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
  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;
    }
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
  }

  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
621
  error = sres_blocking_query(dig->sres, sres_type_srv, domain, 0, &answers);
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  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) {
650
    scount = dig_srv_at(dig, tport, answers, weight, pweight, priority);
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 679 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
    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
720
  tcount = count_transports(dig, tport, tport2);
721 722 723 724
  if (!tcount)
    return 0;

  if (type1) {
Pekka Pessi's avatar
Pekka Pessi committed
725
    error = sres_blocking_query(dig->sres, type1, host, 0, &answers1);
726 727 728 729 730 731 732 733
    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
734
    error = sres_blocking_query(dig->sres, type2, host, 0, &answers2);
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 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
    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);
}