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
|
/*
* ustream-ssl - library for SSL over ustream
*
* Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <libubox/ustream.h>
#include "ustream-ssl.h"
#include "ustream-internal.h"
static void ustream_ssl_error_cb(struct uloop_timeout *t)
{
struct ustream_ssl *us = container_of(t, struct ustream_ssl, error_timer);
static char buffer[128];
int error = us->error;
if (us->notify_error)
us->notify_error(us, error, __ustream_ssl_strerror(us->error, buffer, sizeof(buffer)));
}
static void ustream_ssl_check_conn(struct ustream_ssl *us)
{
if (us->connected || us->error)
return;
if (__ustream_ssl_connect(us) == U_SSL_OK) {
us->connected = true;
if (us->notify_connected)
us->notify_connected(us);
}
}
static bool __ustream_ssl_poll(struct ustream *s)
{
struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);
char *buf;
int len, ret;
bool more = false;
ustream_ssl_check_conn(us);
if (!us->connected || us->error)
return false;
do {
buf = ustream_reserve(&us->stream, 1, &len);
if (!len)
break;
ret = __ustream_ssl_read(us, buf, len);
switch (ret) {
case U_SSL_PENDING:
return more;
case U_SSL_ERROR:
return false;
case 0:
us->stream.eof = true;
ustream_state_change(&us->stream);
return false;
default:
ustream_fill_read(&us->stream, ret);
more = true;
continue;
}
} while (1);
return more;
}
static void ustream_ssl_notify_read(struct ustream *s, int bytes)
{
__ustream_ssl_poll(s);
}
static void ustream_ssl_notify_write(struct ustream *s, int bytes)
{
struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);
ustream_ssl_check_conn(us);
ustream_write_pending(s->next);
}
static void ustream_ssl_notify_state(struct ustream *s)
{
s->next->write_error = true;
ustream_state_change(s->next);
}
static int ustream_ssl_write(struct ustream *s, const char *buf, int len, bool more)
{
struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
if (!us->connected || us->error)
return 0;
if (us->conn->w.data_bytes)
return 0;
return __ustream_ssl_write(us, buf, len);
}
static void ustream_ssl_set_read_blocked(struct ustream *s)
{
struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
ustream_set_read_blocked(us->conn, !!s->read_blocked);
}
static void ustream_ssl_free(struct ustream *s)
{
struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
if (us->conn) {
us->conn->next = NULL;
us->conn->notify_read = NULL;
us->conn->notify_write = NULL;
us->conn->notify_state = NULL;
}
uloop_timeout_cancel(&us->error_timer);
__ustream_ssl_session_free(us->ssl);
us->ctx = NULL;
us->ssl = NULL;
us->conn = NULL;
us->connected = false;
us->error = false;
}
static bool ustream_ssl_poll(struct ustream *s)
{
struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
bool fd_poll;
fd_poll = ustream_poll(us->conn);
return __ustream_ssl_poll(s) || fd_poll;
}
static void ustream_ssl_stream_init(struct ustream_ssl *us)
{
struct ustream *conn = us->conn;
struct ustream *s = &us->stream;
conn->notify_read = ustream_ssl_notify_read;
conn->notify_write = ustream_ssl_notify_write;
conn->notify_state = ustream_ssl_notify_state;
s->free = ustream_ssl_free;
s->write = ustream_ssl_write;
s->poll = ustream_ssl_poll;
s->set_read_blocked = ustream_ssl_set_read_blocked;
ustream_init_defaults(s);
}
static int _ustream_ssl_init(struct ustream_ssl *us, struct ustream *conn, void *ctx, bool server)
{
us->error_timer.cb = ustream_ssl_error_cb;
us->server = server;
us->conn = conn;
us->ctx = ctx;
us->ssl = __ustream_ssl_session_new(us->ctx);
if (!us->ssl)
return -ENOMEM;
conn->next = &us->stream;
ustream_set_io(ctx, us->ssl, conn);
ustream_ssl_stream_init(us);
return 0;
}
const struct ustream_ssl_ops ustream_ssl_ops = {
.context_new = __ustream_ssl_context_new,
.context_set_crt_file = __ustream_ssl_set_crt_file,
.context_set_key_file = __ustream_ssl_set_key_file,
.context_free = __ustream_ssl_context_free,
.init = _ustream_ssl_init,
};
|