Make s_server, s_client check cipher list return codes.
[openssl.git] / crypto / bio / bss_sock.c
1 /* crypto/bio/bss_sock.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 #if !defined(NO_SOCK) || defined(BIO_FD)
60
61 #include <stdio.h>
62 #include <errno.h>
63 #define USE_SOCKETS
64 #include "cryptlib.h"
65 #include <openssl/bio.h>
66
67 #ifndef BIO_FD
68 static int sock_write(BIO *h,char *buf,int num);
69 static int sock_read(BIO *h,char *buf,int size);
70 static int sock_puts(BIO *h,char *str);
71 static long sock_ctrl(BIO *h,int cmd,long arg1,char *arg2);
72 static int sock_new(BIO *h);
73 static int sock_free(BIO *data);
74 int BIO_sock_should_retry(int s);
75 #else
76
77 static int fd_write(BIO *h,char *buf,int num);
78 static int fd_read(BIO *h,char *buf,int size);
79 static int fd_puts(BIO *h,char *str);
80 static long fd_ctrl(BIO *h,int cmd,long arg1,char *arg2);
81 static int fd_new(BIO *h);
82 static int fd_free(BIO *data);
83 int BIO_fd_should_retry(int s);
84 #endif
85
86 #ifndef BIO_FD
87 static BIO_METHOD methods_sockp=
88         {
89         BIO_TYPE_SOCKET,
90         "socket",
91         sock_write,
92         sock_read,
93         sock_puts,
94         NULL, /* sock_gets, */
95         sock_ctrl,
96         sock_new,
97         sock_free,
98         };
99
100 BIO_METHOD *BIO_s_socket(void)
101         {
102         return(&methods_sockp);
103         }
104 #else
105 static BIO_METHOD methods_fdp=
106         {
107         BIO_TYPE_FD,"file descriptor",
108         fd_write,
109         fd_read,
110         fd_puts,
111         NULL, /* fd_gets, */
112         fd_ctrl,
113         fd_new,
114         fd_free,
115         };
116
117 BIO_METHOD *BIO_s_fd(void)
118         {
119         return(&methods_fdp);
120         }
121 #endif
122
123 #ifndef BIO_FD
124 BIO *BIO_new_socket(int fd, int close_flag)
125 #else
126 BIO *BIO_new_fd(int fd,int close_flag)
127 #endif
128         {
129         BIO *ret;
130
131 #ifndef BIO_FD
132         ret=BIO_new(BIO_s_socket());
133 #else
134         ret=BIO_new(BIO_s_fd());
135 #endif
136         if (ret == NULL) return(NULL);
137         BIO_set_fd(ret,fd,close_flag);
138         return(ret);
139         }
140
141 #ifndef BIO_FD
142 static int sock_new(BIO *bi)
143 #else
144 static int fd_new(BIO *bi)
145 #endif
146         {
147         bi->init=0;
148         bi->num=0;
149         bi->ptr=NULL;
150         bi->flags=0;
151         return(1);
152         }
153
154 #ifndef BIO_FD
155 static int sock_free(BIO *a)
156 #else
157 static int fd_free(BIO *a)
158 #endif
159         {
160         if (a == NULL) return(0);
161         if (a->shutdown)
162                 {
163                 if (a->init)
164                         {
165 #ifndef BIO_FD
166                         SHUTDOWN2(a->num);
167 #else                   /* BIO_FD */
168                         close(a->num);
169 #endif
170
171                         }
172                 a->init=0;
173                 a->flags=0;
174                 }
175         return(1);
176         }
177         
178 #ifndef BIO_FD
179 static int sock_read(BIO *b, char *out, int outl)
180 #else
181 static int fd_read(BIO *b, char *out,int outl)
182 #endif
183         {
184         int ret=0;
185
186         if (out != NULL)
187                 {
188 #ifndef BIO_FD
189                 clear_socket_error();
190                 ret=readsocket(b->num,out,outl);
191 #else
192                 clear_sys_error();
193                 ret=read(b->num,out,outl);
194 #endif
195                 BIO_clear_retry_flags(b);
196                 if (ret <= 0)
197                         {
198 #ifndef BIO_FD
199                         if (BIO_sock_should_retry(ret))
200 #else
201                         if (BIO_fd_should_retry(ret))
202 #endif
203                                 BIO_set_retry_read(b);
204                         }
205                 }
206         return(ret);
207         }
208
209 #ifndef BIO_FD
210 static int sock_write(BIO *b, char *in, int inl)
211 #else
212 static int fd_write(BIO *b, char *in, int inl)
213 #endif
214         {
215         int ret;
216         
217 #ifndef BIO_FD
218         clear_socket_error();
219         ret=writesocket(b->num,in,inl);
220 #else
221         clear_sys_error();
222         ret=write(b->num,in,inl);
223 #endif
224         BIO_clear_retry_flags(b);
225         if (ret <= 0)
226                 {
227 #ifndef BIO_FD
228                 if (BIO_sock_should_retry(ret))
229 #else
230                 if (BIO_fd_should_retry(ret))
231 #endif
232                         BIO_set_retry_write(b);
233                 }
234         return(ret);
235         }
236
237 #ifndef BIO_FD
238 static long sock_ctrl(BIO *b, int cmd, long num, char *ptr)
239 #else
240 static long fd_ctrl(BIO *b, int cmd, long num, char *ptr)
241 #endif
242         {
243         long ret=1;
244         int *ip;
245
246         switch (cmd)
247                 {
248         case BIO_CTRL_RESET:
249                 num=0;
250         case BIO_C_FILE_SEEK:
251 #ifdef BIO_FD
252                 ret=(long)lseek(b->num,num,0);
253 #else
254                 ret=0;
255 #endif
256                 break;
257         case BIO_C_FILE_TELL:
258         case BIO_CTRL_INFO:
259 #ifdef BIO_FD
260                 ret=(long)lseek(b->num,0,1);
261 #else
262                 ret=0;
263 #endif
264                 break;
265         case BIO_C_SET_FD:
266 #ifndef BIO_FD
267                 sock_free(b);
268 #else
269                 fd_free(b);
270 #endif
271                 b->num= *((int *)ptr);
272                 b->shutdown=(int)num;
273                 b->init=1;
274                 break;
275         case BIO_C_GET_FD:
276                 if (b->init)
277                         {
278                         ip=(int *)ptr;
279                         if (ip != NULL) *ip=b->num;
280                         ret=b->num;
281                         }
282                 else
283                         ret= -1;
284                 break;
285         case BIO_CTRL_GET_CLOSE:
286                 ret=b->shutdown;
287                 break;
288         case BIO_CTRL_SET_CLOSE:
289                 b->shutdown=(int)num;
290                 break;
291         case BIO_CTRL_PENDING:
292         case BIO_CTRL_WPENDING:
293                 ret=0;
294                 break;
295         case BIO_CTRL_DUP:
296         case BIO_CTRL_FLUSH:
297                 ret=1;
298                 break;
299         default:
300                 ret=0;
301                 break;
302                 }
303         return(ret);
304         }
305
306 #ifdef undef
307 static int sock_gets(BIO *bp, char *buf,int size)
308         {
309         return(-1);
310         }
311 #endif
312
313 #ifndef BIO_FD
314 static int sock_puts(BIO *bp, char *str)
315 #else
316 static int fd_puts(BIO *bp, char *str)
317 #endif
318         {
319         int n,ret;
320
321         n=strlen(str);
322 #ifndef BIO_FD
323         ret=sock_write(bp,str,n);
324 #else
325         ret=fd_write(bp,str,n);
326 #endif
327         return(ret);
328         }
329
330 #ifndef BIO_FD
331 int BIO_sock_should_retry(int i)
332 #else
333 int BIO_fd_should_retry(int i)
334 #endif
335         {
336         int err;
337
338         if ((i == 0) || (i == -1))
339                 {
340 #ifndef BIO_FD
341                 err=get_last_socket_error();
342 #else
343                 err=get_last_sys_error();
344 #endif
345
346 #if defined(WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
347                 if ((i == -1) && (err == 0))
348                         return(1);
349 #endif
350
351 #ifndef BIO_FD
352                 return(BIO_sock_non_fatal_error(err));
353 #else
354                 return(BIO_fd_non_fatal_error(err));
355 #endif
356                 }
357         return(0);
358         }
359
360 #ifndef BIO_FD
361 int BIO_sock_non_fatal_error(int err)
362 #else
363 int BIO_fd_non_fatal_error(int err)
364 #endif
365         {
366         switch (err)
367                 {
368 #if !defined(BIO_FD) && defined(WINDOWS)
369 # if defined(WSAEWOULDBLOCK)
370         case WSAEWOULDBLOCK:
371 # endif
372
373 # if 0 /* This appears to always be an error */
374 #  if defined(WSAENOTCONN)
375         case WSAENOTCONN:
376 #  endif
377 # endif
378 #endif
379
380 #ifdef EWOULDBLOCK
381 # ifdef WSAEWOULDBLOCK
382 #  if WSAEWOULDBLOCK != EWOULDBLOCK
383         case EWOULDBLOCK:
384 #  endif
385 # else
386         case EWOULDBLOCK:
387 # endif
388 #endif
389
390 #if defined(ENOTCONN)
391         case ENOTCONN:
392 #endif
393
394 #ifdef EINTR
395         case EINTR:
396 #endif
397
398 #ifdef EAGAIN
399 #if EWOULDBLOCK != EAGAIN
400         case EAGAIN:
401 # endif
402 #endif
403
404 #ifdef EPROTO
405         case EPROTO:
406 #endif
407
408 #ifdef EINPROGRESS
409         case EINPROGRESS:
410 #endif
411
412 #ifdef EALREADY
413         case EALREADY:
414 #endif
415                 return(1);
416                 /* break; */
417         default:
418                 break;
419                 }
420         return(0);
421         }
422 #endif