ustream-ssl: mbedtls: fix net_sockets.h include warning
[project/ustream-ssl.git] / ustream-io-wolfssl.c
1 /*
2 * ustream-ssl - library for SSL over ustream
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #include <string.h>
20
21 #include <libubox/ustream.h>
22
23 #include "ustream-ssl.h"
24 #include "ustream-internal.h"
25
26 static int s_ustream_read(char *buf, int len, void *ctx)
27 {
28 struct ustream *s = ctx;
29 char *sbuf;
30 int slen;
31
32 if (s->eof)
33 return -3;
34
35 sbuf = ustream_get_read_buf(s, &slen);
36 if (slen > len)
37 slen = len;
38
39 if (!slen)
40 return -2;
41
42 memcpy(buf, sbuf, slen);
43 ustream_consume(s, slen);
44
45 return slen;
46 }
47
48 static int s_ustream_write(char *buf, int len, void *ctx)
49 {
50 struct ustream *s = ctx;
51
52 if (s->write_error)
53 return len;
54
55 return ustream_write(s, buf, len, false);
56 }
57
58 static int io_recv_cb(SSL* ssl, char *buf, int sz, void *ctx)
59 {
60 return s_ustream_read(buf, sz, ctx);
61 }
62
63 static int io_send_cb(SSL* ssl, char *buf, int sz, void *ctx)
64 {
65 return s_ustream_write(buf, sz, ctx);
66 }
67
68 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
69 {
70 #ifndef NO_WOLFSSL_SSLSETIO_SEND_RECV
71 wolfSSL_SSLSetIORecv(ssl, io_recv_cb);
72 wolfSSL_SSLSetIOSend(ssl, io_send_cb);
73 #else
74 wolfSSL_SetIORecv((void *) ctx, io_recv_cb);
75 wolfSSL_SetIOSend((void *) ctx, io_send_cb);
76 if (ssl == NULL)
77 return;
78 #endif
79 wolfSSL_SetIOReadCtx(ssl, conn);
80 wolfSSL_SetIOWriteCtx(ssl, conn);
81 }