[sdks] Add linker sources to android sdks archive (#14686)
[mono-project.git] / mono / btls / btls-bio.c
blob60259d912c2658df8fe6390d71c9e45fa0a74325
1 //
2 // btls-bio.c
3 // MonoBtls
4 //
5 // Created by Martin Baulig on 14/11/15.
6 // Copyright (c) 2015 Xamarin. All rights reserved.
7 //
9 #include "btls-ssl.h"
10 #include "btls-bio.h"
11 #include "../utils/mono-errno.h"
12 #include <errno.h>
14 struct MonoBtlsBio {
15 const void *instance;
16 MonoBtlsReadFunc read_func;
17 MonoBtlsWriteFunc write_func;
18 MonoBtlsControlFunc control_func;
21 #if 0
22 static void
23 mono_debug (const char *message)
25 BIO *bio_err;
26 bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);
27 fprintf (stderr, "DEBUG: %s\n", message);
28 ERR_print_errors (bio_err);
30 #endif
32 static int
33 mono_read (BIO *bio, char *out, int outl)
35 MonoBtlsBio *mono = (MonoBtlsBio *)bio->ptr;
36 int ret, wantMore;
38 if (!mono)
39 return -1;
41 ret = mono->read_func (mono->instance, out, outl, &wantMore);
43 if (ret < 0) {
44 mono_set_errno (EIO);
45 return -1;
47 if (ret > 0)
48 return ret;
50 if (wantMore) {
51 mono_set_errno (EAGAIN);
52 BIO_set_retry_read (bio);
53 return -1;
56 return 0;
59 static int
60 mono_write (BIO *bio, const char *in, int inl)
62 MonoBtlsBio *mono = (MonoBtlsBio *)bio->ptr;
64 if (!mono)
65 return -1;
67 return mono->write_func (mono->instance, in, inl);
70 static long
71 mono_ctrl (BIO *bio, int cmd, long num, void *ptr)
73 MonoBtlsBio *mono = (MonoBtlsBio *)bio->ptr;
75 if (!mono)
76 return -1;
78 // fprintf (stderr, "mono_ctrl: %x - %lx - %p\n", cmd, num, ptr);
79 switch (cmd) {
80 case BIO_CTRL_FLUSH:
81 return (long)mono->control_func (mono->instance, MONO_BTLS_CONTROL_COMMAND_FLUSH, 0);
82 default:
83 return -1;
85 return -1;
88 static int
89 mono_new (BIO *bio)
91 // mono_debug("mono_new!\n");
92 bio->init = 0;
93 bio->num = -1;
94 bio->flags = 0;
95 return 1;
98 static int
99 mono_free (BIO *bio)
101 // mono_debug ("mono_free!\n");
102 if (bio->ptr) {
103 MonoBtlsBio *mono = (MonoBtlsBio *)bio->ptr;
105 bio->ptr = NULL;
106 mono->instance = NULL;
107 mono->read_func = NULL;
108 mono->write_func = NULL;
109 mono->control_func = NULL;
110 free (mono);
112 return 1;
115 static const BIO_METHOD mono_method = {
116 BIO_TYPE_NONE, "mono", mono_write, mono_read,
117 NULL, NULL, mono_ctrl, mono_new, mono_free, NULL
120 BIO *
121 mono_btls_bio_mono_new (void)
123 BIO *bio;
124 MonoBtlsBio *monoBio;
126 bio = BIO_new (&mono_method);
127 if (!bio)
128 return NULL;
130 monoBio = calloc (1, sizeof (MonoBtlsBio));
131 if (!monoBio) {
132 BIO_free (bio);
133 return NULL;
136 bio->ptr = monoBio;
137 bio->init = 0;
139 return bio;
142 void
143 mono_btls_bio_mono_initialize (BIO *bio, const void *instance,
144 MonoBtlsReadFunc read_func, MonoBtlsWriteFunc write_func,
145 MonoBtlsControlFunc control_func)
147 MonoBtlsBio *monoBio = bio->ptr;
149 monoBio->instance = instance;
150 monoBio->read_func = read_func;
151 monoBio->write_func = write_func;
152 monoBio->control_func = control_func;
154 bio->init = 1;
158 mono_btls_bio_read (BIO *bio, void *data, int len)
160 return BIO_read (bio, data, len);
164 mono_btls_bio_write (BIO *bio, const void *data, int len)
166 return BIO_write (bio, data, len);
170 mono_btls_bio_flush (BIO *bio)
172 return BIO_flush (bio);
176 mono_btls_bio_indent (BIO *bio, unsigned indent, unsigned max_indent)
178 return BIO_indent (bio, indent, max_indent);
182 mono_btls_bio_hexdump (BIO *bio, const uint8_t *data, int len, unsigned indent)
184 return BIO_hexdump (bio, data, len, indent);
187 void
188 mono_btls_bio_print_errors (BIO *bio)
190 BIO_print_errors (bio);
193 void
194 mono_btls_bio_free (BIO *bio)
196 BIO_free (bio);
199 BIO *
200 mono_btls_bio_mem_new (void)
202 return BIO_new (BIO_s_mem ());
206 mono_btls_bio_mem_get_data (BIO *bio, void **data)
208 return (int)BIO_get_mem_data (bio, (char**)data);