resolve_sip.c 7.32 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3
/*
 * This file is part of the Sofia-SIP package
 *
Pekka Pessi's avatar
Pekka Pessi committed
4
 * Copyright (C) 2006 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
5 6 7
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
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
 * 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
 *
 */

/**@file resolve_sip.c Use sresolv library to resolve a SIP or SIPS domain.
 *
 * This is an example program for @b sresolv library. 
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @par Created: Tue Jul 16 18:50:14 2002 ppessi
 *
 *
 */

#include <stdint.h>
#include <netinet/in.h>
#include <arpa/inet.h>

/* Typesafe */
#define SRES_CONTEXT_T struct context

43
#include "sofia-sip/sresolv.h"
Pekka Pessi's avatar
Pekka Pessi committed
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

char const name[] = "sip_resolve";

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

enum progress {
  querying_naptr,
  querying_srv,
  querying_cname,
  querying_a6,
  querying_aaaa,
  querying_a
};

/* Application context */
struct context
{
  sres_resolver_t     *sres;
  int                  sr_exitcode;
  int                  sr_ready;

  char const          *sr_canon;
  int                  sr_sips;
  char const          *sr_tport;

  sres_query_t        *sr_query;
  unsigned short       sr_port;

  int                  sr_n_sockets;
77
  sres_socket_t       *sr_sockets;
Pekka Pessi's avatar
Pekka Pessi committed
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 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
  struct pollfd       *sr_pollfds;

#if 0
  char const          *sr_domain;

  enum progress        sr_progress;
  sres_naptr_record_t *sr_naptr;
  sres_srv_record_t   *sr_srv;
  sres_cname_record_t *sr_cname;
  sres_aaaa_record_t  *sr_aaaa;
  sres_a6_record_t    *sr_a6;
  sres_a_record_t     *sr_a;
#endif
};

static int query_srv(struct context *sr, char const *domain);
static int query_a(struct context *sr, char const *domain);

/* Process NAPTR records */
static 
void answer_to_naptr_query(sres_context_t *sr, 
			   sres_query_t *q, 
			   sres_record_t *answers[])
{
  int i;

  sr->sr_query = NULL;

  /* Sort NAPTR records by the order. */
  sres_sort_answers(sr->sres, answers);  

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

    if (na->na_record->r_status)
      /* There was an error */
      continue;

    printf("naptr: %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);

    switch (na->na_flags[0]) {
    case 's': /* srv */
      if (strncasecmp("SIP+", na->na_services, 4))
	/* Something else but SIP */
	break;
      query_srv(sr, na->na_replace);
      sres_free_answers(sr->sres, answers);
      return;

    case 'a':
      if (strncasecmp("SIP+", na->na_services, 4))
	/* Something else but SIP */
	break;
      query_a(sr, na->na_replace);
      sres_free_answers(sr->sres, answers);
      return;
    }
  }

  query_a(sr, /* sr->sr_uri->url_host */ sr->sr_canon);

  sres_free_answers(sr->sres, answers);
}

static
int query_naptr(struct context *sr, char const *domain)
{
  sres_record_t **answers;

  answers = sres_cached_answers(sr->sres, sres_type_naptr, domain);

  if (answers) {
    answer_to_naptr_query(sr, NULL, answers);
    return 0;
  }
  else {
    sr->sr_query = sres_query_make(sr->sres, answer_to_naptr_query, sr,
				   sr->sr_sockets[0], sres_type_naptr, domain);
    return sr->sr_query ? 0 : -1;
  }
}

/* Process SRV records */
static 
void answer_to_srv_query(sres_context_t *sr, sres_query_t *q, 
			 sres_record_t *answers[])
{
  int i;

  sr->sr_query = NULL;

  sres_sort_answers(sr->sres, answers);  /* Sort SRV records by the priority. */

  for (i = 0; answers && answers[i]; i++) {
    sres_srv_record_t const *srv = answers[i]->sr_srv;
    if (srv->srv_record->r_status)
      /* There was an error */
      continue;
    sr->sr_port = srv->srv_port;
    query_a(sr, srv->srv_target);
    return;
  }

  query_a(sr, /* sr->sr_uri->url_host */ sr->sr_canon);

  sres_free_answers(sr->sres, answers);
}

