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