| 1 |
/* crypto/bio/bss_fd.c */ |
| 2 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This package is an SSL implementation written |
| 6 |
* by Eric Young ([email protected]). |
| 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 ([email protected]). |
| 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 ([email protected])" |
| 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 ([email protected])" |
| 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 |
|
| 64 |
#if defined(OPENSSL_NO_POSIX_IO) |
| 65 |
/* |
| 66 |
* Dummy placeholder for BIO_s_fd... |
| 67 |
*/ |
| 68 |
BIO *BIO_new_fd(int fd, int close_flag) |
| 69 |
{ |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
int BIO_fd_non_fatal_error(int err) |
| 74 |
{ |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
int BIO_fd_should_retry(int i) |
| 79 |
{ |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
BIO_METHOD *BIO_s_fd(void) |
| 84 |
{ |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
#else |
| 88 |
/* |
| 89 |
* As for unconditional usage of "UPLINK" interface in this module. |
| 90 |
* Trouble is that unlike Unix file descriptors [which are indexes |
| 91 |
* in kernel-side per-process table], corresponding descriptors on |
| 92 |
* platforms which require "UPLINK" interface seem to be indexes |
| 93 |
* in a user-land, non-global table. Well, in fact they are indexes |
| 94 |
* in stdio _iob[], and recall that _iob[] was the very reason why |
| 95 |
* "UPLINK" interface was introduced in first place. But one way on |
| 96 |
* another. Neither libcrypto or libssl use this BIO meaning that |
| 97 |
* file descriptors can only be provided by application. Therefore |
| 98 |
* "UPLINK" calls are due... |
| 99 |
*/ |
| 100 |
# include "bio_lcl.h" |
| 101 |
|
| 102 |
static int fd_write(BIO *h, const char *buf, int num); |
| 103 |
static int fd_read(BIO *h, char *buf, int size); |
| 104 |
static int fd_puts(BIO *h, const char *str); |
| 105 |
static int fd_gets(BIO *h, char *buf, int size); |
| 106 |
static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
| 107 |
static int fd_new(BIO *h); |
| 108 |
static int fd_free(BIO *data); |
| 109 |
int BIO_fd_should_retry(int s); |
| 110 |
|
| 111 |
static BIO_METHOD methods_fdp = { |
| 112 |
BIO_TYPE_FD, "file descriptor", |
| 113 |
fd_write, |
| 114 |
fd_read, |
| 115 |
fd_puts, |
| 116 |
fd_gets, |
| 117 |
fd_ctrl, |
| 118 |
fd_new, |
| 119 |
fd_free, |
| 120 |
NULL, |
| 121 |
}; |
| 122 |
|
| 123 |
BIO_METHOD *BIO_s_fd(void) |
| 124 |
{ |
| 125 |
return (&methods_fdp); |
| 126 |
} |
| 127 |
|
| 128 |
BIO *BIO_new_fd(int fd, int close_flag) |
| 129 |
{ |
| 130 |
BIO *ret; |
| 131 |
ret = BIO_new(BIO_s_fd()); |
| 132 |
if (ret == NULL) |
| 133 |
return (NULL); |
| 134 |
BIO_set_fd(ret, fd, close_flag); |
| 135 |
return (ret); |
| 136 |
} |
| 137 |
|
| 138 |
static int fd_new(BIO *bi) |
| 139 |
{ |
| 140 |
bi->init = 0; |
| 141 |
bi->num = -1; |
| 142 |
bi->ptr = NULL; |
| 143 |
bi->flags = BIO_FLAGS_UPLINK; /* essentially redundant */ |
| 144 |
return (1); |
| 145 |
} |
| 146 |
|
| 147 |
static int fd_free(BIO *a) |
| 148 |
{ |
| 149 |
if (a == NULL) |
| 150 |
return (0); |
| 151 |
if (a->shutdown) { |
| 152 |
if (a->init) { |
| 153 |
UP_close(a->num); |
| 154 |
} |
| 155 |
a->init = 0; |
| 156 |
a->flags = BIO_FLAGS_UPLINK; |
| 157 |
} |
| 158 |
return (1); |
| 159 |
} |
| 160 |
|
| 161 |
static int fd_read(BIO *b, char *out, int outl) |
| 162 |
{ |
| 163 |
int ret = 0; |
| 164 |
|
| 165 |
if (out != NULL) { |
| 166 |
clear_sys_error(); |
| 167 |
ret = UP_read(b->num, out, outl); |
| 168 |
BIO_clear_retry_flags(b); |
| 169 |
if (ret <= 0) { |
| 170 |
if (BIO_fd_should_retry(ret)) |
| 171 |
BIO_set_retry_read(b); |
| 172 |
} |
| 173 |
} |
| 174 |
return (ret); |
| 175 |
} |
| 176 |
|
| 177 |
static int fd_write(BIO *b, const char *in, int inl) |
| 178 |
{ |
| 179 |
int ret; |
| 180 |
clear_sys_error(); |
| 181 |
ret = UP_write(b->num, in, inl); |
| 182 |
BIO_clear_retry_flags(b); |
| 183 |
if (ret <= 0) { |
| 184 |
if (BIO_fd_should_retry(ret)) |
| 185 |
BIO_set_retry_write(b); |
| 186 |
} |
| 187 |
return (ret); |
| 188 |
} |
| 189 |
|
| 190 |
static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) |
| 191 |
{ |
| 192 |
long ret = 1; |
| 193 |
int *ip; |
| 194 |
|
| 195 |
switch (cmd) { |
| 196 |
case BIO_CTRL_RESET: |
| 197 |
num = 0; |
| 198 |
case BIO_C_FILE_SEEK: |
| 199 |
ret = (long)UP_lseek(b->num, num, 0); |
| 200 |
break; |
| 201 |
case BIO_C_FILE_TELL: |
| 202 |
case BIO_CTRL_INFO: |
| 203 |
ret = (long)UP_lseek(b->num, 0, 1); |
| 204 |
break; |
| 205 |
case BIO_C_SET_FD: |
| 206 |
fd_free(b); |
| 207 |
b->num = *((int *)ptr); |
| 208 |
b->shutdown = (int)num; |
| 209 |
b->init = 1; |
| 210 |
break; |
| 211 |
case BIO_C_GET_FD: |
| 212 |
if (b->init) { |
| 213 |
ip = (int *)ptr; |
| 214 |
if (ip != NULL) |
| 215 |
*ip = b->num; |
| 216 |
ret = b->num; |
| 217 |
} else |
| 218 |
ret = -1; |
| 219 |
break; |
| 220 |
case BIO_CTRL_GET_CLOSE: |
| 221 |
ret = b->shutdown; |
| 222 |
break; |
| 223 |
case BIO_CTRL_SET_CLOSE: |
| 224 |
b->shutdown = (int)num; |
| 225 |
break; |
| 226 |
case BIO_CTRL_PENDING: |
| 227 |
case BIO_CTRL_WPENDING: |
| 228 |
ret = 0; |
| 229 |
break; |
| 230 |
case BIO_CTRL_DUP: |
| 231 |
case BIO_CTRL_FLUSH: |
| 232 |
ret = 1; |
| 233 |
break; |
| 234 |
default: |
| 235 |
ret = 0; |
| 236 |
break; |
| 237 |
} |
| 238 |
return (ret); |
| 239 |
} |
| 240 |
|
| 241 |
static int fd_puts(BIO *bp, const char *str) |
| 242 |
{ |
| 243 |
int n, ret; |
| 244 |
|
| 245 |
n = strlen(str); |
| 246 |
ret = fd_write(bp, str, n); |
| 247 |
return (ret); |
| 248 |
} |
| 249 |
|
| 250 |
static int fd_gets(BIO *bp, char *buf, int size) |
| 251 |
{ |
| 252 |
int ret = 0; |
| 253 |
char *ptr = buf; |
| 254 |
char *end = buf + size - 1; |
| 255 |
|
| 256 |
while ((ptr < end) && (fd_read(bp, ptr, 1) > 0) && (ptr[0] != '\n')) |
| 257 |
ptr++; |
| 258 |
|
| 259 |
ptr[0] = '\0'; |
| 260 |
|
| 261 |
if (buf[0] != '\0') |
| 262 |
ret = strlen(buf); |
| 263 |
return (ret); |
| 264 |
} |
| 265 |
|
| 266 |
int BIO_fd_should_retry(int i) |
| 267 |
{ |
| 268 |
int err; |
| 269 |
|
| 270 |
if ((i == 0) || (i == -1)) { |
| 271 |
err = get_last_sys_error(); |
| 272 |
|
| 273 |
# if defined(OPENSSL_SYS_WINDOWS) && 0/* more microsoft stupidity? perhaps |
| 274 |
* not? Ben 4/1/99 */ |
| 275 |
if ((i == -1) && (err == 0)) |
| 276 |
return (1); |
| 277 |
# endif |
| 278 |
|
| 279 |
return (BIO_fd_non_fatal_error(err)); |
| 280 |
} |
| 281 |
return (0); |
| 282 |
} |
| 283 |
|
| 284 |
int BIO_fd_non_fatal_error(int err) |
| 285 |
{ |
| 286 |
switch (err) { |
| 287 |
|
| 288 |
# ifdef EWOULDBLOCK |
| 289 |
# ifdef WSAEWOULDBLOCK |
| 290 |
# if WSAEWOULDBLOCK != EWOULDBLOCK |
| 291 |
case EWOULDBLOCK: |
| 292 |
# endif |
| 293 |
# else |
| 294 |
case EWOULDBLOCK: |
| 295 |
# endif |
| 296 |
# endif |
| 297 |
|
| 298 |
# if defined(ENOTCONN) |
| 299 |
case ENOTCONN: |
| 300 |
# endif |
| 301 |
|
| 302 |
# ifdef EINTR |
| 303 |
case EINTR: |
| 304 |
# endif |
| 305 |
|
| 306 |
# ifdef EAGAIN |
| 307 |
# if EWOULDBLOCK != EAGAIN |
| 308 |
case EAGAIN: |
| 309 |
# endif |
| 310 |
# endif |
| 311 |
|
| 312 |
# ifdef EPROTO |
| 313 |
case EPROTO: |
| 314 |
# endif |
| 315 |
|
| 316 |
# ifdef EINPROGRESS |
| 317 |
case EINPROGRESS: |
| 318 |
# endif |
| 319 |
|
| 320 |
# ifdef EALREADY |
| 321 |
case EALREADY: |
| 322 |
# endif |
| 323 |
return (1); |
| 324 |
/* break; */ |
| 325 |
default: |
| 326 |
break; |
| 327 |
} |
| 328 |
return (0); |
| 329 |
} |
| 330 |
#endif |