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.]
63 #include <openssl/bio.h>
65 #ifndef OPENSSL_NO_SOCK
67 # if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
68 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
72 typedef struct bio_accept_st
{
80 * If 0, it means normal, if 1, do a connect on bind failure, and if
81 * there is no-one listening, bind with SO_REUSEADDR. If 2, always use
88 static int acpt_write(BIO
*h
, const char *buf
, int num
);
89 static int acpt_read(BIO
*h
, char *buf
, int size
);
90 static int acpt_puts(BIO
*h
, const char *str
);
91 static long acpt_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
92 static int acpt_new(BIO
*h
);
93 static int acpt_free(BIO
*data
);
94 static int acpt_state(BIO
*b
, BIO_ACCEPT
*c
);
95 static void acpt_close_socket(BIO
*data
);
96 static BIO_ACCEPT
*BIO_ACCEPT_new(void);
97 static void BIO_ACCEPT_free(BIO_ACCEPT
*a
);
99 # define ACPT_S_BEFORE 1
100 # define ACPT_S_GET_ACCEPT_SOCKET 2
103 static BIO_METHOD methods_acceptp
= {
109 NULL
, /* connect_gets, */
116 BIO_METHOD
*BIO_s_accept(void)
118 return (&methods_acceptp
);
121 static int acpt_new(BIO
*bi
)
126 bi
->num
= INVALID_SOCKET
;
128 if ((ba
= BIO_ACCEPT_new()) == NULL
)
130 bi
->ptr
= (char *)ba
;
131 ba
->state
= ACPT_S_BEFORE
;
136 static BIO_ACCEPT
*BIO_ACCEPT_new(void)
140 if ((ret
= OPENSSL_malloc(sizeof(BIO_ACCEPT
))) == NULL
)
143 memset(ret
, 0, sizeof(BIO_ACCEPT
));
144 ret
->accept_sock
= INVALID_SOCKET
;
145 ret
->bind_mode
= BIO_BIND_NORMAL
;
149 static void BIO_ACCEPT_free(BIO_ACCEPT
*a
)
154 if (a
->param_addr
!= NULL
)
155 OPENSSL_free(a
->param_addr
);
157 OPENSSL_free(a
->addr
);
158 BIO_free(a
->bio_chain
);
162 static void acpt_close_socket(BIO
*bio
)
166 c
= (BIO_ACCEPT
*)bio
->ptr
;
167 if (c
->accept_sock
!= INVALID_SOCKET
) {
168 shutdown(c
->accept_sock
, 2);
169 closesocket(c
->accept_sock
);
170 c
->accept_sock
= INVALID_SOCKET
;
171 bio
->num
= INVALID_SOCKET
;
175 static int acpt_free(BIO
*a
)
181 data
= (BIO_ACCEPT
*)a
->ptr
;
184 acpt_close_socket(a
);
185 BIO_ACCEPT_free(data
);
193 static int acpt_state(BIO
*b
, BIO_ACCEPT
*c
)
195 BIO
*bio
= NULL
, *dbio
;
202 if (c
->param_addr
== NULL
) {
203 BIOerr(BIO_F_ACPT_STATE
, BIO_R_NO_ACCEPT_PORT_SPECIFIED
);
206 s
= BIO_get_accept_socket(c
->param_addr
, c
->bind_mode
);
207 if (s
== INVALID_SOCKET
)
210 if (c
->accept_nbio
) {
211 if (!BIO_socket_nbio(s
, 1)) {
213 BIOerr(BIO_F_ACPT_STATE
,
214 BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET
);
220 c
->state
= ACPT_S_GET_ACCEPT_SOCKET
;
223 case ACPT_S_GET_ACCEPT_SOCKET
:
224 if (b
->next_bio
!= NULL
) {
225 c
->state
= ACPT_S_OK
;
228 BIO_clear_retry_flags(b
);
230 i
= BIO_accept(c
->accept_sock
, &(c
->addr
));
232 /* -2 return means we should retry */
234 BIO_set_retry_special(b
);
235 b
->retry_reason
= BIO_RR_ACCEPT
;
242 bio
= BIO_new_socket(i
, BIO_CLOSE
);
246 BIO_set_callback(bio
, BIO_get_callback(b
));
247 BIO_set_callback_arg(bio
, BIO_get_callback_arg(b
));
250 if (!BIO_socket_nbio(i
, 1)) {
251 BIOerr(BIO_F_ACPT_STATE
,
252 BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET
);
258 * If the accept BIO has an bio_chain, we dup it and put the new
261 if (c
->bio_chain
!= NULL
) {
262 if ((dbio
= BIO_dup_chain(c
->bio_chain
)) == NULL
)
264 if (!BIO_push(dbio
, bio
))
268 if (BIO_push(b
, bio
) == NULL
)
271 c
->state
= ACPT_S_OK
;
281 if (b
->next_bio
== NULL
) {
282 c
->state
= ACPT_S_GET_ACCEPT_SOCKET
;
294 static int acpt_read(BIO
*b
, char *out
, int outl
)
299 BIO_clear_retry_flags(b
);
300 data
= (BIO_ACCEPT
*)b
->ptr
;
302 while (b
->next_bio
== NULL
) {
303 ret
= acpt_state(b
, data
);
308 ret
= BIO_read(b
->next_bio
, out
, outl
);
309 BIO_copy_next_retry(b
);
313 static int acpt_write(BIO
*b
, const char *in
, int inl
)
318 BIO_clear_retry_flags(b
);
319 data
= (BIO_ACCEPT
*)b
->ptr
;
321 while (b
->next_bio
== NULL
) {
322 ret
= acpt_state(b
, data
);
327 ret
= BIO_write(b
->next_bio
, in
, inl
);
328 BIO_copy_next_retry(b
);
332 static long acpt_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
339 data
= (BIO_ACCEPT
*)b
->ptr
;
344 data
->state
= ACPT_S_BEFORE
;
345 acpt_close_socket(b
);
348 case BIO_C_DO_STATE_MACHINE
:
349 /* use this one to start the connection */
350 ret
= (long)acpt_state(b
, data
);
352 case BIO_C_SET_ACCEPT
:
356 if (data
->param_addr
!= NULL
)
357 OPENSSL_free(data
->param_addr
);
358 data
->param_addr
= BUF_strdup(ptr
);
359 } else if (num
== 1) {
360 data
->accept_nbio
= (ptr
!= NULL
);
361 } else if (num
== 2) {
362 BIO_free(data
->bio_chain
);
363 data
->bio_chain
= (BIO
*)ptr
;
368 data
->nbio
= (int)num
;
372 b
->num
= *((int *)ptr
);
373 data
->accept_sock
= b
->num
;
374 data
->state
= ACPT_S_GET_ACCEPT_SOCKET
;
375 b
->shutdown
= (int)num
;
382 *ip
= data
->accept_sock
;
383 ret
= data
->accept_sock
;
387 case BIO_C_GET_ACCEPT
:
391 *pp
= data
->param_addr
;
397 case BIO_CTRL_GET_CLOSE
:
400 case BIO_CTRL_SET_CLOSE
:
401 b
->shutdown
= (int)num
;
403 case BIO_CTRL_PENDING
:
404 case BIO_CTRL_WPENDING
:
409 case BIO_C_SET_BIND_MODE
:
410 data
->bind_mode
= (int)num
;
412 case BIO_C_GET_BIND_MODE
:
413 ret
= (long)data
->bind_mode
;
417 if (data->param_port) EAY EAY
418 BIO_set_port(dbio,data->param_port);
419 if (data->param_hostname)
420 BIO_set_hostname(dbio,data->param_hostname);
421 BIO_set_nbio(dbio,data->nbio); */
431 static int acpt_puts(BIO
*bp
, const char *str
)
436 ret
= acpt_write(bp
, str
, n
);
440 BIO
*BIO_new_accept(const char *str
)
444 ret
= BIO_new(BIO_s_accept());
447 if (BIO_set_accept_port(ret
, str
))