Some comments inline. Is there a reason not to use ns_name_uncompress()
to decompress the domain?
cheers,
Zane.
On 17/06/11 05:07, Adam Stokes wrote:
- This patch is for utilizing the DNS SRV records
so that Matahari brokers may be prioritized, switched out,
re-arranged without disrupting connectivity.
Signed-off-by: Adam Stokes<astokes(a)fedoraproject.org>
---
src/include/config.h.in | 1 +
src/include/matahari/dnssrv.h | 32 ++++++++++++
src/lib/CMakeLists.txt | 3 +
src/lib/dnssrv_linux.c | 106 ++++++++++++-----------------------------
src/lib/dnssrv_private.h | 42 ----------------
5 files changed, 66 insertions(+), 118 deletions(-)
create mode 100644 src/include/matahari/dnssrv.h
delete mode 100644 src/lib/dnssrv_private.h
diff --git a/src/include/config.h.in b/src/include/config.h.in
index 9f2989c..710e1ce 100644
--- a/src/include/config.h.in
+++ b/src/include/config.h.in
@@ -1,3 +1,4 @@
#cmakedefine HAVE_STRING_H 1
#cmakedefine HAVE_SYS_IOCTL_H 1
#cmakedefine HAVE_ASPRINTF 1
+#cmakedefine HAVE_RESOLV_H 1
diff --git a/src/include/matahari/dnssrv.h b/src/include/matahari/dnssrv.h
new file mode 100644
index 0000000..3aa014f
--- /dev/null
+++ b/src/include/matahari/dnssrv.h
@@ -0,0 +1,32 @@
+/* dnssrv.h - Copyright (c) 2011 Red Hat, Inc.
+ * Written by Adam Stokes<astokes(a)fedoraproject.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * \file
+ * \brief Config API
+ * \ingroup coreapi
+ */
+
+#ifndef __MH_DNSSRV_H__
+#define __MH_DNSSRV_H__
+
+#define MAX_NAME_LEN 1024
+
+extern int srv_lookup(char *query, char *target);
+
+#endif /* __MH_DNSSRV_H__ */
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index d33057b..ab21bba 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -35,6 +35,9 @@ else(WIN32)
foreach (lib ${LIBS})
install(TARGETS ${lib} DESTINATION lib${LIB_SUFFIX})
endforeach (lib ${LIBS})
+ # libresolv
+ add_library(mcommon_resolv SHARED dnssrv_linux.c)
+ target_link_libraries(mcommon mcommon_resolv resolv)
endif(WIN32)
if(WITH-QMF)
diff --git a/src/lib/dnssrv_linux.c b/src/lib/dnssrv_linux.c
index c1e11d8..44c3c19 100644
--- a/src/lib/dnssrv_linux.c
+++ b/src/lib/dnssrv_linux.c
@@ -16,95 +16,49 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include<string.h>
+#include<glib.h>
+#include<stdlib.h>
+
#include<netinet/in.h>
#include<arpa/nameser.h>
#include<resolv.h>
-#include<glib.h>
-#include "dnssrv_private.h"
+#include "matahari/dnssrv.h"
-struct srv_reply **srv_lookup(char *service,
- char *protocol,
- char *domain)
+int
+srv_lookup(char *query, char *target)
{
- struct srv_reply **replies = NULL;
-#ifdef HAVE_RESOLV_A
- struct srv_reply *reply = NULL;
- char name[MAX_NAME_LEN];
- unsigned char querybuf[MAX_NAME_LEN];
- const unsigned char *buf;
+ union {
+ HEADER hdr;
+ unsigned char buf[MAX_NAME_LEN];
+ } answer;
ns_msg nsh;
ns_rr rr;
- int, i, n, len, size;
-
- g_snprintf(name, sizeof(name), "_%s._%s.%s", service, protocol, domain);
- if((size = res_query(name, C_IN, T_SRV, querybuf, sizeof(querybuf)))< 0) {
- return NULL;
- }
-
- if(ns_initparse(querybuf, size,&nsh) != 0) {
- reutnr NULL;
- }
-
- n = 0;
- while(ns_parserr(&nsh, ns_s_an, n,&rr) == 0) {
- 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;
- }
+ char buf[MAX_NAME_LEN], *p;
+ int t, size;
- if(i> size) {
- break;
- }
+ size = res_query(query, C_IN, T_SRV, (unsigned char*)&answer, sizeof(answer));
- reply = g_malloc(sizeof(struct 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);
- break;
- }
-
- reply->prio = (buf[0]<< 8) | buf[1];
- reply->weight = (buf[2]<< 8) | buf[3];
- reply->port = (buf[4]<< 8) | buf[5];
- n++;
- replies = g_renew(struct srv_reply *, replies, n+1);
- replies[n-1] = reply;
- }
- if(replies) {
- replies[n] = NULL;
+ ns_initparse(answer.buf, size,&nsh);
+ if (ns_msg_count(nsh, ns_s_an) == 0) {
+ return -1;
}
-#endif
- return replies;
-}
-void srv_free(struct srv_reply **srv)
-{
- int i;
- if(srv == NULL) {
- return;
+ if (ns_parserr(&nsh, ns_s_an, 0,&rr)< 0) {
+ return -1;
}
- for(i = 0; srv[i]; i++) {
- g_free(srv[i]);
+ size = ns_rr_rdlen(rr);
+ memcpy(buf, ns_rr_rdata(rr), size);
^
I believe the size of this buffer should be NS_MAXDNAME (which is
actually 1025), not MAX_NAME_LEN (1024).
+ p = buf;
+ p += 6;
+ while (*p> 0) {
If you're going to check > 0 rather than != 0 here, p should be declared
as either unsigned char* or signed char* for portability.
+ t = *p;
+ *p = '.';
+ p += t+1;
}
- g_free(srv);
+ p = buf + 7;
A #define to indicate what 6 and 7 are in this context would be nice.
+ strcpy(target, p);
+ return 0;
}
-
-/*
- * Local Variables:
- * mode: c
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- * vim: et
- */
-
diff --git a/src/lib/dnssrv_private.h b/src/lib/dnssrv_private.h
deleted file mode 100644
index 0752311..0000000
--- a/src/lib/dnssrv_private.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/* dnssrv.h - Copyright (c) 2011 Red Hat, Inc.
- * Written by Adam Stokes<astokes(a)fedoraproject.org>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 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
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#define MAX_NAME_LEN 1024
-
-struct srv_reply
-{
- int prio;
- int weight;
- int port;
- char name[];
-};
-
-struct srv_reply **srv_lookup(char *service,
- char *protocol,
- char *domain);
-
-void srv_free(struct srv_reply **srv);
-
-/*
- * Local Variables:
- * mode: c
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- * vim: et
- */