[asterisk-scf-commits] asterisk-scf/integration/ice-util-cpp.git branch "nat-traversal" created.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Wed May 18 08:43:37 CDT 2011


branch "nat-traversal" has been created
        at  313131c3971fc79e9003c7f1555da0642d6eeb34 (commit)

- Log -----------------------------------------------------------------
commit 313131c3971fc79e9003c7f1555da0642d6eeb34
Author: Brent Eagles <beagles at digium.com>
Date:   Sun May 15 17:56:26 2011 -0230

    Adding network helper classes.

diff --git a/include/AsteriskSCF/Helpers/Network.h b/include/AsteriskSCF/Helpers/Network.h
new file mode 100644
index 0000000..4a27dc5
--- /dev/null
+++ b/include/AsteriskSCF/Helpers/Network.h
@@ -0,0 +1,86 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2011, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include <boost/thread.hpp>
+
+namespace AsteriskSCF
+{
+namespace Helpers
+{
+
+//
+// NOTE: I discovered that boost asio covers this territory, only with greater detail. I decided to keep going as I was
+// planning something far more basic to use. It's implemented in terms of asio though.
+//
+
+class ASTERISK_SCF_ICEBOX_EXPORT Address
+{
+    //
+    // We don't want a proper assignment operator, this is to behave more like
+    // an atomic type and the only way to really do that is to hide the members
+    // and only allow modification through construction.
+    //
+    void operator=(const Address&);
+public:
+
+    Address(const std::string& hostname, int port);
+    Address(const Address&);
+
+    int port() const;
+    std::string hostname() const;
+
+    bool isIPV6();
+    
+private:
+    std::string mHostname;
+    int mPort;
+
+    bool mIsIPV6;
+};
+typedef boost::shared_ptr<Address> AddressPtr;
+typedef std::vector<AddressPtr> AddressSeq;
+
+/** 
+ *
+ **/
+class ASTERISK_SCF_ICEBOX_EXPORT DNSQuery
+{
+public:
+    virtual ~DNSQuery();
+    static boost::shared_ptr<DNSQuery> create(const std::string& hostname, const std::string& serviceName);
+    static boost::shared_ptr<DNSQuery> create(const std::string& hostname, const int portNumber);
+    
+    virtual AddressSeq execute() = 0;
+
+protected:
+    DNSQuery();
+
+private:
+    //
+    // Not implemented.
+    //
+    DNSQuery(const DNSQuery&);
+    void operator=(const DNSQuery&);
+};
+typedef boost::shared_ptr<DNSQuery> DNSQueryPtr;
+
+
+} /* End of namespace Helpers */
+} /* End of namespace AsteriskSCF */
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6bd28d1..a9eafb5 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -32,6 +32,9 @@ asterisk_scf_component_add_file(ice-util-cpp ../include/AsteriskSCF/ThreadPool/T
 asterisk_scf_component_add_file(ice-util-cpp ../include/AsteriskSCF/ThreadPool/WorkerThread.h)
 asterisk_scf_component_add_file(ice-util-cpp ThreadPool/ThreadPool.cpp)
 asterisk_scf_component_add_file(ice-util-cpp ThreadPool/WorkerThread.cpp)
+asterisk_scf_component_add_file(ice-util-cpp ../include/AsteriskSCF/Helpers/Network.h)
+asterisk_scf_component_add_file(ice-util-cpp Helpers/Network.cpp)
+
 
 #
 # Note, strictly speaking this isn't for component development, but as it is part of this
diff --git a/src/Helpers/Network.cpp b/src/Helpers/Network.cpp
new file mode 100644
index 0000000..abc5d7e
--- /dev/null
+++ b/src/Helpers/Network.cpp
@@ -0,0 +1,96 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2011, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+
+#include <AsteriskSCF/Helpers/Network.h>
+#include <boost/asio.hpp>
+#include <boost/lexical_cast.hpp>
+
+using namespace std;
+using namespace AsteriskSCF::Helpers;
+using namespace boost::asio::ip;
+
+Address::Address(const string& host, int port) :
+    mHostname(host),
+    mPort(port)
+{
+    address a(address::from_string(host));
+    mIsIPV6 = a.is_v6();
+}
+    
+int Address::port() const
+{
+    return mPort;
+}
+
+string Address::hostname() const
+{
+    return mHostname;
+}
+
+bool Address::isIPV6()
+{
+    return mIsIPV6;
+}
+
+class DNSQueryImpl : public DNSQuery
+{
+public:
+    explicit DNSQueryImpl(const tcp::resolver::query& query) :
+        mQuery(query),
+        mResolver(mIO)
+    {
+    }
+
+    AddressSeq execute()
+    {
+        //
+        // TODO: this can also be made to run asynchronously.
+        //
+        tcp::resolver::iterator server = mResolver.resolve(mQuery);
+        tcp::endpoint ep;
+        AddressSeq result;
+        while (server != tcp::resolver::iterator())
+        {
+            ep = *server++;
+            result.push_back(AddressPtr(new Address(ep.address().to_string(), ep.port())));
+        }
+        return result;
+    }
+
+private:
+    tcp::resolver::query mQuery;
+    boost::asio::io_service mIO;
+    tcp::resolver mResolver;
+};
+
+DNSQueryPtr DNSQuery::create(const string& hostname, const string& serviceName)
+{
+    tcp::resolver::query q(hostname, serviceName);
+    return DNSQueryPtr(new DNSQueryImpl(q));
+}
+
+DNSQueryPtr DNSQuery::create(const string& hostname, const int portnumber)
+{
+    tcp::resolver::query q(hostname, boost::lexical_cast<string>(portnumber));
+    return DNSQueryPtr(new DNSQueryImpl(q));
+}
+
+DNSQuery::DNSQuery()
+{
+    //
+    // No-op.
+    //
+}

-----------------------------------------------------------------------


-- 
asterisk-scf/integration/ice-util-cpp.git



More information about the asterisk-scf-commits mailing list