When an agent is run without a specific broker specified (no --broker
provided), it does some magic to try to figure out where it should
connect. The first thing it does is try to do an SRV lookup against the
local domain. The previous code was using the "domainname" as opposed
to the "dnsdomainname", which is more correct for what is being done.
This implementation addresses the issue at hand on Linux. It leaves the
behavior unchanged on Windows, but I left a note in there as its
possible that there is a similar differentiation on Windows, but that's
not clear to me yet.
Signed-off-by: Russell Bryant <rbryant(a)redhat.com>
---
src/include/matahari/utilities.h | 7 ++++
src/lib/CMakeLists.txt | 2 +-
src/lib/mh_agent.cpp | 2 +-
src/lib/utilities.c | 12 +++++++
src/lib/utilities_linux.c | 65 ++++++++++++++++++++++++++++++++++++++
src/lib/utilities_private.h | 39 ++++++++++++++++++++++
src/lib/utilities_windows.c | 34 ++++++++++++++++++++
7 files changed, 159 insertions(+), 2 deletions(-)
create mode 100644 src/lib/utilities_linux.c
create mode 100644 src/lib/utilities_private.h
create mode 100644 src/lib/utilities_windows.c
diff --git a/src/include/matahari/utilities.h b/src/include/matahari/utilities.h
index 5eed276..89b9db7 100644
--- a/src/include/matahari/utilities.h
+++ b/src/include/matahari/utilities.h
@@ -34,6 +34,13 @@
extern const char *mh_uuid(void);
extern const char *mh_hostname(void);
extern const char *mh_domainname(void);
+
+/**
+ * Get the local dnsdomainname.
+ */
+extern const char *
+mh_dnsdomainname(void);
+
extern char *mh_file_first_line(const char *file);
extern void mh_abort(const char *file, const char *function, int line,
const char *assert_condition, int do_core, int do_fork);
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index e130995..1cd41d4 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -6,7 +6,7 @@ include_directories(${pcre_INCLUDE_DIRS})
# See
http://sourceware.org/autobook/autobook/autobook_91.html for
# how to set the library version appropriately
-add_library (mcommon SHARED utilities.c mainloop.c dnssrv.c dnssrv_${VARIANT}.c)
+add_library (mcommon SHARED utilities.c utilities_${VARIANT}.c mainloop.c dnssrv.c
dnssrv_${VARIANT}.c)
set_target_properties(mcommon PROPERTIES SOVERSION 1.0.0)
target_link_libraries(mcommon ${SIGAR} ${glib_LIBRARIES})
diff --git a/src/lib/mh_agent.cpp b/src/lib/mh_agent.cpp
index 0b8fe7d..0724456 100644
--- a/src/lib/mh_agent.cpp
+++ b/src/lib/mh_agent.cpp
@@ -259,7 +259,7 @@ mh_connect(OptionsMap mh_options, OptionsMap amqp_options, int retry)
if (mh_options.count("servername")) {
fqdn << "_matahari._tcp." <<
mh_options["servername"];
} else {
- fqdn << "_matahari._tcp." << mh_domainname();
+ fqdn << "_matahari._tcp." << mh_dnsdomainname();
}
target = mh_dnssrv_lookup(fqdn.str().c_str());
diff --git a/src/lib/utilities.c b/src/lib/utilities.c
index 4f2ae67..abfcc35 100644
--- a/src/lib/utilities.c
+++ b/src/lib/utilities.c
@@ -369,6 +369,18 @@ mh_domainname(void)
}
const char *
+mh_dnsdomainname(void)
+{
+ char *res;
+
+ res = mh_os_dnsdomainname();
+
+ mh_trace("Got dnsdomainname: '%s'", res);
+
+ return res;
+}
+
+const char *
mh_hostname(void)
{
static char *hostname = NULL;
diff --git a/src/lib/utilities_linux.c b/src/lib/utilities_linux.c
new file mode 100644
index 0000000..c848802
--- /dev/null
+++ b/src/lib/utilities_linux.c
@@ -0,0 +1,65 @@
+/* utilities_linux.c - Copyright (C) 2011, Red Hat, Inc.
+ * Written by Russell Bryant <rbryant(a)redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 software 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
+#include "matahari/logging.h"
+#include "utilities_private.h"
+
+const char *
+mh_os_dnsdomainname(void)
+{
+ static char *dnsdomainname = NULL;
+ int res;
+ char *c;
+ struct addrinfo *ai = NULL;
+ struct addrinfo hints = {
+ hints.ai_socktype = SOCK_DGRAM,
+ hints.ai_flags = AI_CANONNAME,
+ };
+
+ /**
+ * \todo It would be good to eventually add this as a feature in sigar
+ * since we use that for most related things.
+ */
+
+ if (dnsdomainname) {
+ return dnsdomainname;
+ }
+
+ if ((res = getaddrinfo(mh_hostname(), NULL, &hints, &ai))) {
+ mh_notice("Unable to determine dnsdomainname: (%d) %s\n", errno,
strerror(errno));
+ return "";
+ }
+
+ if (!ai) {
+ return "";
+ }
+
+ if ((c = strchr(ai->ai_canonname, '.'))) {
+ dnsdomainname = strdup(++c);
+ }
+
+ return dnsdomainname ? dnsdomainname : "";
+}
diff --git a/src/lib/utilities_private.h b/src/lib/utilities_private.h
new file mode 100644
index 0000000..e39d22c
--- /dev/null
+++ b/src/lib/utilities_private.h
@@ -0,0 +1,39 @@
+/* utilities_private.h - Copyright (C) 2011, Red Hat, Inc.
+ * Written by Russell Bryant <rbryant(a)redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 software 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * \file
+ * \brief Platform specific function prototypes.
+ *
+ * The functions in this header must be implemented by the platform
+ * specific utilities code.
+ * - utilities_linux.c
+ * - utilities_windows.c
+ */
+
+
+#ifndef __MH_UTILITIES_PRIVATE_H__
+#define __MH_UTILITIES_PRIVATE_H__
+
+/**
+ * Get the local dnsdomainname.
+ */
+const char *
+mh_os_dnsdomainname(void);
+
+#endif /* __MH_UTILITIES_PRIVATE_H__ */
diff --git a/src/lib/utilities_windows.c b/src/lib/utilities_windows.c
new file mode 100644
index 0000000..26a3679
--- /dev/null
+++ b/src/lib/utilities_windows.c
@@ -0,0 +1,34 @@
+/* utilities_linux.c - Copyright (C) 2011, Red Hat, Inc.
+ * Written by Russell Bryant <rbryant(a)redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 software 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "config.h"
+
+#include "matahari/utilities.h"
+#include "utilities_private.h"
+
+const char *
+mh_os_dnsdomainname(void)
+{
+ /*
+ * Just use the standard domain name that sigar provides.
+ *
+ * XXX Is there a separate dnsdomainname for Windows?
+ */
+
+ return mh_domainname();
+}
--
1.7.6