Commit 288a86ee authored by Kai Vehmanen's avatar Kai Vehmanen

Added STUN DNS-SRV functionality to stun module. A simple test app is also...

Added STUN DNS-SRV functionality to stun module. A simple test app is also provided. This code is not yet used by other parts of the stun module.

darcs-hash:20060322010119-7659e-d025815b4b0b813b9b41b052c30efc9e380b1cf1.gz
parent c563741e
......@@ -18,6 +18,7 @@ API/ABI changes and versioning
other important information to developers;
- and should be updated _continuously_! />
- Added the stun_dns_lookup.*() interface to sofia-sip/stun.h.
Contributors to this release
----------------------------
......
......@@ -10,18 +10,13 @@ AUTOMAKE_OPTIONS = foreign
# ----------------------------------------------------------------------
# Header paths
INCLUDES = -I$(srcdir)/../su -I../su
INCLUDES = -I$(srcdir)/../su -I../su -I$(srcdir)/../sresolv
# ----------------------------------------------------------------------
# Build targets
# if HAVE_TLS
stun_lib_sources = stun.c stun_common.c
stun_prog_targets = stunc torture_stun
#else
#stun_lib_targets =
#stun_prog_targets =
#endif
stun_lib_sources = stun.c stun_common.c stun_dns.c
stun_prog_targets = stunc torture_stun lookup_stun_server
noinst_LTLIBRARIES = libstun.la
......@@ -45,6 +40,8 @@ LDADD = libstun.la \
torture_stun_LDFLAGS = -static
lookup_stun_server_LDADD = libstun.la ../sresolv/libsresolv.la ../su/libsu.la
# ----------------------------------------------------------------------
# tests
# XXX: torture_stun cannot be used as a automake test as is
......
/*
* 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 lookup_stun_server.c
* @brief Test app for STUN DNS-SRV lookups.
*
* @author Kai Vehmanen <Kai.Vehmanen@nokia.com>
*
* @todo TODO
* - picks one UDP and TLS target:port
* - does not pick up A/AAAA records that might be delivered
* in 'Additional Data' section as defined in RFC2782
*/
#include <stdio.h>
#include <sofia-sip/su.h>
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/su_wait.h>
#define STUN_MAGIC_T su_root_t
#include <sofia-sip/stun.h>
static void lookup_cb(stun_dns_lookup_t *self,
su_root_t *root)
{
const char *tls_target = NULL, *udp_target = NULL;
uint16_t tls_port = 0, udp_port = 0;
int res;
res = stun_dns_lookup_get_results(self,
&tls_target, &tls_port,
&udp_target, &udp_port);
if (res == 0) {
printf("STUN DNS-SRV: stun-tls at %s:%u.\n", tls_target, tls_port);
printf("STUN DNS-SRV: stun (udp) at %s:%u.\n", udp_target, udp_port);
}
su_root_break(root);
}
int main(int argc, char *argv[])
{
su_root_t *root;
stun_dns_lookup_t *lookup;
if (argc < 2) {
printf("usage: ./lookup_stun_server <domain>\n");
return -1;
}
/* step: initialize sofia su OS abstraction layer */
su_init();
/* step: create a su event loop and connect it to glib */
root = su_root_create(NULL);
/* step: initiate the DNS-SRV lookup */
lookup = stun_dns_lookup(root, root, lookup_cb, argv[1]);
/* step: enter the main loop (break fro lookup_cb()) */
su_root_run(root);
/* step: free any allocated resources */
stun_dns_lookup_destroy(lookup);
su_root_destroy(root);
su_deinit();
return 0;
}
......@@ -45,9 +45,10 @@
SOFIA_BEGIN_DECLS
typedef struct stun_handle_s stun_handle_t;
typedef struct stun_request_s stun_request_t;
typedef struct stun_discovery_s stun_discovery_t;
typedef struct stun_handle_s stun_handle_t;
typedef struct stun_request_s stun_request_t;
typedef struct stun_discovery_s stun_discovery_t;
typedef struct stun_dns_lookup_s stun_dns_lookup_t;
#ifndef STUN_MAGIC_T
#define STUN_MAGIC_T struct stun_magic_t
......@@ -120,6 +121,9 @@ typedef enum stun_state_e {
} stun_state_t;
/* -------------------------------------------------------------------
* Calback function prototypes (signals emitted by the stack) */
/* Per discovery */
typedef void (*stun_discovery_f)(stun_discovery_magic_t *magic,
stun_handle_t *sh,
......@@ -144,6 +148,10 @@ typedef int (*stun_send_callback)(stun_magic_t *magic,
unsigned len,
int only_a_keepalive);
/** Callback for delivering DNS lookup results */
typedef void (*stun_dns_lookup_f)(stun_dns_lookup_t *self,
stun_magic_t *magic);
/* -------------------------------------------------------------------
* Functions for managing STUN handles. */
......@@ -215,6 +223,19 @@ int stun_handle_set_uname_pwd(stun_handle_t *sh,
const char *pwd,
int len_pwd);
/* -------------------------------------------------------------------
* Functions for STUN server discovery using DNS (RFC3489/3489bis) */
stun_dns_lookup_t *stun_dns_lookup(stun_magic_t *magic,
su_root_t *root,
stun_dns_lookup_f func,
const char *domain);
int stun_dns_lookup_get_results(stun_dns_lookup_t *self,
const char **tls_target,
uint16_t *tls_port,
const char **udp_target,
uint16_t *udp_port);
void stun_dns_lookup_destroy(stun_dns_lookup_t *self);
SOFIA_END_DECLS
......
/*
* 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>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define STUN_SRV_SERVICE_TLS "_stun-tls._tcp"
#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 HAVE_SU_WAIT_H 1 /* workaround for bug in sresolv.h */
#define SRES_CONTEXT_T stun_dns_lookup_t
#include <sofia-sip/sresolv.h>
#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;
stun_dns_lookup_f stun_cb;
char *stun_tls_target;
char *stun_udp_target;
uint16_t stun_tls_port;
uint16_t stun_udp_port;
unsigned stun_state:2; /**< bit0:udp, bit1:stun-tls */
};
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 */
for (i = 0; answer[i] != NULL; i++) {
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) {
const char *tls_name = STUN_SRV_SERVICE_TLS;
const char *udp_name = STUN_SRV_SERVICE_UDP;
if ((self->stun_state & stun_dns_tls) == 0 &&
strncmp(rr->srv_record->r_name, tls_name, strlen(tls_name)) == 0) {
self->stun_tls_target = su_strdup(self->stun_home, rr->srv_target);
self->stun_tls_port = rr->srv_port;
self->stun_state |= stun_dns_tls;
SU_DEBUG_5(("%s: stun-tls for domain %s is at %s:%u.\n",
__func__, rr->srv_record->r_name, self->stun_tls_target, self->stun_tls_port));
}
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;
SU_DEBUG_5(("%s: stun for domain %s is at %s:%u.\n",
__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);
}
sres_free_answers(self->stun_sres, answer);
}
/**
* Performs a DNS-SRV check for STUN 'stun-tls' and
* '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)
{
stun_dns_lookup_t *self = su_zalloc(NULL, sizeof(stun_dns_lookup_t));
sres_query_t *query;
int socket;
/* see nta.c:outgoing_answer_srv() */
su_home_init(self->stun_home);
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) {
socket = sres_resolver_root_socket(self->stun_sres);
if (socket > 0) {
char *query_udp = su_sprintf(self->stun_home, "%s.%s", STUN_SRV_SERVICE_UDP, domain);
char *query_tcp = su_sprintf(self->stun_home, "%s.%s", STUN_SRV_SERVICE_TLS, domain);
query = sres_query_make(self->stun_sres, priv_sres_cb, self, socket, sres_type_srv, query_udp);
query = sres_query_make(self->stun_sres, priv_sres_cb, self, socket, sres_type_srv, query_tcp);
}
else {
sres_resolver_destroy(self->stun_sres);
su_free(NULL, self), self = NULL;
}
}
else {
su_free(NULL, self), self = NULL;
}
return self;
}
/**
* Fetches the results of a completed STUN DNS-SRV lookup.
*
* @param self context pointer
* @param tls_target location where to stored the 'target'
* SRV field for stun-tls service
* @param tls_port location where to store port number
* @param udp_target location where to stored the 'target'
* SRV field for stun service (udp)
* @param udp_port location where to store port number
*
* @return 0 on success, non-zero otherwise
*/
int stun_dns_lookup_get_results(stun_dns_lookup_t *self,
const char **tls_target,
uint16_t *tls_port,
const char **udp_target,
uint16_t *udp_port)
{
int result = -1;
if (self->stun_state == stun_dns_done) {
if (tls_target) *tls_target = self->stun_tls_target;
if (tls_port) *tls_port = self->stun_tls_port;
if (udp_target) *udp_target = self->stun_udp_target;
if (udp_port) *udp_port = self->stun_udp_port;
result = 0;
}
return result;
}
/**
* 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);
su_home_destroy(self->stun_home);
su_free(NULL, self);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment