diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/C++NPv2/Client_Logging_Daemon.cpp | 2 | ||||
-rw-r--r-- | examples/C++NPv2/Makefile | 1 | ||||
-rw-r--r-- | examples/C++NPv2/TPCLS.mak | 36 | ||||
-rw-r--r-- | examples/C++NPv2/TPCLS_export.h | 50 | ||||
-rw-r--r-- | examples/C++NPv2/TPC_Logging_Server.cpp | 102 | ||||
-rw-r--r-- | examples/C++NPv2/TPC_Logging_Server.h | 86 |
6 files changed, 276 insertions, 1 deletions
diff --git a/examples/C++NPv2/Client_Logging_Daemon.cpp b/examples/C++NPv2/Client_Logging_Daemon.cpp index bdd2a8391c9..3aaac3a1100 100644 --- a/examples/C++NPv2/Client_Logging_Daemon.cpp +++ b/examples/C++NPv2/Client_Logging_Daemon.cpp @@ -326,7 +326,7 @@ int Client_Logging_Daemon::init (int argc, ACE_TCHAR *argv[]) { u_short cld_port = ACE_DEFAULT_SERVICE_PORT; u_short sld_port = ACE_DEFAULT_LOGGING_SERVER_PORT; ACE_TCHAR sld_host[MAXHOSTNAMELEN]; - ACE_OS::strcpy (sld_host, ACE_LOCALHOST); + ACE_OS_String::strcpy (sld_host, ACE_LOCALHOST); ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("p:r:s:"), 0); get_opt.long_option (ACE_TEXT ("client_port"), 'p', diff --git a/examples/C++NPv2/Makefile b/examples/C++NPv2/Makefile index d827621821f..06f643a0198 100644 --- a/examples/C++NPv2/Makefile +++ b/examples/C++NPv2/Makefile @@ -17,6 +17,7 @@ EXAMPLES = Reactor_Logging_Server \ SLDex \ CLD \ TPLS \ + TPCLS \ display_logfile all: diff --git a/examples/C++NPv2/TPCLS.mak b/examples/C++NPv2/TPCLS.mak new file mode 100644 index 00000000000..417d3681d78 --- /dev/null +++ b/examples/C++NPv2/TPCLS.mak @@ -0,0 +1,36 @@ +#---------------------------------------------------------------------------- +# $Id$ +# +# Makefile for the TPCLS library in the C++NPv2, Chapter 7 example. +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- +# Local macros +#---------------------------------------------------------------------------- + +SHLIB = libTPCLS.$(SOEXT) +FILES = TPC_Logging_Server \ + Logging_Handler +LSRC = $(addsuffix .cpp,$(FILES)) + +LIBS += $(ACELIB) + +BUILD = $(VSHLIB) + +#---------------------------------------------------------------------------- +# Include macros and targets +#---------------------------------------------------------------------------- + +include $(ACE_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(ACE_ROOT)/include/makeinclude/macros.GNU +include $(ACE_ROOT)/include/makeinclude/rules.common.GNU +include $(ACE_ROOT)/include/makeinclude/rules.nonested.GNU +include $(ACE_ROOT)/include/makeinclude/rules.lib.GNU +include $(ACE_ROOT)/include/makeinclude/rules.local.GNU + +# To remake the DLL_Test source with xlC on AIX, it works +# best to wipe out any previously-created tempinc directory. +# The compiler/linker isn't too smart about instantiating templates... +ifdef TEMPINCDIR +COMPILE.cc := $(RM) -rf tempinc; $(COMPILE.cc) +endif diff --git a/examples/C++NPv2/TPCLS_export.h b/examples/C++NPv2/TPCLS_export.h new file mode 100644 index 00000000000..71039b40aa7 --- /dev/null +++ b/examples/C++NPv2/TPCLS_export.h @@ -0,0 +1,50 @@ + +// -*- C++ -*- +// $Id$ +// Definition for Win32 Export directives. +// This file is generated automatically by generate_export_file.pl TPCLS +// ------------------------------ +#ifndef TPCLS_EXPORT_H +#define TPCLS_EXPORT_H + +#include "ace/config-all.h" + +#if !defined (TPCLS_HAS_DLL) +# define TPCLS_HAS_DLL 1 +#endif /* ! TPCLS_HAS_DLL */ + +#if defined (TPCLS_HAS_DLL) && (TPCLS_HAS_DLL == 1) +# if defined (TPCLS_BUILD_DLL) +# define TPCLS_Export ACE_Proper_Export_Flag +# define TPCLS_SINGLETON_DECLARATION(T) ACE_EXPORT_SINGLETON_DECLARATION (T) +# define TPCLS_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) ACE_EXPORT_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) +# else /* TPCLS_BUILD_DLL */ +# define TPCLS_Export ACE_Proper_Import_Flag +# define TPCLS_SINGLETON_DECLARATION(T) ACE_IMPORT_SINGLETON_DECLARATION (T) +# define TPCLS_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) ACE_IMPORT_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) +# endif /* TPCLS_BUILD_DLL */ +#else /* TPCLS_HAS_DLL == 1 */ +# define TPCLS_Export +# define TPCLS_SINGLETON_DECLARATION(T) +# define TPCLS_SINGLETON_DECLARE(SINGLETON_TYPE, CLASS, LOCK) +#endif /* TPCLS_HAS_DLL == 1 */ + +// Set TPCLS_NTRACE = 0 to turn on library specific tracing even if +// tracing is turned off for ACE. +#if !defined (TPCLS_NTRACE) +# if (ACE_NTRACE == 1) +# define TPCLS_NTRACE 1 +# else /* (ACE_NTRACE == 1) */ +# define TPCLS_NTRACE 0 +# endif /* (ACE_NTRACE == 1) */ +#endif /* !TPCLS_NTRACE */ + +#if (TPCLS_NTRACE == 1) +# define TPCLS_TRACE(X) +#else /* (TPCLS_NTRACE == 1) */ +# define TPCLS_TRACE(X) ACE_TRACE_IMPL(X) +#endif /* (TPCLS_NTRACE == 1) */ + +#endif /* TPCLS_EXPORT_H */ + +// End of auto generated file. diff --git a/examples/C++NPv2/TPC_Logging_Server.cpp b/examples/C++NPv2/TPC_Logging_Server.cpp new file mode 100644 index 00000000000..88fe739cbe5 --- /dev/null +++ b/examples/C++NPv2/TPC_Logging_Server.cpp @@ -0,0 +1,102 @@ +/* +** $Id$ +** +** This is the Thread-per-connection logging server example from Chapter 7. +** +** Copyright 2002 Addison Wesley. All Rights Reserved. +*/ + +#include "ace/OS.h" +#include "ace/FILE_Addr.h" +#include "ace/FILE_Connector.h" +#include "Reactor_Logging_Server_Adapter.h" +#include "TPC_Logging_Server.h" +#include "TPCLS_export.h" + +int TPC_Logging_Handler::open (void *) { + static const ACE_TCHAR LOGFILE_SUFFIX[] = ACE_TEXT (".log"); + ACE_TCHAR filename[MAXHOSTNAMELEN + sizeof (LOGFILE_SUFFIX)]; + ACE_INET_Addr logging_peer_addr; + + peer ().get_remote_addr (logging_peer_addr); + logging_peer_addr.get_host_name (filename, MAXHOSTNAMELEN); + ACE_OS_String::strcat (filename, LOGFILE_SUFFIX); + + ACE_FILE_Connector connector; + connector.connect (log_file_, + ACE_FILE_Addr (filename), + 0, // No timeout. + ACE_Addr::sap_any, // Ignored. + 0, // Don't try to reuse the addr. + O_RDWR | O_CREAT | O_APPEND, + ACE_DEFAULT_FILE_PERMS); + + logging_handler_.peer ().set_handle (peer ().get_handle ()); + + return activate (THR_NEW_LWP | THR_DETACHED); +} + + +#if !defined (TPC_CERTIFICATE_FILENAME) +# define TPC_CERTIFICATE_FILENAME "tpc-cert.pem" +#endif /* !TPC_CERTIFICATE_FILENAME */ +#if !defined (TPC_KEY_FILENAME) +# define TPC_KEY_FILENAME "tpc-key.pem" +#endif /* !TPC_KEY_FILENAME */ + +int TPC_Logging_Acceptor::open + (const ACE_SOCK_Acceptor::PEER_ADDR &local_addr, + ACE_Reactor *reactor, + int flags, int use_select, int reuse_addr) { + if (PARENT::open (local_addr, reactor, flags, + use_select, reuse_addr) != 0) + return -1; + OpenSSL_add_ssl_algorithms (); + ssl_ctx_ = SSL_CTX_new (SSLv3_server_method ()); + if (ssl_ctx_ == 0) return -1; + + if (SSL_CTX_use_certificate_file (ssl_ctx_, + TPC_CERTIFICATE_FILENAME, + SSL_FILETYPE_PEM) <= 0 + || SSL_CTX_use_PrivateKey_file (ssl_ctx_, + TPC_KEY_FILENAME, + SSL_FILETYPE_PEM) <= 0 + || !SSL_CTX_check_private_key (ssl_ctx_)) + return -1; + ssl_ = SSL_new (ssl_ctx_); + return ssl_ == 0 ? -1 : 0; +} + + +int TPC_Logging_Acceptor::handle_close (ACE_HANDLE h, + ACE_Reactor_Mask mask) { + PARENT::handle_close (h, mask); + delete this; + return 0; +} + + +int TPC_Logging_Acceptor::accept_svc_handler + (TPC_Logging_Handler *sh) { + if (PARENT::accept_svc_handler (sh) == -1) return -1; + SSL_clear (ssl_); // Reset for new SSL connection. + SSL_set_fd (ssl_, sh->get_handle ()); + + SSL_set_verify + (ssl_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0); + if (SSL_accept (ssl_) == -1 + || SSL_shutdown (ssl_) == -1) return -1; + return 0; +} + + +typedef Reactor_Logging_Server_Adapter<TPC_Logging_Acceptor> + TPC_Logging_Server; + +ACE_FACTORY_DEFINE (TPCLS, TPC_Logging_Server) + +#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION) +template class Reactor_Logging_Server_Adapter<TPC_Logging_Acceptor>; +#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA) +#pragma instantiate Reactor_Logging_Server_Adapter<TPC_Logging_Acceptor> +#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */ diff --git a/examples/C++NPv2/TPC_Logging_Server.h b/examples/C++NPv2/TPC_Logging_Server.h new file mode 100644 index 00000000000..cd30f50113f --- /dev/null +++ b/examples/C++NPv2/TPC_Logging_Server.h @@ -0,0 +1,86 @@ +/* +** $Id$ +** +** This is the Thread-per-connection logging server example from Chapter 7. +** +** Copyright 2002 Addison Wesley. All Rights Reserved. +*/ + +#ifndef _TPC_LOGGING_SERVER_H +#define _TPC_LOGGING_SERVER_H + +#include "ace/Acceptor.h" +#include "ace/INET_Addr.h" +#include "ace/Reactor.h" +#include "ace/Svc_Handler.h" +#include "ace/FILE_IO.h" +#include "Logging_Handler.h" + +#include "ace/SOCK_Acceptor.h" +#include <openssl/ssl.h> + +class TPC_Logging_Handler + : public ACE_Svc_Handler<ACE_SOCK_Stream, ACE_NULL_SYNCH> { +protected: + ACE_FILE_IO log_file_; // File of log records. + + // Connection to peer service handler. + Logging_Handler logging_handler_; + +public: + TPC_Logging_Handler () : logging_handler_ (log_file_) {} + + virtual int open (void *); + + virtual int svc (void) { + for (;;) + switch (logging_handler_.log_record ()) { + case -1: return -1; // Error. + case 0: return 0; // Client closed connection. + default: continue; // Default case. + } + /* NOTREACHED */ + return 0; + } +}; + + +class TPC_Logging_Acceptor + : public ACE_Acceptor<TPC_Logging_Handler, ACE_SOCK_Acceptor> { +protected: + // The SSL ``context'' data structure. + SSL_CTX *ssl_ctx_; + + // The SSL data structure corresponding to authenticated + // SSL connections. + SSL *ssl_; + +public: + typedef ACE_Acceptor<TPC_Logging_Handler, ACE_SOCK_Acceptor> + PARENT; + typedef ACE_SOCK_Acceptor::PEER_ADDR PEER_ADDR; + + // Constructor. + TPC_Logging_Acceptor (ACE_Reactor *r) + : PARENT (r), ssl_ctx_ (0), ssl_ (0) {} + + // Destructor frees the SSL resources. + virtual ~TPC_Logging_Acceptor (void) { + SSL_free (this->ssl_); + SSL_CTX_free (this->ssl_ctx_); + } + + // Initialize the acceptor instance. + virtual int open (const ACE_SOCK_Acceptor::PEER_ADDR &local_addr, + ACE_Reactor *reactor = ACE_Reactor::instance (), + int flags = 0, int use_select = 1, int reuse_addr = 1); + + virtual int handle_close + (ACE_HANDLE = ACE_INVALID_HANDLE, + ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK); + + // Connection establishment and authentication hook method. + virtual int accept_svc_handler (TPC_Logging_Handler *sh); +}; + +#endif /* _TPC_LOGGING_SERVER_H */ |