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