static
int query_srv(struct context *sr, char const *domain)
{
  sres_record_t **answers;

  answers = sres_cached_answers(sr->sres, sres_type_srv, domain);

  if (answers) {
    answer_to_srv_query(sr, NULL, answers);
    return 0;
  }
  else {
    sr->sr_query = sres_query_make(sr->sres, answer_to_srv_query, sr,
				   sr->sr_sockets[0], sres_type_srv, domain);
    return sr->sr_query ? 0 : -1;
  }
}

/* Process A records */
static 
void answer_to_a_query(sres_context_t *sr, sres_query_t *q, 
		       sres_record_t *answers[])
{
  int i;

  sr->sr_query = NULL;

  for (i = 0; answers && answers[i]; i++) {
    char addr[64];
    sres_a_record_t const *a = answers[i]->sr_a;

    if (a->a_record->r_status)
      continue;			      /* There was an error */

    inet_ntop(AF_INET, &a->a_addr, addr, sizeof(addr));
    printf("%s@%s:%u\n", sr->sr_tport, addr, sr->sr_port);
    sr->sr_exitcode = 0;
  }

  sres_free_answers(sr->sres, answers);

  sr->sr_ready = 1;
}

static
int query_a(struct context *sr, char const *domain)
{
  sres_record_t **answers;

  answers = sres_cached_answers(sr->sres, sres_type_a, domain);

  if (answers) {
    answer_to_a_query(sr, NULL, answers);
    return 0;
  }
  else {
    sr->sr_query = sres_query_make(sr->sres, answer_to_a_query, sr,
				   sr->sr_sockets[0], sres_type_a, domain);
    return sr->sr_query ? 0 : -1;
  }
}

void usage(void)
{
  fprintf(stderr, "usage: resolve_sip [-s] [@dnsserver] domain\n");
  exit(1);
}

int prepare_run(struct context *sr)
{
  sr->sr_n_sockets = 1;
  sr->sr_sockets = calloc(1, sizeof(*sr->sr_sockets));
  sr->sr_pollfds = calloc(1, sizeof(*sr->sr_pollfds));
  
  if (!sr->sr_sockets || !sr->sr_pollfds ||
      (sres_resolver_sockets(sr->sres, sr->sr_sockets, 1) == -1))
    return 0;
  
  sr->sr_pollfds[0].fd = sr->sr_sockets[0];
  sr->sr_pollfds[0].events = POLLIN | POLLERR;

  return 1;
}

void run(struct context *sr)
{
  int i, n, events;

  n = sr->sr_n_sockets;

  while (!sr->sr_ready) {
    events = poll(sr->sr_pollfds, n, 500);

    if (events) 
      for (i = 0; i < n; i++) {
	if (sr->sr_pollfds[i].revents)
	  sres_resolver_receive(sr->sres, sr->sr_pollfds[i].fd);
      }

    /* No harm is done (except wasted CPU) if timer is called more often */
    sres_resolver_timer(sr->sres, sr->sr_sockets[0]); 
  }
}

int main(int argc, char *argv[])
{
  struct context sr[1] = {{ 0 }};
  char const *dnsserver = NULL;

  sr->sr_exitcode = 1;
  sr->sr_tport = "*";

  if (argv[1] && strcmp(argv[1], "-s") == 0)
    sr->sr_sips = 1, argv++;

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

  if (argv[1] == NULL)
    usage();

  sr->sres = sres_resolver_new(getenv("SRESOLV_CONF"));

  if (sr->sres) 
    if (prepare_run(sr)) 
      if (query_naptr(sr, sr->sr_canon = argv[1]) == 0)
	run(sr);

  sres_resolver_unref(sr->sres), sr->sres = NULL;

  return sr->sr_exitcode;
}