summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio/bss_acpt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bio/bss_acpt.c')
-rw-r--r--src/lib/libcrypto/bio/bss_acpt.c167
1 files changed, 73 insertions, 94 deletions
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c
index e49902fa9f..8ea1db158b 100644
--- a/src/lib/libcrypto/bio/bss_acpt.c
+++ b/src/lib/libcrypto/bio/bss_acpt.c
@@ -56,22 +56,25 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#ifndef NO_SOCK 59#ifndef OPENSSL_NO_SOCK
60 60
61#include <stdio.h> 61#include <stdio.h>
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 66
67/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ 67#ifdef OPENSSL_SYS_WIN16
68
69#ifdef WIN16
70#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ 68#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
71#else 69#else
72#define SOCKET_PROTOCOL IPPROTO_TCP 70#define SOCKET_PROTOCOL IPPROTO_TCP
73#endif 71#endif
74 72
73#if (defined(OPENSSL_SYS_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
75typedef struct bio_accept_st 78typedef 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 95static int acpt_write(BIO *h, const char *buf, int num);
89static int acpt_write(BIO *h,char *buf,int num); 96static int acpt_read(BIO *h, char *buf, int size);
90static int acpt_read(BIO *h,char *buf,int size); 97static int acpt_puts(BIO *h, const char *str);
91static int acpt_puts(BIO *h,char *str); 98static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
92static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2);
93static int acpt_new(BIO *h); 99static int acpt_new(BIO *h);
94static int acpt_free(BIO *data); 100static int acpt_free(BIO *data);
95#else
96static int acpt_write();
97static int acpt_read();
98static int acpt_puts();
99static long acpt_ctrl();
100static int acpt_new();
101static int acpt_free();
102#endif
103
104#ifndef NOPROTO
105static int acpt_state(BIO *b, BIO_ACCEPT *c); 101static int acpt_state(BIO *b, BIO_ACCEPT *c);
106static void acpt_close_socket(BIO *data); 102static void acpt_close_socket(BIO *data);
107BIO_ACCEPT *BIO_ACCEPT_new(void ); 103BIO_ACCEPT *BIO_ACCEPT_new(void );
108void BIO_ACCEPT_free(BIO_ACCEPT *a); 104void BIO_ACCEPT_free(BIO_ACCEPT *a);
109 105
110#else
111
112static int acpt_state();
113static void acpt_close_socket();
114BIO_ACCEPT *BIO_ACCEPT_new();
115void 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
@@ -130,15 +118,15 @@ static BIO_METHOD methods_acceptp=
130 acpt_ctrl, 118 acpt_ctrl,
131 acpt_new, 119 acpt_new,
132 acpt_free, 120 acpt_free,
121 NULL,
133 }; 122 };
134 123
135BIO_METHOD *BIO_s_accept() 124BIO_METHOD *BIO_s_accept(void)
136 { 125 {
137 return(&methods_acceptp); 126 return(&methods_acceptp);
138 } 127 }
139 128
140static int acpt_new(bi) 129static int acpt_new(BIO *bi)
141BIO *bi;
142 { 130 {
143 BIO_ACCEPT *ba; 131 BIO_ACCEPT *ba;
144 132
@@ -153,29 +141,31 @@ BIO *bi;
153 return(1); 141 return(1);
154 } 142 }
155 143
156BIO_ACCEPT *BIO_ACCEPT_new() 144BIO_ACCEPT *BIO_ACCEPT_new(void)
157 { 145 {
158 BIO_ACCEPT *ret; 146 BIO_ACCEPT *ret;
159 147
160 if ((ret=(BIO_ACCEPT *)Malloc(sizeof(BIO_ACCEPT))) == NULL) 148 if ((ret=(BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL)
161 return(NULL); 149 return(NULL);
162 150
163 memset(ret,0,sizeof(BIO_ACCEPT)); 151 memset(ret,0,sizeof(BIO_ACCEPT));
164 ret->accept_sock=INVALID_SOCKET; 152 ret->accept_sock=INVALID_SOCKET;
153 ret->bind_mode=BIO_BIND_NORMAL;
165 return(ret); 154 return(ret);
166 } 155 }
167 156
168void BIO_ACCEPT_free(a) 157void BIO_ACCEPT_free(BIO_ACCEPT *a)
169BIO_ACCEPT *a;
170 { 158 {
171 if (a->param_addr != NULL) Free(a->param_addr); 159 if(a == NULL)
172 if (a->addr != NULL) Free(a->addr); 160 return;
161
162 if (a->param_addr != NULL) OPENSSL_free(a->param_addr);
163 if (a->addr != NULL) OPENSSL_free(a->addr);
173 if (a->bio_chain != NULL) BIO_free(a->bio_chain); 164 if (a->bio_chain != NULL) BIO_free(a->bio_chain);
174 Free(a); 165 OPENSSL_free(a);
175 } 166 }
176 167
177static void acpt_close_socket(bio) 168static void acpt_close_socket(BIO *bio)
178BIO *bio;
179 { 169 {
180 BIO_ACCEPT *c; 170 BIO_ACCEPT *c;
181 171
@@ -183,18 +173,13 @@ BIO *bio;
183 if (c->accept_sock != INVALID_SOCKET) 173 if (c->accept_sock != INVALID_SOCKET)
184 { 174 {
185 shutdown(c->accept_sock,2); 175 shutdown(c->accept_sock,2);
186# ifdef WINDOWS
187 closesocket(c->accept_sock); 176 closesocket(c->accept_sock);
188# else
189 close(c->accept_sock);
190# endif
191 c->accept_sock=INVALID_SOCKET; 177 c->accept_sock=INVALID_SOCKET;
192 bio->num=INVALID_SOCKET; 178 bio->num=INVALID_SOCKET;
193 } 179 }
194 } 180 }
195 181
196static int acpt_free(a) 182static int acpt_free(BIO *a)
197BIO *a;
198 { 183 {
199 BIO_ACCEPT *data; 184 BIO_ACCEPT *data;
200 185
@@ -212,12 +197,9 @@ BIO *a;
212 return(1); 197 return(1);
213 } 198 }
214 199
215static int acpt_state(b,c) 200static int acpt_state(BIO *b, BIO_ACCEPT *c)
216BIO *b;
217BIO_ACCEPT *c;
218 { 201 {
219 BIO *bio=NULL,*dbio; 202 BIO *bio=NULL,*dbio;
220 unsigned long l=1;
221 int s= -1; 203 int s= -1;
222 int i; 204 int i;
223 205
@@ -230,56 +212,58 @@ again:
230 BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); 212 BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
231 return(-1); 213 return(-1);
232 } 214 }
233 s=BIO_get_accept_socket(c->param_addr); 215 s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
234 if (s == INVALID_SOCKET) 216 if (s == INVALID_SOCKET)
235 return(-1); 217 return(-1);
236 218
237#ifdef FIONBIO
238 if (c->accept_nbio) 219 if (c->accept_nbio)
239 { 220 {
240 i=BIO_socket_ioctl(b->num,FIONBIO,&l); 221 if (!BIO_socket_nbio(s,1))
241 if (i < 0)
242 { 222 {
243#ifdef WINDOWS
244 closesocket(s); 223 closesocket(s);
245#else
246 close(s);
247# endif
248 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); 224 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
249 return(-1); 225 return(-1);
250 } 226 }
251 } 227 }
252#endif
253 c->accept_sock=s; 228 c->accept_sock=s;
254 b->num=s; 229 b->num=s;
255 c->state=ACPT_S_GET_ACCEPT_SOCKET; 230 c->state=ACPT_S_GET_ACCEPT_SOCKET;
256 return(1); 231 return(1);
257 break; 232 /* break; */
258 case ACPT_S_GET_ACCEPT_SOCKET: 233 case ACPT_S_GET_ACCEPT_SOCKET:
259 if (b->next_bio != NULL) 234 if (b->next_bio != NULL)
260 { 235 {
261 c->state=ACPT_S_OK; 236 c->state=ACPT_S_OK;
262 goto again; 237 goto again;
263 } 238 }
239 BIO_clear_retry_flags(b);
240 b->retry_reason=0;
264 i=BIO_accept(c->accept_sock,&(c->addr)); 241 i=BIO_accept(c->accept_sock,&(c->addr));
242
243 /* -2 return means we should retry */
244 if(i == -2)
245 {
246 BIO_set_retry_special(b);
247 b->retry_reason=BIO_RR_ACCEPT;
248 return -1;
249 }
250
265 if (i < 0) return(i); 251 if (i < 0) return(i);
252
266 bio=BIO_new_socket(i,BIO_CLOSE); 253 bio=BIO_new_socket(i,BIO_CLOSE);
267 if (bio == NULL) goto err; 254 if (bio == NULL) goto err;
268 255
269 BIO_set_callback(bio,BIO_get_callback(b)); 256 BIO_set_callback(bio,BIO_get_callback(b));
270 BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); 257 BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
271 258
272#ifdef FIONBIO
273 if (c->nbio) 259 if (c->nbio)
274 { 260 {
275 i=BIO_socket_ioctl(i,FIONBIO,&l); 261 if (!BIO_socket_nbio(i,1))
276 if (i < 0)
277 { 262 {
278 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); 263 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
279 goto err; 264 goto err;
280 } 265 }
281 } 266 }
282#endif
283 267
284 /* If the accept BIO has an bio_chain, we dup it and 268 /* If the accept BIO has an bio_chain, we dup it and
285 * put the new socket at the end. */ 269 * put the new socket at the end. */
@@ -298,15 +282,9 @@ err:
298 if (bio != NULL) 282 if (bio != NULL)
299 BIO_free(bio); 283 BIO_free(bio);
300 else if (s >= 0) 284 else if (s >= 0)
301 {
302#ifdef WINDOWS
303 closesocket(s); 285 closesocket(s);
304#else
305 close(s);
306# endif
307 }
308 return(0); 286 return(0);
309 break; 287 /* break; */
310 case ACPT_S_OK: 288 case ACPT_S_OK:
311 if (b->next_bio == NULL) 289 if (b->next_bio == NULL)
312 { 290 {
@@ -314,23 +292,20 @@ err:
314 goto again; 292 goto again;
315 } 293 }
316 return(1); 294 return(1);
317 break; 295 /* break; */
318 default: 296 default:
319 return(0); 297 return(0);
320 break; 298 /* break; */
321 } 299 }
322 300
323 } 301 }
324 302
325static int acpt_read(b,out,outl) 303static int acpt_read(BIO *b, char *out, int outl)
326BIO *b;
327char *out;
328int outl;
329 { 304 {
330 int ret=0; 305 int ret=0;
331 BIO_ACCEPT *data; 306 BIO_ACCEPT *data;
332 307
333 BIO_clear_retry_flags(b); 308 BIO_clear_retry_flags(b);
334 data=(BIO_ACCEPT *)b->ptr; 309 data=(BIO_ACCEPT *)b->ptr;
335 310
336 while (b->next_bio == NULL) 311 while (b->next_bio == NULL)
@@ -344,10 +319,7 @@ int outl;
344 return(ret); 319 return(ret);
345 } 320 }
346 321
347static int acpt_write(b,in,inl) 322static int acpt_write(BIO *b, const char *in, int inl)
348BIO *b;
349char *in;
350int inl;
351 { 323 {
352 int ret; 324 int ret;
353 BIO_ACCEPT *data; 325 BIO_ACCEPT *data;
@@ -366,11 +338,7 @@ int inl;
366 return(ret); 338 return(ret);
367 } 339 }
368 340
369static long acpt_ctrl(b,cmd,num,ptr) 341static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
370BIO *b;
371int cmd;
372long num;
373char *ptr;
374 { 342 {
375 BIO *dbio; 343 BIO *dbio;
376 int *ip; 344 int *ip;
@@ -399,7 +367,7 @@ char *ptr;
399 { 367 {
400 b->init=1; 368 b->init=1;
401 if (data->param_addr != NULL) 369 if (data->param_addr != NULL)
402 Free(data->param_addr); 370 OPENSSL_free(data->param_addr);
403 data->param_addr=BUF_strdup(ptr); 371 data->param_addr=BUF_strdup(ptr);
404 } 372 }
405 else if (num == 1) 373 else if (num == 1)
@@ -417,13 +385,21 @@ char *ptr;
417 case BIO_C_SET_NBIO: 385 case BIO_C_SET_NBIO:
418 data->nbio=(int)num; 386 data->nbio=(int)num;
419 break; 387 break;
388 case BIO_C_SET_FD:
389 b->init=1;
390 b->num= *((int *)ptr);
391 data->accept_sock=b->num;
392 data->state=ACPT_S_GET_ACCEPT_SOCKET;
393 b->shutdown=(int)num;
394 b->init=1;
395 break;
420 case BIO_C_GET_FD: 396 case BIO_C_GET_FD:
421 if (b->init) 397 if (b->init)
422 { 398 {
423 ip=(int *)ptr; 399 ip=(int *)ptr;
424 if (ip != NULL) 400 if (ip != NULL)
425 *ip=data->accept_sock; 401 *ip=data->accept_sock;
426 ret=b->num; 402 ret=data->accept_sock;
427 } 403 }
428 else 404 else
429 ret= -1; 405 ret= -1;
@@ -454,6 +430,12 @@ char *ptr;
454 break; 430 break;
455 case BIO_CTRL_FLUSH: 431 case BIO_CTRL_FLUSH:
456 break; 432 break;
433 case BIO_C_SET_BIND_MODE:
434 data->bind_mode=(int)num;
435 break;
436 case BIO_C_GET_BIND_MODE:
437 ret=(long)data->bind_mode;
438 break;
457 case BIO_CTRL_DUP: 439 case BIO_CTRL_DUP:
458 dbio=(BIO *)ptr; 440 dbio=(BIO *)ptr;
459/* if (data->param_port) EAY EAY 441/* if (data->param_port) EAY EAY
@@ -470,9 +452,7 @@ char *ptr;
470 return(ret); 452 return(ret);
471 } 453 }
472 454
473static int acpt_puts(bp,str) 455static int acpt_puts(BIO *bp, const char *str)
474BIO *bp;
475char *str;
476 { 456 {
477 int n,ret; 457 int n,ret;
478 458
@@ -481,8 +461,7 @@ char *str;
481 return(ret); 461 return(ret);
482 } 462 }
483 463
484BIO *BIO_new_accept(str) 464BIO *BIO_new_accept(char *str)
485char *str;
486 { 465 {
487 BIO *ret; 466 BIO *ret;
488 467