summaryrefslogtreecommitdiff
path: root/cpp/src/IceSSL/WinRTTransceiverI.cpp
blob: be0d632eaf857ff3da8aa5e58d38691724eeacb8 (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
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
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceSSL/Config.h>

#ifdef ICE_OS_WINRT

#include <IceSSL/WinRTTransceiverI.h>
#include <IceSSL/Instance.h>
#include <IceSSL/SSLEngine.h>

using namespace std;
using namespace Ice;
using namespace IceSSL;

using namespace Platform;
using namespace Windows::Networking;
using namespace Windows::Networking::Sockets;

IceInternal::NativeInfoPtr
IceSSL::TransceiverI::getNativeInfo()
{
    return _delegate->getNativeInfo();
}

IceInternal::SocketOperation
IceSSL::TransceiverI::initialize(IceInternal::Buffer& readBuffer, IceInternal::Buffer& writeBuffer)
{
    if(!_connected)
    {
        IceInternal::SocketOperation status = _delegate->initialize(readBuffer, writeBuffer);
        if(status != IceInternal::SocketOperationNone)
        {
            return status;
        }
        _connected = true;

        //
        // Continue connecting, this will call startWrite/finishWrite to upgrade the stream
        // to SSL.
        //
        return IceInternal::SocketOperationConnect;
    }
    else if(!_upgraded)
    {
        _upgraded = true;
    }
    return IceInternal::SocketOperationNone;
}

IceInternal::SocketOperation
#ifdef ICE_CPP11_MAPPING
IceSSL::TransceiverI::closing(bool initiator, exception_ptr ex)
#else
IceSSL::TransceiverI::closing(bool initiator, const Ice::LocalException& ex)
#endif
{
	return _delegate->closing(initiator, ex);
}

void
IceSSL::TransceiverI::close()
{
    _delegate->close();
}

IceInternal::SocketOperation
IceSSL::TransceiverI::write(IceInternal::Buffer& buf)
{
    return _delegate->write(buf);
}

IceInternal::SocketOperation
IceSSL::TransceiverI::read(IceInternal::Buffer& buf)
{
    return _delegate->read(buf);
}

bool
IceSSL::TransceiverI::startWrite(IceInternal::Buffer& buf)
{
    if(_connected && !_upgraded)
    {
        StreamSocket^ stream = safe_cast<StreamSocket^>(_delegate->getNativeInfo()->fd());
        HostName^ host = ref new HostName(ref new String(IceUtil::stringToWstring(_host).c_str()));
        try
        {
            Windows::Foundation::IAsyncAction^ action = stream->UpgradeToSslAsync(SocketProtectionLevel::Tls12, host);
            getNativeInfo()->queueAction(IceInternal::SocketOperationWrite, action);
        }
        catch(Platform::Exception^ ex)
        {
            IceInternal::checkErrorCode(__FILE__, __LINE__, ex->HResult);
        }
        return true;
    }
    return _delegate->startWrite(buf);
}

void
IceSSL::TransceiverI::finishWrite(IceInternal::Buffer& buf)
{
    if(_connected && !_upgraded)
    {
        IceInternal::AsyncInfo* asyncInfo = getNativeInfo()->getAsyncInfo(IceInternal::SocketOperationWrite);
        if(asyncInfo->count == SOCKET_ERROR)
        {
            IceInternal::checkErrorCode(__FILE__, __LINE__, asyncInfo->error);
        }
        return;
    }
    _delegate->finishWrite(buf);
}

void
IceSSL::TransceiverI::startRead(IceInternal::Buffer& buf)
{
    _delegate->startRead(buf);
}

void
IceSSL::TransceiverI::finishRead(IceInternal::Buffer& buf)
{
    _delegate->finishRead(buf);
}

string
IceSSL::TransceiverI::protocol() const
{
    return _instance->protocol();
}

string
IceSSL::TransceiverI::toString() const
{
    return _delegate->toString();
}

string
IceSSL::TransceiverI::toDetailedString() const
{
    return toString();
}

Ice::ConnectionInfoPtr
IceSSL::TransceiverI::getInfo() const
{
    NativeConnectionInfoPtr info = ICE_MAKE_SHARED(NativeConnectionInfo);
    StreamSocket^ stream = safe_cast<StreamSocket^>(_delegate->getNativeInfo()->fd());
    info->nativeCerts.push_back(ICE_MAKE_SHARED(Certificate, stream->Information->ServerCertificate));
    info->certs.push_back(info->nativeCerts.back()->encode());
    for(auto iter = stream->Information->ServerIntermediateCertificates->First(); iter->HasCurrent; iter->MoveNext())
    {
        info->nativeCerts.push_back(ICE_MAKE_SHARED(Certificate, iter->Current));
        info->certs.push_back(info->nativeCerts.back()->encode());
    }
    info->adapterName = _adapterName;
    info->incoming = _incoming;
    info->underlying = _delegate->getInfo();
    return info;
}

void
IceSSL::TransceiverI::checkSendSize(const IceInternal::Buffer&)
{
}

void
IceSSL::TransceiverI::setBufferSize(int rcvSize, int sndSize)
{
    _delegate->setBufferSize(rcvSize, sndSize);
}

IceSSL::TransceiverI::TransceiverI(const InstancePtr& instance,
                                   const IceInternal::TransceiverPtr& delegate,
                                   const string& hostOrAdapterName,
                                   bool incoming) :
    _instance(instance),
    _engine(WinRTEnginePtr::dynamicCast(instance->engine())),
    _host(incoming ? "" : hostOrAdapterName),
    _adapterName(incoming ? hostOrAdapterName : ""),
    _incoming(incoming),
    _delegate(delegate),
    _connected(false),
    _upgraded(false)
{
}

IceSSL::TransceiverI::~TransceiverI()
{
}

#endif