Commit 12c393b8 authored by Kai Vehmanen's avatar Kai Vehmanen

Minor update to STUN DNS-SRV interface.

darcs-hash:20060327205116-7659e-7fd96c0a307243a31864413d1368c25587248d41.gz
parent c087c329
2006-03-27 Kai Vehmanen <kai.vehmanen@nokia.com>
* stun.h: Added stun_server_address().
* stun.h: Added stun_dns_lookup*() interface.
* stun.h: Renamed get_nattype to test_nattype and
get_lifetime to test_lifetime. Removed deprecated
wrappers.
......
......@@ -42,22 +42,28 @@
#define STUN_MAGIC_T su_root_t
#include <sofia-sip/stun.h>
static char* g_domain = NULL;
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 (tcp) at %s:%u.\n", tls_target, tls_port);
printf("STUN DNS-SRV: stun (udp) at %s:%u.\n", udp_target, udp_port);
}
# define RESULT(x) (x == 0 ? "OK" : "ERRORS")
const char *tcp_target = NULL, *udp_target = NULL, *stp_target = NULL;
uint16_t tcp_port = 0, udp_port = 0, stp_port = 0;
int res = 0;
printf("STUN DNS-SRV lookup results:\n");
res = stun_dns_lookup_udp_addr(self, &udp_target, &udp_port);
printf(" _stun._udp.%s: %s:%u (%s).\n", g_domain, udp_target, udp_port, RESULT(res));
res = stun_dns_lookup_tcp_addr(self, &tcp_target, &tcp_port);
printf(" _stun._tcp.%s: %s:%u (%s).\n", g_domain, tcp_target, tcp_port, RESULT(res));
res = stun_dns_lookup_stp_addr(self, &stp_target, &stp_port);
printf(" _stun-tls._tcp.%s: %s:%u (%s).\n", g_domain, stp_target, stp_port, RESULT(res));
su_root_break(root);
}
......@@ -65,12 +71,14 @@ 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;
}
g_domain = argv[1];
/* step: initialize sofia su OS abstraction layer */
su_init();
......@@ -78,13 +86,19 @@ int main(int argc, char *argv[])
root = su_root_create(NULL);
/* step: initiate the DNS-SRV lookup */
lookup = stun_dns_lookup(root, root, lookup_cb, argv[1]);
lookup = stun_dns_lookup(root, root, lookup_cb, g_domain);
/* step: enter the main loop (break fro lookup_cb()) */
su_root_run(root);
if (lookup) {
/* step: enter the main loop (break fro lookup_cb()) */
su_root_run(root);
/* step: free any allocated resources */
stun_dns_lookup_destroy(lookup);
}
else {
printf("ERROR: failed to create lookup object.\n");
}
/* step: free any allocated resources */
stun_dns_lookup_destroy(lookup);
su_root_destroy(root);
su_deinit();
......
......@@ -232,13 +232,12 @@ 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);
int stun_dns_lookup_udp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *port);
int stun_dns_lookup_tcp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *port);
int stun_dns_lookup_stp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *port);
/* --------------------------------------------------------------------
* Deprecated functions. These are supported with limited
* compatibility. */
......
......@@ -973,9 +973,7 @@ static void priv_lookup_cb(stun_dns_lookup_t *self,
int res;
stun_handle_t *sh = (stun_handle_t *)magic;
res = stun_dns_lookup_get_results(self,
NULL, NULL,
&udp_target, &udp_port);
res = stun_dns_lookup_udp_addr(self, &udp_target, &udp_port);
if (res == 0) {
/* XXX: assumption that same host and port used for UDP/TLS */
if (udp_target) {
......@@ -1016,6 +1014,7 @@ static void priv_lookup_cb(stun_dns_lookup_t *self,
SU_DEBUG_5(("Warning: unknown pending DNS-SRV action.\n"));
}
su_free(sh->sh_home, sh->sh_dns_pend_tags), sh->sh_dns_pend_tags = NULL;
sh->sh_dns_pend_action = 0;
}
}
......@@ -1032,10 +1031,12 @@ static int priv_dns_queue_action(stun_handle_t *sh,
tag_type_t tag, tag_value_t value, ...)
{
ta_list ta;
if (!sh->sh_dns_pend_action) {
if (!sh->sh_dns_lookup) {
sh->sh_dns_lookup = stun_dns_lookup((stun_magic_t*)sh, sh->sh_root, priv_lookup_cb, sh->sh_domain);
ta_start(ta, tag, value);
assert(sh->sh_dns_pend_tags == NULL);
sh->sh_dns_pend_tags = tl_tlist(sh->sh_home,
ta_tags(ta));
ta_end(ta);
......
......@@ -37,7 +37,7 @@
#include "config.h"
#endif
#define STUN_SRV_SERVICE_TLS "_stun._tcp"
#define STUN_SRV_SERVICE_TCP "_stun._tcp"
#define STUN_SRV_SERVICE_UDP "_stun._udp"
#include <sofia-sip/stun.h>
......@@ -57,9 +57,9 @@ struct stun_dns_lookup_s {
sres_resolver_t *stun_sres;
int stun_socket;
stun_dns_lookup_f stun_cb;
char *stun_tls_target;
char *stun_tcp_target;
char *stun_udp_target;
uint16_t stun_tls_port;
uint16_t stun_tcp_port;
uint16_t stun_udp_port;
unsigned stun_state:2; /**< bit0:udp, bit1:tcp */
};
......@@ -87,15 +87,15 @@ static void priv_sres_cb(stun_dns_lookup_t *self,
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 *tcp_name = STUN_SRV_SERVICE_TCP;
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;
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;
self->stun_state |= stun_dns_tls;
SU_DEBUG_5(("%s: stun (tcp) for domain %s is at %s:%u.\n",
__func__, rr->srv_record->r_name, self->stun_tls_target, self->stun_tls_port));
__func__, rr->srv_record->r_name, self->stun_tcp_target, self->stun_tcp_port));
}
else if ((self->stun_state & stun_dns_udp) == 0 &&
strncmp(rr->srv_record->r_name, udp_name, strlen(udp_name)) == 0) {
......@@ -143,9 +143,9 @@ stun_dns_lookup_t *stun_dns_lookup(stun_magic_t *magic,
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) {
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);
char *query_tcp = su_sprintf(self->stun_home, "%s.%s", STUN_SRV_SERVICE_TCP, domain);
self->stun_socket = socket;
......@@ -154,8 +154,8 @@ stun_dns_lookup_t *stun_dns_lookup(stun_magic_t *magic,
}
else {
sres_resolver_destroy(self->stun_sres);
su_free(NULL, self), self = NULL;
self->stun_socket = -1;
su_free(NULL, self), self = NULL;
}
}
else {
......@@ -166,30 +166,34 @@ stun_dns_lookup_t *stun_dns_lookup(stun_magic_t *magic,
}
/**
* Fetches the results of a completed STUN DNS-SRV lookup.
* 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);
}
/**
* Fetches the results of a completed STUN DNS-SRV lookup
* for "_stun._udp" service (RFC3489/3489bis).
*
* @param self context pointer
* @param tls_target location where to stored the 'target'
* SRV field for stun service (tcp)
* @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
* @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_get_results(stun_dns_lookup_t *self,
const char **tls_target,
uint16_t *tls_port,
const char **udp_target,
uint16_t *udp_port)
int stun_dns_lookup_udp_addr(stun_dns_lookup_t *self, const char **target, uint16_t *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;
if (target) *target = self->stun_udp_target;
if (port) *port = self->stun_udp_port;
result = 0;
}
......@@ -197,14 +201,43 @@ int stun_dns_lookup_get_results(stun_dns_lookup_t *self,
}
/**
* Destroys the 'self' object created by stun_dns_lookup_destroy().
*/
void stun_dns_lookup_destroy(stun_dns_lookup_t *self)
* 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)
{
if (self->stun_sres)
sres_resolver_destroy(self->stun_sres);
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;
}
su_home_destroy(self->stun_home);
su_free(NULL, self);
return result;
}
/**
* 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;
}
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