3 #include <libubox/ustream.h>
5 #include "ustream-io.h"
6 #include "ustream-ssl.h"
8 #ifdef CYASSL_OPENSSL_H_
10 /* not defined in the header file */
11 typedef int (*CallbackIORecv
)(char *buf
, int sz
, void *ctx
);
12 typedef int (*CallbackIOSend
)(char *buf
, int sz
, void *ctx
);
14 void SetCallbackIORecv_Ctx(SSL_CTX
*, CallbackIORecv
);
15 void SetCallbackIOSend_Ctx(SSL_CTX
*, CallbackIOSend
);
16 void SetCallbackIO_ReadCtx(SSL
* ssl
, void *rctx
);
17 void SetCallbackIO_WriteCtx(SSL
* ssl
, void *wctx
);
19 static int s_ustream_read(char *buf
, int len
, void *ctx
)
21 struct ustream
*s
= ctx
;
28 sbuf
= ustream_get_read_buf(s
, &slen
);
35 memcpy(buf
, sbuf
, slen
);
36 ustream_consume(s
, slen
);
41 static int s_ustream_write(char *buf
, int len
, void *ctx
)
43 struct ustream
*s
= ctx
;
45 return ustream_write(s
, buf
, len
, false);
48 void ustream_set_io(SSL_CTX
*ctx
, SSL
*ssl
, struct ustream
*conn
)
50 SetCallbackIO_ReadCtx(ssl
, conn
);
51 SetCallbackIO_WriteCtx(ssl
, conn
);
52 SetCallbackIORecv_Ctx(ctx
, s_ustream_read
);
53 SetCallbackIOSend_Ctx(ctx
, s_ustream_write
);
69 s_ustream_free(BIO
*b
)
81 s_ustream_read(BIO
*b
, char *buf
, int len
)
90 s
= (struct ustream
*)b
->ptr
;
94 sbuf
= ustream_get_read_buf(s
, &slen
);
96 BIO_clear_retry_flags(b
);
98 BIO_set_retry_read(b
);
105 memcpy(buf
, sbuf
, slen
);
106 ustream_consume(s
, slen
);
112 s_ustream_write(BIO
*b
, const char *buf
, int len
)
116 if (!buf
|| len
<= 0)
119 s
= (struct ustream
*)b
->ptr
;
123 return ustream_write(s
, buf
, len
, false);
127 s_ustream_gets(BIO
*b
, char *buf
, int len
)
133 s_ustream_puts(BIO
*b
, const char *str
)
135 return s_ustream_write(b
, str
, strlen(str
));
138 static long s_ustream_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
148 static BIO_METHOD methods_ustream
= {
149 100 | BIO_TYPE_SOURCE_SINK
,
161 static BIO
*ustream_bio_new(struct ustream
*s
)
165 bio
= BIO_new(&methods_ustream
);
170 void ustream_set_io(SSL_CTX
*ctx
, SSL
*ssl
, struct ustream
*conn
)
172 BIO
*bio
= ustream_bio_new(conn
);
173 SSL_set_bio(ssl
, bio
, bio
);