1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#ifdef WIN32
# pragma warning(disable:4786) // TODO: Comment about what the disabled warning is.
#endif
#include <Ice/SslFactory.h>
#include <Ice/SslSystem.h>
#include <Ice/Properties.h>
#include <Ice/SslAcceptor.h>
#include <Ice/SslTransceiver.h>
#include <Ice/Instance.h>
#include <Ice/TraceLevels.h>
#include <Ice/Logger.h>
#include <Ice/Network.h>
#include <Ice/Exception.h>
#include <Ice/SecurityException.h>
#include <sstream>
using namespace std;
using namespace Ice;
using namespace IceInternal;
using std::string;
using std::ostringstream;
using IceSecurity::Ssl::Connection;
using IceSecurity::Ssl::Factory;
using IceSecurity::Ssl::System;
using IceSecurity::Ssl::ShutdownException;
SOCKET
IceInternal::SslAcceptor::fd()
{
return _fd;
}
void
IceInternal::SslAcceptor::close()
{
if (_traceLevels->network >= 1)
{
ostringstream s;
s << "stopping to accept ssl connections at " << toString();
_logger->trace(_traceLevels->networkCat, s.str());
}
SOCKET fd = _fd;
_fd = INVALID_SOCKET;
closeSocket(fd);
}
void
IceInternal::SslAcceptor::shutdown()
{
if (_traceLevels->network >= 2)
{
ostringstream s;
s << "shutting down accepting ssl connections at " << toString();
_logger->trace(_traceLevels->networkCat, s.str());
}
::shutdown(_fd, SHUT_RD); // Shutdown socket for reading
}
void
IceInternal::SslAcceptor::listen()
{
try
{
doListen(_fd, _backlog);
}
catch(...)
{
_fd = INVALID_SOCKET;
throw;
}
if (_traceLevels->network >= 1)
{
ostringstream s;
s << "accepting ssl connections at " << toString();
_logger->trace(_traceLevels->networkCat, s.str());
}
}
TransceiverPtr
IceInternal::SslAcceptor::accept(int timeout)
{
SOCKET fd = doAccept(_fd, timeout);
if (_traceLevels->network >= 1)
{
ostringstream s;
s << "accepted ssl connection\n" << fdToString(fd);
_logger->trace(_traceLevels->networkCat, s.str());
}
PropertiesPtr properties = _instance->properties();
// This is the Ice SSL Configuration File on which we will base
// all connections in this communicator.
string configFile = properties->getProperty("Ice.Ssl.Config");
// Get an instance of the SslSystem singleton.
System* sslSystem = Factory::getSystem(configFile);
if (!sslSystem->isTraceSet())
{
sslSystem->setTrace(_traceLevels);
}
if (!sslSystem->isLoggerSet())
{
sslSystem->setLogger(_logger);
}
if (!sslSystem->isPropertiesSet())
{
sslSystem->setProperties(properties);
}
// Initialize the server (if needed)
if (!sslSystem->isConfigLoaded())
{
sslSystem->loadConfig();
}
Connection* sslConnection = 0;
try
{
sslConnection = sslSystem->createServerConnection(fd);
}
catch (...)
{
Factory::releaseSystem(sslSystem);
sslSystem = 0;
// Shutdown the connection.
throw;
}
TransceiverPtr transPtr = new SslTransceiver(_instance, fd, sslConnection);
Factory::releaseSystem(sslSystem);
sslSystem = 0;
return transPtr;
}
string
IceInternal::SslAcceptor::toString() const
{
return addrToString(_addr);
}
bool
IceInternal::SslAcceptor::equivalent(const string& host, int port) const
{
struct sockaddr_in addr;
getAddress(host.c_str(), port, addr);
if (addr.sin_addr.s_addr == htonl(INADDR_LOOPBACK))
{
return port == ntohs(_addr.sin_port);
}
struct sockaddr_in localAddr;
getLocalAddress(ntohs(_addr.sin_port), localAddr);
return memcmp(&addr, &localAddr, sizeof(struct sockaddr_in)) == 0;
}
int
IceInternal::SslAcceptor::effectivePort()
{
return ntohs(_addr.sin_port);
}
IceInternal::SslAcceptor::SslAcceptor(const InstancePtr& instance, int port) :
_instance(instance),
_traceLevels(instance->traceLevels()),
_logger(instance->logger()),
_backlog(0)
{
if (_backlog <= 0)
{
_backlog = 5;
}
try
{
memset(&_addr, 0, sizeof(_addr));
_addr.sin_family = AF_INET;
_addr.sin_port = htons(port);
_addr.sin_addr.s_addr = htonl(INADDR_ANY);
_fd = createSocket(false);
doBind(_fd, _addr);
}
catch(...)
{
_fd = INVALID_SOCKET;
throw;
}
}
IceInternal::SslAcceptor::~SslAcceptor()
{
assert(_fd == INVALID_SOCKET);
}
|