stun_dns.c 6.83 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
/**@internal
26
 * @file stun_dns.c
27 28
 * @brief Functins to discover STUN server address using DNS-SRV.
 *
29
 * Refs:
30 31
 *   - RFC3489/3489bis
 *   - RFC2782
32
 *
33 34 35 36 37
 * @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
#include "stun_internal.h"

57 58
#include <string.h>

59 60 61 62 63
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;
64
  su_socket_t        stun_socket;
65
  stun_dns_lookup_f  stun_cb;
66
  char              *stun_tcp_target;
67
  char              *stun_udp_target;
68
  uint16_t           stun_tcp_port;
69
  uint16_t           stun_udp_port;
70
  unsigned           stun_state:2;       /**< bit0:udp, bit1:tcp */
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
};

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 */

93
  for (i = 0; answer && answer[i] != NULL; i++) {
94 95
    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) {
96
      const char *tcp_name = STUN_SRV_SERVICE_TCP;
97 98
      const char *udp_name = STUN_SRV_SERVICE_UDP;
      if ((self->stun_state & stun_dns_tls) == 0 &&
99 100 101
	  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;
102
	self->stun_state |= stun_dns_tls;
103 104
	SU_DEBUG_5(("%s: stun (tcp) for domain %s is at %s:%u.\n",
		    __func__, rr->srv_record->r_name, self->stun_tcp_target, self->stun_tcp_port));
105 106 107 108 109 110
      }
      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;
111 112
	SU_DEBUG_5(("%s: stun (udp) for domain %s is at %s:%u.\n",
		    __func__, rr->srv_record->r_name, self->stun_udp_target, self->stun_udp_port));
113 114 115 116 117 118
      }
    }
  }

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

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

  sres_free_answers(self->stun_sres, answer);
}

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

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

145
  self = su_home_new(sizeof(stun_dns_lookup_t));
146

147
  /* see nta.c:outgoing_answer_srv() */
148 149 150 151 152
  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) {
153 154 155 156 157 158 159 160 161 162 163 164 165
    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);
166 167 168 169
  }
  else {
    su_free(NULL, self), self = NULL;
  }
170

171 172 173 174
  return self;
}

/**
175 176 177 178 179 180 181
 * 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);

182
  su_home_unref(self->stun_home);
183 184 185 186 187
}

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

  return result;
}

/**
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
218
 */
219
int stun_dns_lookup_tcp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *port)
220
{
221 222 223 224 225 226
  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;
  }
227

228
  return result;
229 230
}

231 232 233 234 235 236 237 238 239 240
/**
 * 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
241
 */
242 243 244 245 246 247 248
int stun_dns_lookup_stp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *port)
{
  /* XXX: not implemented */
  return -1;
}