2 ** This is the Thread-per-connection logging server example from Chapter 7.
4 ** Copyright 2002 Addison Wesley. All Rights Reserved.
7 #include "ace/OS_NS_string.h"
8 #include "ace/FILE_Addr.h"
9 #include "ace/FILE_Connector.h"
10 #include "ace/os_include/os_netdb.h"
11 #include "Reactor_Logging_Server_Adapter.h"
12 #include "TPC_Logging_Server.h"
13 #include "TPCLS_export.h"
15 int TPC_Logging_Handler::open (void *) {
16 static const ACE_TCHAR LOGFILE_SUFFIX
[] = ACE_TEXT (".log");
17 ACE_TCHAR filename
[MAXHOSTNAMELEN
+ sizeof (LOGFILE_SUFFIX
)];
18 ACE_INET_Addr logging_peer_addr
;
20 peer ().get_remote_addr (logging_peer_addr
);
21 logging_peer_addr
.get_host_name (filename
, MAXHOSTNAMELEN
);
22 ACE_OS::strcat (filename
, LOGFILE_SUFFIX
);
24 ACE_FILE_Connector connector
;
25 connector
.connect (log_file_
,
26 ACE_FILE_Addr (filename
),
28 ACE_Addr::sap_any
, // Ignored.
29 0, // Don't try to reuse the addr.
30 O_RDWR
| O_CREAT
| O_APPEND
,
31 ACE_DEFAULT_FILE_PERMS
);
33 logging_handler_
.peer ().set_handle (peer ().get_handle ());
35 return activate (THR_NEW_LWP
| THR_DETACHED
);
39 #if !defined (TPC_CERTIFICATE_FILENAME)
40 # define TPC_CERTIFICATE_FILENAME "tpc-cert.pem"
41 #endif /* !TPC_CERTIFICATE_FILENAME */
42 #if !defined (TPC_KEY_FILENAME)
43 # define TPC_KEY_FILENAME "tpc-key.pem"
44 #endif /* !TPC_KEY_FILENAME */
46 int TPC_Logging_Acceptor::open
47 (const ACE_SOCK_Acceptor::PEER_ADDR
&local_addr
,
49 int flags
, int use_select
, int reuse_addr
) {
50 if (PARENT::open (local_addr
, reactor
, flags
,
51 use_select
, reuse_addr
) != 0)
53 OpenSSL_add_ssl_algorithms ();
54 ssl_ctx_
= SSL_CTX_new (SSLv23_server_method ());
55 if (ssl_ctx_
== 0) return -1;
57 if (SSL_CTX_use_certificate_file (ssl_ctx_
,
58 TPC_CERTIFICATE_FILENAME
,
59 SSL_FILETYPE_PEM
) <= 0
60 || SSL_CTX_use_PrivateKey_file (ssl_ctx_
,
62 SSL_FILETYPE_PEM
) <= 0
63 || !SSL_CTX_check_private_key (ssl_ctx_
))
65 ssl_
= SSL_new (ssl_ctx_
);
66 return ssl_
== 0 ? -1 : 0;
70 int TPC_Logging_Acceptor::handle_close (ACE_HANDLE h
,
71 ACE_Reactor_Mask mask
) {
72 PARENT::handle_close (h
, mask
);
78 int TPC_Logging_Acceptor::accept_svc_handler
79 (TPC_Logging_Handler
*sh
) {
80 if (PARENT::accept_svc_handler (sh
) == -1) return -1;
81 SSL_clear (ssl_
); // Reset for new SSL connection.
82 #if defined (ACE_WIN32)
83 // ACE_WIN32 is the only platform where ACE_HANDLE is not an int.
84 // See ace/config-lite.h for the typedefs.
85 SSL_set_fd (ssl_
, reinterpret_cast<int> (sh
->get_handle ()));
87 SSL_set_fd (ssl_
, sh
->get_handle ());
88 #endif /* ACE_WIN32 */
92 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
,
94 if (SSL_accept (ssl_
) == -1
95 || SSL_shutdown (ssl_
) == -1) return -1;
99 typedef Reactor_Logging_Server_Adapter
<TPC_Logging_Acceptor
>
102 ACE_FACTORY_DEFINE (TPCLS
, TPC_Logging_Server
)