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. 8296465Sdelphij * 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). 15296465Sdelphij * 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. 22296465Sdelphij * 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 :-). 37296465Sdelphij * 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)" 40296465Sdelphij * 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. 52296465Sdelphij * 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" 63160814Ssimon/* 64160814Ssimon * As for unconditional usage of "UPLINK" interface in this module. 65160814Ssimon * Trouble is that unlike Unix file descriptors [which are indexes 66160814Ssimon * in kernel-side per-process table], corresponding descriptors on 67160814Ssimon * platforms which require "UPLINK" interface seem to be indexes 68160814Ssimon * in a user-land, non-global table. Well, in fact they are indexes 69160814Ssimon * in stdio _iob[], and recall that _iob[] was the very reason why 70160814Ssimon * "UPLINK" interface was introduced in first place. But one way on 71160814Ssimon * another. Neither libcrypto or libssl use this BIO meaning that 72160814Ssimon * file descriptors can only be provided by application. Therefore 73160814Ssimon * "UPLINK" calls are due... 74160814Ssimon */ 75160814Ssimon#include "bio_lcl.h" 7655714Skris 77109998Smarkmstatic int fd_write(BIO *h, const char *buf, int num); 78109998Smarkmstatic int fd_read(BIO *h, char *buf, int size); 79109998Smarkmstatic int fd_puts(BIO *h, const char *str); 80109998Smarkmstatic long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2); 81109998Smarkmstatic int fd_new(BIO *h); 82109998Smarkmstatic int fd_free(BIO *data); 83109998Smarkmint BIO_fd_should_retry(int s); 84109998Smarkm 85296465Sdelphijstatic BIO_METHOD methods_fdp = { 86296465Sdelphij BIO_TYPE_FD, "file descriptor", 87296465Sdelphij fd_write, 88296465Sdelphij fd_read, 89296465Sdelphij fd_puts, 90296465Sdelphij NULL, /* fd_gets, */ 91296465Sdelphij fd_ctrl, 92296465Sdelphij fd_new, 93296465Sdelphij fd_free, 94296465Sdelphij NULL, 95296465Sdelphij}; 96109998Smarkm 97109998SmarkmBIO_METHOD *BIO_s_fd(void) 98296465Sdelphij{ 99296465Sdelphij return (&methods_fdp); 100296465Sdelphij} 101109998Smarkm 102296465SdelphijBIO *BIO_new_fd(int fd, int close_flag) 103296465Sdelphij{ 104296465Sdelphij BIO *ret; 105296465Sdelphij ret = BIO_new(BIO_s_fd()); 106296465Sdelphij if (ret == NULL) 107296465Sdelphij return (NULL); 108296465Sdelphij BIO_set_fd(ret, fd, close_flag); 109296465Sdelphij return (ret); 110296465Sdelphij} 111109998Smarkm 112109998Smarkmstatic int fd_new(BIO *bi) 113296465Sdelphij{ 114296465Sdelphij bi->init = 0; 115296465Sdelphij bi->num = -1; 116296465Sdelphij bi->ptr = NULL; 117296465Sdelphij bi->flags = BIO_FLAGS_UPLINK; /* essentially redundant */ 118296465Sdelphij return (1); 119296465Sdelphij} 120109998Smarkm 121109998Smarkmstatic int fd_free(BIO *a) 122296465Sdelphij{ 123296465Sdelphij if (a == NULL) 124296465Sdelphij return (0); 125296465Sdelphij if (a->shutdown) { 126296465Sdelphij if (a->init) { 127296465Sdelphij UP_close(a->num); 128296465Sdelphij } 129296465Sdelphij a->init = 0; 130296465Sdelphij a->flags = BIO_FLAGS_UPLINK; 131296465Sdelphij } 132296465Sdelphij return (1); 133296465Sdelphij} 134109998Smarkm 135296465Sdelphijstatic int fd_read(BIO *b, char *out, int outl) 136296465Sdelphij{ 137296465Sdelphij int ret = 0; 138109998Smarkm 139296465Sdelphij if (out != NULL) { 140296465Sdelphij clear_sys_error(); 141296465Sdelphij ret = UP_read(b->num, out, outl); 142296465Sdelphij BIO_clear_retry_flags(b); 143296465Sdelphij if (ret <= 0) { 144296465Sdelphij if (BIO_fd_should_retry(ret)) 145296465Sdelphij BIO_set_retry_read(b); 146296465Sdelphij } 147296465Sdelphij } 148296465Sdelphij return (ret); 149296465Sdelphij} 150296465Sdelphij 151109998Smarkmstatic int fd_write(BIO *b, const char *in, int inl) 152296465Sdelphij{ 153296465Sdelphij int ret; 154296465Sdelphij clear_sys_error(); 155296465Sdelphij ret = UP_write(b->num, in, inl); 156296465Sdelphij BIO_clear_retry_flags(b); 157296465Sdelphij if (ret <= 0) { 158296465Sdelphij if (BIO_fd_should_retry(ret)) 159296465Sdelphij BIO_set_retry_write(b); 160296465Sdelphij } 161296465Sdelphij return (ret); 162296465Sdelphij} 163109998Smarkm 164109998Smarkmstatic long fd_ctrl(BIO *b, int cmd, long num, void *ptr) 165296465Sdelphij{ 166296465Sdelphij long ret = 1; 167296465Sdelphij int *ip; 168109998Smarkm 169296465Sdelphij switch (cmd) { 170296465Sdelphij case BIO_CTRL_RESET: 171296465Sdelphij num = 0; 172296465Sdelphij case BIO_C_FILE_SEEK: 173296465Sdelphij ret = (long)UP_lseek(b->num, num, 0); 174296465Sdelphij break; 175296465Sdelphij case BIO_C_FILE_TELL: 176296465Sdelphij case BIO_CTRL_INFO: 177296465Sdelphij ret = (long)UP_lseek(b->num, 0, 1); 178296465Sdelphij break; 179296465Sdelphij case BIO_C_SET_FD: 180296465Sdelphij fd_free(b); 181296465Sdelphij b->num = *((int *)ptr); 182296465Sdelphij b->shutdown = (int)num; 183296465Sdelphij b->init = 1; 184296465Sdelphij break; 185296465Sdelphij case BIO_C_GET_FD: 186296465Sdelphij if (b->init) { 187296465Sdelphij ip = (int *)ptr; 188296465Sdelphij if (ip != NULL) 189296465Sdelphij *ip = b->num; 190296465Sdelphij ret = b->num; 191296465Sdelphij } else 192296465Sdelphij ret = -1; 193296465Sdelphij break; 194296465Sdelphij case BIO_CTRL_GET_CLOSE: 195296465Sdelphij ret = b->shutdown; 196296465Sdelphij break; 197296465Sdelphij case BIO_CTRL_SET_CLOSE: 198296465Sdelphij b->shutdown = (int)num; 199296465Sdelphij break; 200296465Sdelphij case BIO_CTRL_PENDING: 201296465Sdelphij case BIO_CTRL_WPENDING: 202296465Sdelphij ret = 0; 203296465Sdelphij break; 204296465Sdelphij case BIO_CTRL_DUP: 205296465Sdelphij case BIO_CTRL_FLUSH: 206296465Sdelphij ret = 1; 207296465Sdelphij break; 208296465Sdelphij default: 209296465Sdelphij ret = 0; 210296465Sdelphij break; 211296465Sdelphij } 212296465Sdelphij return (ret); 213296465Sdelphij} 214109998Smarkm 215109998Smarkmstatic int fd_puts(BIO *bp, const char *str) 216296465Sdelphij{ 217296465Sdelphij int n, ret; 218109998Smarkm 219296465Sdelphij n = strlen(str); 220296465Sdelphij ret = fd_write(bp, str, n); 221296465Sdelphij return (ret); 222296465Sdelphij} 223109998Smarkm 224109998Smarkmint BIO_fd_should_retry(int i) 225296465Sdelphij{ 226296465Sdelphij int err; 227109998Smarkm 228296465Sdelphij if ((i == 0) || (i == -1)) { 229296465Sdelphij err = get_last_sys_error(); 230109998Smarkm 231296465Sdelphij#if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps 232296465Sdelphij * not? Ben 4/1/99 */ 233296465Sdelphij if ((i == -1) && (err == 0)) 234296465Sdelphij return (1); 235109998Smarkm#endif 236109998Smarkm 237296465Sdelphij return (BIO_fd_non_fatal_error(err)); 238296465Sdelphij } 239296465Sdelphij return (0); 240296465Sdelphij} 241109998Smarkm 242109998Smarkmint BIO_fd_non_fatal_error(int err) 243296465Sdelphij{ 244296465Sdelphij switch (err) { 245109998Smarkm 246109998Smarkm#ifdef EWOULDBLOCK 247109998Smarkm# ifdef WSAEWOULDBLOCK 248109998Smarkm# if WSAEWOULDBLOCK != EWOULDBLOCK 249296465Sdelphij case EWOULDBLOCK: 250109998Smarkm# endif 251109998Smarkm# else 252296465Sdelphij case EWOULDBLOCK: 253109998Smarkm# endif 254109998Smarkm#endif 255109998Smarkm 256109998Smarkm#if defined(ENOTCONN) 257296465Sdelphij case ENOTCONN: 258109998Smarkm#endif 259109998Smarkm 260109998Smarkm#ifdef EINTR 261296465Sdelphij case EINTR: 262109998Smarkm#endif 263109998Smarkm 264109998Smarkm#ifdef EAGAIN 265296465Sdelphij# if EWOULDBLOCK != EAGAIN 266296465Sdelphij case EAGAIN: 267109998Smarkm# endif 268109998Smarkm#endif 269109998Smarkm 270109998Smarkm#ifdef EPROTO 271296465Sdelphij case EPROTO: 272109998Smarkm#endif 273109998Smarkm 274109998Smarkm#ifdef EINPROGRESS 275296465Sdelphij case EINPROGRESS: 276109998Smarkm#endif 277109998Smarkm 278109998Smarkm#ifdef EALREADY 279296465Sdelphij case EALREADY: 280109998Smarkm#endif 281296465Sdelphij return (1); 282296465Sdelphij /* break; */ 283296465Sdelphij default: 284296465Sdelphij break; 285296465Sdelphij } 286296465Sdelphij return (0); 287296465Sdelphij} 288