ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / src / IceSSL / EndpointI.cpp
blob9c8e63da6551d2426d84075e35c20e656843e1a4
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #include <IceSSL/EndpointI.h>
11 #include <IceSSL/AcceptorI.h>
12 #include <IceSSL/ConnectorI.h>
13 #include <IceSSL/TransceiverI.h>
14 #include <IceSSL/Instance.h>
15 #include <Ice/Network.h>
16 #include <Ice/BasicStream.h>
17 #include <Ice/LocalException.h>
18 #include <Ice/DefaultsAndOverrides.h>
19 #include <Ice/HashUtil.h>
21 using namespace std;
22 using namespace Ice;
23 using namespace IceSSL;
25 IceSSL::EndpointI::EndpointI(const InstancePtr& instance, const string& ho, Int po, Int ti, const string& conId,
26 bool co) :
27 _instance(instance),
28 _host(ho),
29 _port(po),
30 _timeout(ti),
31 _connectionId(conId),
32 _compress(co)
36 IceSSL::EndpointI::EndpointI(const InstancePtr& instance, const string& str, bool oaEndpoint) :
37 _instance(instance),
38 _port(0),
39 _timeout(-1),
40 _compress(false)
42 const string delim = " \t\n\r";
44 string::size_type beg;
45 string::size_type end = 0;
47 while(true)
49 beg = str.find_first_not_of(delim, end);
50 if(beg == string::npos)
52 break;
55 end = str.find_first_of(delim, beg);
56 if(end == string::npos)
58 end = str.length();
61 string option = str.substr(beg, end - beg);
62 if(option.length() != 2 || option[0] != '-')
64 EndpointParseException ex(__FILE__, __LINE__);
65 ex.str = "expected an endpoint option but found `" + option + "' in endpoint `ssl " + str + "'";
66 throw ex;
69 string argument;
70 string::size_type argumentBeg = str.find_first_not_of(delim, end);
71 if(argumentBeg != string::npos && str[argumentBeg] != '-')
73 beg = argumentBeg;
74 end = str.find_first_of(delim, beg);
75 if(end == string::npos)
77 end = str.length();
79 argument = str.substr(beg, end - beg);
80 if(argument[0] == '\"' && argument[argument.size() - 1] == '\"')
82 argument = argument.substr(1, argument.size() - 2);
86 switch(option[1])
88 case 'h':
90 if(argument.empty())
92 EndpointParseException ex(__FILE__, __LINE__);
93 ex.str = "no argument provided for -h option in endpoint `ssl " + str + "'";
94 throw ex;
96 const_cast<string&>(_host) = argument;
97 break;
100 case 'p':
102 if(argument.empty())
104 EndpointParseException ex(__FILE__, __LINE__);
105 ex.str = "no argument provided for -p option in endpoint `ssl " + str + "'";
106 throw ex;
108 istringstream p(argument);
109 if(!(p >> const_cast<Int&>(_port)) || !p.eof())
111 EndpointParseException ex(__FILE__, __LINE__);
112 ex.str = "invalid port value `" + argument + "' in endpoint `ssl " + str + "'";
113 throw ex;
115 else if(_port < 0 || _port > 65535)
117 EndpointParseException ex(__FILE__, __LINE__);
118 ex.str = "port value `" + argument + "' out of range in endpoint `ssl " + str + "'";
119 throw ex;
121 break;
124 case 't':
126 if(argument.empty())
128 EndpointParseException ex(__FILE__, __LINE__);
129 ex.str = "no argument provided for -t option in endpoint `ssl " + str + "'";
130 throw ex;
132 istringstream t(argument);
133 if(!(t >> const_cast<Int&>(_timeout)) || !t.eof())
135 EndpointParseException ex(__FILE__, __LINE__);
136 ex.str = "invalid timeout value `" + argument + "' in endpoint `ssl " + str + "'";
137 throw ex;
139 break;
142 case 'z':
144 if(!argument.empty())
146 EndpointParseException ex(__FILE__, __LINE__);
147 ex.str = "unexpected argument `" + argument + "' provided for -z option in `ssl " + str + "'";
148 throw ex;
150 const_cast<bool&>(_compress) = true;
151 break;
154 default:
156 EndpointParseException ex(__FILE__, __LINE__);
157 ex.str = "unknown option `" + option + "' in `ssl " + str + "'";
158 throw ex;
163 if(_host.empty())
165 const_cast<string&>(_host) = _instance->defaultHost();
167 else if(_host == "*")
169 if(oaEndpoint)
171 const_cast<string&>(_host) = string();
173 else
175 EndpointParseException ex(__FILE__, __LINE__);
176 ex.str = "`-h *' not valid for proxy endpoint `ssl " + str + "'";
177 throw ex;
182 IceSSL::EndpointI::EndpointI(const InstancePtr& instance, IceInternal::BasicStream* s) :
183 _instance(instance),
184 _port(0),
185 _timeout(-1),
186 _compress(false)
188 s->startReadEncaps();
189 s->read(const_cast<string&>(_host), false);
190 s->read(const_cast<Int&>(_port));
191 s->read(const_cast<Int&>(_timeout));
192 s->read(const_cast<bool&>(_compress));
193 s->endReadEncaps();
196 void
197 IceSSL::EndpointI::streamWrite(IceInternal::BasicStream* s) const
199 s->write(EndpointType);
200 s->startWriteEncaps();
201 s->write(_host, false);
202 s->write(_port);
203 s->write(_timeout);
204 s->write(_compress);
205 s->endWriteEncaps();
208 string
209 IceSSL::EndpointI::toString() const
212 // WARNING: Certain features, such as proxy validation in Glacier2,
213 // depend on the format of proxy strings. Changes to toString() and
214 // methods called to generate parts of the reference string could break
215 // these features. Please review for all features that depend on the
216 // format of proxyToString() before changing this and related code.
218 ostringstream s;
219 s << "ssl";
221 if(!_host.empty())
223 s << " -h ";
224 bool addQuote = _host.find(':') != string::npos;
225 if(addQuote)
227 s << "\"";
229 s << _host;
230 if(addQuote)
232 s << "\"";
236 s << " -p " << _port;
237 if(_timeout != -1)
239 s << " -t " << _timeout;
241 if(_compress)
243 s << " -z";
245 return s.str();
249 // COMPILERFIX: VC6 complains about an ambiguous "EndpointInfo" symbol when this class is defined inside
250 // getInfo(). Moving the definition into an anonymous namespace works around it.
252 namespace
254 class InfoI : public IceSSL::EndpointInfo
256 public:
258 InfoI(Int to, bool comp, const string& host, Int port) :
259 IceSSL::EndpointInfo(to, comp, host, port)
263 virtual Short
264 type() const
266 return EndpointType;
269 virtual bool
270 datagram() const
272 return false;
275 virtual bool
276 secure() const
278 return true;
283 Ice::EndpointInfoPtr
284 IceSSL::EndpointI::getInfo() const
286 return new InfoI(_timeout, _compress, _host, _port);
289 Short
290 IceSSL::EndpointI::type() const
292 return EndpointType;
296 IceSSL::EndpointI::timeout() const
298 return _timeout;
301 IceInternal::EndpointIPtr
302 IceSSL::EndpointI::timeout(Int timeout) const
304 if(timeout == _timeout)
306 return const_cast<EndpointI*>(this);
308 else
310 return new EndpointI(_instance, _host, _port, timeout, _connectionId, _compress);
314 IceInternal::EndpointIPtr
315 IceSSL::EndpointI::connectionId(const string& connectionId) const
317 if(connectionId == _connectionId)
319 return const_cast<EndpointI*>(this);
321 else
323 return new EndpointI(_instance, _host, _port, _timeout, connectionId, _compress);
327 bool
328 IceSSL::EndpointI::compress() const
330 return _compress;
333 IceInternal::EndpointIPtr
334 IceSSL::EndpointI::compress(bool compress) const
336 if(compress == _compress)
338 return const_cast<EndpointI*>(this);
340 else
342 return new EndpointI(_instance, _host, _port, _timeout, _connectionId, compress);
346 bool
347 IceSSL::EndpointI::datagram() const
349 return false;
352 bool
353 IceSSL::EndpointI::secure() const
355 return true;
358 IceInternal::TransceiverPtr
359 IceSSL::EndpointI::transceiver(IceInternal::EndpointIPtr& endp) const
361 endp = const_cast<EndpointI*>(this);
362 return 0;
365 vector<IceInternal::ConnectorPtr>
366 IceSSL::EndpointI::connectors() const
368 return connectors(IceInternal::getAddresses(_host, _port, _instance->protocolSupport(), true));
371 void
372 IceSSL::EndpointI::connectors_async(const IceInternal::EndpointI_connectorsPtr& callback) const
374 _instance->endpointHostResolver()->resolve(_host, _port, const_cast<EndpointI*>(this), callback);
377 IceInternal::AcceptorPtr
378 IceSSL::EndpointI::acceptor(IceInternal::EndpointIPtr& endp, const string& adapterName) const
380 AcceptorI* p = new AcceptorI(_instance, adapterName, _host, _port);
381 endp = new EndpointI(_instance, _host, p->effectivePort(), _timeout, _connectionId, _compress);
382 return p;
385 vector<IceInternal::EndpointIPtr>
386 IceSSL::EndpointI::expand() const
388 vector<IceInternal::EndpointIPtr> endps;
389 vector<string> hosts = IceInternal::getHostsForEndpointExpand(_host, _instance->protocolSupport(), false);
390 if(hosts.empty())
392 endps.push_back(const_cast<EndpointI*>(this));
394 else
396 for(vector<string>::const_iterator p = hosts.begin(); p != hosts.end(); ++p)
398 endps.push_back(new EndpointI(_instance, *p, _port, _timeout, _connectionId, _compress));
401 return endps;
404 bool
405 IceSSL::EndpointI::equivalent(const IceInternal::EndpointIPtr& endpoint) const
407 const EndpointI* sslEndpointI = dynamic_cast<const EndpointI*>(endpoint.get());
408 if(!sslEndpointI)
410 return false;
412 return sslEndpointI->_host == _host && sslEndpointI->_port == _port;
415 bool
416 IceSSL::EndpointI::operator==(const Ice::LocalObject& r) const
418 const EndpointI* p = dynamic_cast<const EndpointI*>(&r);
419 if(!p)
421 return false;
424 if(this == p)
426 return true;
429 if(_host != p->_host)
431 return false;
434 if(_port != p->_port)
436 return false;
439 if(_timeout != p->_timeout)
441 return false;
444 if(_connectionId != p->_connectionId)
446 return false;
449 if(_compress != p->_compress)
451 return false;
454 return true;
457 bool
458 IceSSL::EndpointI::operator<(const Ice::LocalObject& r) const
460 const EndpointI* p = dynamic_cast<const EndpointI*>(&r);
461 if(!p)
463 const IceInternal::EndpointI* e = dynamic_cast<const IceInternal::EndpointI*>(&r);
464 if(!e)
466 return false;
468 return type() < e->type();
471 if(this == p)
473 return false;
476 if(_host < p->_host)
478 return true;
480 else if (p->_host < _host)
482 return false;
485 if(_port < p->_port)
487 return true;
489 else if(p->_port < _port)
491 return false;
494 if(_timeout < p->_timeout)
496 return true;
498 else if(p->_timeout < _timeout)
500 return false;
503 if(_connectionId < p->_connectionId)
505 return true;
507 else if(p->_connectionId < _connectionId)
509 return false;
512 if(!_compress && p->_compress)
514 return true;
516 else if(p->_compress < _compress)
518 return false;
521 return false;
524 Ice::Int
525 IceSSL::EndpointI::hashInit() const
527 Int h = 0;
528 IceInternal::hashAdd(h, _host);
529 IceInternal::hashAdd(h, _port);
530 IceInternal::hashAdd(h, _timeout);
531 IceInternal::hashAdd(h, _connectionId);
532 IceInternal::hashAdd(h, _compress);
533 return h;
536 vector<IceInternal::ConnectorPtr>
537 IceSSL::EndpointI::connectors(const vector<struct sockaddr_storage>& addresses) const
539 vector<IceInternal::ConnectorPtr> connectors;
540 for(unsigned int i = 0; i < addresses.size(); ++i)
542 connectors.push_back(new ConnectorI(_instance, _host, addresses[i], _timeout, _connectionId));
544 return connectors;
547 IceSSL::EndpointFactoryI::EndpointFactoryI(const InstancePtr& instance)
548 : _instance(instance)
552 IceSSL::EndpointFactoryI::~EndpointFactoryI()
556 Short
557 IceSSL::EndpointFactoryI::type() const
559 return EndpointType;
562 string
563 IceSSL::EndpointFactoryI::protocol() const
565 return "ssl";
568 IceInternal::EndpointIPtr
569 IceSSL::EndpointFactoryI::create(const string& str, bool oaEndpoint) const
571 return new EndpointI(_instance, str, oaEndpoint);
574 IceInternal::EndpointIPtr
575 IceSSL::EndpointFactoryI::read(IceInternal::BasicStream* s) const
577 return new EndpointI(_instance, s);
580 void
581 IceSSL::EndpointFactoryI::destroy()
583 _instance = 0;