bss_fd.c revision 280297
155714Skris/* crypto/bio/bss_fd.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280297Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280297Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280297Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280297Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280297Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59109998Smarkm#include <stdio.h>
60109998Smarkm#include <errno.h>
61109998Smarkm#define USE_SOCKETS
62109998Smarkm#include "cryptlib.h"
63238405Sjkim
64238405Sjkim#if defined(OPENSSL_NO_POSIX_IO)
65160814Ssimon/*
66238405Sjkim * One can argue that one should implement dummy placeholder for
67238405Sjkim * BIO_s_fd here...
68238405Sjkim */
69238405Sjkim#else
70238405Sjkim/*
71160814Ssimon * As for unconditional usage of "UPLINK" interface in this module.
72160814Ssimon * Trouble is that unlike Unix file descriptors [which are indexes
73160814Ssimon * in kernel-side per-process table], corresponding descriptors on
74160814Ssimon * platforms which require "UPLINK" interface seem to be indexes
75160814Ssimon * in a user-land, non-global table. Well, in fact they are indexes
76160814Ssimon * in stdio _iob[], and recall that _iob[] was the very reason why
77160814Ssimon * "UPLINK" interface was introduced in first place. But one way on
78160814Ssimon * another. Neither libcrypto or libssl use this BIO meaning that
79160814Ssimon * file descriptors can only be provided by application. Therefore
80160814Ssimon * "UPLINK" calls are due...
81160814Ssimon */
82280297Sjkim# include "bio_lcl.h"
8355714Skris
84109998Smarkmstatic int fd_write(BIO *h, const char *buf, int num);
85109998Smarkmstatic int fd_read(BIO *h, char *buf, int size);
86109998Smarkmstatic int fd_puts(BIO *h, const char *str);
87238405Sjkimstatic int fd_gets(BIO *h, char *buf, int size);
88109998Smarkmstatic long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);
89109998Smarkmstatic int fd_new(BIO *h);
90109998Smarkmstatic int fd_free(BIO *data);
91109998Smarkmint BIO_fd_should_retry(int s);
92109998Smarkm
93280297Sjkimstatic BIO_METHOD methods_fdp = {
94280297Sjkim    BIO_TYPE_FD, "file descriptor",
95280297Sjkim    fd_write,
96280297Sjkim    fd_read,
97280297Sjkim    fd_puts,
98280297Sjkim    fd_gets,
99280297Sjkim    fd_ctrl,
100280297Sjkim    fd_new,
101280297Sjkim    fd_free,
102280297Sjkim    NULL,
103280297Sjkim};
104109998Smarkm
105109998SmarkmBIO_METHOD *BIO_s_fd(void)
106280297Sjkim{
107280297Sjkim    return (&methods_fdp);
108280297Sjkim}
109109998Smarkm
110280297SjkimBIO *BIO_new_fd(int fd, int close_flag)
111280297Sjkim{
112280297Sjkim    BIO *ret;
113280297Sjkim    ret = BIO_new(BIO_s_fd());
114280297Sjkim    if (ret == NULL)
115280297Sjkim        return (NULL);
116280297Sjkim    BIO_set_fd(ret, fd, close_flag);
117280297Sjkim    return (ret);
118280297Sjkim}
119109998Smarkm
120109998Smarkmstatic int fd_new(BIO *bi)
121280297Sjkim{
122280297Sjkim    bi->init = 0;
123280297Sjkim    bi->num = -1;
124280297Sjkim    bi->ptr = NULL;
125280297Sjkim    bi->flags = BIO_FLAGS_UPLINK; /* essentially redundant */
126280297Sjkim    return (1);
127280297Sjkim}
128109998Smarkm
129109998Smarkmstatic int fd_free(BIO *a)
130280297Sjkim{
131280297Sjkim    if (a == NULL)
132280297Sjkim        return (0);
133280297Sjkim    if (a->shutdown) {
134280297Sjkim        if (a->init) {
135280297Sjkim            UP_close(a->num);
136280297Sjkim        }
137280297Sjkim        a->init = 0;
138280297Sjkim        a->flags = BIO_FLAGS_UPLINK;
139280297Sjkim    }
140280297Sjkim    return (1);
141280297Sjkim}
142109998Smarkm
143280297Sjkimstatic int fd_read(BIO *b, char *out, int outl)
144280297Sjkim{
145280297Sjkim    int ret = 0;
146109998Smarkm
147280297Sjkim    if (out != NULL) {
148280297Sjkim        clear_sys_error();
149280297Sjkim        ret = UP_read(b->num, out, outl);
150280297Sjkim        BIO_clear_retry_flags(b);
151280297Sjkim        if (ret <= 0) {
152280297Sjkim            if (BIO_fd_should_retry(ret))
153280297Sjkim                BIO_set_retry_read(b);
154280297Sjkim        }
155280297Sjkim    }
156280297Sjkim    return (ret);
157280297Sjkim}
158280297Sjkim
159109998Smarkmstatic int fd_write(BIO *b, const char *in, int inl)
160280297Sjkim{
161280297Sjkim    int ret;
162280297Sjkim    clear_sys_error();
163280297Sjkim    ret = UP_write(b->num, in, inl);
164280297Sjkim    BIO_clear_retry_flags(b);
165280297Sjkim    if (ret <= 0) {
166280297Sjkim        if (BIO_fd_should_retry(ret))
167280297Sjkim            BIO_set_retry_write(b);
168280297Sjkim    }
169280297Sjkim    return (ret);
170280297Sjkim}
171109998Smarkm
172109998Smarkmstatic long fd_ctrl(BIO *b, int cmd, long num, void *ptr)
173280297Sjkim{
174280297Sjkim    long ret = 1;
175280297Sjkim    int *ip;
176109998Smarkm
177280297Sjkim    switch (cmd) {
178280297Sjkim    case BIO_CTRL_RESET:
179280297Sjkim        num = 0;
180280297Sjkim    case BIO_C_FILE_SEEK:
181280297Sjkim        ret = (long)UP_lseek(b->num, num, 0);
182280297Sjkim        break;
183280297Sjkim    case BIO_C_FILE_TELL:
184280297Sjkim    case BIO_CTRL_INFO:
185280297Sjkim        ret = (long)UP_lseek(b->num, 0, 1);
186280297Sjkim        break;
187280297Sjkim    case BIO_C_SET_FD:
188280297Sjkim        fd_free(b);
189280297Sjkim        b->num = *((int *)ptr);
190280297Sjkim        b->shutdown = (int)num;
191280297Sjkim        b->init = 1;
192280297Sjkim        break;
193280297Sjkim    case BIO_C_GET_FD:
194280297Sjkim        if (b->init) {
195280297Sjkim            ip = (int *)ptr;
196280297Sjkim            if (ip != NULL)
197280297Sjkim                *ip = b->num;
198280297Sjkim            ret = b->num;
199280297Sjkim        } else
200280297Sjkim            ret = -1;
201280297Sjkim        break;
202280297Sjkim    case BIO_CTRL_GET_CLOSE:
203280297Sjkim        ret = b->shutdown;
204280297Sjkim        break;
205280297Sjkim    case BIO_CTRL_SET_CLOSE:
206280297Sjkim        b->shutdown = (int)num;
207280297Sjkim        break;
208280297Sjkim    case BIO_CTRL_PENDING:
209280297Sjkim    case BIO_CTRL_WPENDING:
210280297Sjkim        ret = 0;
211280297Sjkim        break;
212280297Sjkim    case BIO_CTRL_DUP:
213280297Sjkim    case BIO_CTRL_FLUSH:
214280297Sjkim        ret = 1;
215280297Sjkim        break;
216280297Sjkim    default:
217280297Sjkim        ret = 0;
218280297Sjkim        break;
219280297Sjkim    }
220280297Sjkim    return (ret);
221280297Sjkim}
222109998Smarkm
223109998Smarkmstatic int fd_puts(BIO *bp, const char *str)
224280297Sjkim{
225280297Sjkim    int n, ret;
226109998Smarkm
227280297Sjkim    n = strlen(str);
228280297Sjkim    ret = fd_write(bp, str, n);
229280297Sjkim    return (ret);
230280297Sjkim}
231109998Smarkm
232238405Sjkimstatic int fd_gets(BIO *bp, char *buf, int size)
233280297Sjkim{
234280297Sjkim    int ret = 0;
235280297Sjkim    char *ptr = buf;
236280297Sjkim    char *end = buf + size - 1;
237238405Sjkim
238280297Sjkim    while ((ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n'))
239280297Sjkim        ptr++;
240238405Sjkim
241280297Sjkim    ptr[0] = '\0';
242238405Sjkim
243280297Sjkim    if (buf[0] != '\0')
244280297Sjkim        ret = strlen(buf);
245280297Sjkim    return (ret);
246280297Sjkim}
247238405Sjkim
248109998Smarkmint BIO_fd_should_retry(int i)
249280297Sjkim{
250280297Sjkim    int err;
251109998Smarkm
252280297Sjkim    if ((i == 0) || (i == -1)) {
253280297Sjkim        err = get_last_sys_error();
254109998Smarkm
255280297Sjkim# if defined(OPENSSL_SYS_WINDOWS) && 0/* more microsoft stupidity? perhaps
256280297Sjkim                                       * not? Ben 4/1/99 */
257280297Sjkim        if ((i == -1) && (err == 0))
258280297Sjkim            return (1);
259280297Sjkim# endif
260109998Smarkm
261280297Sjkim        return (BIO_fd_non_fatal_error(err));
262280297Sjkim    }
263280297Sjkim    return (0);
264280297Sjkim}
265109998Smarkm
266109998Smarkmint BIO_fd_non_fatal_error(int err)
267280297Sjkim{
268280297Sjkim    switch (err) {
269109998Smarkm
270280297Sjkim# ifdef EWOULDBLOCK
271280297Sjkim#  ifdef WSAEWOULDBLOCK
272280297Sjkim#   if WSAEWOULDBLOCK != EWOULDBLOCK
273280297Sjkim    case EWOULDBLOCK:
274280297Sjkim#   endif
275280297Sjkim#  else
276280297Sjkim    case EWOULDBLOCK:
277109998Smarkm#  endif
278109998Smarkm# endif
279109998Smarkm
280280297Sjkim# if defined(ENOTCONN)
281280297Sjkim    case ENOTCONN:
282280297Sjkim# endif
283109998Smarkm
284280297Sjkim# ifdef EINTR
285280297Sjkim    case EINTR:
286280297Sjkim# endif
287109998Smarkm
288280297Sjkim# ifdef EAGAIN
289280297Sjkim#  if EWOULDBLOCK != EAGAIN
290280297Sjkim    case EAGAIN:
291280297Sjkim#  endif
292109998Smarkm# endif
293109998Smarkm
294280297Sjkim# ifdef EPROTO
295280297Sjkim    case EPROTO:
296280297Sjkim# endif
297109998Smarkm
298280297Sjkim# ifdef EINPROGRESS
299280297Sjkim    case EINPROGRESS:
300280297Sjkim# endif
301109998Smarkm
302280297Sjkim# ifdef EALREADY
303280297Sjkim    case EALREADY:
304280297Sjkim# endif
305280297Sjkim        return (1);
306280297Sjkim        /* break; */
307280297Sjkim    default:
308280297Sjkim        break;
309280297Sjkim    }
310280297Sjkim    return (0);
311280297Sjkim}
312109998Smarkm#endif
313