summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/SslConnector.cpp
blob: c1b8b33d40999bc13df0aa3c5b09785611e1f6cb (plain)
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
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include <Ice/SslFactory.h>
#include <Ice/SslSystem.h>
#include <Ice/SslConnector.h>
#include <Ice/SslTransceiver.h>
#include <Ice/Instance.h>
#include <Ice/TraceLevels.h>
#include <Ice/Logger.h>
#include <Ice/Network.h>
#include <Ice/Properties.h>
#include <Ice/Exception.h>
#include <Ice/SslException.h>
#include <sstream>

using namespace std;
using namespace Ice;
using namespace IceInternal;

using std::ostringstream;
using std::string;
using IceSecurity::Ssl::Connection;
using IceSecurity::Ssl::Factory;
using IceSecurity::Ssl::System;
using IceSecurity::Ssl::ShutdownException;

TransceiverPtr
IceInternal::SslConnector::connect(int timeout)
{
    if (_traceLevels->network >= 2)
    {
	ostringstream s;
	s << "trying to establish ssl connection to " << toString();
	_logger->trace(_traceLevels->networkCat, s.str());
    }

    int fd = createSocket(false);
    doConnect(fd, _addr, timeout);

    if (_traceLevels->network >= 1)
    {
	ostringstream s;
	s << "ssl connection established\n" << fdToString(fd);
	_logger->trace(_traceLevels->networkCat, s.str());
    }

    // This is the Ice SSL Configuration File on which we will base
    // all connections in this communicator.
    string configFile = _instance->properties()->getProperty("Ice.Ssl.Config");

    // Get an instance of the SslOpenSSL singleton.
    System* sslSystem = Factory::getSystem(configFile);

    if (!sslSystem->isTraceSet())
    {
        sslSystem->setTrace(_traceLevels);
    }

    if (!sslSystem->isLoggerSet())
    {
        sslSystem->setLogger(_logger);
    }

    // Initialize the server (if needed)
    if (!sslSystem->isConfigLoaded())
    {
        sslSystem->loadConfig();
    }

    Connection* sslConnection = 0;

    try
    {
        sslConnection = sslSystem->createClientConnection(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::SslConnector::toString() const
{
    return addrToString(_addr);
}

IceInternal::SslConnector::SslConnector(const InstancePtr& instance, const string& host, int port) :
    _instance(instance),
    _traceLevels(instance->traceLevels()),
    _logger(instance->logger())
{
    getAddress(host.c_str(), port, _addr);
}

IceInternal::SslConnector::~SslConnector()
{
}