remove commented out include/link directories
[project/ustream-ssl.git] / ustream-io.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-io.h"
24 #include "ustream-ssl.h"
25
26 #ifdef CYASSL_OPENSSL_H_
27
28 /* not defined in the header file */
29 typedef int (*CallbackIORecv)(char *buf, int sz, void *ctx);
30 typedef int (*CallbackIOSend)(char *buf, int sz, void *ctx);
31
32 void SetCallbackIORecv_Ctx(SSL_CTX*, CallbackIORecv);
33 void SetCallbackIOSend_Ctx(SSL_CTX*, CallbackIOSend);
34 void SetCallbackIO_ReadCtx(SSL* ssl, void *rctx);
35 void SetCallbackIO_WriteCtx(SSL* ssl, void *wctx);
36
37 static int s_ustream_read(char *buf, int len, void *ctx)
38 {
39 struct ustream *s = ctx;
40 char *sbuf;
41 int slen;
42
43 if (s->eof)
44 return -3;
45
46 sbuf = ustream_get_read_buf(s, &slen);
47 if (slen > len)
48 slen = len;
49
50 if (!slen)
51 return -2;
52
53 memcpy(buf, sbuf, slen);
54 ustream_consume(s, slen);
55
56 return slen;
57 }
58
59 static int s_ustream_write(char *buf, int len, void *ctx)
60 {
61 struct ustream *s = ctx;
62
63 return ustream_write(s, buf, len, false);
64 }
65
66 void ustream_set_io(SSL_CTX *ctx, SSL *ssl, struct ustream *conn)
67 {
68 SetCallbackIO_ReadCtx(ssl, conn);
69 SetCallbackIO_WriteCtx(ssl, conn);
70 SetCallbackIORecv_Ctx(ctx, s_ustream_read);
71 SetCallbackIOSend_Ctx(ctx, s_ustream_write);
72 }
73
74 #else
75
76 static int
77 s_ustream_new(BIO *b)
78 {
79 b->init = 1;
80 b->num = 0;
81 b->ptr = NULL;
82 b->flags = 0;
83 return 1;
84 }
85
86 static int
87 s_ustream_free(BIO *b)
88 {
89 if (!b)
90 return 0;
91
92 b->ptr = NULL;
93 b->init = 0;
94 b->flags = 0;
95 return 1;
96 }
97
98 static int
99 s_ustream_read(BIO *b, char *buf, int len)
100 {
101 struct ustream *s;
102 char *sbuf;
103 int slen;
104
105 if (!buf || len <= 0)
106 return 0;
107
108 s = (struct ustream *)b->ptr;
109 if (!s)
110 return 0;
111
112 sbuf = ustream_get_read_buf(s, &slen);
113
114 BIO_clear_retry_flags(b);
115 if (!slen) {
116 BIO_set_retry_read(b);
117 return -1;
118 }
119
120 if (slen > len)
121 slen = len;
122
123 memcpy(buf, sbuf, slen);
124 ustream_consume(s, slen);
125
126 return slen;
127 }
128
129 static int
130 s_ustream_write(BIO *b, const char *buf, int len)
131 {
132 struct ustream *s;
133
134 if (!buf || len <= 0)
135 return 0;
136
137 s = (struct ustream *)b->ptr;
138 if (!s)
139 return 0;
140
141 return ustream_write(s, buf, len, false);
142 }
143
144 static int
145 s_ustream_gets(BIO *b, char *buf, int len)
146 {
147 return -1;
148 }
149
150 static int
151 s_ustream_puts(BIO *b, const char *str)
152 {
153 return s_ustream_write(b, str, strlen(str));
154 }
155
156 static long s_ustream_ctrl(BIO *b, int cmd, long num, void *ptr)
157 {
158 switch (cmd) {
159 case BIO_CTRL_FLUSH:
160 return 1;
161 default:
162 return 0;
163 };
164 }
165
166 static BIO_METHOD methods_ustream = {
167 100 | BIO_TYPE_SOURCE_SINK,
168 "ustream",
169 s_ustream_write,
170 s_ustream_read,
171 s_ustream_puts,
172 s_ustream_gets,
173 s_ustream_ctrl,
174 s_ustream_new,
175 s_ustream_free,
176 NULL,
177 };
178
179 static BIO *ustream_bio_new(struct ustream *s)
180 {
181 BIO *bio;
182
183 bio = BIO_new(&methods_ustream);
184 bio->ptr = s;
185 return bio;
186 }
187
188 void ustream_set_io(SSL_CTX *ctx, SSL *ssl, struct ustream *conn)
189 {
190 BIO *bio = ustream_bio_new(conn);
191 SSL_set_bio(ssl, bio, bio);
192 }
193
194 #endif