diff options
Diffstat (limited to 'src/lib/libcrypto/bio/bss_acpt.c')
-rw-r--r-- | src/lib/libcrypto/bio/bss_acpt.c | 132 |
1 files changed, 49 insertions, 83 deletions
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c index e49902fa9f..47af80f76d 100644 --- a/src/lib/libcrypto/bio/bss_acpt.c +++ b/src/lib/libcrypto/bio/bss_acpt.c | |||
@@ -62,9 +62,7 @@ | |||
62 | #include <errno.h> | 62 | #include <errno.h> |
63 | #define USE_SOCKETS | 63 | #define USE_SOCKETS |
64 | #include "cryptlib.h" | 64 | #include "cryptlib.h" |
65 | #include "bio.h" | 65 | #include <openssl/bio.h> |
66 | |||
67 | /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ | ||
68 | 66 | ||
69 | #ifdef WIN16 | 67 | #ifdef WIN16 |
70 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ | 68 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ |
@@ -72,6 +70,11 @@ | |||
72 | #define SOCKET_PROTOCOL IPPROTO_TCP | 70 | #define SOCKET_PROTOCOL IPPROTO_TCP |
73 | #endif | 71 | #endif |
74 | 72 | ||
73 | #if (defined(VMS) && __VMS_VER < 70000000) | ||
74 | /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ | ||
75 | #undef FIONBIO | ||
76 | #endif | ||
77 | |||
75 | typedef struct bio_accept_st | 78 | typedef struct bio_accept_st |
76 | { | 79 | { |
77 | int state; | 80 | int state; |
@@ -82,39 +85,24 @@ typedef struct bio_accept_st | |||
82 | 85 | ||
83 | char *addr; | 86 | char *addr; |
84 | int nbio; | 87 | int nbio; |
88 | /* If 0, it means normal, if 1, do a connect on bind failure, | ||
89 | * and if there is no-one listening, bind with SO_REUSEADDR. | ||
90 | * If 2, always use SO_REUSEADDR. */ | ||
91 | int bind_mode; | ||
85 | BIO *bio_chain; | 92 | BIO *bio_chain; |
86 | } BIO_ACCEPT; | 93 | } BIO_ACCEPT; |
87 | 94 | ||
88 | #ifndef NOPROTO | ||
89 | static int acpt_write(BIO *h,char *buf,int num); | 95 | static int acpt_write(BIO *h,char *buf,int num); |
90 | static int acpt_read(BIO *h,char *buf,int size); | 96 | static int acpt_read(BIO *h,char *buf,int size); |
91 | static int acpt_puts(BIO *h,char *str); | 97 | static int acpt_puts(BIO *h,char *str); |
92 | static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2); | 98 | static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2); |
93 | static int acpt_new(BIO *h); | 99 | static int acpt_new(BIO *h); |
94 | static int acpt_free(BIO *data); | 100 | static int acpt_free(BIO *data); |
95 | #else | ||
96 | static int acpt_write(); | ||
97 | static int acpt_read(); | ||
98 | static int acpt_puts(); | ||
99 | static long acpt_ctrl(); | ||
100 | static int acpt_new(); | ||
101 | static int acpt_free(); | ||
102 | #endif | ||
103 | |||
104 | #ifndef NOPROTO | ||
105 | static int acpt_state(BIO *b, BIO_ACCEPT *c); | 101 | static int acpt_state(BIO *b, BIO_ACCEPT *c); |
106 | static void acpt_close_socket(BIO *data); | 102 | static void acpt_close_socket(BIO *data); |
107 | BIO_ACCEPT *BIO_ACCEPT_new(void ); | 103 | BIO_ACCEPT *BIO_ACCEPT_new(void ); |
108 | void BIO_ACCEPT_free(BIO_ACCEPT *a); | 104 | void BIO_ACCEPT_free(BIO_ACCEPT *a); |
109 | 105 | ||
110 | #else | ||
111 | |||
112 | static int acpt_state(); | ||
113 | static void acpt_close_socket(); | ||
114 | BIO_ACCEPT *BIO_ACCEPT_new(); | ||
115 | void BIO_ACCEPT_free(); | ||
116 | #endif | ||
117 | |||
118 | #define ACPT_S_BEFORE 1 | 106 | #define ACPT_S_BEFORE 1 |
119 | #define ACPT_S_GET_ACCEPT_SOCKET 2 | 107 | #define ACPT_S_GET_ACCEPT_SOCKET 2 |
120 | #define ACPT_S_OK 3 | 108 | #define ACPT_S_OK 3 |
@@ -132,13 +120,12 @@ static BIO_METHOD methods_acceptp= | |||
132 | acpt_free, | 120 | acpt_free, |
133 | }; | 121 | }; |
134 | 122 | ||
135 | BIO_METHOD *BIO_s_accept() | 123 | BIO_METHOD *BIO_s_accept(void) |
136 | { | 124 | { |
137 | return(&methods_acceptp); | 125 | return(&methods_acceptp); |
138 | } | 126 | } |
139 | 127 | ||
140 | static int acpt_new(bi) | 128 | static int acpt_new(BIO *bi) |
141 | BIO *bi; | ||
142 | { | 129 | { |
143 | BIO_ACCEPT *ba; | 130 | BIO_ACCEPT *ba; |
144 | 131 | ||
@@ -153,7 +140,7 @@ BIO *bi; | |||
153 | return(1); | 140 | return(1); |
154 | } | 141 | } |
155 | 142 | ||
156 | BIO_ACCEPT *BIO_ACCEPT_new() | 143 | BIO_ACCEPT *BIO_ACCEPT_new(void) |
157 | { | 144 | { |
158 | BIO_ACCEPT *ret; | 145 | BIO_ACCEPT *ret; |
159 | 146 | ||
@@ -162,20 +149,22 @@ BIO_ACCEPT *BIO_ACCEPT_new() | |||
162 | 149 | ||
163 | memset(ret,0,sizeof(BIO_ACCEPT)); | 150 | memset(ret,0,sizeof(BIO_ACCEPT)); |
164 | ret->accept_sock=INVALID_SOCKET; | 151 | ret->accept_sock=INVALID_SOCKET; |
152 | ret->bind_mode=BIO_BIND_NORMAL; | ||
165 | return(ret); | 153 | return(ret); |
166 | } | 154 | } |
167 | 155 | ||
168 | void BIO_ACCEPT_free(a) | 156 | void BIO_ACCEPT_free(BIO_ACCEPT *a) |
169 | BIO_ACCEPT *a; | ||
170 | { | 157 | { |
158 | if(a == NULL) | ||
159 | return; | ||
160 | |||
171 | if (a->param_addr != NULL) Free(a->param_addr); | 161 | if (a->param_addr != NULL) Free(a->param_addr); |
172 | if (a->addr != NULL) Free(a->addr); | 162 | if (a->addr != NULL) Free(a->addr); |
173 | if (a->bio_chain != NULL) BIO_free(a->bio_chain); | 163 | if (a->bio_chain != NULL) BIO_free(a->bio_chain); |
174 | Free(a); | 164 | Free(a); |
175 | } | 165 | } |
176 | 166 | ||
177 | static void acpt_close_socket(bio) | 167 | static void acpt_close_socket(BIO *bio) |
178 | BIO *bio; | ||
179 | { | 168 | { |
180 | BIO_ACCEPT *c; | 169 | BIO_ACCEPT *c; |
181 | 170 | ||
@@ -183,18 +172,13 @@ BIO *bio; | |||
183 | if (c->accept_sock != INVALID_SOCKET) | 172 | if (c->accept_sock != INVALID_SOCKET) |
184 | { | 173 | { |
185 | shutdown(c->accept_sock,2); | 174 | shutdown(c->accept_sock,2); |
186 | # ifdef WINDOWS | ||
187 | closesocket(c->accept_sock); | 175 | closesocket(c->accept_sock); |
188 | # else | ||
189 | close(c->accept_sock); | ||
190 | # endif | ||
191 | c->accept_sock=INVALID_SOCKET; | 176 | c->accept_sock=INVALID_SOCKET; |
192 | bio->num=INVALID_SOCKET; | 177 | bio->num=INVALID_SOCKET; |
193 | } | 178 | } |
194 | } | 179 | } |
195 | 180 | ||
196 | static int acpt_free(a) | 181 | static int acpt_free(BIO *a) |
197 | BIO *a; | ||
198 | { | 182 | { |
199 | BIO_ACCEPT *data; | 183 | BIO_ACCEPT *data; |
200 | 184 | ||
@@ -212,12 +196,9 @@ BIO *a; | |||
212 | return(1); | 196 | return(1); |
213 | } | 197 | } |
214 | 198 | ||
215 | static int acpt_state(b,c) | 199 | static int acpt_state(BIO *b, BIO_ACCEPT *c) |
216 | BIO *b; | ||
217 | BIO_ACCEPT *c; | ||
218 | { | 200 | { |
219 | BIO *bio=NULL,*dbio; | 201 | BIO *bio=NULL,*dbio; |
220 | unsigned long l=1; | ||
221 | int s= -1; | 202 | int s= -1; |
222 | int i; | 203 | int i; |
223 | 204 | ||
@@ -230,31 +211,24 @@ again: | |||
230 | BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); | 211 | BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); |
231 | return(-1); | 212 | return(-1); |
232 | } | 213 | } |
233 | s=BIO_get_accept_socket(c->param_addr); | 214 | s=BIO_get_accept_socket(c->param_addr,c->bind_mode); |
234 | if (s == INVALID_SOCKET) | 215 | if (s == INVALID_SOCKET) |
235 | return(-1); | 216 | return(-1); |
236 | 217 | ||
237 | #ifdef FIONBIO | ||
238 | if (c->accept_nbio) | 218 | if (c->accept_nbio) |
239 | { | 219 | { |
240 | i=BIO_socket_ioctl(b->num,FIONBIO,&l); | 220 | if (!BIO_socket_nbio(s,1)) |
241 | if (i < 0) | ||
242 | { | 221 | { |
243 | #ifdef WINDOWS | ||
244 | closesocket(s); | 222 | closesocket(s); |
245 | #else | ||
246 | close(s); | ||
247 | # endif | ||
248 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); | 223 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); |
249 | return(-1); | 224 | return(-1); |
250 | } | 225 | } |
251 | } | 226 | } |
252 | #endif | ||
253 | c->accept_sock=s; | 227 | c->accept_sock=s; |
254 | b->num=s; | 228 | b->num=s; |
255 | c->state=ACPT_S_GET_ACCEPT_SOCKET; | 229 | c->state=ACPT_S_GET_ACCEPT_SOCKET; |
256 | return(1); | 230 | return(1); |
257 | break; | 231 | /* break; */ |
258 | case ACPT_S_GET_ACCEPT_SOCKET: | 232 | case ACPT_S_GET_ACCEPT_SOCKET: |
259 | if (b->next_bio != NULL) | 233 | if (b->next_bio != NULL) |
260 | { | 234 | { |
@@ -269,17 +243,14 @@ again: | |||
269 | BIO_set_callback(bio,BIO_get_callback(b)); | 243 | BIO_set_callback(bio,BIO_get_callback(b)); |
270 | BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); | 244 | BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); |
271 | 245 | ||
272 | #ifdef FIONBIO | ||
273 | if (c->nbio) | 246 | if (c->nbio) |
274 | { | 247 | { |
275 | i=BIO_socket_ioctl(i,FIONBIO,&l); | 248 | if (!BIO_socket_nbio(i,1)) |
276 | if (i < 0) | ||
277 | { | 249 | { |
278 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); | 250 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); |
279 | goto err; | 251 | goto err; |
280 | } | 252 | } |
281 | } | 253 | } |
282 | #endif | ||
283 | 254 | ||
284 | /* If the accept BIO has an bio_chain, we dup it and | 255 | /* If the accept BIO has an bio_chain, we dup it and |
285 | * put the new socket at the end. */ | 256 | * put the new socket at the end. */ |
@@ -298,15 +269,9 @@ err: | |||
298 | if (bio != NULL) | 269 | if (bio != NULL) |
299 | BIO_free(bio); | 270 | BIO_free(bio); |
300 | else if (s >= 0) | 271 | else if (s >= 0) |
301 | { | ||
302 | #ifdef WINDOWS | ||
303 | closesocket(s); | 272 | closesocket(s); |
304 | #else | ||
305 | close(s); | ||
306 | # endif | ||
307 | } | ||
308 | return(0); | 273 | return(0); |
309 | break; | 274 | /* break; */ |
310 | case ACPT_S_OK: | 275 | case ACPT_S_OK: |
311 | if (b->next_bio == NULL) | 276 | if (b->next_bio == NULL) |
312 | { | 277 | { |
@@ -314,23 +279,20 @@ err: | |||
314 | goto again; | 279 | goto again; |
315 | } | 280 | } |
316 | return(1); | 281 | return(1); |
317 | break; | 282 | /* break; */ |
318 | default: | 283 | default: |
319 | return(0); | 284 | return(0); |
320 | break; | 285 | /* break; */ |
321 | } | 286 | } |
322 | 287 | ||
323 | } | 288 | } |
324 | 289 | ||
325 | static int acpt_read(b,out,outl) | 290 | static int acpt_read(BIO *b, char *out, int outl) |
326 | BIO *b; | ||
327 | char *out; | ||
328 | int outl; | ||
329 | { | 291 | { |
330 | int ret=0; | 292 | int ret=0; |
331 | BIO_ACCEPT *data; | 293 | BIO_ACCEPT *data; |
332 | 294 | ||
333 | BIO_clear_retry_flags(b); | 295 | BIO_clear_retry_flags(b); |
334 | data=(BIO_ACCEPT *)b->ptr; | 296 | data=(BIO_ACCEPT *)b->ptr; |
335 | 297 | ||
336 | while (b->next_bio == NULL) | 298 | while (b->next_bio == NULL) |
@@ -344,10 +306,7 @@ int outl; | |||
344 | return(ret); | 306 | return(ret); |
345 | } | 307 | } |
346 | 308 | ||
347 | static int acpt_write(b,in,inl) | 309 | static int acpt_write(BIO *b, char *in, int inl) |
348 | BIO *b; | ||
349 | char *in; | ||
350 | int inl; | ||
351 | { | 310 | { |
352 | int ret; | 311 | int ret; |
353 | BIO_ACCEPT *data; | 312 | BIO_ACCEPT *data; |
@@ -366,11 +325,7 @@ int inl; | |||
366 | return(ret); | 325 | return(ret); |
367 | } | 326 | } |
368 | 327 | ||
369 | static long acpt_ctrl(b,cmd,num,ptr) | 328 | static long acpt_ctrl(BIO *b, int cmd, long num, char *ptr) |
370 | BIO *b; | ||
371 | int cmd; | ||
372 | long num; | ||
373 | char *ptr; | ||
374 | { | 329 | { |
375 | BIO *dbio; | 330 | BIO *dbio; |
376 | int *ip; | 331 | int *ip; |
@@ -417,13 +372,21 @@ char *ptr; | |||
417 | case BIO_C_SET_NBIO: | 372 | case BIO_C_SET_NBIO: |
418 | data->nbio=(int)num; | 373 | data->nbio=(int)num; |
419 | break; | 374 | break; |
375 | case BIO_C_SET_FD: | ||
376 | b->init=1; | ||
377 | b->num= *((int *)ptr); | ||
378 | data->accept_sock=b->num; | ||
379 | data->state=ACPT_S_GET_ACCEPT_SOCKET; | ||
380 | b->shutdown=(int)num; | ||
381 | b->init=1; | ||
382 | break; | ||
420 | case BIO_C_GET_FD: | 383 | case BIO_C_GET_FD: |
421 | if (b->init) | 384 | if (b->init) |
422 | { | 385 | { |
423 | ip=(int *)ptr; | 386 | ip=(int *)ptr; |
424 | if (ip != NULL) | 387 | if (ip != NULL) |
425 | *ip=data->accept_sock; | 388 | *ip=data->accept_sock; |
426 | ret=b->num; | 389 | ret=data->accept_sock; |
427 | } | 390 | } |
428 | else | 391 | else |
429 | ret= -1; | 392 | ret= -1; |
@@ -454,6 +417,12 @@ char *ptr; | |||
454 | break; | 417 | break; |
455 | case BIO_CTRL_FLUSH: | 418 | case BIO_CTRL_FLUSH: |
456 | break; | 419 | break; |
420 | case BIO_C_SET_BIND_MODE: | ||
421 | data->bind_mode=(int)num; | ||
422 | break; | ||
423 | case BIO_C_GET_BIND_MODE: | ||
424 | ret=(long)data->bind_mode; | ||
425 | break; | ||
457 | case BIO_CTRL_DUP: | 426 | case BIO_CTRL_DUP: |
458 | dbio=(BIO *)ptr; | 427 | dbio=(BIO *)ptr; |
459 | /* if (data->param_port) EAY EAY | 428 | /* if (data->param_port) EAY EAY |
@@ -470,9 +439,7 @@ char *ptr; | |||
470 | return(ret); | 439 | return(ret); |
471 | } | 440 | } |
472 | 441 | ||
473 | static int acpt_puts(bp,str) | 442 | static int acpt_puts(BIO *bp, char *str) |
474 | BIO *bp; | ||
475 | char *str; | ||
476 | { | 443 | { |
477 | int n,ret; | 444 | int n,ret; |
478 | 445 | ||
@@ -481,8 +448,7 @@ char *str; | |||
481 | return(ret); | 448 | return(ret); |
482 | } | 449 | } |
483 | 450 | ||
484 | BIO *BIO_new_accept(str) | 451 | BIO *BIO_new_accept(char *str) |
485 | char *str; | ||
486 | { | 452 | { |
487 | BIO *ret; | 453 | BIO *ret; |
488 | 454 | ||