2 * ustream-ssl - library for SSL over ustream
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <libubox/ustream.h>
22 #include "ustream-ssl.h"
23 #include "ustream-internal.h"
25 static void ustream_ssl_error_cb(struct uloop_timeout *t)
27 struct ustream_ssl *us = container_of(t, struct ustream_ssl, error_timer);
28 static char buffer[128];
29 int error = us->error;
32 us->notify_error(us, error, __ustream_ssl_strerror(us->error, buffer, sizeof(buffer)));
35 static void ustream_ssl_check_conn(struct ustream_ssl *us)
37 if (us->connected || us->error)
40 if (__ustream_ssl_connect(us) == U_SSL_OK) {
42 if (us->notify_connected)
43 us->notify_connected(us);
44 ustream_write_pending(&us->stream);
48 static bool __ustream_ssl_poll(struct ustream *s)
50 struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);
55 ustream_ssl_check_conn(us);
56 if (!us->connected || us->error)
60 buf = ustream_reserve(&us->stream, 1, &len);
64 ret = __ustream_ssl_read(us, buf, len);
71 us->stream.eof = true;
72 ustream_state_change(&us->stream);
75 ustream_fill_read(&us->stream, ret);
84 static void ustream_ssl_notify_read(struct ustream *s, int bytes)
86 __ustream_ssl_poll(s);
89 static void ustream_ssl_notify_write(struct ustream *s, int bytes)
91 struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);
93 ustream_ssl_check_conn(us);
94 ustream_write_pending(s->next);
97 static void ustream_ssl_notify_state(struct ustream *s)
99 s->next->write_error = true;
100 ustream_state_change(s->next);
103 static int ustream_ssl_write(struct ustream *s, const char *buf, int len, bool more)
105 struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
107 if (!us->connected || us->error)
110 if (us->conn->w.data_bytes)
113 return __ustream_ssl_write(us, buf, len);
116 static void ustream_ssl_set_read_blocked(struct ustream *s)
118 struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
120 ustream_set_read_blocked(us->conn, !!s->read_blocked);
123 static void ustream_ssl_free(struct ustream *s)
125 struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
128 us->conn->next = NULL;
129 us->conn->notify_read = NULL;
130 us->conn->notify_write = NULL;
131 us->conn->notify_state = NULL;
134 uloop_timeout_cancel(&us->error_timer);
135 __ustream_ssl_session_free(us->ssl);
139 us->connected = false;
143 static bool ustream_ssl_poll(struct ustream *s)
145 struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
148 fd_poll = ustream_poll(us->conn);
149 return __ustream_ssl_poll(s) || fd_poll;
152 static void ustream_ssl_stream_init(struct ustream_ssl *us)
154 struct ustream *conn = us->conn;
155 struct ustream *s = &us->stream;
157 conn->notify_read = ustream_ssl_notify_read;
158 conn->notify_write = ustream_ssl_notify_write;
159 conn->notify_state = ustream_ssl_notify_state;
161 s->free = ustream_ssl_free;
162 s->write = ustream_ssl_write;
163 s->poll = ustream_ssl_poll;
164 s->set_read_blocked = ustream_ssl_set_read_blocked;
165 ustream_init_defaults(s);
168 static int _ustream_ssl_init(struct ustream_ssl *us, struct ustream *conn, struct ustream_ssl_ctx *ctx, bool server)
170 us->error_timer.cb = ustream_ssl_error_cb;
175 us->ssl = __ustream_ssl_session_new(us->ctx);
179 conn->next = &us->stream;
180 ustream_set_io(ctx, us->ssl, conn);
181 ustream_ssl_stream_init(us);
182 ustream_ssl_check_conn(us);
187 const struct ustream_ssl_ops ustream_ssl_ops = {
188 .context_new = __ustream_ssl_context_new,
189 .context_set_crt_file = __ustream_ssl_set_crt_file,
190 .context_set_key_file = __ustream_ssl_set_key_file,
191 .context_free = __ustream_ssl_context_free,
192 .init = _ustream_ssl_init,