2 * Copyright 1995-2020 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
12 #include "bio_local.h"
13 #include "internal/cryptlib.h"
15 #ifndef OPENSSL_NO_SOCK
17 # include <openssl/bio.h>
20 /* Watt-32 uses same names */
24 # define sock_write SockWrite
25 # define sock_read SockRead
26 # define sock_puts SockPuts
29 static int sock_write(BIO *h, const char *buf, int num);
30 static int sock_read(BIO *h, char *buf, int size);
31 static int sock_puts(BIO *h, const char *str);
32 static long sock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
33 static int sock_new(BIO *h);
34 static int sock_free(BIO *data);
35 int BIO_sock_should_retry(int s);
37 static const BIO_METHOD methods_sockp = {
40 /* TODO: Convert to new style write function */
43 /* TODO: Convert to new style read function */
47 NULL, /* sock_gets, */
51 NULL, /* sock_callback_ctrl */
54 const BIO_METHOD *BIO_s_socket(void)
56 return &methods_sockp;
59 BIO *BIO_new_socket(int fd, int close_flag)
63 ret = BIO_new(BIO_s_socket());
66 BIO_set_fd(ret, fd, close_flag);
70 static int sock_new(BIO *bi)
79 static int sock_free(BIO *a)
85 BIO_closesocket(a->num);
93 static int sock_read(BIO *b, char *out, int outl)
99 ret = readsocket(b->num, out, outl);
100 BIO_clear_retry_flags(b);
102 if (BIO_sock_should_retry(ret))
103 BIO_set_retry_read(b);
105 b->flags |= BIO_FLAGS_IN_EOF;
111 static int sock_write(BIO *b, const char *in, int inl)
115 clear_socket_error();
116 ret = writesocket(b->num, in, inl);
117 BIO_clear_retry_flags(b);
119 if (BIO_sock_should_retry(ret))
120 BIO_set_retry_write(b);
125 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
133 b->num = *((int *)ptr);
134 b->shutdown = (int)num;
146 case BIO_CTRL_GET_CLOSE:
149 case BIO_CTRL_SET_CLOSE:
150 b->shutdown = (int)num;
157 ret = (b->flags & BIO_FLAGS_IN_EOF) != 0 ? 1 : 0;
166 static int sock_puts(BIO *bp, const char *str)
171 ret = sock_write(bp, str, n);
175 int BIO_sock_should_retry(int i)
179 if ((i == 0) || (i == -1)) {
180 err = get_last_socket_error();
182 return BIO_sock_non_fatal_error(err);
187 int BIO_sock_non_fatal_error(int err)
190 # if defined(OPENSSL_SYS_WINDOWS)
191 # if defined(WSAEWOULDBLOCK)
197 # ifdef WSAEWOULDBLOCK
198 # if WSAEWOULDBLOCK != EWOULDBLOCK
206 # if defined(ENOTCONN)
215 # if EWOULDBLOCK != EAGAIN
238 #endif /* #ifndef OPENSSL_NO_SOCK */