155714Skris/* crypto/bio/bss_sock.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 5955714Skris#include <stdio.h> 6055714Skris#include <errno.h> 6155714Skris#define USE_SOCKETS 6255714Skris#include "cryptlib.h" 63194206Ssimon 64194206Ssimon#ifndef OPENSSL_NO_SOCK 65194206Ssimon 66280297Sjkim# include <openssl/bio.h> 6755714Skris 68280297Sjkim# ifdef WATT32 69280297Sjkim# define sock_write SockWrite /* Watt-32 uses same names */ 70280297Sjkim# define sock_read SockRead 71280297Sjkim# define sock_puts SockPuts 72280297Sjkim# endif 73109998Smarkm 7468651Skrisstatic int sock_write(BIO *h, const char *buf, int num); 7568651Skrisstatic int sock_read(BIO *h, char *buf, int size); 7668651Skrisstatic int sock_puts(BIO *h, const char *str); 7768651Skrisstatic long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2); 7855714Skrisstatic int sock_new(BIO *h); 7955714Skrisstatic int sock_free(BIO *data); 8055714Skrisint BIO_sock_should_retry(int s); 8155714Skris 82280297Sjkimstatic BIO_METHOD methods_sockp = { 83280297Sjkim BIO_TYPE_SOCKET, 84280297Sjkim "socket", 85280297Sjkim sock_write, 86280297Sjkim sock_read, 87280297Sjkim sock_puts, 88280297Sjkim NULL, /* sock_gets, */ 89280297Sjkim sock_ctrl, 90280297Sjkim sock_new, 91280297Sjkim sock_free, 92280297Sjkim NULL, 93280297Sjkim}; 9455714Skris 9555714SkrisBIO_METHOD *BIO_s_socket(void) 96280297Sjkim{ 97280297Sjkim return (&methods_sockp); 98280297Sjkim} 9955714Skris 10055714SkrisBIO *BIO_new_socket(int fd, int close_flag) 101280297Sjkim{ 102280297Sjkim BIO *ret; 10355714Skris 104280297Sjkim ret = BIO_new(BIO_s_socket()); 105280297Sjkim if (ret == NULL) 106280297Sjkim return (NULL); 107280297Sjkim BIO_set_fd(ret, fd, close_flag); 108280297Sjkim return (ret); 109280297Sjkim} 11055714Skris 11155714Skrisstatic int sock_new(BIO *bi) 112280297Sjkim{ 113280297Sjkim bi->init = 0; 114280297Sjkim bi->num = 0; 115280297Sjkim bi->ptr = NULL; 116280297Sjkim bi->flags = 0; 117280297Sjkim return (1); 118280297Sjkim} 11955714Skris 12055714Skrisstatic int sock_free(BIO *a) 121280297Sjkim{ 122280297Sjkim if (a == NULL) 123280297Sjkim return (0); 124280297Sjkim if (a->shutdown) { 125280297Sjkim if (a->init) { 126280297Sjkim SHUTDOWN2(a->num); 127280297Sjkim } 128280297Sjkim a->init = 0; 129280297Sjkim a->flags = 0; 130280297Sjkim } 131280297Sjkim return (1); 132280297Sjkim} 133280297Sjkim 13455714Skrisstatic int sock_read(BIO *b, char *out, int outl) 135280297Sjkim{ 136280297Sjkim int ret = 0; 13755714Skris 138280297Sjkim if (out != NULL) { 139280297Sjkim clear_socket_error(); 140280297Sjkim ret = readsocket(b->num, out, outl); 141280297Sjkim BIO_clear_retry_flags(b); 142280297Sjkim if (ret <= 0) { 143280297Sjkim if (BIO_sock_should_retry(ret)) 144280297Sjkim BIO_set_retry_read(b); 145280297Sjkim } 146280297Sjkim } 147280297Sjkim return (ret); 148280297Sjkim} 14955714Skris 15068651Skrisstatic int sock_write(BIO *b, const char *in, int inl) 151280297Sjkim{ 152280297Sjkim int ret; 15355714Skris 154280297Sjkim clear_socket_error(); 155280297Sjkim ret = writesocket(b->num, in, inl); 156280297Sjkim BIO_clear_retry_flags(b); 157280297Sjkim if (ret <= 0) { 158280297Sjkim if (BIO_sock_should_retry(ret)) 159280297Sjkim BIO_set_retry_write(b); 160280297Sjkim } 161280297Sjkim return (ret); 162280297Sjkim} 163280297Sjkim 16468651Skrisstatic long sock_ctrl(BIO *b, int cmd, long num, void *ptr) 165280297Sjkim{ 166280297Sjkim long ret = 1; 167280297Sjkim int *ip; 16855714Skris 169280297Sjkim switch (cmd) { 170280297Sjkim case BIO_C_SET_FD: 171280297Sjkim sock_free(b); 172280297Sjkim b->num = *((int *)ptr); 173280297Sjkim b->shutdown = (int)num; 174280297Sjkim b->init = 1; 175280297Sjkim break; 176280297Sjkim case BIO_C_GET_FD: 177280297Sjkim if (b->init) { 178280297Sjkim ip = (int *)ptr; 179280297Sjkim if (ip != NULL) 180280297Sjkim *ip = b->num; 181280297Sjkim ret = b->num; 182280297Sjkim } else 183280297Sjkim ret = -1; 184280297Sjkim break; 185280297Sjkim case BIO_CTRL_GET_CLOSE: 186280297Sjkim ret = b->shutdown; 187280297Sjkim break; 188280297Sjkim case BIO_CTRL_SET_CLOSE: 189280297Sjkim b->shutdown = (int)num; 190280297Sjkim break; 191280297Sjkim case BIO_CTRL_DUP: 192280297Sjkim case BIO_CTRL_FLUSH: 193280297Sjkim ret = 1; 194280297Sjkim break; 195280297Sjkim default: 196280297Sjkim ret = 0; 197280297Sjkim break; 198280297Sjkim } 199280297Sjkim return (ret); 200280297Sjkim} 20155714Skris 20268651Skrisstatic int sock_puts(BIO *bp, const char *str) 203280297Sjkim{ 204280297Sjkim int n, ret; 20555714Skris 206280297Sjkim n = strlen(str); 207280297Sjkim ret = sock_write(bp, str, n); 208280297Sjkim return (ret); 209280297Sjkim} 21055714Skris 21155714Skrisint BIO_sock_should_retry(int i) 212280297Sjkim{ 213280297Sjkim int err; 21455714Skris 215280297Sjkim if ((i == 0) || (i == -1)) { 216280297Sjkim err = get_last_socket_error(); 21755714Skris 218280297Sjkim# if defined(OPENSSL_SYS_WINDOWS) && 0/* more microsoft stupidity? perhaps 219280297Sjkim * not? Ben 4/1/99 */ 220280297Sjkim if ((i == -1) && (err == 0)) 221280297Sjkim return (1); 222280297Sjkim# endif 22355714Skris 224280297Sjkim return (BIO_sock_non_fatal_error(err)); 225280297Sjkim } 226280297Sjkim return (0); 227280297Sjkim} 22855714Skris 22955714Skrisint BIO_sock_non_fatal_error(int err) 230280297Sjkim{ 231280297Sjkim switch (err) { 232280297Sjkim# if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE) 233280297Sjkim# if defined(WSAEWOULDBLOCK) 234280297Sjkim case WSAEWOULDBLOCK: 235280297Sjkim# endif 23655714Skris 237280297Sjkim# if 0 /* This appears to always be an error */ 238280297Sjkim# if defined(WSAENOTCONN) 239280297Sjkim case WSAENOTCONN: 240280297Sjkim# endif 24155714Skris# endif 24255714Skris# endif 24355714Skris 244280297Sjkim# ifdef EWOULDBLOCK 245280297Sjkim# ifdef WSAEWOULDBLOCK 246280297Sjkim# if WSAEWOULDBLOCK != EWOULDBLOCK 247280297Sjkim case EWOULDBLOCK: 248280297Sjkim# endif 249280297Sjkim# else 250280297Sjkim case EWOULDBLOCK: 25155714Skris# endif 25255714Skris# endif 25355714Skris 254280297Sjkim# if defined(ENOTCONN) 255280297Sjkim case ENOTCONN: 256280297Sjkim# endif 25755714Skris 258280297Sjkim# ifdef EINTR 259280297Sjkim case EINTR: 260280297Sjkim# endif 26155714Skris 262280297Sjkim# ifdef EAGAIN 263280297Sjkim# if EWOULDBLOCK != EAGAIN 264280297Sjkim case EAGAIN: 265280297Sjkim# endif 26655714Skris# endif 26755714Skris 268280297Sjkim# ifdef EPROTO 269280297Sjkim case EPROTO: 270280297Sjkim# endif 27155714Skris 272280297Sjkim# ifdef EINPROGRESS 273280297Sjkim case EINPROGRESS: 274280297Sjkim# endif 27555714Skris 276280297Sjkim# ifdef EALREADY 277280297Sjkim case EALREADY: 278280297Sjkim# endif 279280297Sjkim return (1); 280280297Sjkim /* break; */ 281280297Sjkim default: 282280297Sjkim break; 283280297Sjkim } 284280297Sjkim return (0); 285280297Sjkim} 286194206Ssimon 287280297Sjkim#endif /* #ifndef OPENSSL_NO_SOCK */ 288