1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
61 #include "internal/cryptlib.h"
62 #include <openssl/bio.h>
64 #ifndef OPENSSL_NO_SOCK
67 * We are currently using deprecated functions here, and GCC warns
68 * us about them, but since we know, we don't want to hear it.
70 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
72 # if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
73 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
77 typedef struct bio_accept_st {
85 * If 0, it means normal, if 1, do a connect on bind failure, and if
86 * there is no-one listening, bind with SO_REUSEADDR. If 2, always use
93 static int acpt_write(BIO *h, const char *buf, int num);
94 static int acpt_read(BIO *h, char *buf, int size);
95 static int acpt_puts(BIO *h, const char *str);
96 static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
97 static int acpt_new(BIO *h);
98 static int acpt_free(BIO *data);
99 static int acpt_state(BIO *b, BIO_ACCEPT *c);
100 static void acpt_close_socket(BIO *data);
101 static BIO_ACCEPT *BIO_ACCEPT_new(void);
102 static void BIO_ACCEPT_free(BIO_ACCEPT *a);
104 # define ACPT_S_BEFORE 1
105 # define ACPT_S_GET_ACCEPT_SOCKET 2
108 static BIO_METHOD methods_acceptp = {
114 NULL, /* connect_gets, */
121 BIO_METHOD *BIO_s_accept(void)
123 return (&methods_acceptp);
126 static int acpt_new(BIO *bi)
131 bi->num = (int)INVALID_SOCKET;
133 if ((ba = BIO_ACCEPT_new()) == NULL)
135 bi->ptr = (char *)ba;
136 ba->state = ACPT_S_BEFORE;
141 static BIO_ACCEPT *BIO_ACCEPT_new(void)
145 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
147 ret->accept_sock = (int)INVALID_SOCKET;
148 ret->bind_mode = BIO_BIND_NORMAL;
152 static void BIO_ACCEPT_free(BIO_ACCEPT *a)
157 OPENSSL_free(a->param_addr);
158 OPENSSL_free(a->addr);
159 BIO_free(a->bio_chain);
163 static void acpt_close_socket(BIO *bio)
167 c = (BIO_ACCEPT *)bio->ptr;
168 if (c->accept_sock != (int)INVALID_SOCKET) {
169 shutdown(c->accept_sock, 2);
170 closesocket(c->accept_sock);
171 c->accept_sock = (int)INVALID_SOCKET;
172 bio->num = (int)INVALID_SOCKET;
176 static int acpt_free(BIO *a)
182 data = (BIO_ACCEPT *)a->ptr;
185 acpt_close_socket(a);
186 BIO_ACCEPT_free(data);
194 static int acpt_state(BIO *b, BIO_ACCEPT *c)
196 BIO *bio = NULL, *dbio;
203 if (c->param_addr == NULL) {
204 BIOerr(BIO_F_ACPT_STATE, BIO_R_NO_ACCEPT_PORT_SPECIFIED);
207 s = BIO_get_accept_socket(c->param_addr, c->bind_mode);
208 if (s == (int)INVALID_SOCKET)
211 if (c->accept_nbio) {
212 if (!BIO_socket_nbio(s, 1)) {
214 BIOerr(BIO_F_ACPT_STATE,
215 BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
221 c->state = ACPT_S_GET_ACCEPT_SOCKET;
224 case ACPT_S_GET_ACCEPT_SOCKET:
225 if (b->next_bio != NULL) {
226 c->state = ACPT_S_OK;
229 BIO_clear_retry_flags(b);
231 i = BIO_accept(c->accept_sock, &(c->addr));
233 /* -2 return means we should retry */
235 BIO_set_retry_special(b);
236 b->retry_reason = BIO_RR_ACCEPT;
243 bio = BIO_new_socket(i, BIO_CLOSE);
247 BIO_set_callback(bio, BIO_get_callback(b));
248 BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
251 if (!BIO_socket_nbio(i, 1)) {
252 BIOerr(BIO_F_ACPT_STATE,
253 BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
259 * If the accept BIO has an bio_chain, we dup it and put the new
262 if (c->bio_chain != NULL) {
263 if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
265 if (!BIO_push(dbio, bio))
269 if (BIO_push(b, bio) == NULL)
272 c->state = ACPT_S_OK;
282 if (b->next_bio == NULL) {
283 c->state = ACPT_S_GET_ACCEPT_SOCKET;
295 static int acpt_read(BIO *b, char *out, int outl)
300 BIO_clear_retry_flags(b);
301 data = (BIO_ACCEPT *)b->ptr;
303 while (b->next_bio == NULL) {
304 ret = acpt_state(b, data);
309 ret = BIO_read(b->next_bio, out, outl);
310 BIO_copy_next_retry(b);
314 static int acpt_write(BIO *b, const char *in, int inl)
319 BIO_clear_retry_flags(b);
320 data = (BIO_ACCEPT *)b->ptr;
322 while (b->next_bio == NULL) {
323 ret = acpt_state(b, data);
328 ret = BIO_write(b->next_bio, in, inl);
329 BIO_copy_next_retry(b);
333 static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
340 data = (BIO_ACCEPT *)b->ptr;
345 data->state = ACPT_S_BEFORE;
346 acpt_close_socket(b);
349 case BIO_C_DO_STATE_MACHINE:
350 /* use this one to start the connection */
351 ret = (long)acpt_state(b, data);
353 case BIO_C_SET_ACCEPT:
357 OPENSSL_free(data->param_addr);
358 data->param_addr = OPENSSL_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))