1 /* crypto/bio/bss_acpt.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
67 /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */
70 #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
72 #define SOCKET_PROTOCOL IPPROTO_TCP
75 typedef struct bio_accept_st
85 /* If 0, it means normal, if 1, do a connect on bind failure,
86 * and if there is no-one listening, bind with SO_REUSEADDR.
87 * If 2, always use SO_REUSEADDR. */
93 static int acpt_write(BIO *h,char *buf,int num);
94 static int acpt_read(BIO *h,char *buf,int size);
95 static int acpt_puts(BIO *h,char *str);
96 static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2);
97 static int acpt_new(BIO *h);
98 static int acpt_free(BIO *data);
100 static int acpt_write();
101 static int acpt_read();
102 static int acpt_puts();
103 static long acpt_ctrl();
104 static int acpt_new();
105 static int acpt_free();
109 static int acpt_state(BIO *b, BIO_ACCEPT *c);
110 static void acpt_close_socket(BIO *data);
111 BIO_ACCEPT *BIO_ACCEPT_new(void );
112 void BIO_ACCEPT_free(BIO_ACCEPT *a);
116 static int acpt_state();
117 static void acpt_close_socket();
118 BIO_ACCEPT *BIO_ACCEPT_new();
119 void BIO_ACCEPT_free();
122 #define ACPT_S_BEFORE 1
123 #define ACPT_S_GET_ACCEPT_SOCKET 2
126 static BIO_METHOD methods_acceptp=
133 NULL, /* connect_gets, */
139 BIO_METHOD *BIO_s_accept()
141 return(&methods_acceptp);
144 static int acpt_new(bi)
150 bi->num=INVALID_SOCKET;
152 if ((ba=BIO_ACCEPT_new()) == NULL)
155 ba->state=ACPT_S_BEFORE;
160 BIO_ACCEPT *BIO_ACCEPT_new()
164 if ((ret=(BIO_ACCEPT *)Malloc(sizeof(BIO_ACCEPT))) == NULL)
167 memset(ret,0,sizeof(BIO_ACCEPT));
168 ret->accept_sock=INVALID_SOCKET;
169 ret->bind_mode=BIO_BIND_NORMAL;
173 void BIO_ACCEPT_free(a)
179 if (a->param_addr != NULL) Free(a->param_addr);
180 if (a->addr != NULL) Free(a->addr);
181 if (a->bio_chain != NULL) BIO_free(a->bio_chain);
185 static void acpt_close_socket(bio)
190 c=(BIO_ACCEPT *)bio->ptr;
191 if (c->accept_sock != INVALID_SOCKET)
193 shutdown(c->accept_sock,2);
194 closesocket(c->accept_sock);
195 c->accept_sock=INVALID_SOCKET;
196 bio->num=INVALID_SOCKET;
200 static int acpt_free(a)
205 if (a == NULL) return(0);
206 data=(BIO_ACCEPT *)a->ptr;
210 acpt_close_socket(a);
211 BIO_ACCEPT_free(data);
219 static int acpt_state(b,c)
231 if (c->param_addr == NULL)
233 BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
236 s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
237 if (s == INVALID_SOCKET)
242 if (!BIO_socket_nbio(s,1))
245 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
251 c->state=ACPT_S_GET_ACCEPT_SOCKET;
254 case ACPT_S_GET_ACCEPT_SOCKET:
255 if (b->next_bio != NULL)
260 i=BIO_accept(c->accept_sock,&(c->addr));
261 if (i < 0) return(i);
262 bio=BIO_new_socket(i,BIO_CLOSE);
263 if (bio == NULL) goto err;
265 BIO_set_callback(bio,BIO_get_callback(b));
266 BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
270 if (!BIO_socket_nbio(i,1))
272 BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
277 /* If the accept BIO has an bio_chain, we dup it and
278 * put the new socket at the end. */
279 if (c->bio_chain != NULL)
281 if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL)
283 if (!BIO_push(dbio,bio)) goto err;
286 if (BIO_push(b,bio) == NULL) goto err;
298 if (b->next_bio == NULL)
300 c->state=ACPT_S_GET_ACCEPT_SOCKET;
312 static int acpt_read(b,out,outl)
320 BIO_clear_retry_flags(b);
321 data=(BIO_ACCEPT *)b->ptr;
323 while (b->next_bio == NULL)
325 ret=acpt_state(b,data);
326 if (ret <= 0) return(ret);
329 ret=BIO_read(b->next_bio,out,outl);
330 BIO_copy_next_retry(b);
334 static int acpt_write(b,in,inl)
342 BIO_clear_retry_flags(b);
343 data=(BIO_ACCEPT *)b->ptr;
345 while (b->next_bio == NULL)
347 ret=acpt_state(b,data);
348 if (ret <= 0) return(ret);
351 ret=BIO_write(b->next_bio,in,inl);
352 BIO_copy_next_retry(b);
356 static long acpt_ctrl(b,cmd,num,ptr)
368 data=(BIO_ACCEPT *)b->ptr;
374 data->state=ACPT_S_BEFORE;
375 acpt_close_socket(b);
378 case BIO_C_DO_STATE_MACHINE:
379 /* use this one to start the connection */
380 ret=(long)acpt_state(b,data);
382 case BIO_C_SET_ACCEPT:
388 if (data->param_addr != NULL)
389 Free(data->param_addr);
390 data->param_addr=BUF_strdup(ptr);
394 data->accept_nbio=(ptr != NULL);
398 if (data->bio_chain != NULL)
399 BIO_free(data->bio_chain);
400 data->bio_chain=(BIO *)ptr;
409 b->num= *((int *)ptr);
410 data->accept_sock=b->num;
411 data->state=ACPT_S_GET_ACCEPT_SOCKET;
412 b->shutdown=(int)num;
420 *ip=data->accept_sock;
421 ret=data->accept_sock;
426 case BIO_C_GET_ACCEPT:
432 *pp=data->param_addr;
440 case BIO_CTRL_GET_CLOSE:
443 case BIO_CTRL_SET_CLOSE:
444 b->shutdown=(int)num;
446 case BIO_CTRL_PENDING:
447 case BIO_CTRL_WPENDING:
452 case BIO_C_SET_BIND_MODE:
453 data->bind_mode=(int)num;
455 case BIO_C_GET_BIND_MODE:
456 ret=(long)data->bind_mode;
460 /* if (data->param_port) EAY EAY
461 BIO_set_port(dbio,data->param_port);
462 if (data->param_hostname)
463 BIO_set_hostname(dbio,data->param_hostname);
464 BIO_set_nbio(dbio,data->nbio); */
474 static int acpt_puts(bp,str)
481 ret=acpt_write(bp,str,n);
485 BIO *BIO_new_accept(str)
490 ret=BIO_new(BIO_s_accept());
491 if (ret == NULL) return(NULL);
492 if (BIO_set_accept_port(ret,str))