sresolv.c 7.83 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
/*
 * 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
 *
 */

/**@CFILE sresolv.c
 * @brief Sofia DNS Resolver interface using su_root_t.
 * 
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Teemu Jalava <Teemu.Jalava@nokia.com>
 * @author Mikko Haataja
 *
 * @todo The resolver should allow handling arbitrary records, too.
 */

35
#include "config.h"
Pekka Pessi's avatar
Pekka Pessi committed
36

37 38 39
#define SU_TIMER_ARG_T  struct sres_sofia_s
#define SU_WAKEUP_ARG_T struct sres_sofia_register_s
#define SRES_ASYNC_T    struct sres_sofia_s
Pekka Pessi's avatar
Pekka Pessi committed
40

41
#include <sofia-sip/sresolv.h>
Pekka Pessi's avatar
Pekka Pessi committed
42

43 44
#define SU_LOG sresolv_log
#include <sofia-sip/su_debug.h>
Pekka Pessi's avatar
Pekka Pessi committed
45

46 47
#include <string.h>
#include <assert.h>
Pekka Pessi's avatar
Pekka Pessi committed
48 49 50 51 52 53

/* ====================================================================== */
/* Glue functions for Sofia root (reactor) */

#define TAG_NAMESPACE "sres"

54 55
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
56

57
tag_typedef_t srestag_any = NSTAG_TYPEDEF(*);
Pekka Pessi's avatar
Pekka Pessi committed
58 59 60
tag_typedef_t srestag_resolv_conf = STRTAG_TYPEDEF(resolv_conf);
tag_typedef_t srestag_resolv_conf_ref = REFTAG_TYPEDEF(srestag_resolv_conf);

61 62 63 64
typedef struct sres_sofia_s sres_sofia_t;
typedef struct sres_sofia_register_s sres_sofia_register_t;

struct sres_sofia_register_s {
65 66 67
  sres_sofia_t *reg_ptr;
  int reg_socket;
  int reg_index;		/**< Registration index */
68 69 70
};

struct sres_sofia_s {
Pekka Pessi's avatar
Pekka Pessi committed
71 72 73
  sres_resolver_t *srs_resolver;
  su_root_t  	  *srs_root;
  su_timer_t 	  *srs_timer;
74
  int              srs_socket;
75 76 77 78 79 80
  sres_sofia_register_t srs_reg[SRES_MAX_NAMESERVERS];
};

static int sres_sofia_update(sres_sofia_t *, 
			     int new_socket,
			     int old_socket);
Pekka Pessi's avatar
Pekka Pessi committed
81 82 83 84 85

static void sres_sofia_timer(su_root_magic_t *magic, 
			     su_timer_t *t,
			     sres_sofia_t *arg);

86 87
static int sres_sofia_poll(su_root_magic_t *, su_wait_t *, 
			   sres_sofia_register_t *);
Pekka Pessi's avatar
Pekka Pessi committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

/**Create a resolver.
 *
 * The function sres_resolver_create() is used to allocate and initialize
 * the resolver object using the Sofia asynchronous reactor #su_root_t.
 */
