You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2008/02/01 00:14:55 UTC

svn commit: r617286 - in /incubator/qpid/trunk/qpid/cpp/src/qpid: Url.cpp Url.h

Author: aconway
Date: Thu Jan 31 15:14:49 2008
New Revision: 617286

URL: http://svn.apache.org/viewvc?rev=617286&view=rev
Log:

Generate URLs for local host.

Modified:
    incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp?rev=617286&r1=617285&r2=617286&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/Url.cpp Thu Jan 31 15:14:49 2008
@@ -16,15 +16,54 @@
  *
  */
 
-#include "Url.h"
+#include "qpid/Url.h"
+#include "qpid/Exception.h"
+#include "qpid/Msg.h"
+
 #include <sstream>
 #include <boost/spirit.hpp>
 #include <boost/spirit/actor.hpp>
 
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <errno.h>
+
 using namespace boost::spirit;
 using namespace std;
 
 namespace qpid {
+
+Url Url::getHostnameUrl(uint16_t port) {
+    char name[HOST_NAME_MAX];
+    if (::gethostname(name, sizeof(name)) != 0)
+        throw Exception(QPID_MSG("Cannot get host name: " << strError(errno)));
+    return Url(TcpAddress(name, port));
+}
+
+static const string LOCALHOST("127.0.0.1");
+
+Url Url::getIpAddressesUrl(uint16_t port) {
+    Url url;
+    int s = socket (PF_INET, SOCK_STREAM, 0);
+    for (int i=1;;i++) {
+        struct ifreq ifr;
+        ifr.ifr_ifindex = i;
+        if (::ioctl (s, SIOCGIFNAME, &ifr) < 0)
+            break;
+        /* now ifr.ifr_name is set */
+        if (::ioctl (s, SIOCGIFADDR, &ifr) < 0)
+            continue;
+        struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
+        string addr(inet_ntoa(sin->sin_addr));
+        if (addr != LOCALHOST)
+            url.push_back(TcpAddress(addr, port));
+    }
+    close (s);
+    return url;
+}
 
 string Url::str() const {
     ostringstream os;

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h?rev=617286&r1=617285&r2=617286&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/Url.h Thu Jan 31 15:14:49 2008
@@ -52,6 +52,14 @@
 
 /** An AMQP URL contains a list of addresses */
 struct Url : public std::vector<Address> {
+
+    /** Url with the hostname as returned by gethostname(2)  */
+    static Url getHostnameUrl(uint16_t port);
+
+    /** Url with local IP address(es), may be more than one address
+     * on a multi-homed host. */
+    static Url getIpAddressesUrl(uint16_t port);
+
     struct InvalidUrl : public Exception {
         InvalidUrl(const std::string& s) : Exception(s) {}
     };