myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [3057] Unix domain sockets implementation wrapping the


From: Giuseppe Scrivano
Subject: [myserver-commit] [3057] Unix domain sockets implementation wrapping the `Socket' functionalities and related test cases.
Date: Mon, 27 Apr 2009 12:43:32 +0000

Revision: 3057
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3057
Author:   gscrivano
Date:     2009-04-27 12:43:32 +0000 (Mon, 27 Apr 2009)
Log Message:
-----------
Unix domain sockets implementation wrapping the `Socket' functionalities and 
related test cases.  The Unix domain sockets are implemented in the 
`UnixSocket' class.

Modified Paths:
--------------
    trunk/myserver/configure.in
    trunk/myserver/include/base/Makefile.am
    trunk/myserver/src/base/Makefile.am
    trunk/myserver/src/base/socket/socket.cpp
    trunk/myserver/tests/Makefile.am

Added Paths:
-----------
    trunk/myserver/include/base/unix_socket/
    trunk/myserver/include/base/unix_socket/Makefile.am
    trunk/myserver/include/base/unix_socket/unix_socket.h
    trunk/myserver/src/base/unix_socket/
    trunk/myserver/src/base/unix_socket/Makefile.am
    trunk/myserver/src/base/unix_socket/unix_socket.cpp
    trunk/myserver/tests/test_unix_socket.cpp

Modified: trunk/myserver/configure.in
===================================================================
--- trunk/myserver/configure.in 2009-04-23 21:21:00 UTC (rev 3056)
+++ trunk/myserver/configure.in 2009-04-27 12:43:32 UTC (rev 3057)
@@ -370,6 +370,7 @@
     include/base/string/Makefile
     include/base/regex/Makefile
     include/base/thread/Makefile
+    include/base/unix_socket/Makefile
     include/protocol/Makefile
     include/protocol/ftp/Makefile
     include/protocol/control/Makefile
@@ -420,6 +421,7 @@
     src/base/string/Makefile
     src/base/regex/Makefile
     src/base/thread/Makefile
+    src/base/unix_socket/Makefile
     src/protocol/Makefile
     src/protocol/ftp/Makefile
     src/protocol/control/Makefile

Modified: trunk/myserver/include/base/Makefile.am
===================================================================
--- trunk/myserver/include/base/Makefile.am     2009-04-23 21:21:00 UTC (rev 
3056)
+++ trunk/myserver/include/base/Makefile.am     2009-04-27 12:43:32 UTC (rev 
3057)
@@ -1,4 +1,4 @@
 baseincludedir=$(includedir)/myserver/include/base
 baseinclude_HEADERS = utility.h
-SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 
mem_buff multicast pipe process regex safetime socket socket_pair ssl string 
sync thread xml
+SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 
mem_buff multicast pipe process regex safetime socket socket_pair ssl string 
sync thread unix_socket xml
 

Added: trunk/myserver/include/base/unix_socket/Makefile.am
===================================================================
--- trunk/myserver/include/base/unix_socket/Makefile.am                         
(rev 0)
+++ trunk/myserver/include/base/unix_socket/Makefile.am 2009-04-27 12:43:32 UTC 
(rev 3057)
@@ -0,0 +1,4 @@
+unix_socketincludedir=$(includedir)/myserver/include/base/unix_socket
+unix_socketinclude_HEADERS = unix_socket.h
+SUBDIRS =
+

Added: trunk/myserver/include/base/unix_socket/unix_socket.h
===================================================================
--- trunk/myserver/include/base/unix_socket/unix_socket.h                       
        (rev 0)
+++ trunk/myserver/include/base/unix_socket/unix_socket.h       2009-04-27 
12:43:32 UTC (rev 3057)
@@ -0,0 +1,61 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+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 3 of the License, or
+(at your option) any later version.
+
+This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef UNIX_SOCKET_H
+#define UNIX_SOCKET_H
+
+#include "stdafx.h"
+#include <include/base/socket/socket.h>
+
+
+extern "C"
+{
+#ifndef WIN32
+#include <sys/un.h>
+#endif
+}
+
+
+class UnixSocket: public Socket
+{
+public:
+  UnixSocket ();
+  int bind(const char *path);
+  int shutdown ();
+  int close ();
+  int connect (const char* path);
+  Socket accept ();
+  int socket ();
+protected:
+
+#ifdef AF_UNIX
+  sockaddr_un addr;
+#endif
+
+private:
+#ifdef AF_UNIX
+  void makeAddrInfo (sockaddr_un *info, const char *path)
+  {
+    memset(info, 0, sizeof(struct sockaddr_un));
+    info->sun_family = AF_UNIX;
+    strcpy (info->sun_path, path);
+  }
+#endif
+};
+
+#endif

Modified: trunk/myserver/src/base/Makefile.am
===================================================================
--- trunk/myserver/src/base/Makefile.am 2009-04-23 21:21:00 UTC (rev 3056)
+++ trunk/myserver/src/base/Makefile.am 2009-04-27 12:43:32 UTC (rev 3057)
@@ -1,5 +1,5 @@
 lib_LIBRARIES = libbase.a
 libbase_a_SOURCES = utility.cpp
-SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 
mem_buff multicast pipe process regex safetime socket socket_pair ssl string 
sync thread xml
+SUBDIRS = base64 dynamic_lib file files_cache find_data hash_map home_dir md5 
mem_buff multicast pipe process regex safetime socket socket_pair ssl string 
sync thread xml unix_socket
 INCLUDES = $(all_includes)
 

Modified: trunk/myserver/src/base/socket/socket.cpp
===================================================================
--- trunk/myserver/src/base/socket/socket.cpp   2009-04-23 21:21:00 UTC (rev 
3056)
+++ trunk/myserver/src/base/socket/socket.cpp   2009-04-27 12:43:32 UTC (rev 
3057)
@@ -97,6 +97,7 @@
 }
 /*!
  *Create the socket.
+ *\return 0 on error.
  */
 int Socket::socket(int af, int type, int protocol)
 {
@@ -163,15 +164,16 @@
  */
 int Socket::bind(MYSERVER_SOCKADDR* sa,int namelen)
 {
-   if ( sa == NULL || (sa->ss_family != AF_INET && sa->ss_family != AF_INET6) )
+   if ( sa == NULL )
       return -1;
-  if ( (sa->ss_family == AF_INET && namelen != sizeof(sockaddr_in)) 
+
+  if ((sa->ss_family == AF_INET && namelen != sizeof(sockaddr_in))
 #if HAVE_IPV6
   || (sa->ss_family == AF_INET6 && namelen != sizeof(sockaddr_in6))
 #endif
- )
+       )
+    return -1;
 
-     return -1;
 #ifdef WIN32
   return ::bind((SOCKET)socketHandle,(const struct sockaddr*)sa,namelen);
 #else
@@ -185,9 +187,9 @@
 int Socket::listen(int max)
 {
 #ifdef WIN32
-  return ::listen(socketHandle,max);
+  return ::listen(socketHandle, max);
 #else
-  return ::listen((int)socketHandle,max);
+  return ::listen((int)socketHandle, max);
 #endif
 }
 
@@ -592,10 +594,11 @@
 /*!
  *Connect the socket.
  */
-int Socket::connect(MYSERVER_SOCKADDR* sa, int na)
+int Socket::connect (MYSERVER_SOCKADDR* sa, int na)
 {
-  if ( sa == NULL || (sa->ss_family != AF_INET && sa->ss_family != AF_INET6) )
+  if ( sa == NULL )
     return -1;
+
   if ( (sa->ss_family == AF_INET && na != sizeof(sockaddr_in)) 
 #if HAVE_IPV6
     || (sa->ss_family == AF_INET6 && na != sizeof(sockaddr_in6))

Added: trunk/myserver/src/base/unix_socket/Makefile.am
===================================================================
--- trunk/myserver/src/base/unix_socket/Makefile.am                             
(rev 0)
+++ trunk/myserver/src/base/unix_socket/Makefile.am     2009-04-27 12:43:32 UTC 
(rev 3057)
@@ -0,0 +1,5 @@
+lib_LIBRARIES = libunix_socket.a
+libunix_socket_a_SOURCES = unix_socket.cpp
+SUBDIRS =
+INCLUDES = $(all_includes)
+

Added: trunk/myserver/src/base/unix_socket/unix_socket.cpp
===================================================================
--- trunk/myserver/src/base/unix_socket/unix_socket.cpp                         
(rev 0)
+++ trunk/myserver/src/base/unix_socket/unix_socket.cpp 2009-04-27 12:43:32 UTC 
(rev 3057)
@@ -0,0 +1,122 @@
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+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 3 of the License, or
+(at your option) any later version.
+
+This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#include "stdafx.h"
+#include <include/base/unix_socket/unix_socket.h>
+
+UnixSocket::UnixSocket ()
+{
+
+}
+
+/*!
+ *Bind the socket to a file.  If the file exists it is removed.
+ *\param path Path to the file bound by the socket.
+ *\return 0 on success.
+ */
+int UnixSocket::bind(const char* path)
+{
+  if (path == NULL)
+    return -1;
+
+#ifdef AF_UNIX
+  makeAddrInfo (&addr, path);
+  unlink (path);
+
+  return Socket::bind ((MYSERVER_SOCKADDR*)&addr, sizeof (sockaddr_un));
+#else
+  return -1;
+#endif
+}
+
+/*!
+ *Shutdown the socket.
+ *\return 0 on success.
+ */
+int UnixSocket::shutdown ()
+{
+#ifdef AF_UNIX
+  Socket::shutdown (2);
+  unlink (addr.sun_path);
+#else
+  return -1;
+#endif
+}
+
+
+/*!
+ *Free the socket.
+ *\return 0 on success.
+ */
+int UnixSocket::close ()
+{
+#ifdef AF_UNIX
+  Socket::close ();
+#else
+  return -1;
+#endif
+}
+
+/*!
+ *Open the socket.
+ *\return the socket handle on success.
+ */
+int UnixSocket::socket ()
+{
+#ifdef AF_UNIX
+  return Socket::socket (AF_UNIX, SOCK_STREAM, 0);
+#else
+  return 0;
+#endif
+}
+
+/*!
+ *Connect to an Unix socket by its path.
+ *\param path Path to the Unix socket.
+ *\return 0 on success.
+ */
+int UnixSocket::connect (const char* path)
+{
+  if (path == NULL)
+    return -1;
+
+#ifdef AF_UNIX
+  makeAddrInfo (&addr, path);
+
+  return Socket::connect ((MYSERVER_SOCKADDR*)&addr, sizeof (addr));
+#else
+  return -1;
+#endif
+}
+
+/*!
+ *Accept a new connection.
+ */
+Socket UnixSocket::accept ()
+{
+
+#ifdef AF_UNIX
+  sockaddr_un addr;
+  int len = sizeof (addr);
+  return Socket::accept ((MYSERVER_SOCKADDR*)&addr, &len);
+#else
+  Socket s;
+  s.setHandle (0);
+  return s;
+#endif
+}

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am    2009-04-23 21:21:00 UTC (rev 3056)
+++ trunk/myserver/tests/Makefile.am    2009-04-27 12:43:32 UTC (rev 3057)
@@ -2,5 +2,5 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_socket_pair.cpp test_utility.cpp  
test_xml_validator.cpp test_ip.cpp test_plugin_info.cpp test_fork_server.cpp
+tests_suite_SOURCES = main.cpp test_connection.cpp test_ftp.cpp 
test_log_manager.cpp test_mime_manager.cpp test_mutex.cpp 
test_security_domain.cpp test_validator.cpp test_auth_domain.cpp 
test_connections_scheduler.cpp test_gzip.cpp test_log_stream_factory.cpp 
test_pipe.cpp test_security_manager.cpp test_validator_factory.cpp 
test_base64.cpp test_file_stream.cpp test_hashmap.cpp test_md5.cpp 
test_recursive_mutex.cpp test_semaphore.cpp test_xml.cpp 
test_cached_file_buffer.cpp test_file_stream_creator.cpp test_homedir.cpp 
test_mem_buff.cpp test_regex.cpp test_socket_stream_creator.cpp 
test_cached_file.cpp test_files_utility.cpp test_http_request.cpp 
test_http_req_security_domain.cpp test_mem_stream.cpp test_safetime.cpp 
test_thread.cpp test_cached_file_factory.cpp test_filter_chain.cpp 
test_http_response.cpp test_multicast.cpp test_security_cache.cpp 
test_security_token.cpp test_socket_pair.cpp test_unix_socket.cpp 
test_utility.cpp  test_xml_validator.cpp test_ip.cpp test_plugin_info.cpp 
test_fork_server.cpp
 tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB) 
$(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(SSL_LIB) $(ZLIB_LIB) 
$(XML_LIBS) $(LDFLAGS)

Added: trunk/myserver/tests/test_unix_socket.cpp
===================================================================
--- trunk/myserver/tests/test_unix_socket.cpp                           (rev 0)
+++ trunk/myserver/tests/test_unix_socket.cpp   2009-04-27 12:43:32 UTC (rev 
3057)
@@ -0,0 +1,147 @@
+/*
+ MyServer
+ Copyright (C) 2009 Free Software Foundation, Inc.
+ 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 3 of the License, or
+ (at your option) any later version.
+ 
+ This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <include/base/socket_pair/socket_pair.h>
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <include/base/utility.h>
+#include <include/base/file/files_utility.h>
+#include <include/base/unix_socket/unix_socket.h>
+#include <include/base/thread/thread.h>
+#include <string.h>
+
+#ifdef AF_UNIX
+
+#define TEST_STRING "Hello World!"
+
+struct UnixSocketServerType
+{
+  bool result;
+  UnixSocket *socket;
+};
+
+void* test_unix_socket_server (void* pParam)
+{
+  UnixSocketServerType *server = (UnixSocketServerType*)pParam;
+  server->result = false;
+
+  Socket client = server->socket->accept ();
+
+  if (client.getHandle () == 0)
+    return NULL;
+  
+  char buffer [256];
+  if (client.recv(buffer, 256, 0, MYSERVER_SEC (1)) == 0)
+    {
+      client.close ();
+      return NULL;
+    }
+  
+
+  server->result = !strcmpi (buffer, TEST_STRING);
+ 
+  client.close ();
+  return NULL;
+}
+
+
+
+class TestUnixSocket : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestUnixSocket );
+  CPPUNIT_TEST( testBind );
+  CPPUNIT_TEST( testClient );
+  CPPUNIT_TEST_SUITE_END();
+  
+  UnixSocket *sock;
+public:
+
+  void setUp () 
+  {
+    sock = new UnixSocket ();
+  }
+
+  void tearDown ()
+  {
+    delete sock;
+  }
+
+  void testBind ()
+  {
+    int ret;
+    string path;
+    FilesUtility::temporaryFileName(0, path);
+
+
+    CPPUNIT_ASSERT (sock->socket ());
+
+    //FIXME: find a better place inside FilesUtility.
+    struct stat F_Stats;
+    ret = stat(path.c_str (), &F_Stats);
+    CPPUNIT_ASSERT (ret);
+    /////////////////////////////////////////////////
+
+    CPPUNIT_ASSERT_EQUAL (sock->bind (path.c_str ()), 0);
+
+    ret = stat(path.c_str (), &F_Stats);
+    CPPUNIT_ASSERT_EQUAL (ret, 0);
+
+    CPPUNIT_ASSERT_EQUAL (sock->shutdown (), 0);
+
+    CPPUNIT_ASSERT_EQUAL (sock->close (), 0);
+
+    ret = stat(path.c_str (), &F_Stats);
+    CPPUNIT_ASSERT (ret);
+  }
+
+  void testClient ()
+  {
+    ThreadID tid;
+    UnixSocketServerType data;
+    data.socket = sock;
+    data.result = false;
+
+    int ret;
+    string path;
+    FilesUtility::temporaryFileName (0, path);
+    sock->socket ();
+    sock->bind (path.c_str ());
+    CPPUNIT_ASSERT_EQUAL (sock->listen (1), 0);
+  
+    int res = Thread::create (&tid, test_unix_socket_server, &data);
+
+    UnixSocket client;
+    client.socket ();
+    CPPUNIT_ASSERT_EQUAL (client.connect (path.c_str ()), 0);
+    int length = strlen (TEST_STRING) + 1;
+    CPPUNIT_ASSERT_EQUAL (client.send (TEST_STRING, length, 0), length);
+
+    Thread::join (tid);
+
+    client.shutdown ();
+    client.close ();
+
+    CPPUNIT_ASSERT_EQUAL (data.result, true);
+  }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestUnixSocket );
+
+#endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]