5 // Created by Martin Baulig on 14/11/15.
6 // Copyright (c) 2015 Xamarin. All rights reserved.
15 MonoBtlsReadFunc read_func
;
16 MonoBtlsWriteFunc write_func
;
17 MonoBtlsControlFunc control_func
;
22 mono_debug (const char *message
)
25 bio_err
= BIO_new_fp (stderr
, BIO_NOCLOSE
);
26 fprintf (stderr
, "DEBUG: %s\n", message
);
27 ERR_print_errors (bio_err
);
32 mono_read (BIO
*bio
, char *out
, int outl
)
34 MonoBtlsBio
*mono
= (MonoBtlsBio
*)bio
->ptr
;
40 ret
= mono
->read_func (mono
->instance
, out
, outl
, &wantMore
);
51 BIO_set_retry_read (bio
);
59 mono_write (BIO
*bio
, const char *in
, int inl
)
61 MonoBtlsBio
*mono
= (MonoBtlsBio
*)bio
->ptr
;
66 return mono
->write_func (mono
->instance
, in
, inl
);
70 mono_ctrl (BIO
*bio
, int cmd
, long num
, void *ptr
)
72 MonoBtlsBio
*mono
= (MonoBtlsBio
*)bio
->ptr
;
77 // fprintf (stderr, "mono_ctrl: %x - %lx - %p\n", cmd, num, ptr);
80 return mono
->control_func (mono
->instance
, MONO_BTLS_CONTROL_COMMAND_FLUSH
, 0);
90 // mono_debug("mono_new!\n");
100 // mono_debug ("mono_free!\n");
102 MonoBtlsBio
*mono
= (MonoBtlsBio
*)bio
->ptr
;
105 mono
->instance
= NULL
;
106 mono
->read_func
= NULL
;
107 mono
->write_func
= NULL
;
108 mono
->control_func
= NULL
;
114 static const BIO_METHOD mono_method
= {
115 BIO_TYPE_NONE
, "mono", mono_write
, mono_read
,
116 NULL
, NULL
, mono_ctrl
, mono_new
, mono_free
, NULL
120 mono_btls_bio_mono_new (void)
123 MonoBtlsBio
*monoBio
;
125 bio
= BIO_new (&mono_method
);
129 monoBio
= calloc (1, sizeof (MonoBtlsBio
));
142 mono_btls_bio_mono_initialize (BIO
*bio
, const void *instance
,
143 MonoBtlsReadFunc read_func
, MonoBtlsWriteFunc write_func
,
144 MonoBtlsControlFunc control_func
)
146 MonoBtlsBio
*monoBio
= bio
->ptr
;
148 monoBio
->instance
= instance
;
149 monoBio
->read_func
= read_func
;
150 monoBio
->write_func
= write_func
;
151 monoBio
->control_func
= control_func
;
157 mono_btls_bio_read (BIO
*bio
, void *data
, int len
)
159 return BIO_read (bio
, data
, len
);
163 mono_btls_bio_write (BIO
*bio
, const void *data
, int len
)
165 return BIO_write (bio
, data
, len
);
169 mono_btls_bio_flush (BIO
*bio
)
171 return BIO_flush (bio
);
175 mono_btls_bio_indent (BIO
*bio
, unsigned indent
, unsigned max_indent
)
177 return BIO_indent (bio
, indent
, max_indent
);
181 mono_btls_bio_hexdump (BIO
*bio
, const uint8_t *data
, int len
, unsigned indent
)
183 return BIO_hexdump (bio
, data
, len
, indent
);
187 mono_btls_bio_print_errors (BIO
*bio
)
189 BIO_print_errors (bio
);
193 mono_btls_bio_free (BIO
*bio
)
199 mono_btls_bio_mem_new (void)
201 return BIO_new (BIO_s_mem ());
205 mono_btls_bio_mem_get_data (BIO
*bio
, void **data
)
207 return (int)BIO_get_mem_data (bio
, (char**)data
);