aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorWilmer van der Gaast <wilmer@gaast.net>2006-10-07 19:46:28 +0200
committerWilmer van der Gaast <wilmer@gaast.net>2006-10-07 19:46:28 +0200
commit36e9f62a6e6fdb1217b3b819320ac5a94025c448 (patch)
treec91d57d1f8fa144e1ffce2ae3143de5b30460b0c /lib
parent090f1cbe72373b31e753af4a1442ddd53b02791b (diff)
Added SRV lookups to automatically find out the correct server for a
domain.
Diffstat (limited to 'lib')
-rw-r--r--lib/misc.c57
-rw-r--r--lib/misc.h10
2 files changed, 67 insertions, 0 deletions
diff --git a/lib/misc.c b/lib/misc.c
index 64666492..9061af39 100644
--- a/lib/misc.c
+++ b/lib/misc.c
@@ -39,6 +39,11 @@
#include <glib.h>
#include <time.h>
+#ifdef HAVE_RESOLV_A
+#include <arpa/nameser.h>
+#include <resolv.h>
+#endif
+
void strip_linefeed(gchar *text)
{
int i, j;
@@ -487,3 +492,55 @@ int bool2int( char *value )
return 0;
}
+
+struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain )
+{
+ struct ns_srv_reply *reply = NULL;
+#ifdef HAVE_RESOLV_A
+ char name[1024];
+ unsigned char querybuf[1024];
+ const unsigned char *buf;
+ ns_msg nsh;
+ ns_rr rr;
+ int i, len, size;
+
+ g_snprintf( name, sizeof( name ), "_%s._%s.%s", service, protocol, domain );
+
+ if( ( size = res_query( name, ns_c_in, ns_t_srv, querybuf, sizeof( querybuf ) ) ) <= 0 )
+ return NULL;
+
+ if( ns_initparse( querybuf, size, &nsh ) != 0 )
+ return NULL;
+
+ if( ns_parserr( &nsh, ns_s_an, 0, &rr ) != 0 )
+ return NULL;
+
+ size = ns_rr_rdlen( rr );
+ buf = ns_rr_rdata( rr );
+
+ len = 0;
+ for( i = 6; i < size && buf[i]; i += buf[i] + 1 )
+ len += buf[i] + 1;
+
+ if( i > size )
+ return NULL;
+
+ reply = g_malloc( sizeof( struct ns_srv_reply ) + len );
+ memcpy( reply->name, buf + 7, len );
+
+ for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 )
+ reply->name[i] = '.';
+
+ if( i > len )
+ {
+ g_free( reply );
+ return NULL;
+ }
+
+ reply->prio = ( buf[0] << 8 ) | buf[1];
+ reply->weight = ( buf[2] << 8 ) | buf[3];
+ reply->port = ( buf[4] << 8 ) | buf[5];
+#endif
+
+ return reply;
+}
diff --git a/lib/misc.h b/lib/misc.h
index b021e642..55dabfc4 100644
--- a/lib/misc.h
+++ b/lib/misc.h
@@ -29,6 +29,14 @@
#include <gmodule.h>
#include <time.h>
+struct ns_srv_reply
+{
+ int prio;
+ int weight;
+ int port;
+ char name[];
+};
+
G_MODULE_EXPORT void strip_linefeed( gchar *text );
G_MODULE_EXPORT char *add_cr( char *text );
G_MODULE_EXPORT char *strip_newlines(char *source);
@@ -53,4 +61,6 @@ G_MODULE_EXPORT void random_bytes( unsigned char *buf, int count );
G_MODULE_EXPORT int is_bool( char *value );
G_MODULE_EXPORT int bool2int( char *value );
+G_MODULE_EXPORT struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain );
+
#endif