Browse Source

applied jinmei's patches from http://bind10.isc.org/ticket/183 (with a minor cleanup)
changed IPPROTO_TCP to 0 in the non-asio socket call, which I don't think we need and more importantly, does not work here.


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac183@1949 e5f2f494-b856-4b98-b285-d166d9295462

Jelte Jansen 15 years ago
parent
commit
2def58b09b
2 changed files with 24 additions and 29 deletions
  1. 22 29
      src/lib/cc/session.cc
  2. 2 0
      src/lib/cc/session_unittests.cc

+ 22 - 29
src/lib/cc/session.cc

@@ -59,7 +59,7 @@ class SessionImpl {
 public:
     SessionImpl() : sequence_(-1) { queue_ = Element::createFromString("[]"); }
     virtual ~SessionImpl() {}
-    virtual void establish(const char* socket_file = NULL) = 0;
+    virtual void establish(const char& socket_file) = 0;
     virtual int getSocket() = 0;
     virtual void disconnect() = 0;
     virtual void writeData(const void* data, size_t datalen) = 0;
@@ -78,7 +78,7 @@ public:
     ASIOSession(io_service& io_service) :
         io_service_(io_service), socket_(io_service_), data_length_(0)
     {}
-    virtual void establish(const char* socket_file = NULL);
+    virtual void establish(const char& socket_file);
     virtual void disconnect();
     virtual int getSocket() { return (socket_.native()); }
     virtual void writeData(const void* data, size_t datalen);
@@ -100,15 +100,9 @@ private:
 
 
 void
-ASIOSession::establish(const char* socket_file) {
-    if (!socket_file) {
-        socket_file = getenv("BIND10_MSGQ_SOCKET_FILE");
-    }
-    if (!socket_file) {
-        socket_file = BIND10_MSGQ_SOCKET_FILE;
-    }
+ASIOSession::establish(const char& socket_file) {
     try {
-        socket_.connect(boost::asio::local::stream_protocol::endpoint(socket_file), error_);
+        socket_.connect(boost::asio::local::stream_protocol::endpoint(&socket_file), error_);
     } catch (boost::system::system_error& se) {
         isc_throw(SessionError, se.what());
     }
@@ -192,7 +186,7 @@ public:
     SocketSession() : sock_(-1) {}
     virtual ~SocketSession() { disconnect(); }
     virtual int getSocket() { return (sock_); }
-    void establish(const char* socket_file = NULL);
+    void establish(const char& socket_file);
     virtual void disconnect()
     {
         if (sock_ >= 0) {
@@ -227,29 +221,21 @@ public:
 }
 
 void
-SocketSession::establish(const char* socket_file) {
-    int s;
+SocketSession::establish(const char& socket_file) {
     struct sockaddr_un sun;
 
-    s = socket(AF_UNIX, SOCK_STREAM, IPPROTO_TCP);
-    if (s < 0) {
-        isc_throw(SessionError, "socket() failed");
-    }
-
-    if (!socket_file) {
-        socket_file = getenv("BIND10_MSGQ_SOCKET_FILE");
-    }
-    if (!socket_file) {
-        socket_file = BIND10_MSGQ_SOCKET_FILE;
+    if (strlen(&socket_file) >= sizeof(sun.sun_path)) {
+        isc_throw(SessionError, "Unable to connect to message queue; "
+                  "socket file path too long: " << socket_file);
     }
+    sun.sun_family = AF_UNIX;
+    strncpy(sun.sun_path, &socket_file, sizeof(sun.sun_path) - 1);
 
-    if (strlen(socket_file) >= sizeof(sun.sun_path)) {
-        isc_throw(SessionError, "Unable to connect to message queue; socket file path too long");
+    int s = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (s < 0) {
+        isc_throw(SessionError, "socket() failed");
     }
 
-    sun.sun_family = AF_UNIX;
-    strncpy(sun.sun_path, socket_file, sizeof(sun.sun_path) - 1);
-
     if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
         close(s);
         isc_throw(SessionError, "Unable to connect to message queue");
@@ -312,7 +298,14 @@ Session::startRead(boost::function<void()> read_callback) {
 
 void
 Session::establish(const char* socket_file) {
-    impl_->establish(socket_file);
+    if (socket_file == NULL) {
+        socket_file = getenv("BIND10_MSGQ_SOCKET_FILE");
+    }
+    if (socket_file == NULL) {
+        socket_file = BIND10_MSGQ_SOCKET_FILE;
+    }
+
+    impl_->establish(*socket_file);
 
     // once established, encapsulate the implementation object so that we
     // can safely release the internal resource when exception happens

+ 2 - 0
src/lib/cc/session_unittests.cc

@@ -17,7 +17,9 @@
 #include "config.h"
 #include <gtest/gtest.h>
 #include <session.h>
+#ifdef HAVE_BOOST_SYSTEM
 #include <boost/asio.hpp>
+#endif
 #include <exceptions/exceptions.h>
 
 using namespace isc::cc;