]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bss_acpt.c
Rename some BUF_xxx to OPENSSL_xxx
[thirdparty/openssl.git] / crypto / bio / bss_acpt.c
CommitLineData
d02b48c6 1/* crypto/bio/bss_acpt.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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.
0f113f3e 8 *
d02b48c6
RE
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).
0f113f3e 15 *
d02b48c6
RE
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.
0f113f3e 22 *
d02b48c6
RE
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 :-).
0f113f3e 37 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 40 *
d02b48c6
RE
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.
0f113f3e 52 *
d02b48c6
RE
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
d02b48c6
RE
59#include <stdio.h>
60#include <errno.h>
61#define USE_SOCKETS
b39fc560 62#include "internal/cryptlib.h"
ec577822 63#include <openssl/bio.h>
d02b48c6 64
9ba4cc00
RL
65#ifndef OPENSSL_NO_SOCK
66
0f113f3e 67# if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
75e0770d 68/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
0f113f3e
MC
69# undef FIONBIO
70# endif
71
72typedef struct bio_accept_st {
73 int state;
74 char *param_addr;
75 int accept_sock;
76 int accept_nbio;
77 char *addr;
78 int nbio;
79 /*
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
82 * SO_REUSEADDR.
83 */
84 int bind_mode;
85 BIO *bio_chain;
86} BIO_ACCEPT;
d02b48c6 87
0e1c0612
UM
88static int acpt_write(BIO *h, const char *buf, int num);
89static int acpt_read(BIO *h, char *buf, int size);
90static int acpt_puts(BIO *h, const char *str);
91static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
d02b48c6
RE
92static int acpt_new(BIO *h);
93static int acpt_free(BIO *data);
d02b48c6
RE
94static int acpt_state(BIO *b, BIO_ACCEPT *c);
95static void acpt_close_socket(BIO *data);
0f113f3e 96static BIO_ACCEPT *BIO_ACCEPT_new(void);
b3e72fc3 97static void BIO_ACCEPT_free(BIO_ACCEPT *a);
d02b48c6 98
0f113f3e
MC
99# define ACPT_S_BEFORE 1
100# define ACPT_S_GET_ACCEPT_SOCKET 2
101# define ACPT_S_OK 3
102
103static BIO_METHOD methods_acceptp = {
104 BIO_TYPE_ACCEPT,
105 "socket accept",
106 acpt_write,
107 acpt_read,
108 acpt_puts,
109 NULL, /* connect_gets, */
110 acpt_ctrl,
111 acpt_new,
112 acpt_free,
113 NULL,
114};
d02b48c6 115
6b691a5c 116BIO_METHOD *BIO_s_accept(void)
0f113f3e
MC
117{
118 return (&methods_acceptp);
119}
d02b48c6 120
6b691a5c 121static int acpt_new(BIO *bi)
0f113f3e
MC
122{
123 BIO_ACCEPT *ba;
124
125 bi->init = 0;
b13fdc48 126 bi->num = (int)INVALID_SOCKET;
0f113f3e
MC
127 bi->flags = 0;
128 if ((ba = BIO_ACCEPT_new()) == NULL)
129 return (0);
130 bi->ptr = (char *)ba;
131 ba->state = ACPT_S_BEFORE;
132 bi->shutdown = 1;
133 return (1);
134}
d02b48c6 135
b3e72fc3 136static BIO_ACCEPT *BIO_ACCEPT_new(void)
0f113f3e
MC
137{
138 BIO_ACCEPT *ret;
d02b48c6 139
b51bce94 140 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
0f113f3e 141 return (NULL);
b13fdc48 142 ret->accept_sock = (int)INVALID_SOCKET;
0f113f3e
MC
143 ret->bind_mode = BIO_BIND_NORMAL;
144 return (ret);
145}
d02b48c6 146
b3e72fc3 147static void BIO_ACCEPT_free(BIO_ACCEPT *a)
0f113f3e
MC
148{
149 if (a == NULL)
150 return;
151
b548a1f1
RS
152 OPENSSL_free(a->param_addr);
153 OPENSSL_free(a->addr);
ca3a82c3 154 BIO_free(a->bio_chain);
0f113f3e
MC
155 OPENSSL_free(a);
156}
d02b48c6 157
6b691a5c 158static void acpt_close_socket(BIO *bio)
0f113f3e
MC
159{
160 BIO_ACCEPT *c;
161
162 c = (BIO_ACCEPT *)bio->ptr;
b13fdc48 163 if (c->accept_sock != (int)INVALID_SOCKET) {
0f113f3e
MC
164 shutdown(c->accept_sock, 2);
165 closesocket(c->accept_sock);
b13fdc48
AP
166 c->accept_sock = (int)INVALID_SOCKET;
167 bio->num = (int)INVALID_SOCKET;
0f113f3e
MC
168 }
169}
d02b48c6 170
6b691a5c 171static int acpt_free(BIO *a)
0f113f3e
MC
172{
173 BIO_ACCEPT *data;
174
175 if (a == NULL)
176 return (0);
177 data = (BIO_ACCEPT *)a->ptr;
178
179 if (a->shutdown) {
180 acpt_close_socket(a);
181 BIO_ACCEPT_free(data);
182 a->ptr = NULL;
183 a->flags = 0;
184 a->init = 0;
185 }
186 return (1);
187}
188
6b691a5c 189static int acpt_state(BIO *b, BIO_ACCEPT *c)
0f113f3e
MC
190{
191 BIO *bio = NULL, *dbio;
192 int s = -1;
193 int i;
194
195 again:
196 switch (c->state) {
197 case ACPT_S_BEFORE:
198 if (c->param_addr == NULL) {
199 BIOerr(BIO_F_ACPT_STATE, BIO_R_NO_ACCEPT_PORT_SPECIFIED);
200 return (-1);
201 }
202 s = BIO_get_accept_socket(c->param_addr, c->bind_mode);
b13fdc48 203 if (s == (int)INVALID_SOCKET)
0f113f3e
MC
204 return (-1);
205
206 if (c->accept_nbio) {
207 if (!BIO_socket_nbio(s, 1)) {
208 closesocket(s);
209 BIOerr(BIO_F_ACPT_STATE,
210 BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
211 return (-1);
212 }
213 }
214 c->accept_sock = s;
215 b->num = s;
216 c->state = ACPT_S_GET_ACCEPT_SOCKET;
217 return (1);
218 /* break; */
219 case ACPT_S_GET_ACCEPT_SOCKET:
220 if (b->next_bio != NULL) {
221 c->state = ACPT_S_OK;
222 goto again;
223 }
224 BIO_clear_retry_flags(b);
225 b->retry_reason = 0;
226 i = BIO_accept(c->accept_sock, &(c->addr));
227
228 /* -2 return means we should retry */
229 if (i == -2) {
230 BIO_set_retry_special(b);
231 b->retry_reason = BIO_RR_ACCEPT;
232 return -1;
233 }
234
235 if (i < 0)
236 return (i);
237
238 bio = BIO_new_socket(i, BIO_CLOSE);
239 if (bio == NULL)
240 goto err;
241
242 BIO_set_callback(bio, BIO_get_callback(b));
243 BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
244
245 if (c->nbio) {
246 if (!BIO_socket_nbio(i, 1)) {
247 BIOerr(BIO_F_ACPT_STATE,
248 BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
249 goto err;
250 }
251 }
252
253 /*
254 * If the accept BIO has an bio_chain, we dup it and put the new
255 * socket at the end.
256 */
257 if (c->bio_chain != NULL) {
258 if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
259 goto err;
260 if (!BIO_push(dbio, bio))
261 goto err;
262 bio = dbio;
263 }
264 if (BIO_push(b, bio) == NULL)
265 goto err;
266
267 c->state = ACPT_S_OK;
268 return (1);
269 err:
270 if (bio != NULL)
271 BIO_free(bio);
272 else if (s >= 0)
273 closesocket(s);
274 return (0);
275 /* break; */
276 case ACPT_S_OK:
277 if (b->next_bio == NULL) {
278 c->state = ACPT_S_GET_ACCEPT_SOCKET;
279 goto again;
280 }
281 return (1);
282 /* break; */
283 default:
284 return (0);
285 /* break; */
286 }
287
288}
d02b48c6 289
6b691a5c 290static int acpt_read(BIO *b, char *out, int outl)
0f113f3e
MC
291{
292 int ret = 0;
293 BIO_ACCEPT *data;
d02b48c6 294
0f113f3e
MC
295 BIO_clear_retry_flags(b);
296 data = (BIO_ACCEPT *)b->ptr;
d02b48c6 297
0f113f3e
MC
298 while (b->next_bio == NULL) {
299 ret = acpt_state(b, data);
300 if (ret <= 0)
301 return (ret);
302 }
d02b48c6 303
0f113f3e
MC
304 ret = BIO_read(b->next_bio, out, outl);
305 BIO_copy_next_retry(b);
306 return (ret);
307}
d02b48c6 308
0e1c0612 309static int acpt_write(BIO *b, const char *in, int inl)
0f113f3e
MC
310{
311 int ret;
312 BIO_ACCEPT *data;
d02b48c6 313
0f113f3e
MC
314 BIO_clear_retry_flags(b);
315 data = (BIO_ACCEPT *)b->ptr;
d02b48c6 316
0f113f3e
MC
317 while (b->next_bio == NULL) {
318 ret = acpt_state(b, data);
319 if (ret <= 0)
320 return (ret);
321 }
d02b48c6 322
0f113f3e
MC
323 ret = BIO_write(b->next_bio, in, inl);
324 BIO_copy_next_retry(b);
325 return (ret);
326}
d02b48c6 327
0e1c0612 328static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
329{
330 int *ip;
331 long ret = 1;
332 BIO_ACCEPT *data;
333 char **pp;
334
335 data = (BIO_ACCEPT *)b->ptr;
336
337 switch (cmd) {
338 case BIO_CTRL_RESET:
339 ret = 0;
340 data->state = ACPT_S_BEFORE;
341 acpt_close_socket(b);
342 b->flags = 0;
343 break;
344 case BIO_C_DO_STATE_MACHINE:
345 /* use this one to start the connection */
346 ret = (long)acpt_state(b, data);
347 break;
348 case BIO_C_SET_ACCEPT:
349 if (ptr != NULL) {
350 if (num == 0) {
351 b->init = 1;
b548a1f1 352 OPENSSL_free(data->param_addr);
7644a9ae 353 data->param_addr = OPENSSL_strdup(ptr);
0f113f3e
MC
354 } else if (num == 1) {
355 data->accept_nbio = (ptr != NULL);
356 } else if (num == 2) {
ca3a82c3 357 BIO_free(data->bio_chain);
0f113f3e
MC
358 data->bio_chain = (BIO *)ptr;
359 }
360 }
361 break;
362 case BIO_C_SET_NBIO:
363 data->nbio = (int)num;
364 break;
365 case BIO_C_SET_FD:
366 b->init = 1;
367 b->num = *((int *)ptr);
368 data->accept_sock = b->num;
369 data->state = ACPT_S_GET_ACCEPT_SOCKET;
370 b->shutdown = (int)num;
371 b->init = 1;
372 break;
373 case BIO_C_GET_FD:
374 if (b->init) {
375 ip = (int *)ptr;
376 if (ip != NULL)
377 *ip = data->accept_sock;
378 ret = data->accept_sock;
379 } else
380 ret = -1;
381 break;
382 case BIO_C_GET_ACCEPT:
383 if (b->init) {
384 if (ptr != NULL) {
385 pp = (char **)ptr;
386 *pp = data->param_addr;
387 } else
388 ret = -1;
389 } else
390 ret = -1;
391 break;
392 case BIO_CTRL_GET_CLOSE:
393 ret = b->shutdown;
394 break;
395 case BIO_CTRL_SET_CLOSE:
396 b->shutdown = (int)num;
397 break;
398 case BIO_CTRL_PENDING:
399 case BIO_CTRL_WPENDING:
400 ret = 0;
401 break;
402 case BIO_CTRL_FLUSH:
403 break;
404 case BIO_C_SET_BIND_MODE:
405 data->bind_mode = (int)num;
406 break;
407 case BIO_C_GET_BIND_MODE:
408 ret = (long)data->bind_mode;
409 break;
410 case BIO_CTRL_DUP:
35a1cc90
MC
411/*- dbio=(BIO *)ptr;
412 if (data->param_port) EAY EAY
413 BIO_set_port(dbio,data->param_port);
414 if (data->param_hostname)
415 BIO_set_hostname(dbio,data->param_hostname);
416 BIO_set_nbio(dbio,data->nbio); */
0f113f3e
MC
417 break;
418
419 default:
420 ret = 0;
421 break;
422 }
423 return (ret);
424}
d02b48c6 425
0e1c0612 426static int acpt_puts(BIO *bp, const char *str)
0f113f3e
MC
427{
428 int n, ret;
d02b48c6 429
0f113f3e
MC
430 n = strlen(str);
431 ret = acpt_write(bp, str, n);
432 return (ret);
433}
d02b48c6 434
c45a48c1 435BIO *BIO_new_accept(const char *str)
0f113f3e
MC
436{
437 BIO *ret;
438
439 ret = BIO_new(BIO_s_accept());
440 if (ret == NULL)
441 return (NULL);
442 if (BIO_set_accept_port(ret, str))
443 return (ret);
ca3a82c3
RS
444 BIO_free(ret);
445 return (NULL);
0f113f3e 446}
d02b48c6
RE
447
448#endif