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 = {
41 /* TODO: Convert to new style write function */
44 /* TODO: Convert to new style read function */
48 NULL, /* sock_gets, */
55 const BIO_METHOD *BIO_s_socket(void)
57 return (&methods_sockp);
60 BIO *BIO_new_socket(int fd, int close_flag)
64 ret = BIO_new(BIO_s_socket());
67 BIO_set_fd(ret, fd, close_flag);
71 static int sock_new(BIO *bi)
80 static int sock_free(BIO *a)
86 BIO_closesocket(a->num);
94 static int sock_read(BIO *b, char *out, int outl)
100 ret = readsocket(b->num, out, outl);
101 BIO_clear_retry_flags(b);
103 if (BIO_sock_should_retry(ret))
104 BIO_set_retry_read(b);
110 static int sock_write(BIO *b, const char *in, int inl)
114 clear_socket_error();
115 ret = writesocket(b->num, in, inl);
116 BIO_clear_retry_flags(b);
118 if (BIO_sock_should_retry(ret))
119 BIO_set_retry_write(b);
124 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
132 b->num = *((int *)ptr);
133 b->shutdown = (int)num;
145 case BIO_CTRL_GET_CLOSE:
148 case BIO_CTRL_SET_CLOSE:
149 b->shutdown = (int)num;
162 static int sock_puts(BIO *bp, const char *str)
167 ret = sock_write(bp, str, n);
171 int BIO_sock_should_retry(int i)
175 if ((i == 0) || (i == -1)) {
176 err = get_last_socket_error();
178 return (BIO_sock_non_fatal_error(err));
183 int BIO_sock_non_fatal_error(int err)
186 # if defined(OPENSSL_SYS_WINDOWS)
187 # if defined(WSAEWOULDBLOCK)
193 # ifdef WSAEWOULDBLOCK
194 # if WSAEWOULDBLOCK != EWOULDBLOCK
202 # if defined(ENOTCONN)
211 # if EWOULDBLOCK != EAGAIN
234 #endif /* #ifndef OPENSSL_NO_SOCK */