sres_resolver_t *
sres_resolver_create(su_root_t *root, 
		     char const *conf_file_path,
		     tag_type_t tag, tag_value_t value, ...)
{
  sres_resolver_t *res;
  sres_sofia_t *srs;
  ta_list ta;

103
  if (root == NULL)
104
    return su_seterrno(EFAULT), (void *)NULL;
Pekka Pessi's avatar
Pekka Pessi committed
105 106 107 108 109 110 111

  ta_start(ta, tag, value);
  tl_gets(ta_args(ta),
	  SRESTAG_RESOLV_CONF_REF(conf_file_path),
	  TAG_END());
  ta_end(ta);

112
  res = sres_resolver_new_with_cache(conf_file_path, NULL, NULL);
113
  srs = res ? su_zalloc(0, sizeof *srs) : NULL;
Pekka Pessi's avatar
Pekka Pessi committed
114 115

  if (res && srs) {
116
    su_timer_t *t;
Pekka Pessi's avatar
Pekka Pessi committed
117 118 119

    srs->srs_resolver = res;
    srs->srs_root = root;
120
    srs->srs_socket = -1;
Pekka Pessi's avatar
Pekka Pessi committed
121

122
    sres_resolver_set_async(res, sres_sofia_update, srs, 0);
Pekka Pessi's avatar
Pekka Pessi committed
123
    
124 125 126 127 128 129 130 131 132
    t = su_timer_create(su_root_task(root), SRES_RETRANSMIT_INTERVAL);
    srs->srs_timer = t;

    if (!srs->srs_timer)
      SU_DEBUG_3(("sres: cannot create timer\n"));
    else if (su_timer_set_for_ever(t, sres_sofia_timer, srs) < 0)
      SU_DEBUG_3(("sres: cannot set timer\n"));
    else
      return res;		/* Success! */
Pekka Pessi's avatar
Pekka Pessi committed
133

134
    sres_resolver_destroy(res), res = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
135 136 137 138 139 140
  }

  return res;
}

/** Destroy a resolver object. */
141
int 
Pekka Pessi's avatar
Pekka Pessi committed
142 143
sres_resolver_destroy(sres_resolver_t *res)
{
144 145 146 147
  sres_sofia_t *srs;

  if (res == NULL)
    return su_seterrno(EFAULT);
Pekka Pessi's avatar
Pekka Pessi committed
148

149
  srs = sres_resolver_get_async(res, sres_sofia_update);
150 151
  if (srs == NULL)
    return su_seterrno(EINVAL);
Pekka Pessi's avatar
Pekka Pessi committed
152

153 154 155
  sres_resolver_unref(srs->srs_resolver); 

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
156 157
}

158
/**Update registered socket.
159
 *
160 161
 * @retval 0 if success
 * @retval -1 upon failure
162
 */
163 164 165 166 167 168
static int sres_sofia_update(sres_sofia_t *srs,
			     int new_socket,
			     int old_socket)
{
  char const *what = NULL;
  su_wait_t wait[1];
169 170
  sres_sofia_register_t *reg = NULL;
  sres_sofia_register_t *old_reg = NULL;
171 172 173
  int i, index = -1, error = 0;
  int N = SRES_MAX_NAMESERVERS;

174 175 176 177 178
  SU_DEBUG_9(("sres_sofia_update(%p, %d, %d)\n", srs, new_socket, old_socket));

  if (srs == NULL)
    return 0;

179 180
  if (old_socket == new_socket) {
    if (old_socket == -1) {
181
      sres_resolver_set_async(srs->srs_resolver, sres_sofia_update, NULL, 0);
182 183
      /* Destroy srs */
      for (i = 0; i < N; i++) {
184
	if (!srs->srs_reg[i].reg_index)
185
	  continue;
186
	su_root_deregister(srs->srs_root, srs->srs_reg[i].reg_index);
187 188 189 190 191
	memset(&srs->srs_reg[i], 0, sizeof(srs->srs_reg[i]));
      }
      su_timer_destroy(srs->srs_timer), srs->srs_timer = NULL;
      su_free(NULL, srs);
    }
192
    return 0;
193
  }
Pekka Pessi's avatar
Pekka Pessi committed
194

195 196
  if (old_socket != -1)
    for (i = 0; i < N; i++)
197 198
      if ((srs->srs_reg + i)->reg_socket == old_socket) {
	old_reg = srs->srs_reg + i;
199 200
	break;
      }
Pekka Pessi's avatar
Pekka Pessi committed
201

202
  if (new_socket != -1) {
203
    if (old_reg == NULL) {
204
      for (i = 0; i < N; i++) {
205
	if (!(srs->srs_reg + i)->reg_ptr)
206 207 208 209 210
	  break;
      }
      if (i > N)
	return su_seterrno(ENOMEM);

211
      reg = srs->srs_reg + i;
212 213
    }
    else 
214
      reg = old_reg;
215 216
  }

217
  if (reg) {
218
    if (su_wait_create(wait, new_socket, SU_WAIT_IN | SU_WAIT_ERR) == -1) {
219
      reg = NULL;
220 221 222 223
      what = "su_wait_create";
      error = su_errno();
    }

224 225
    if (reg)
      index = su_root_register(srs->srs_root, wait, sres_sofia_poll, reg, 0);
226 227

    if (index < 0) {
228
      reg = NULL;
229 230 231 232 233 234
      what = "su_root_register";
      error = su_errno();
      su_wait_destroy(wait);
    }
  }

235 236 237 238 239
  if (old_reg) {
    if (old_socket == srs->srs_socket)
      srs->srs_socket = -1;
    su_root_deregister(srs->srs_root, old_reg->reg_index);
    memset(old_reg, 0, sizeof *old_reg);
240 241
  }

242
  if (reg) {
243
    srs->srs_socket = new_socket;
244 245 246 247

    reg->reg_ptr = srs;
    reg->reg_socket = new_socket;
    reg->reg_index = index;
248 249 250 251 252 253 254 255
  }

  if (!what)
    return 0;		/* success */

  SU_DEBUG_3(("sres: %s: %s\n", what, su_strerror(error)));

  return su_seterrno(error);			
Pekka Pessi's avatar
Pekka Pessi committed
256 257 258
}


