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