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 = {
42 /* TODO: Convert to new style read function */
46 NULL, /* sock_gets, */
53 const BIO_METHOD *BIO_s_socket(void)
55 return (&methods_sockp);
58 BIO *BIO_new_socket(int fd, int close_flag)
62 ret = BIO_new(BIO_s_socket());
65 BIO_set_fd(ret, fd, close_flag);
69 static int sock_new(BIO *bi)
78 static int sock_free(BIO *a)
84 BIO_closesocket(a->num);
92 static int sock_read(BIO *b, char *out, int outl)
98 ret = readsocket(b->num, out, outl);
99 BIO_clear_retry_flags(b);
101 if (BIO_sock_should_retry(ret))
102 BIO_set_retry_read(b);
108 static int sock_write(BIO *b, const char *in, int inl)
112 clear_socket_error();
113 ret = writesocket(b->num, in, inl);
114 BIO_clear_retry_flags(b);
116 if (BIO_sock_should_retry(ret))
117 BIO_set_retry_write(b);
122 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
130 b->num = *((int *)ptr);
131 b->shutdown = (int)num;
143 case BIO_CTRL_GET_CLOSE:
146 case BIO_CTRL_SET_CLOSE:
147 b->shutdown = (int)num;
160 static int sock_puts(BIO *bp, const char *str)
165 ret = sock_write(bp, str, n);
169 int BIO_sock_should_retry(int i)
173 if ((i == 0) || (i == -1)) {
174 err = get_last_socket_error();
176 return (BIO_sock_non_fatal_error(err));
181 int BIO_sock_non_fatal_error(int err)
184 # if defined(OPENSSL_SYS_WINDOWS)
185 # if defined(WSAEWOULDBLOCK)
191 # ifdef WSAEWOULDBLOCK
192 # if WSAEWOULDBLOCK != EWOULDBLOCK
200 # if defined(ENOTCONN)
209 # if EWOULDBLOCK != EAGAIN
233 #endif /* #ifndef OPENSSL_NO_SOCK */