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