1919#include "phenom/openssl.h"
2020#include <openssl/bio.h>
2121
22+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
23+ #define BIO_set_init (b , val ) (b)->init = (val)
24+ #define BIO_set_data (b , val ) (b)->ptr = (val)
25+ #define BIO_get_data (b ) (b)->ptr
26+ #endif
27+
2228/* Implements an OpenSSL BIO that invokes phenom streams */
2329
2430static bool should_retry (ph_stream_t * stm )
@@ -33,10 +39,17 @@ static bool should_retry(ph_stream_t *stm)
3339 }
3440}
3541
42+ static void bio_stm_clear (BIO * h )
43+ {
44+ BIO_set_init (h , 0 );
45+ BIO_set_data (h , NULL );
46+ BIO_clear_flags (h , ~0 );
47+ }
48+
3649static int bio_stm_write (BIO * h , const char * buf , int size )
3750{
3851 uint64_t nwrote ;
39- ph_stream_t * stm = h -> ptr ;
52+ ph_stream_t * stm = BIO_get_data ( h ) ;
4053
4154 if (buf == NULL || size == 0 || stm == NULL ) {
4255 return 0 ;
@@ -62,7 +75,7 @@ static int bio_stm_puts(BIO *h, const char *str)
6275static int bio_stm_read (BIO * h , char * buf , int size )
6376{
6477 uint64_t nread ;
65- ph_stream_t * stm = h -> ptr ;
78+ ph_stream_t * stm = BIO_get_data ( h ) ;
6679
6780 if (buf == NULL || size == 0 || stm == NULL ) {
6881 return 0 ;
@@ -83,7 +96,7 @@ static int bio_stm_read(BIO *h, char *buf, int size)
8396static long bio_stm_ctrl (BIO * h , int cmd , // NOLINT(runtime/int)
8497 long arg1 , void * arg2 ) // NOLINT(runtime/int)
8598{
86- ph_stream_t * stm = h -> ptr ;
99+ ph_stream_t * stm = BIO_get_data ( h ) ;
87100
88101 switch (cmd ) {
89102 case BIO_CTRL_FLUSH :
@@ -98,11 +111,7 @@ static long bio_stm_ctrl(BIO *h, int cmd, // NOLINT(runtime/int)
98111
99112static int bio_stm_new (BIO * h )
100113{
101- h -> init = 0 ;
102- h -> num = 0 ;
103- h -> ptr = NULL ;
104- h -> flags = 0 ;
105-
114+ bio_stm_clear (h );
106115 return 1 ;
107116}
108117
@@ -112,40 +121,56 @@ static int bio_stm_free(BIO *h)
112121 return 0 ;
113122 }
114123
115- h -> ptr = NULL ;
116- h -> init = 0 ;
117- h -> flags = 0 ;
118-
124+ bio_stm_clear (h );
119125 return 1 ;
120126}
121127
122- static BIO_METHOD method_stm = {
123- // There are no clear rules on how the type numbers are assigned, so we'll
124- // just pick 'P' as our type number and hope it doesn't collide any time
125- // soon.
126- 80 /* 'P' */ | BIO_TYPE_SOURCE_SINK ,
127- "phenom-stream" ,
128- bio_stm_write ,
129- bio_stm_read ,
130- bio_stm_puts ,
131- NULL , /* no gets */
132- bio_stm_ctrl ,
133- bio_stm_new ,
134- bio_stm_free ,
135- NULL , /* no callback ctrl */
136- };
137-
138128BIO * ph_openssl_bio_wrap_stream (ph_stream_t * stm )
139129{
140- BIO * h ;
130+ static BIO_METHOD * bm ;
131+
132+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
133+ static BIO_METHOD old_meth = {
134+ // There are no clear rules on how the type numbers are assigned, so we'll
135+ // just pick 'P' as our type number and hope it doesn't collide any time
136+ // soon. TODO: consider using: BIO_TYPE_BIO | BIO_get_new_index ();
137+ 80 | BIO_TYPE_SOURCE_SINK ,
138+ "phenom-stream" ,
139+ bio_stm_write ,
140+ bio_stm_read ,
141+ bio_stm_puts ,
142+ NULL , /* no gets */
143+ bio_stm_ctrl ,
144+ bio_stm_new ,
145+ bio_stm_free ,
146+ NULL , /* no callback ctrl */
147+ };
148+
149+ bm = & old_meth ;
150+ #else
151+ if (!bm ) {
152+ bm = BIO_meth_new (80 /* 'P' */ | BIO_TYPE_SOURCE_SINK , "phenom-stream" );
153+ if (!bm ) {
154+ return NULL ;
155+ }
156+
157+ BIO_meth_set_write (bm , bio_stm_write );
158+ BIO_meth_set_read (bm , bio_stm_read );
159+ BIO_meth_set_puts (bm , bio_stm_puts );
160+ BIO_meth_set_ctrl (bm , bio_stm_ctrl );
161+ BIO_meth_set_create (bm , bio_stm_new );
162+ BIO_meth_set_destroy (bm , bio_stm_free );
163+ }
164+ #endif
141165
142- h = BIO_new (& method_stm );
166+ BIO * h = BIO_new (bm );
143167 if (!h ) {
144168 return NULL ;
145169 }
146170
147- h -> ptr = stm ;
148- h -> init = 1 ;
171+ BIO_set_data (h , stm );
172+ BIO_set_init (h , 1 );
173+
149174 return h ;
150175}
151176
0 commit comments