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