2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
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);
109 static int sock_write(BIO *b, const char *in, int inl)
113 clear_socket_error();
114 ret = writesocket(b->num, in, inl);
115 BIO_clear_retry_flags(b);
117 if (BIO_sock_should_retry(ret))
118 BIO_set_retry_write(b);
123 static long sock_ctrl(BIO *b, int cmd, long num, void *ptr)
131 b->num = *((int *)ptr);
132 b->shutdown = (int)num;
144 case BIO_CTRL_GET_CLOSE:
147 case BIO_CTRL_SET_CLOSE:
148 b->shutdown = (int)num;
161 static int sock_puts(BIO *bp, const char *str)
166 ret = sock_write(bp, str, n);
170 int BIO_sock_should_retry(int i)
174 if ((i == 0) || (i == -1)) {
175 err = get_last_socket_error();
177 return BIO_sock_non_fatal_error(err);
182 int BIO_sock_non_fatal_error(int err)
185 # if defined(OPENSSL_SYS_WINDOWS)
186 # if defined(WSAEWOULDBLOCK)
192 # ifdef WSAEWOULDBLOCK
193 # if WSAEWOULDBLOCK != EWOULDBLOCK
201 # if defined(ENOTCONN)
210 # if EWOULDBLOCK != EAGAIN
233 #endif /* #ifndef OPENSSL_NO_SOCK */