diff options
Diffstat (limited to 'src/lib/libcrypto/bio/bss_acpt.c')
-rw-r--r-- | src/lib/libcrypto/bio/bss_acpt.c | 500 |
1 files changed, 500 insertions, 0 deletions
diff --git a/src/lib/libcrypto/bio/bss_acpt.c b/src/lib/libcrypto/bio/bss_acpt.c new file mode 100644 index 0000000000..e49902fa9f --- /dev/null +++ b/src/lib/libcrypto/bio/bss_acpt.c | |||
@@ -0,0 +1,500 @@ | |||
1 | /* crypto/bio/bss_acpt.c */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | |||
59 | #ifndef NO_SOCK | ||
60 | |||
61 | #include <stdio.h> | ||
62 | #include <errno.h> | ||
63 | #define USE_SOCKETS | ||
64 | #include "cryptlib.h" | ||
65 | #include "bio.h" | ||
66 | |||
67 | /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ | ||
68 | |||
69 | #ifdef WIN16 | ||
70 | #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ | ||
71 | #else | ||
72 | #define SOCKET_PROTOCOL IPPROTO_TCP | ||
73 | #endif | ||
74 | |||
75 | typedef struct bio_accept_st | ||
76 | { | ||
77 | int state; | ||
78 | char *param_addr; | ||
79 | |||
80 | int accept_sock; | ||
81 | int accept_nbio; | ||
82 | |||
83 | char *addr; | ||
84 | int nbio; | ||
85 | BIO *bio_chain; | ||
86 | } BIO_ACCEPT; | ||
87 | |||
88 | #ifndef NOPROTO | ||
89 | static int acpt_write(BIO *h,char *buf,int num); | ||
90 | static int acpt_read(BIO *h,char *buf,int size); | ||
91 | static int acpt_puts(BIO *h,char *str); | ||
92 | static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
93 | static int acpt_new(BIO *h); | ||
94 | 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); | ||
106 | static void acpt_close_socket(BIO *data); | ||
107 | BIO_ACCEPT *BIO_ACCEPT_new(void ); | ||
108 | void BIO_ACCEPT_free(BIO_ACCEPT *a); | ||
109 | |||
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 | ||
119 | #define ACPT_S_GET_ACCEPT_SOCKET 2 | ||
120 | #define ACPT_S_OK 3 | ||
121 | |||
122 | static BIO_METHOD methods_acceptp= | ||
123 | { | ||
124 | BIO_TYPE_ACCEPT, | ||
125 | "socket accept", | ||
126 | acpt_write, | ||
127 | acpt_read, | ||
128 | acpt_puts, | ||
129 | NULL, /* connect_gets, */ | ||
130 | acpt_ctrl, | ||
131 | acpt_new, | ||
132 | acpt_free, | ||
133 | }; | ||
134 | |||
135 | BIO_METHOD *BIO_s_accept() | ||
136 | { | ||
137 | return(&methods_acceptp); | ||
138 | } | ||
139 | |||
140 | static int acpt_new(bi) | ||
141 | BIO *bi; | ||
142 | { | ||
143 | BIO_ACCEPT *ba; | ||
144 | |||
145 | bi->init=0; | ||
146 | bi->num=INVALID_SOCKET; | ||
147 | bi->flags=0; | ||
148 | if ((ba=BIO_ACCEPT_new()) == NULL) | ||
149 | return(0); | ||
150 | bi->ptr=(char *)ba; | ||
151 | ba->state=ACPT_S_BEFORE; | ||
152 | bi->shutdown=1; | ||
153 | return(1); | ||
154 | } | ||
155 | |||
156 | BIO_ACCEPT *BIO_ACCEPT_new() | ||
157 | { | ||
158 | BIO_ACCEPT *ret; | ||
159 | |||
160 | if ((ret=(BIO_ACCEPT *)Malloc(sizeof(BIO_ACCEPT))) == NULL) | ||
161 | return(NULL); | ||
162 | |||
163 | memset(ret,0,sizeof(BIO_ACCEPT)); | ||
164 | ret->accept_sock=INVALID_SOCKET; | ||
165 | return(ret); | ||
166 | } | ||
167 | |||
168 | void BIO_ACCEPT_free(a) | ||
169 | BIO_ACCEPT *a; | ||
170 | { | ||
171 | if (a->param_addr != NULL) Free(a->param_addr); | ||
172 | if (a->addr != NULL) Free(a->addr); | ||
173 | if (a->bio_chain != NULL) BIO_free(a->bio_chain); | ||
174 | Free(a); | ||
175 | } | ||
176 | |||
177 | static void acpt_close_socket(bio) | ||
178 | BIO *bio; | ||
179 | { | ||
180 | BIO_ACCEPT *c; | ||
181 | |||
182 | c=(BIO_ACCEPT *)bio->ptr; | ||
183 | if (c->accept_sock != INVALID_SOCKET) | ||
184 | { | ||
185 | shutdown(c->accept_sock,2); | ||
186 | # ifdef WINDOWS | ||
187 | closesocket(c->accept_sock); | ||
188 | # else | ||
189 | close(c->accept_sock); | ||
190 | # endif | ||
191 | c->accept_sock=INVALID_SOCKET; | ||
192 | bio->num=INVALID_SOCKET; | ||
193 | } | ||
194 | } | ||
195 | |||
196 | static int acpt_free(a) | ||
197 | BIO *a; | ||
198 | { | ||
199 | BIO_ACCEPT *data; | ||
200 | |||
201 | if (a == NULL) return(0); | ||
202 | data=(BIO_ACCEPT *)a->ptr; | ||
203 | |||
204 | if (a->shutdown) | ||
205 | { | ||
206 | acpt_close_socket(a); | ||
207 | BIO_ACCEPT_free(data); | ||
208 | a->ptr=NULL; | ||
209 | a->flags=0; | ||
210 | a->init=0; | ||
211 | } | ||
212 | return(1); | ||
213 | } | ||
214 | |||
215 | static int acpt_state(b,c) | ||
216 | BIO *b; | ||
217 | BIO_ACCEPT *c; | ||
218 | { | ||
219 | BIO *bio=NULL,*dbio; | ||
220 | unsigned long l=1; | ||
221 | int s= -1; | ||
222 | int i; | ||
223 | |||
224 | again: | ||
225 | switch (c->state) | ||
226 | { | ||
227 | case ACPT_S_BEFORE: | ||
228 | if (c->param_addr == NULL) | ||
229 | { | ||
230 | BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED); | ||
231 | return(-1); | ||
232 | } | ||
233 | s=BIO_get_accept_socket(c->param_addr); | ||
234 | if (s == INVALID_SOCKET) | ||
235 | return(-1); | ||
236 | |||
237 | #ifdef FIONBIO | ||
238 | if (c->accept_nbio) | ||
239 | { | ||
240 | i=BIO_socket_ioctl(b->num,FIONBIO,&l); | ||
241 | if (i < 0) | ||
242 | { | ||
243 | #ifdef WINDOWS | ||
244 | closesocket(s); | ||
245 | #else | ||
246 | close(s); | ||
247 | # endif | ||
248 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET); | ||
249 | return(-1); | ||
250 | } | ||
251 | } | ||
252 | #endif | ||
253 | c->accept_sock=s; | ||
254 | b->num=s; | ||
255 | c->state=ACPT_S_GET_ACCEPT_SOCKET; | ||
256 | return(1); | ||
257 | break; | ||
258 | case ACPT_S_GET_ACCEPT_SOCKET: | ||
259 | if (b->next_bio != NULL) | ||
260 | { | ||
261 | c->state=ACPT_S_OK; | ||
262 | goto again; | ||
263 | } | ||
264 | i=BIO_accept(c->accept_sock,&(c->addr)); | ||
265 | if (i < 0) return(i); | ||
266 | bio=BIO_new_socket(i,BIO_CLOSE); | ||
267 | if (bio == NULL) goto err; | ||
268 | |||
269 | BIO_set_callback(bio,BIO_get_callback(b)); | ||
270 | BIO_set_callback_arg(bio,BIO_get_callback_arg(b)); | ||
271 | |||
272 | #ifdef FIONBIO | ||
273 | if (c->nbio) | ||
274 | { | ||
275 | i=BIO_socket_ioctl(i,FIONBIO,&l); | ||
276 | if (i < 0) | ||
277 | { | ||
278 | BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET); | ||
279 | goto err; | ||
280 | } | ||
281 | } | ||
282 | #endif | ||
283 | |||
284 | /* If the accept BIO has an bio_chain, we dup it and | ||
285 | * put the new socket at the end. */ | ||
286 | if (c->bio_chain != NULL) | ||
287 | { | ||
288 | if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL) | ||
289 | goto err; | ||
290 | if (!BIO_push(dbio,bio)) goto err; | ||
291 | bio=dbio; | ||
292 | } | ||
293 | if (BIO_push(b,bio) == NULL) goto err; | ||
294 | |||
295 | c->state=ACPT_S_OK; | ||
296 | return(1); | ||
297 | err: | ||
298 | if (bio != NULL) | ||
299 | BIO_free(bio); | ||
300 | else if (s >= 0) | ||
301 | { | ||
302 | #ifdef WINDOWS | ||
303 | closesocket(s); | ||
304 | #else | ||
305 | close(s); | ||
306 | # endif | ||
307 | } | ||
308 | return(0); | ||
309 | break; | ||
310 | case ACPT_S_OK: | ||
311 | if (b->next_bio == NULL) | ||
312 | { | ||
313 | c->state=ACPT_S_GET_ACCEPT_SOCKET; | ||
314 | goto again; | ||
315 | } | ||
316 | return(1); | ||
317 | break; | ||
318 | default: | ||
319 | return(0); | ||
320 | break; | ||
321 | } | ||
322 | |||
323 | } | ||
324 | |||
325 | static int acpt_read(b,out,outl) | ||
326 | BIO *b; | ||
327 | char *out; | ||
328 | int outl; | ||
329 | { | ||
330 | int ret=0; | ||
331 | BIO_ACCEPT *data; | ||
332 | |||
333 | BIO_clear_retry_flags(b); | ||
334 | data=(BIO_ACCEPT *)b->ptr; | ||
335 | |||
336 | while (b->next_bio == NULL) | ||
337 | { | ||
338 | ret=acpt_state(b,data); | ||
339 | if (ret <= 0) return(ret); | ||
340 | } | ||
341 | |||
342 | ret=BIO_read(b->next_bio,out,outl); | ||
343 | BIO_copy_next_retry(b); | ||
344 | return(ret); | ||
345 | } | ||
346 | |||
347 | static int acpt_write(b,in,inl) | ||
348 | BIO *b; | ||
349 | char *in; | ||
350 | int inl; | ||
351 | { | ||
352 | int ret; | ||
353 | BIO_ACCEPT *data; | ||
354 | |||
355 | BIO_clear_retry_flags(b); | ||
356 | data=(BIO_ACCEPT *)b->ptr; | ||
357 | |||
358 | while (b->next_bio == NULL) | ||
359 | { | ||
360 | ret=acpt_state(b,data); | ||
361 | if (ret <= 0) return(ret); | ||
362 | } | ||
363 | |||
364 | ret=BIO_write(b->next_bio,in,inl); | ||
365 | BIO_copy_next_retry(b); | ||
366 | return(ret); | ||
367 | } | ||
368 | |||
369 | static long acpt_ctrl(b,cmd,num,ptr) | ||
370 | BIO *b; | ||
371 | int cmd; | ||
372 | long num; | ||
373 | char *ptr; | ||
374 | { | ||
375 | BIO *dbio; | ||
376 | int *ip; | ||
377 | long ret=1; | ||
378 | BIO_ACCEPT *data; | ||
379 | char **pp; | ||
380 | |||
381 | data=(BIO_ACCEPT *)b->ptr; | ||
382 | |||
383 | switch (cmd) | ||
384 | { | ||
385 | case BIO_CTRL_RESET: | ||
386 | ret=0; | ||
387 | data->state=ACPT_S_BEFORE; | ||
388 | acpt_close_socket(b); | ||
389 | b->flags=0; | ||
390 | break; | ||
391 | case BIO_C_DO_STATE_MACHINE: | ||
392 | /* use this one to start the connection */ | ||
393 | ret=(long)acpt_state(b,data); | ||
394 | break; | ||
395 | case BIO_C_SET_ACCEPT: | ||
396 | if (ptr != NULL) | ||
397 | { | ||
398 | if (num == 0) | ||
399 | { | ||
400 | b->init=1; | ||
401 | if (data->param_addr != NULL) | ||
402 | Free(data->param_addr); | ||
403 | data->param_addr=BUF_strdup(ptr); | ||
404 | } | ||
405 | else if (num == 1) | ||
406 | { | ||
407 | data->accept_nbio=(ptr != NULL); | ||
408 | } | ||
409 | else if (num == 2) | ||
410 | { | ||
411 | if (data->bio_chain != NULL) | ||
412 | BIO_free(data->bio_chain); | ||
413 | data->bio_chain=(BIO *)ptr; | ||
414 | } | ||
415 | } | ||
416 | break; | ||
417 | case BIO_C_SET_NBIO: | ||
418 | data->nbio=(int)num; | ||
419 | break; | ||
420 | case BIO_C_GET_FD: | ||
421 | if (b->init) | ||
422 | { | ||
423 | ip=(int *)ptr; | ||
424 | if (ip != NULL) | ||
425 | *ip=data->accept_sock; | ||
426 | ret=b->num; | ||
427 | } | ||
428 | else | ||
429 | ret= -1; | ||
430 | break; | ||
431 | case BIO_C_GET_ACCEPT: | ||
432 | if (b->init) | ||
433 | { | ||
434 | if (ptr != NULL) | ||
435 | { | ||
436 | pp=(char **)ptr; | ||
437 | *pp=data->param_addr; | ||
438 | } | ||
439 | else | ||
440 | ret= -1; | ||
441 | } | ||
442 | else | ||
443 | ret= -1; | ||
444 | break; | ||
445 | case BIO_CTRL_GET_CLOSE: | ||
446 | ret=b->shutdown; | ||
447 | break; | ||
448 | case BIO_CTRL_SET_CLOSE: | ||
449 | b->shutdown=(int)num; | ||
450 | break; | ||
451 | case BIO_CTRL_PENDING: | ||
452 | case BIO_CTRL_WPENDING: | ||
453 | ret=0; | ||
454 | break; | ||
455 | case BIO_CTRL_FLUSH: | ||
456 | break; | ||
457 | case BIO_CTRL_DUP: | ||
458 | dbio=(BIO *)ptr; | ||
459 | /* if (data->param_port) EAY EAY | ||
460 | BIO_set_port(dbio,data->param_port); | ||
461 | if (data->param_hostname) | ||
462 | BIO_set_hostname(dbio,data->param_hostname); | ||
463 | BIO_set_nbio(dbio,data->nbio); */ | ||
464 | break; | ||
465 | |||
466 | default: | ||
467 | ret=0; | ||
468 | break; | ||
469 | } | ||
470 | return(ret); | ||
471 | } | ||
472 | |||
473 | static int acpt_puts(bp,str) | ||
474 | BIO *bp; | ||
475 | char *str; | ||
476 | { | ||
477 | int n,ret; | ||
478 | |||
479 | n=strlen(str); | ||
480 | ret=acpt_write(bp,str,n); | ||
481 | return(ret); | ||
482 | } | ||
483 | |||
484 | BIO *BIO_new_accept(str) | ||
485 | char *str; | ||
486 | { | ||
487 | BIO *ret; | ||
488 | |||
489 | ret=BIO_new(BIO_s_accept()); | ||
490 | if (ret == NULL) return(NULL); | ||
491 | if (BIO_set_accept_port(ret,str)) | ||
492 | return(ret); | ||
493 | else | ||
494 | { | ||
495 | BIO_free(ret); | ||
496 | return(NULL); | ||
497 | } | ||
498 | } | ||
499 | |||
500 | #endif | ||