5 // Created by Martin Baulig on 14/11/15.
6 // Copyright (c) 2015 Xamarin. All rights reserved.
11 #include "../utils/mono-errno.h"
16 MonoBtlsReadFunc read_func
;
17 MonoBtlsWriteFunc write_func
;
18 MonoBtlsControlFunc control_func
;
23 mono_debug (const char *message
)
26 bio_err
= BIO_new_fp (stderr
, BIO_NOCLOSE
);
27 fprintf (stderr
, "DEBUG: %s\n", message
);
28 ERR_print_errors (bio_err
);
33 mono_read (BIO
*bio
, char *out
, int outl
)
35 MonoBtlsBio
*mono
= (MonoBtlsBio
*)bio
->ptr
;
41 ret
= mono
->read_func (mono
->instance
, out
, outl
, &wantMore
);
51 mono_set_errno (EAGAIN
);
52 BIO_set_retry_read (bio
);
60 mono_write (BIO
*bio
, const char *in
, int inl
)
62 MonoBtlsBio
*mono
= (MonoBtlsBio
*)bio
->ptr
;
67 return mono
->write_func (mono
->instance
, in
, inl
);
71 mono_ctrl (BIO
*bio
, int cmd
, long num
, void *ptr
)
73 MonoBtlsBio
*mono
= (MonoBtlsBio
*)bio
->ptr
;
78 // fprintf (stderr, "mono_ctrl: %x - %lx - %p\n", cmd, num, ptr);
81 return (long)mono
->control_func (mono
->instance
, MONO_BTLS_CONTROL_COMMAND_FLUSH
, 0);
91 // mono_debug("mono_new!\n");
101 // mono_debug ("mono_free!\n");
103 MonoBtlsBio
*mono
= (MonoBtlsBio
*)bio
->ptr
;
106 mono
->instance
= NULL
;
107 mono
->read_func
= NULL
;
108 mono
->write_func
= NULL
;
109 mono
->control_func
= NULL
;
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
121 mono_btls_bio_mono_new (void)
124 MonoBtlsBio
*monoBio
;
126 bio
= BIO_new (&mono_method
);
130 monoBio
= calloc (1, sizeof (MonoBtlsBio
));
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
;
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
);
188 mono_btls_bio_print_errors (BIO
*bio
)
190 BIO_print_errors (bio
);
194 mono_btls_bio_free (BIO
*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
);