stun_dns.c 6.82 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 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * 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
 *
 */

/**
 * @file stun_dns.c 
 * @brief Functins to discover STUN server address using DNS-SRV.
 *
 * Refs: 
 *   - RFC3489/3489bis
 *   - RFC2782
 * 
 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
 */

#include "config.h"

38
#define STUN_SRV_SERVICE_TCP "_stun._tcp"
39 40 41 42 43 44 45 46 47
#define STUN_SRV_SERVICE_UDP "_stun._udp"

#include <sofia-sip/stun.h>
#include <sofia-sip/su.h>
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/su_wait.h>
#define SRES_CONTEXT_T stun_dns_lookup_t
#include <sofia-sip/sresolv.h>

48 49 50 51 52 53 54
#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
static char const __func__[] = "stun_dns";
#endif

55 56 57 58 59 60 61
#include "stun_internal.h"

struct stun_dns_lookup_s {
  su_home_t          stun_home[1];
  su_root_t         *stun_root;
  stun_magic_t      *stun_magic;
  sres_resolver_t   *stun_sres;
62
  su_socket_t        stun_socket;
63
  stun_dns_lookup_f  stun_cb;
64
  char              *stun_tcp_target;
65
  char              *stun_udp_target;
66
  uint16_t           stun_tcp_port;
67
  uint16_t           stun_udp_port;
68
  unsigned           stun_state:2;       /**< bit0:udp, bit1:tcp */
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
};

enum stun_dns_state {
  stun_dns_udp = 1,
  stun_dns_tls = 2,
  stun_dns_done = stun_dns_udp | stun_dns_tls
};

/**
 * Internal callback used for gathering DNS replies.
 */
static void priv_sres_cb(stun_dns_lookup_t *self,
			 sres_query_t *q,
			 sres_record_t **answer)
{
  int i;

  sres_sort_answers(self->stun_sres, answer);

  /* note: picks the first ones (sort puts records with most
   *       weight at start */

91
  for (i = 0; answer && answer[i] != NULL; i++) {
92 93
    sres_srv_record_t *rr = (sres_srv_record_t *) answer[i]->sr_srv;
    if (rr && rr->srv_record && rr->srv_record->r_type == sres_type_srv) {
94
      const char *tcp_name = STUN_SRV_SERVICE_TCP;
95 96
      const char *udp_name = STUN_SRV_SERVICE_UDP;
      if ((self->stun_state & stun_dns_tls) == 0 &&
97 98 99
	  strncmp(rr->srv_record->r_name, tcp_name, strlen(tcp_name)) == 0) {
	self->stun_tcp_target = su_strdup(self->stun_home, rr->srv_target);
	self->stun_tcp_port = rr->srv_port;
100
	self->stun_state |= stun_dns_tls;
101
	SU_DEBUG_5(("%s: stun (tcp) for domain %s is at %s:%u.\n", 
102
		    __func__, rr->srv_record->r_name, self->stun_tcp_target, self->stun_tcp_port)); 
103 104 105 106 107 108
      }
      else if ((self->stun_state & stun_dns_udp) == 0 &&
	       strncmp(rr->srv_record->r_name, udp_name, strlen(udp_name)) == 0) {
	self->stun_udp_target = su_strdup(self->stun_home, rr->srv_target);
	self->stun_udp_port = rr->srv_port;
	self->stun_state |= stun_dns_udp;
109
	SU_DEBUG_5(("%s: stun (udp) for domain %s is at %s:%u.\n", 
110 111 112 113 114 115 116
		    __func__, rr->srv_record->r_name, self->stun_udp_target, self->stun_udp_port)); 
      }
    }
  }

  if (self->stun_state == stun_dns_done) {
    self->stun_cb(self, self->stun_magic);
117

118
    sres_resolver_timer(self->stun_sres, -1);
119 120 121 122 123 124
  }

  sres_free_answers(self->stun_sres, answer);
}

