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.c455
1 files changed, 224 insertions, 231 deletions
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c
index 04bfb6fb9d..d772515d64 100644
--- a/src/lib/libcrypto/bio/bss_acpt.c
+++ b/src/lib/libcrypto/bio/bss_acpt.c
@@ -75,8 +75,7 @@
75#undef FIONBIO 75#undef FIONBIO
76#endif 76#endif
77 77
78typedef struct bio_accept_st 78typedef struct bio_accept_st {
79 {
80 int state; 79 int state;
81 char *param_addr; 80 char *param_addr;
82 81
@@ -90,7 +89,7 @@ typedef struct bio_accept_st
90 * If 2, always use SO_REUSEADDR. */ 89 * If 2, always use SO_REUSEADDR. */
91 int bind_mode; 90 int bind_mode;
92 BIO *bio_chain; 91 BIO *bio_chain;
93 } BIO_ACCEPT; 92} BIO_ACCEPT;
94 93
95static int acpt_write(BIO *h, const char *buf, int num); 94static int acpt_write(BIO *h, const char *buf, int num);
96static int acpt_read(BIO *h, char *buf, int size); 95static int acpt_read(BIO *h, char *buf, int size);
@@ -107,8 +106,7 @@ static void BIO_ACCEPT_free(BIO_ACCEPT *a);
107#define ACPT_S_GET_ACCEPT_SOCKET 2 106#define ACPT_S_GET_ACCEPT_SOCKET 2
108#define ACPT_S_OK 3 107#define ACPT_S_OK 3
109 108
110static BIO_METHOD methods_acceptp= 109static BIO_METHOD methods_acceptp = {
111 {
112 BIO_TYPE_ACCEPT, 110 BIO_TYPE_ACCEPT,
113 "socket accept", 111 "socket accept",
114 acpt_write, 112 acpt_write,
@@ -119,321 +117,313 @@ static BIO_METHOD methods_acceptp=
119 acpt_new, 117 acpt_new,
120 acpt_free, 118 acpt_free,
121 NULL, 119 NULL,
122 }; 120};
123 121
124BIO_METHOD *BIO_s_accept(void) 122BIO_METHOD
125 { 123*BIO_s_accept(void)
126 return(&methods_acceptp); 124{
127 } 125 return (&methods_acceptp);
126}
128 127
129static int acpt_new(BIO *bi) 128static int
130 { 129acpt_new(BIO *bi)
130{
131 BIO_ACCEPT *ba; 131 BIO_ACCEPT *ba;
132 132
133 bi->init=0; 133 bi->init = 0;
134 bi->num=-1; 134 bi->num = -1;
135 bi->flags=0; 135 bi->flags = 0;
136 if ((ba=BIO_ACCEPT_new()) == NULL) 136 if ((ba = BIO_ACCEPT_new()) == NULL)
137 return(0); 137 return (0);
138 bi->ptr=(char *)ba; 138 bi->ptr = (char *)ba;
139 ba->state=ACPT_S_BEFORE; 139 ba->state = ACPT_S_BEFORE;
140 bi->shutdown=1; 140 bi->shutdown = 1;
141 return(1); 141 return (1);
142 } 142}
143 143
144static BIO_ACCEPT *BIO_ACCEPT_new(void) 144static BIO_ACCEPT
145 { 145*BIO_ACCEPT_new(void)
146{
146 BIO_ACCEPT *ret; 147 BIO_ACCEPT *ret;
147 148
148 if ((ret=(BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL) 149 if ((ret = (BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL)
149 return(NULL); 150 return (NULL);
150 151
151 memset(ret,0,sizeof(BIO_ACCEPT)); 152 memset(ret, 0, sizeof(BIO_ACCEPT));
152 ret->accept_sock=-1; 153 ret->accept_sock = -1;
153 ret->bind_mode=BIO_BIND_NORMAL; 154 ret->bind_mode = BIO_BIND_NORMAL;
154 return(ret); 155 return (ret);
155 } 156}
156 157
157static void BIO_ACCEPT_free(BIO_ACCEPT *a) 158static void
158 { 159BIO_ACCEPT_free(BIO_ACCEPT *a)
159 if(a == NULL) 160{
160 return; 161 if (a == NULL)
161 162 return;
162 if (a->param_addr != NULL) OPENSSL_free(a->param_addr); 163
163 if (a->addr != NULL) OPENSSL_free(a->addr); 164 if (a->param_addr != NULL)
164 if (a->bio_chain != NULL) BIO_free(a->bio_chain); 165 OPENSSL_free(a->param_addr);
166 if (a->addr != NULL)
167 OPENSSL_free(a->addr);
168 if (a->bio_chain != NULL)
169 BIO_free(a->bio_chain);
165 OPENSSL_free(a); 170 OPENSSL_free(a);
166 } 171}
167 172
168static void acpt_close_socket(BIO *bio) 173static void
169 { 174acpt_close_socket(BIO *bio)
175{
170 BIO_ACCEPT *c; 176 BIO_ACCEPT *c;
171 177
172 c=(BIO_ACCEPT *)bio->ptr; 178 c = (BIO_ACCEPT *)bio->ptr;
173 if (c->accept_sock != -1) 179 if (c->accept_sock != -1) {
174 {
175 shutdown(c->accept_sock, SHUT_RDWR); 180 shutdown(c->accept_sock, SHUT_RDWR);
176 close(c->accept_sock); 181 close(c->accept_sock);
177 c->accept_sock=-1; 182 c->accept_sock = -1;
178 bio->num=-1; 183 bio->num = -1;
179 }
180 } 184 }
185}
181 186
182static int acpt_free(BIO *a) 187static int
183 { 188acpt_free(BIO *a)
189{
184 BIO_ACCEPT *data; 190 BIO_ACCEPT *data;
185 191
186 if (a == NULL) return(0); 192 if (a == NULL)
187 data=(BIO_ACCEPT *)a->ptr; 193 return (0);
188 194 data = (BIO_ACCEPT *)a->ptr;
189 if (a->shutdown) 195
190 { 196 if (a->shutdown) {
191 acpt_close_socket(a); 197 acpt_close_socket(a);
192 BIO_ACCEPT_free(data); 198 BIO_ACCEPT_free(data);
193 a->ptr=NULL; 199 a->ptr = NULL;
194 a->flags=0; 200 a->flags = 0;
195 a->init=0; 201 a->init = 0;
196 }
197 return(1);
198 } 202 }
199 203 return (1);
200static int acpt_state(BIO *b, BIO_ACCEPT *c) 204}
201 { 205
202 BIO *bio=NULL,*dbio; 206static int
203 int s= -1; 207acpt_state(BIO *b, BIO_ACCEPT *c)
208{
209 BIO *bio = NULL, *dbio;
210 int s = -1;
204 int i; 211 int i;
205 212
206again: 213again:
207 switch (c->state) 214 switch (c->state) {
208 {
209 case ACPT_S_BEFORE: 215 case ACPT_S_BEFORE:
210 if (c->param_addr == NULL) 216 if (c->param_addr == NULL) {
211 { 217 BIOerr(BIO_F_ACPT_STATE, BIO_R_NO_ACCEPT_PORT_SPECIFIED);
212 BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); 218 return (-1);
213 return(-1); 219 }
214 } 220 s = BIO_get_accept_socket(c->param_addr, c->bind_mode);
215 s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
216 if (s == -1) 221 if (s == -1)
217 return(-1); 222 return (-1);
218 223
219 if (c->accept_nbio) 224 if (c->accept_nbio) {
220 { 225 if (!BIO_socket_nbio(s, 1)) {
221 if (!BIO_socket_nbio(s,1))
222 {
223 close(s); 226 close(s);
224 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); 227 BIOerr(BIO_F_ACPT_STATE, BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
225 return(-1); 228 return (-1);
226 }
227 } 229 }
228 c->accept_sock=s; 230 }
229 b->num=s; 231 c->accept_sock = s;
230 c->state=ACPT_S_GET_ACCEPT_SOCKET; 232 b->num = s;
231 return(1); 233 c->state = ACPT_S_GET_ACCEPT_SOCKET;
234 return (1);
232 /* break; */ 235 /* break; */
233 case ACPT_S_GET_ACCEPT_SOCKET: 236 case ACPT_S_GET_ACCEPT_SOCKET:
234 if (b->next_bio != NULL) 237 if (b->next_bio != NULL) {
235 { 238 c->state = ACPT_S_OK;
236 c->state=ACPT_S_OK;
237 goto again; 239 goto again;
238 } 240 }
239 BIO_clear_retry_flags(b); 241 BIO_clear_retry_flags(b);
240 b->retry_reason=0; 242 b->retry_reason = 0;
241 i=BIO_accept(c->accept_sock,&(c->addr)); 243 i = BIO_accept(c->accept_sock, &(c->addr));
242 244
243 /* -2 return means we should retry */ 245 /* -2 return means we should retry */
244 if(i == -2) 246 if (i == -2) {
245 {
246 BIO_set_retry_special(b); 247 BIO_set_retry_special(b);
247 b->retry_reason=BIO_RR_ACCEPT; 248 b->retry_reason = BIO_RR_ACCEPT;
248 return -1; 249 return -1;
249 } 250 }
250 251
251 if (i < 0) return(i); 252 if (i < 0)
253 return (i);
252 254
253 bio=BIO_new_socket(i,BIO_CLOSE); 255 bio = BIO_new_socket(i, BIO_CLOSE);
254 if (bio == NULL) goto err; 256 if (bio == NULL)
257 goto err;
255 258
256 BIO_set_callback(bio,BIO_get_callback(b)); 259 BIO_set_callback(bio, BIO_get_callback(b));
257 BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); 260 BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
258 261
259 if (c->nbio) 262 if (c->nbio) {
260 { 263 if (!BIO_socket_nbio(i, 1)) {
261 if (!BIO_socket_nbio(i,1)) 264 BIOerr(BIO_F_ACPT_STATE, BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
262 {
263 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
264 goto err; 265 goto err;
265 }
266 } 266 }
267 }
267 268
268 /* If the accept BIO has an bio_chain, we dup it and 269 /* If the accept BIO has an bio_chain, we dup it and
269 * put the new socket at the end. */ 270 * put the new socket at the end. */
270 if (c->bio_chain != NULL) 271 if (c->bio_chain != NULL) {
271 { 272 if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
272 if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL)
273 goto err; 273 goto err;
274 if (!BIO_push(dbio,bio)) goto err; 274 if (!BIO_push(dbio, bio)) goto err;
275 bio=dbio; 275 bio = dbio;
276 } 276 }
277 if (BIO_push(b,bio) == NULL) goto err; 277 if (BIO_push(b, bio)
278 == NULL) goto err;
279
280 c->state = ACPT_S_OK;
281 return (1);
278 282
279 c->state=ACPT_S_OK;
280 return(1);
281err: 283err:
282 if (bio != NULL) 284 if (bio != NULL)
283 BIO_free(bio); 285 BIO_free(bio);
284 else if (s >= 0) 286 else if (s >= 0)
285 close(s); 287 close(s);
286 return(0); 288 return (0);
287 /* break; */ 289 /* break; */
288 case ACPT_S_OK: 290 case ACPT_S_OK:
289 if (b->next_bio == NULL) 291 if (b->next_bio == NULL) {
290 { 292 c->state = ACPT_S_GET_ACCEPT_SOCKET;
291 c->state=ACPT_S_GET_ACCEPT_SOCKET;
292 goto again; 293 goto again;
293 } 294 }
294 return(1); 295 return (1);
295 /* break; */ 296 /* break; */
296 default: 297 default:
297 return(0); 298 return (0);
298 /* break; */ 299 /* break; */
299 }
300
301 } 300 }
301}
302 302
303static int acpt_read(BIO *b, char *out, int outl) 303static int
304 { 304acpt_read(BIO *b, char *out, int outl)
305 int ret=0; 305{
306 int ret = 0;
306 BIO_ACCEPT *data; 307 BIO_ACCEPT *data;
307 308
308 BIO_clear_retry_flags(b); 309 BIO_clear_retry_flags(b);
309 data=(BIO_ACCEPT *)b->ptr; 310 data = (BIO_ACCEPT *)b->ptr;
310 311
311 while (b->next_bio == NULL) 312 while (b->next_bio == NULL) {
312 { 313 ret = acpt_state(b, data);
313 ret=acpt_state(b,data); 314 if (ret <= 0)
314 if (ret <= 0) return(ret); 315 return (ret);
315 } 316 }
316 317
317 ret=BIO_read(b->next_bio,out,outl); 318 ret = BIO_read(b->next_bio, out, outl);
318 BIO_copy_next_retry(b); 319 BIO_copy_next_retry(b);
319 return(ret); 320 return (ret);
320 } 321}
321 322
322static int acpt_write(BIO *b, const char *in, int inl) 323static int
323 { 324acpt_write(BIO *b, const char *in, int inl)
325{
324 int ret; 326 int ret;
325 BIO_ACCEPT *data; 327 BIO_ACCEPT *data;
326 328
327 BIO_clear_retry_flags(b); 329 BIO_clear_retry_flags(b);
328 data=(BIO_ACCEPT *)b->ptr; 330 data = (BIO_ACCEPT *)b->ptr;
329 331
330 while (b->next_bio == NULL) 332 while (b->next_bio == NULL) {
331 { 333 ret = acpt_state(b, data);
332 ret=acpt_state(b,data); 334 if (ret <= 0)
333 if (ret <= 0) return(ret); 335 return (ret);
334 } 336 }
335 337
336 ret=BIO_write(b->next_bio,in,inl); 338 ret = BIO_write(b->next_bio, in, inl);
337 BIO_copy_next_retry(b); 339 BIO_copy_next_retry(b);
338 return(ret); 340 return (ret);
339 } 341}
340 342
341static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr) 343static long
342 { 344acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
345{
343 int *ip; 346 int *ip;
344 long ret=1; 347 long ret = 1;
345 BIO_ACCEPT *data; 348 BIO_ACCEPT *data;
346 char **pp; 349 char **pp;
347 350
348 data=(BIO_ACCEPT *)b->ptr; 351 data = (BIO_ACCEPT *)b->ptr;
349 352
350 switch (cmd) 353 switch (cmd) {
351 {
352 case BIO_CTRL_RESET: 354 case BIO_CTRL_RESET:
353 ret=0; 355 ret = 0;
354 data->state=ACPT_S_BEFORE; 356 data->state = ACPT_S_BEFORE;
355 acpt_close_socket(b); 357 acpt_close_socket(b);
356 b->flags=0; 358 b->flags = 0;
357 break; 359 break;
358 case BIO_C_DO_STATE_MACHINE: 360 case BIO_C_DO_STATE_MACHINE:
359 /* use this one to start the connection */ 361 /* use this one to start the connection */
360 ret=(long)acpt_state(b,data); 362 ret = (long)acpt_state(b, data);
361 break; 363 break;
362 case BIO_C_SET_ACCEPT: 364 case BIO_C_SET_ACCEPT:
363 if (ptr != NULL) 365 if (ptr != NULL) {
364 { 366 if (num == 0) {
365 if (num == 0) 367 b->init = 1;
366 {
367 b->init=1;
368 if (data->param_addr != NULL) 368 if (data->param_addr != NULL)
369 OPENSSL_free(data->param_addr); 369 OPENSSL_free(data->param_addr);
370 data->param_addr=BUF_strdup(ptr); 370 data->param_addr = BUF_strdup(ptr);
371 } 371 } else if (num == 1) {
372 else if (num == 1) 372 data->accept_nbio = (ptr != NULL);
373 { 373 } else if (num == 2) {
374 data->accept_nbio=(ptr != NULL);
375 }
376 else if (num == 2)
377 {
378 if (data->bio_chain != NULL) 374 if (data->bio_chain != NULL)
379 BIO_free(data->bio_chain); 375 BIO_free(data->bio_chain);
380 data->bio_chain=(BIO *)ptr; 376 data->bio_chain = (BIO *)ptr;
381 }
382 } 377 }
378 }
383 break; 379 break;
384 case BIO_C_SET_NBIO: 380 case BIO_C_SET_NBIO:
385 data->nbio=(int)num; 381 data->nbio = (int)num;
386 break; 382 break;
387 case BIO_C_SET_FD: 383 case BIO_C_SET_FD:
388 b->init=1; 384 b->init = 1;
389 b->num= *((int *)ptr); 385 b->num= *((int *)ptr);
390 data->accept_sock=b->num; 386 data->accept_sock = b->num;
391 data->state=ACPT_S_GET_ACCEPT_SOCKET; 387 data->state = ACPT_S_GET_ACCEPT_SOCKET;
392 b->shutdown=(int)num; 388 b->shutdown = (int)num;
393 b->init=1; 389 b->init = 1;
394 break; 390 break;
395 case BIO_C_GET_FD: 391 case BIO_C_GET_FD:
396 if (b->init) 392 if (b->init) {
397 { 393 ip = (int *)ptr;
398 ip=(int *)ptr;
399 if (ip != NULL) 394 if (ip != NULL)
400 *ip=data->accept_sock; 395 *ip = data->accept_sock;
401 ret=data->accept_sock; 396 ret = data->accept_sock;
402 } 397 } else
403 else 398 ret = -1;
404 ret= -1;
405 break; 399 break;
406 case BIO_C_GET_ACCEPT: 400 case BIO_C_GET_ACCEPT:
407 if (b->init) 401 if (b->init) {
408 { 402 if (ptr != NULL) {
409 if (ptr != NULL) 403 pp = (char **)ptr;
410 { 404 *pp = data->param_addr;
411 pp=(char **)ptr; 405 } else
412 *pp=data->param_addr; 406 ret = -1;
413 } 407 } else
414 else 408 ret = -1;
415 ret= -1;
416 }
417 else
418 ret= -1;
419 break; 409 break;
420 case BIO_CTRL_GET_CLOSE: 410 case BIO_CTRL_GET_CLOSE:
421 ret=b->shutdown; 411 ret = b->shutdown;
422 break; 412 break;
423 case BIO_CTRL_SET_CLOSE: 413 case BIO_CTRL_SET_CLOSE:
424 b->shutdown=(int)num; 414 b->shutdown = (int)num;
425 break; 415 break;
426 case BIO_CTRL_PENDING: 416 case BIO_CTRL_PENDING:
427 case BIO_CTRL_WPENDING: 417 case BIO_CTRL_WPENDING:
428 ret=0; 418 ret = 0;
429 break; 419 break;
430 case BIO_CTRL_FLUSH: 420 case BIO_CTRL_FLUSH:
431 break; 421 break;
432 case BIO_C_SET_BIND_MODE: 422 case BIO_C_SET_BIND_MODE:
433 data->bind_mode=(int)num; 423 data->bind_mode = (int)num;
434 break; 424 break;
435 case BIO_C_GET_BIND_MODE: 425 case BIO_C_GET_BIND_MODE:
436 ret=(long)data->bind_mode; 426 ret = (long)data->bind_mode;
437 break; 427 break;
438 case BIO_CTRL_DUP: 428 case BIO_CTRL_DUP:
439/* dbio=(BIO *)ptr; 429/* dbio=(BIO *)ptr;
@@ -441,38 +431,41 @@ static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
441 BIO_set_port(dbio,data->param_port); 431 BIO_set_port(dbio,data->param_port);
442 if (data->param_hostname) 432 if (data->param_hostname)
443 BIO_set_hostname(dbio,data->param_hostname); 433 BIO_set_hostname(dbio,data->param_hostname);
444 BIO_set_nbio(dbio,data->nbio); */ 434 BIO_set_nbio(dbio,data->nbio);
435*/
445 break; 436 break;
446 437
447 default: 438 default:
448 ret=0; 439 ret = 0;
449 break; 440 break;
450 }
451 return(ret);
452 } 441 }
453 442 return (ret);
454static int acpt_puts(BIO *bp, const char *str) 443}
455 { 444
456 int n,ret; 445static int
457 446acpt_puts(BIO *bp, const char *str)
458 n=strlen(str); 447{
459 ret=acpt_write(bp,str,n); 448 int n, ret;
460 return(ret); 449
461 } 450 n = strlen(str);
462 451 ret = acpt_write(bp, str, n);
463BIO *BIO_new_accept(char *str) 452 return (ret);
464 { 453}
454
455BIO
456*BIO_new_accept(char *str)
457{
465 BIO *ret; 458 BIO *ret;
466 459
467 ret=BIO_new(BIO_s_accept()); 460 ret = BIO_new(BIO_s_accept());
468 if (ret == NULL) return(NULL); 461 if (ret == NULL)
469 if (BIO_set_accept_port(ret,str)) 462 return (NULL);
470 return(ret); 463 if (BIO_set_accept_port(ret, str))
471 else 464 return (ret);
472 { 465 else {
473 BIO_free(ret); 466 BIO_free(ret);
474 return(NULL); 467 return (NULL);
475 }
476 } 468 }
469}
477 470
478#endif 471#endif