bss_fd.c revision 109998
1/* crypto/bio/bss_fd.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#include <stdio.h>
60#include <errno.h>
61#define USE_SOCKETS
62#include "cryptlib.h"
63#include <openssl/bio.h>
64
65static int fd_write(BIO *h, const char *buf, int num);
66static int fd_read(BIO *h, char *buf, int size);
67static int fd_puts(BIO *h, const char *str);
68static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69static int fd_new(BIO *h);
70static int fd_free(BIO *data);
71int BIO_fd_should_retry(int s);
72
73static BIO_METHOD methods_fdp=
74	{
75	BIO_TYPE_FD,"file descriptor",
76	fd_write,
77	fd_read,
78	fd_puts,
79	NULL, /* fd_gets, */
80	fd_ctrl,
81	fd_new,
82	fd_free,
83	NULL,
84	};
85
86BIO_METHOD *BIO_s_fd(void)
87	{
88	return(&methods_fdp);
89	}
90
91BIO *BIO_new_fd(int fd,int close_flag)
92	{
93	BIO *ret;
94	ret=BIO_new(BIO_s_fd());
95	if (ret == NULL) return(NULL);
96	BIO_set_fd(ret,fd,close_flag);
97	return(ret);
98	}
99
100static int fd_new(BIO *bi)
101	{
102	bi->init=0;
103	bi->num=0;
104	bi->ptr=NULL;
105	bi->flags=0;
106	return(1);
107	}
108
109static int fd_free(BIO *a)
110	{
111	if (a == NULL) return(0);
112	if (a->shutdown)
113		{
114		if (a->init)
115			{
116			close(a->num);
117			}
118		a->init=0;
119		a->flags=0;
120		}
121	return(1);
122	}
123
124static int fd_read(BIO *b, char *out,int outl)
125	{
126	int ret=0;
127
128	if (out != NULL)
129		{
130		clear_sys_error();
131		ret=read(b->num,out,outl);
132		BIO_clear_retry_flags(b);
133		if (ret <= 0)
134			{
135			if (BIO_fd_should_retry(ret))
136				BIO_set_retry_read(b);
137			}
138		}
139	return(ret);
140	}
141
142static int fd_write(BIO *b, const char *in, int inl)
143	{
144	int ret;
145	clear_sys_error();
146	ret=write(b->num,in,inl);
147	BIO_clear_retry_flags(b);
148	if (ret <= 0)
149		{
150		if (BIO_fd_should_retry(ret))
151			BIO_set_retry_write(b);
152		}
153	return(ret);
154	}
155
156static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
157	{
158	long ret=1;
159	int *ip;
160
161	switch (cmd)
162		{
163	case BIO_CTRL_RESET:
164		num=0;
165	case BIO_C_FILE_SEEK:
166		ret=(long)lseek(b->num,num,0);
167		break;
168	case BIO_C_FILE_TELL:
169	case BIO_CTRL_INFO:
170		ret=(long)lseek(b->num,0,1);
171		break;
172	case BIO_C_SET_FD:
173		fd_free(b);
174		b->num= *((int *)ptr);
175		b->shutdown=(int)num;
176		b->init=1;
177		break;
178	case BIO_C_GET_FD:
179		if (b->init)
180			{
181			ip=(int *)ptr;
182			if (ip != NULL) *ip=b->num;
183			ret=b->num;
184			}
185		else
186			ret= -1;
187		break;
188	case BIO_CTRL_GET_CLOSE:
189		ret=b->shutdown;
190		break;
191	case BIO_CTRL_SET_CLOSE:
192		b->shutdown=(int)num;
193		break;
194	case BIO_CTRL_PENDING:
195	case BIO_CTRL_WPENDING:
196		ret=0;
197		break;
198	case BIO_CTRL_DUP:
199	case BIO_CTRL_FLUSH:
200		ret=1;
201		break;
202	default:
203		ret=0;
204		break;
205		}
206	return(ret);
207	}
208
209static int fd_puts(BIO *bp, const char *str)
210	{
211	int n,ret;
212
213	n=strlen(str);
214	ret=fd_write(bp,str,n);
215	return(ret);
216	}
217
218int BIO_fd_should_retry(int i)
219	{
220	int err;
221
222	if ((i == 0) || (i == -1))
223		{
224		err=get_last_sys_error();
225
226#if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
227		if ((i == -1) && (err == 0))
228			return(1);
229#endif
230
231		return(BIO_fd_non_fatal_error(err));
232		}
233	return(0);
234	}
235
236int BIO_fd_non_fatal_error(int err)
237	{
238	switch (err)
239		{
240
241#ifdef EWOULDBLOCK
242# ifdef WSAEWOULDBLOCK
243#  if WSAEWOULDBLOCK != EWOULDBLOCK
244	case EWOULDBLOCK:
245#  endif
246# else
247	case EWOULDBLOCK:
248# endif
249#endif
250
251#if defined(ENOTCONN)
252	case ENOTCONN:
253#endif
254
255#ifdef EINTR
256	case EINTR:
257#endif
258
259#ifdef EAGAIN
260#if EWOULDBLOCK != EAGAIN
261	case EAGAIN:
262# endif
263#endif
264
265#ifdef EPROTO
266	case EPROTO:
267#endif
268
269#ifdef EINPROGRESS
270	case EINPROGRESS:
271#endif
272
273#ifdef EALREADY
274	case EALREADY:
275#endif
276		return(1);
277		/* break; */
278	default:
279		break;
280		}
281	return(0);
282	}
283