summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/src/tls_service_v1.cpp
blob: 8746a767d7ddde54fc388c3c529b6ff58b59db83 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (C) 2020 Codership Oy <info@codership.com>
 *
 * This file is part of wsrep-lib.
 *
 * Wsrep-lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Wsrep-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "tls_service_v1.hpp"

#include "wsrep/tls_service.hpp"
#include "wsrep/logger.hpp"
#include "v26/wsrep_tls_service.h"
#include "service_helpers.hpp"

#include <cassert>

namespace wsrep_tls_service_v1
{
    static wsrep::tls_service* tls_service_impl{0};
    static std::atomic<size_t> use_count;

    static int tls_stream_init_cb(
        wsrep_tls_context_t*,
        wsrep_tls_stream_t* stream)
    {
        assert(tls_service_impl);
        stream->opaque =
            tls_service_impl->create_tls_stream(stream->fd);
        if (not stream->opaque)
        {
            return ENOMEM;
        }
        return 0;
    }

    static void tls_stream_deinit_cb(
        wsrep_tls_context_t*,
        wsrep_tls_stream_t* stream)
    {
        assert(tls_service_impl);
        tls_service_impl->destroy(
            reinterpret_cast<wsrep::tls_stream*>(stream->opaque));
    }

    static int tls_stream_get_error_number_cb(
        wsrep_tls_context_t*,
        const wsrep_tls_stream_t* stream)
    {
        assert(tls_service_impl);
        return tls_service_impl->get_error_number(
            reinterpret_cast<const wsrep::tls_stream*>(stream->opaque));
    }

    static const void* tls_stream_get_error_category_cb(
        wsrep_tls_context_t*,
        const wsrep_tls_stream_t* stream)
    {
        assert(tls_service_impl);
        return tls_service_impl->get_error_category(
            reinterpret_cast<const wsrep::tls_stream*>(stream->opaque));
    }

    static const char* tls_error_message_get_cb(
        wsrep_tls_context_t*,
        const wsrep_tls_stream_t* stream,
        int value, const void* category)
    {
        assert(tls_service_impl);
        return tls_service_impl->get_error_message(
            reinterpret_cast<const wsrep::tls_stream*>(stream->opaque), value, category);
    }

    static enum wsrep_tls_result map_return_value(ssize_t status)
    {
        switch (status)
        {
        case wsrep::tls_service::success:
            return wsrep_tls_result_success;
        case wsrep::tls_service::want_read:
            return wsrep_tls_result_want_read;
        case wsrep::tls_service::want_write:
            return wsrep_tls_result_want_write;
        case wsrep::tls_service::eof:
            return wsrep_tls_result_eof;
        case wsrep::tls_service::error:
            return wsrep_tls_result_error;
        default:
            assert(status < 0);
            return wsrep_tls_result_error;
        }
    }


    static enum wsrep_tls_result
    tls_stream_client_handshake_cb(
        wsrep_tls_context_t*,
        wsrep_tls_stream_t* stream)
    {
        assert(tls_service_impl);
        return map_return_value(
            tls_service_impl->client_handshake(
                reinterpret_cast<wsrep::tls_stream*>(stream->opaque)));
    }

    static enum wsrep_tls_result
    tls_stream_server_handshake_cb(
        wsrep_tls_context_t*,
        wsrep_tls_stream_t* stream)
    {
        assert(tls_service_impl);
        return map_return_value(
            tls_service_impl->server_handshake(
                reinterpret_cast<wsrep::tls_stream*>(stream->opaque)));
    }

    static enum wsrep_tls_result tls_stream_read_cb(
        wsrep_tls_context_t*,
        wsrep_tls_stream_t* stream,
        void* buf,
        size_t max_count,
        size_t* bytes_transferred)
    {
        assert(tls_service_impl);
        auto result(tls_service_impl->read(
                        reinterpret_cast<wsrep::tls_stream*>(stream->opaque),
                        buf, max_count));
        *bytes_transferred = result.bytes_transferred;
        return map_return_value(result.status);
    }

    static enum wsrep_tls_result tls_stream_write_cb(
        wsrep_tls_context_t*,
        wsrep_tls_stream_t* stream,
        const void* buf,
        size_t count,
        size_t* bytes_transferred)
    {
        assert(tls_service_impl);
        auto result(tls_service_impl->write(
                        reinterpret_cast<wsrep::tls_stream*>(stream->opaque),
                        buf, count));
        *bytes_transferred = result.bytes_transferred;
        return map_return_value(result.status);
    }

    static enum wsrep_tls_result
    tls_stream_shutdown_cb(wsrep_tls_context_t*,
                           wsrep_tls_stream_t* stream)
    {
        assert(tls_service_impl);
        // @todo Handle other values than success.
        return map_return_value(
            tls_service_impl->shutdown(
                reinterpret_cast<wsrep::tls_stream*>(stream->opaque)));
    }

    static wsrep_tls_service_v1_t tls_service_callbacks =
    {
        tls_stream_init_cb,
        tls_stream_deinit_cb,
        tls_stream_get_error_number_cb,
        tls_stream_get_error_category_cb,
        tls_stream_client_handshake_cb,
        tls_stream_server_handshake_cb,
        tls_stream_read_cb,
        tls_stream_write_cb,
        tls_stream_shutdown_cb,
        tls_error_message_get_cb,
        0 // we pass NULL context for now.
    };
}

int wsrep::tls_service_v1_probe(void* dlh)
{
    typedef int (*init_fn)(wsrep_tls_service_v1_t*);
    typedef void (*deinit_fn)();
    if (wsrep_impl::service_probe<init_fn>(
            dlh, WSREP_TLS_SERVICE_INIT_FUNC_V1, "tls service v1") ||
        wsrep_impl::service_probe<deinit_fn>(
            dlh, WSREP_TLS_SERVICE_DEINIT_FUNC_V1, "tls service v1"))
    {
        wsrep::log_warning() << "Provider does not support tls service v1";
        return 1;
    }
    return 0;
}

int wsrep::tls_service_v1_init(void* dlh,
                               wsrep::tls_service* tls_service)
{
    if (not (dlh && tls_service)) return EINVAL;

    typedef int (*init_fn)(wsrep_tls_service_v1_t*);
    wsrep_tls_service_v1::tls_service_impl = tls_service;
    int ret(0);
    if ((ret = wsrep_impl::service_init<init_fn>(
             dlh, WSREP_TLS_SERVICE_INIT_FUNC_V1,
             &wsrep_tls_service_v1::tls_service_callbacks,
             "tls service v1")))
    {
        wsrep_tls_service_v1::tls_service_impl = 0;
    }
    else
    {
        ++wsrep_tls_service_v1::use_count;
    }
    return ret;
}

void wsrep::tls_service_v1_deinit(void* dlh)
{
    typedef int (*deinit_fn)();
    wsrep_impl::service_deinit<deinit_fn>(
        dlh, WSREP_TLS_SERVICE_DEINIT_FUNC_V1, "tls service v1");
    --wsrep_tls_service_v1::use_count;
    if (wsrep_tls_service_v1::use_count == 0)
    {
        wsrep_tls_service_v1::tls_service_impl = 0;
    }
}