give the main context a type instead of making it void *
[project/ustream-ssl.git] / ustream-io-cyassl.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 #ifdef HAVE_CYASSL_VERSION_H
27 #include <cyassl/version.h>
28 #else
29 #define LIBCYASSL_VERSION_HEX 0
30 #endif
31
32 static int s_ustream_read(char *buf, int len, void *ctx)
33 {
34         struct ustream *s = ctx;
35         char *sbuf;
36         int slen;
37
38         if (s->eof)
39                 return -3;
40
41         sbuf = ustream_get_read_buf(s, &slen);
42         if (slen > len)
43                 slen = len;
44
45         if (!slen)
46                 return -2;
47
48         memcpy(buf, sbuf, slen);
49         ustream_consume(s, slen);
50
51         return slen;
52 }
53
54 static int s_ustream_write(char *buf, int len, void *ctx)
55 {
56         struct ustream *s = ctx;
57
58         if (s->write_error)
59                 return len;
60
61         return ustream_write(s, buf, len, false);
62 }
63
64 #if (LIBCYASSL_VERSION_HEX > 0)
65 static int io_recv_cb(SSL* ssl, char *buf, int sz, void *ctx)
66 {
67         return s_ustream_read(buf, sz, ctx);
68 }
69
70 static int io_send_cb(SSL* ssl, char *buf, int sz, void *ctx)
71 {
72         return s_ustream_write(buf, sz, ctx);
73 }
74 #else
75 /* not defined in the header file */
76 typedef int (*CallbackIORecv)(char *buf, int sz, void *ctx);
77 typedef int (*CallbackIOSend)(char *buf, int sz, void *ctx);
78
79 void SetCallbackIORecv_Ctx(SSL_CTX*, CallbackIORecv);
80 void SetCallbackIOSend_Ctx(SSL_CTX*, CallbackIOSend);
81 void SetCallbackIO_ReadCtx(SSL* ssl, void *rctx);
82 void SetCallbackIO_WriteCtx(SSL* ssl, void *wctx);
83
84 #define CyaSSL_SetIOReadCtx SetCallbackIO_ReadCtx
85 #define CyaSSL_SetIOWriteCtx SetCallbackIO_WriteCtx
86 #define CyaSSL_SetIORecv SetCallbackIORecv_Ctx
87 #define CyaSSL_SetIOSend SetCallbackIOSend_Ctx
88
89 static int io_recv_cb(char *buf, int sz, void *ctx)
90 {
91         return s_ustream_read(buf, sz, ctx);
92 }
93
94 static int io_send_cb(char *buf, int sz, void *ctx)
95 {
96         return s_ustream_write(buf, sz, ctx);
97 }
98 #endif
99
100 __hidden void ustream_set_io(void *ctx, void *ssl, struct ustream *conn)
101 {
102         CyaSSL_SetIOReadCtx(ssl, conn);
103         CyaSSL_SetIOWriteCtx(ssl, conn);
104         CyaSSL_SetIORecv(ctx, io_recv_cb);
105         CyaSSL_SetIOSend(ctx, io_send_cb);
106 }