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