avoid installing internal header files
[project/ustream-ssl.git] / ustream-io-openssl.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
27 s_ustream_new(BIO *b)
28 {
29 b->init = 1;
30 b->num = 0;
31 b->ptr = NULL;
32 b->flags = 0;
33 return 1;
34 }
35
36 static int
37 s_ustream_free(BIO *b)
38 {
39 if (!b)
40 return 0;
41
42 b->ptr = NULL;
43 b->init = 0;
44 b->flags = 0;
45 return 1;
46 }
47
48 static int
49 s_ustream_read(BIO *b, char *buf, int len)
50 {
51 struct ustream *s;
52 char *sbuf;
53 int slen;
54
55 if (!buf || len <= 0)
56 return 0;
57
58 s = (struct ustream *)b->ptr;
59 if (!s)
60 return 0;
61
62 sbuf = ustream_get_read_buf(s, &slen);
63
64 BIO_clear_retry_flags(b);
65 if (!slen) {
66 BIO_set_retry_read(b);
67 return -1;
68 }
69
70 if (slen > len)
71 slen = len;
72
73 memcpy(buf, sbuf, slen);
74 ustream_consume(s, slen);
75
76 return slen;
77 }
78
79 static int
80 s_ustream_write(BIO *b, const char *buf, int len)
81 {
82 struct ustream *s;
83
84 if (!buf || len <= 0)
85 return 0;
86
87 s = (struct ustream *)b->ptr;
88 if (!s)
89 return 0;
90
91 if (s->write_error)
92 return len;
93
94 return ustream_write(s, buf, len, false);
95 }
96
97 static int
98 s_ustream_gets(BIO *b, char *buf, int len)
99 {
100 return -1;
101 }
102
103 static int
104 s_ustream_puts(BIO *b, const char *str)
105 {
106 return s_ustream_write(b, str, strlen(str));
107 }
108
109 static long s_ustream_ctrl(BIO *b, int cmd, long num, void *ptr)
110 {
111 switch (cmd) {
112 case BIO_CTRL_FLUSH:
113 return 1;
114 default:
115 return 0;
116 };
117 }
118
119 static BIO_METHOD methods_ustream = {
120 100 | BIO_TYPE_SOURCE_SINK,
121 "ustream",
122 s_ustream_write,
123 s_ustream_read,
124 s_ustream_puts,
125 s_ustream_gets,
126 s_ustream_ctrl,
127 s_ustream_new,
128 s_ustream_free,
129 NULL,
130 };
131
132 static BIO *ustream_bio_new(struct ustream *s)
133 {
134 BIO *bio;
135
136 bio = BIO_new(&methods_ustream);
137 bio->ptr = s;
138 return bio;
139 }
140
141 __hidden void ustream_set_io(void *ctx, void *ssl, struct ustream *conn)
142 {
143 BIO *bio = ustream_bio_new(conn);
144 SSL_set_bio(ssl, bio, bio);
145 }