/**
125
 * Performs a DNS-SRV check for STUN 'stun' (tcp) and
126 127 128 129 130 131 132 133 134 135
 * 'stun' (udp) services for 'domain'.
 *
 * The result will be delivered asynchronously in the
 * 'func' callback. 'root' will be used as the event loop.
 */
stun_dns_lookup_t *stun_dns_lookup(stun_magic_t *magic, 
				   su_root_t *root,
				   stun_dns_lookup_f func, 
				   const char *domain)
{
136
  stun_dns_lookup_t *self;
137
  sres_query_t *query;
138 139 140 141

  if (!domain || 
      strlen(domain) + strlen(STUN_SRV_SERVICE_UDP ".") + 1>= SRES_MAXDNAME)
    return NULL;
142
  
143
  self = su_home_new(sizeof(stun_dns_lookup_t));
144

145
  /* see nta.c:outgoing_answer_srv() */
146 147 148 149 150
  self->stun_magic = magic;
  self->stun_cb = func;
  self->stun_root = root;
  self->stun_sres = sres_resolver_create(root, NULL, TAG_END());
  if (self->stun_sres) {
151 152 153 154 155 156 157 158 159 160 161 162 163
    char srvname[SRES_MAXDNAME + 1];

    snprintf(srvname, sizeof srvname, "%s.%s", STUN_SRV_SERVICE_UDP, domain);

    query = sres_query(self->stun_sres, priv_sres_cb, self,
		       sres_type_srv,
		       srvname);

    snprintf(srvname, sizeof srvname, "%s.%s", STUN_SRV_SERVICE_TCP, domain);

    query = sres_query(self->stun_sres, priv_sres_cb, self,
		       sres_type_srv,
		       srvname);
164 165 166 167 168 169 170 171 172
  }
  else {
    su_free(NULL, self), self = NULL;
  }
  
  return self;
}

/**
173 174 175 176 177 178 179
 * Destroys the 'self' object created by stun_dns_lookup_destroy().
 */
void stun_dns_lookup_destroy(stun_dns_lookup_t *self)
{
  if (self->stun_sres)
    sres_resolver_destroy(self->stun_sres);

180
  su_home_unref(self->stun_home);
181 182 183 184 185
}

/**
 * Fetches the results of a completed STUN DNS-SRV lookup
 * for "_stun._udp" service (RFC3489/3489bis).
186 187
 *
 * @param self context pointer
188 189 190
 * @param target location where to stored the 'target'
 *        SRV field for stun service
 * @param port location where to store port number
191 192 193
 *
 * @return 0 on success, non-zero otherwise
 */ 
194
int stun_dns_lookup_udp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *port)
195 196 197
{
  int result = -1;
  if (self->stun_state == stun_dns_done) {
198 199
    if (target) *target = self->stun_udp_target;
    if (port) *port = self->stun_udp_port;
200 201 202 203 204 205 206
    result = 0;
  }

  return result;
}

/**
207 208 209 210 211 212 213 214 215 216 217
 * Fetches the results of a completed STUN DNS-SRV lookup
 * for "_stun._tcp" service (RFC3489).
 *
 * @param self context pointer
 * @param target location where to stored the 'target'
 *        SRV field for stun service
 * @param port location where to store port number
 *
 * @return 0 on success, non-zero otherwise
 */ 
int stun_dns_lookup_tcp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *port)
218
{
219 220 221 222 223 224
  int result = -1;
  if (self->stun_state == stun_dns_done) {
    if (target) *target = self->stun_tcp_target;
    if (port) *port = self->stun_tcp_port;
    result = 0;
  }
225

226
  return result;
227 228
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
/**
 * Fetches the results of a completed STUN DNS-SRV lookup
 * for "_stun._udp" service (3489bis, "Short-Term Password").
 *
 * @param self context pointer
 * @param target location where to stored the 'target'
 *        SRV field for stun service
 * @param port location where to store port number
 *
 * @return 0 on success, non-zero otherwise
 */ 
int stun_dns_lookup_stp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *port)
{
  /* XXX: not implemented */
  return -1;
}