b514d9e394996aafed6ce18cdc4abc4577a84da1
[openssl.git] / crypto / bio / bss_acpt.c
1 /* crypto/bio/bss_acpt.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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 #include <stdio.h>
60 #include <errno.h>
61 #define USE_SOCKETS
62 #include "cryptlib.h"
63 #include <openssl/bio.h>
64
65 #ifndef OPENSSL_NO_SOCK
66
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 */
69 #  undef FIONBIO
70 # endif
71
72 typedef 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;
87
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);
98
99 # define ACPT_S_BEFORE                   1
100 # define ACPT_S_GET_ACCEPT_SOCKET        2
101 # define ACPT_S_OK                       3
102
103 static 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 };
115
116 BIO_METHOD *BIO_s_accept(void)
117 {
118     return (&methods_acceptp);
119 }
120
121 static int acpt_new(BIO *bi)
122 {
123     BIO_ACCEPT *ba;
124
125     bi->init = 0;
126     bi->num = INVALID_SOCKET;
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 }
135
136 static BIO_ACCEPT *BIO_ACCEPT_new(void)
137 {
138     BIO_ACCEPT *ret;
139
140     if ((ret = (BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL)
141         return (NULL);
142
143     memset(ret, 0, sizeof(BIO_ACCEPT));
144     ret->accept_sock = INVALID_SOCKET;
145     ret->bind_mode = BIO_BIND_NORMAL;
146     return (ret);
147 }
148
149 static void BIO_ACCEPT_free(BIO_ACCEPT *a)
150 {
151     if (a == NULL)
152         return;
153
154     if (a->param_addr != NULL)
155         OPENSSL_free(a->param_addr);
156     if (a->addr != NULL)
157         OPENSSL_free(a->addr);
158     if (a->bio_chain != NULL)
159         BIO_free(a->bio_chain);
160     OPENSSL_free(a);
161 }
162
163 static void acpt_close_socket(BIO *bio)
164 {
165     BIO_ACCEPT *c;
166
167     c = (BIO_ACCEPT *)bio->ptr;
168     if (c->accept_sock != INVALID_SOCKET) {
169         shutdown(c->accept_sock, 2);
170         closesocket(c->accept_sock);
171         c->accept_sock = INVALID_SOCKET;
172         bio->num = INVALID_SOCKET;
173     }
174 }
175
176 static int acpt_free(BIO *a)
177 {
178     BIO_ACCEPT *data;
179
180     if (a == NULL)
181         return (0);
182     data = (BIO_ACCEPT *)a->ptr;
183
184     if (a->shutdown) {
185         acpt_close_socket(a);
186         BIO_ACCEPT_free(data);
187         a->ptr = NULL;
188         a->flags = 0;
189         a->init = 0;
190     }
191     return (1);
192 }
193
194 static int acpt_state(BIO *b, BIO_ACCEPT *c)
195 {
196     BIO *bio = NULL, *dbio;
197     int s = -1;
198     int i;
199
200  again:
201     switch (c->state) {
202     case ACPT_S_BEFORE:
203         if (c->param_addr == NULL) {
204             BIOerr(BIO_F_ACPT_STATE, BIO_R_NO_ACCEPT_PORT_SPECIFIED);
205             return (-1);
206         }
207         s = BIO_get_accept_socket(c->param_addr, c->bind_mode);
208         if (s == INVALID_SOCKET)
209             return (-1);
210
211         if (c->accept_nbio) {
212             if (!BIO_socket_nbio(s, 1)) {
213                 closesocket(s);
214                 BIOerr(BIO_F_ACPT_STATE,
215                        BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
216                 return (-1);
217             }
218         }
219         c->accept_sock = s;
220         b->num = s;
221         c->state = ACPT_S_GET_ACCEPT_SOCKET;
222         return (1);
223         /* break; */
224     case ACPT_S_GET_ACCEPT_SOCKET:
225         if (b->next_bio != NULL) {
226             c->state = ACPT_S_OK;
227             goto again;
228         }
229         BIO_clear_retry_flags(b);
230         b->retry_reason = 0;
231         i = BIO_accept(c->accept_sock, &(c->addr));
232
233         /* -2 return means we should retry */
234         if (i == -2) {
235             BIO_set_retry_special(b);
236             b->retry_reason = BIO_RR_ACCEPT;
237             return -1;
238         }
239
240         if (i < 0)
241             return (i);
242
243         bio = BIO_new_socket(i, BIO_CLOSE);
244         if (bio == NULL)
245             goto err;
246
247         BIO_set_callback(bio, BIO_get_callback(b));
248         BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
249
250         if (c->nbio) {
251             if (!BIO_socket_nbio(i, 1)) {
252                 BIOerr(BIO_F_ACPT_STATE,
253                        BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
254                 goto err;
255             }
256         }
257
258         /*
259          * If the accept BIO has an bio_chain, we dup it and put the new
260          * socket at the end.
261          */
262         if (c->bio_chain != NULL) {
263             if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
264                 goto err;
265             if (!BIO_push(dbio, bio))
266                 goto err;
267             bio = dbio;
268         }
269         if (BIO_push(b, bio) == NULL)
270             goto err;
271
272         c->state = ACPT_S_OK;
273         return (1);
274  err:
275         if (bio != NULL)
276             BIO_free(bio);
277         else if (s >= 0)
278             closesocket(s);
279         return (0);
280         /* break; */
281     case ACPT_S_OK:
282         if (b->next_bio == NULL) {
283             c->state = ACPT_S_GET_ACCEPT_SOCKET;
284             goto again;
285         }
286         return (1);
287         /* break; */
288     default:
289         return (0);
290         /* break; */
291     }
292
293 }
294
295 static int acpt_read(BIO *b, char *out, int outl)
296 {
297     int ret = 0;
298     BIO_ACCEPT *data;
299
300     BIO_clear_retry_flags(b);
301     data = (BIO_ACCEPT *)b->ptr;
302
303     while (b->next_bio == NULL) {
304         ret = acpt_state(b, data);
305         if (ret <= 0)
306             return (ret);
307     }
308
309     ret = BIO_read(b->next_bio, out, outl);
310     BIO_copy_next_retry(b);
311     return (ret);
312 }
313
314 static int acpt_write(BIO *b, const char *in, int inl)
315 {
316     int ret;
317     BIO_ACCEPT *data;
318
319     BIO_clear_retry_flags(b);
320     data = (BIO_ACCEPT *)b->ptr;
321
322     while (b->next_bio == NULL) {
323         ret = acpt_state(b, data);
324         if (ret <= 0)
325             return (ret);
326     }
327
328     ret = BIO_write(b->next_bio, in, inl);
329     BIO_copy_next_retry(b);
330     return (ret);
331 }
332
333 static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
334 {
335     int *ip;
336     long ret = 1;
337     BIO_ACCEPT *data;
338     char **pp;
339
340     data = (BIO_ACCEPT *)b->ptr;
341
342     switch (cmd) {
343     case BIO_CTRL_RESET:
344         ret = 0;
345         data->state = ACPT_S_BEFORE;
346         acpt_close_socket(b);
347         b->flags = 0;
348         break;
349     case BIO_C_DO_STATE_MACHINE:
350         /* use this one to start the connection */
351         ret = (long)acpt_state(b, data);
352         break;
353     case BIO_C_SET_ACCEPT:
354         if (ptr != NULL) {
355             if (num == 0) {
356                 b->init = 1;
357                 if (data->param_addr != NULL)
358                     OPENSSL_free(data->param_addr);
359                 data->param_addr = BUF_strdup(ptr);
360             } else if (num == 1) {
361                 data->accept_nbio = (ptr != NULL);
362             } else if (num == 2) {
363                 if (data->bio_chain != NULL)
364                     BIO_free(data->bio_chain);
365                 data->bio_chain = (BIO *)ptr;
366             }
367         }
368         break;
369     case BIO_C_SET_NBIO:
370         data->nbio = (int)num;
371         break;
372     case BIO_C_SET_FD:
373         b->init = 1;
374         b->num = *((int *)ptr);
375         data->accept_sock = b->num;
376         data->state = ACPT_S_GET_ACCEPT_SOCKET;
377         b->shutdown = (int)num;
378         b->init = 1;
379         break;
380     case BIO_C_GET_FD:
381         if (b->init) {
382             ip = (int *)ptr;
383             if (ip != NULL)
384                 *ip = data->accept_sock;
385             ret = data->accept_sock;
386         } else
387             ret = -1;
388         break;
389     case BIO_C_GET_ACCEPT:
390         if (b->init) {
391             if (ptr != NULL) {
392                 pp = (char **)ptr;
393                 *pp = data->param_addr;
394             } else
395                 ret = -1;
396         } else
397             ret = -1;
398         break;
399     case BIO_CTRL_GET_CLOSE:
400         ret = b->shutdown;
401         break;
402     case BIO_CTRL_SET_CLOSE:
403         b->shutdown = (int)num;
404         break;
405     case BIO_CTRL_PENDING:
406     case BIO_CTRL_WPENDING:
407         ret = 0;
408         break;
409     case BIO_CTRL_FLUSH:
410         break;
411     case BIO_C_SET_BIND_MODE:
412         data->bind_mode = (int)num;
413         break;
414     case BIO_C_GET_BIND_MODE:
415         ret = (long)data->bind_mode;
416         break;
417     case BIO_CTRL_DUP:
418 /*-             dbio=(BIO *)ptr;
419                 if (data->param_port) EAY EAY
420                         BIO_set_port(dbio,data->param_port);
421                 if (data->param_hostname)
422                         BIO_set_hostname(dbio,data->param_hostname);
423                 BIO_set_nbio(dbio,data->nbio); */
424         break;
425
426     default:
427         ret = 0;
428         break;
429     }
430     return (ret);
431 }
432
433 static int acpt_puts(BIO *bp, const char *str)
434 {
435     int n, ret;
436
437     n = strlen(str);
438     ret = acpt_write(bp, str, n);
439     return (ret);
440 }
441
442 BIO *BIO_new_accept(const char *str)
443 {
444     BIO *ret;
445
446     ret = BIO_new(BIO_s_accept());
447     if (ret == NULL)
448         return (NULL);
449     if (BIO_set_accept_port(ret, str))
450         return (ret);
451     else {
452         BIO_free(ret);
453         return (NULL);
454     }
455 }
456
457 #endif