2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include "internal/cryptlib.h"
16 #ifndef OPENSSL_NO_SOCK
18 # include <openssl/bio.h>
21 /* Watt-32 uses same names */
25 # define sock_write SockWrite
26 # define sock_read SockRead
27 # define sock_puts SockPuts
30 static int sock_write(BIO *h, const char *buf, int num);
31 static int sock_read(BIO *h, char *buf, int size);
32 static int sock_puts(BIO *h, const char *str);
33 static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
34 static int sock_new(BIO *h);
35 static int sock_free(BIO *data);
36 int BIO_sock_should_retry(int s);
38 static const BIO_METHOD methods_sockp = {
44 NULL, /* sock_gets, */
51 const BIO_METHOD *BIO_s_socket(void)
53 return (&methods_sockp);
56 BIO *BIO_new_socket(int fd, int close_flag)
60 ret = BIO_new(BIO_s_socket());
63 BIO_set_fd(ret, fd, close_flag);
67 static int sock_new(BIO *bi)
76 static int sock_free(BIO *a)
82 BIO_closesocket(a->num);
90 static int sock_read(BIO *b, char *out, int outl)
96 ret = readsocket(b->num, out, outl);
97 BIO_clear_retry_flags(b);
99 if (BIO_sock_should_retry(ret))
100 BIO_set_retry_read(b);
106 static int sock_write(BIO *b, const char *in, int inl)
110 clear_socket_error();
111 ret = writesocket(b->num, in, inl);
112 BIO_clear_retry_flags(b);
114 if (BIO_sock_should_retry(ret))
115 BIO_set_retry_write(b);
120 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
128 b->num = *((int *)ptr);
129 b->shutdown = (int)num;
141 case BIO_CTRL_GET_CLOSE:
144 case BIO_CTRL_SET_CLOSE:
145 b->shutdown = (int)num;
158 static int sock_puts(BIO *bp, const char *str)
163 ret = sock_write(bp, str, n);
167 int BIO_sock_should_retry(int i)
171 if ((i == 0) || (i == -1)) {
172 err = get_last_socket_error();
174 return (BIO_sock_non_fatal_error(err));
179 int BIO_sock_non_fatal_error(int err)
182 # if defined(OPENSSL_SYS_WINDOWS)
183 # if defined(WSAEWOULDBLOCK)
189 # ifdef WSAEWOULDBLOCK
190 # if WSAEWOULDBLOCK != EWOULDBLOCK
198 # if defined(ENOTCONN)
207 # if EWOULDBLOCK != EAGAIN
231 #endif /* #ifndef OPENSSL_NO_SOCK */