259
/** Return a socket registered to su_root_t object.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
 *
 * @retval sockfd if succesful
 * @retval -1 upon an error
 *
 * @ERRORS
 * @ERROR EFAULT Invalid argument passed.
 * @ERROR EINVAL Resolver is not using su_root_t.
 */
int sres_resolver_root_socket(sres_resolver_t *res)
{
  sres_sofia_t *srs;
  int i, N = SRES_MAX_NAMESERVERS;

  if (res == NULL)
    return su_seterrno(EFAULT);

276
  srs = sres_resolver_get_async(res, sres_sofia_update);
277 278 279 280

  if (!srs)
    return su_seterrno(EINVAL);

281 282 283
  if (sres_resolver_set_async(res, sres_sofia_update, srs, 1) < 0)
    return -1;

284 285 286 287
  if (srs->srs_socket != -1)
    return srs->srs_socket;

  for (i = 0; i < N; i++) {
288
    if (!srs->srs_reg[i].reg_ptr)
289 290 291
      break;
  }

292 293 294 295 296 297 298 299
  if (i < N) {
    srs->srs_socket = srs->srs_reg[i].reg_socket;
  }
  else {
    int socket;
    if (sres_resolver_sockets(res, &socket, 1) < 0)
      return -1;
  }
300 301 302 303 304

  return srs->srs_socket; 
}


Pekka Pessi's avatar
Pekka Pessi committed
305 306 307 308 309
/** Sofia timer wrapper. */
static 
void 
sres_sofia_timer(su_root_magic_t *magic, su_timer_t *t, sres_sofia_t *srs)
{
310
  sres_resolver_timer(srs->srs_resolver, -1);
Pekka Pessi's avatar
Pekka Pessi committed
311 312
}

313

314
/** Sofia poll/select wrapper, called by su_root_t object */
Pekka Pessi's avatar
Pekka Pessi committed
315 316 317 318
static 
int 
sres_sofia_poll(su_root_magic_t *magic, 
		su_wait_t *w, 
319
		sres_sofia_register_t *reg)
Pekka Pessi's avatar
Pekka Pessi committed
320
{
321
  sres_sofia_t *srs = reg->reg_ptr;
Pekka Pessi's avatar
Pekka Pessi committed
322
  int retval = 0;
323
  int socket = reg->reg_socket;
324
  int events = su_wait_events(w, socket);
Pekka Pessi's avatar
Pekka Pessi committed
325 326

  if (events & SU_WAIT_ERR)
327 328 329
    retval = sres_resolver_error(srs->srs_resolver, socket);
  if (events & SU_WAIT_IN)
    retval = sres_resolver_receive(srs->srs_resolver, socket);
Pekka Pessi's avatar
Pekka Pessi committed
330 331 332

  return retval;
}