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
12 #include "bio_local.h"
13 #include "internal/cryptlib.h"
14 #include "internal/ktls.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, */
52 NULL
, /* sock_callback_ctrl */
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
);
68 # ifndef OPENSSL_NO_KTLS
71 * The new socket is created successfully regardless of ktls_enable.
72 * ktls_enable doesn't change any functionality of the socket, except
73 * changing the setsockopt to enable the processing of ktls_start.
74 * Thus, it is not a problem to call it for non-TLS sockets.
82 static int sock_new(BIO
*bi
)
91 static int sock_free(BIO
*a
)
97 BIO_closesocket(a
->num
);
105 static int sock_read(BIO
*b
, char *out
, int outl
)
110 clear_socket_error();
111 # ifndef OPENSSL_NO_KTLS
112 if (BIO_get_ktls_recv(b
))
113 ret
= ktls_read_record(b
->num
, out
, outl
);
116 ret
= readsocket(b
->num
, out
, outl
);
117 BIO_clear_retry_flags(b
);
119 if (BIO_sock_should_retry(ret
))
120 BIO_set_retry_read(b
);
126 static int sock_write(BIO
*b
, const char *in
, int inl
)
130 clear_socket_error();
131 # ifndef OPENSSL_NO_KTLS
132 if (BIO_should_ktls_ctrl_msg_flag(b
)) {
133 unsigned char record_type
= (intptr_t)b
->ptr
;
134 ret
= ktls_send_ctrl_message(b
->num
, record_type
, in
, inl
);
137 BIO_clear_ktls_ctrl_msg_flag(b
);
141 ret
= writesocket(b
->num
, in
, inl
);
142 BIO_clear_retry_flags(b
);
144 if (BIO_sock_should_retry(ret
))
145 BIO_set_retry_write(b
);
150 static long sock_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
154 # ifndef OPENSSL_NO_KTLS
155 struct tls12_crypto_info_aes_gcm_128
*crypto_info
;
161 b
->num
= *((int *)ptr
);
162 b
->shutdown
= (int)num
;
174 case BIO_CTRL_GET_CLOSE
:
177 case BIO_CTRL_SET_CLOSE
:
178 b
->shutdown
= (int)num
;
184 # ifndef OPENSSL_NO_KTLS
185 case BIO_CTRL_SET_KTLS
:
186 crypto_info
= (struct tls12_crypto_info_aes_gcm_128
*)ptr
;
187 ret
= ktls_start(b
->num
, crypto_info
, sizeof(*crypto_info
), num
);
189 BIO_set_ktls_flag(b
, num
);
191 case BIO_CTRL_GET_KTLS_SEND
:
192 return BIO_should_ktls_flag(b
, 1);
193 case BIO_CTRL_GET_KTLS_RECV
:
194 return BIO_should_ktls_flag(b
, 0);
195 case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG
:
196 BIO_set_ktls_ctrl_msg_flag(b
);
197 b
->ptr
= (void *)num
;
200 case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG
:
201 BIO_clear_ktls_ctrl_msg_flag(b
);
212 static int sock_puts(BIO
*bp
, const char *str
)
217 ret
= sock_write(bp
, str
, n
);
221 int BIO_sock_should_retry(int i
)
225 if ((i
== 0) || (i
== -1)) {
226 err
= get_last_socket_error();
228 return BIO_sock_non_fatal_error(err
);
233 int BIO_sock_non_fatal_error(int err
)
236 # if defined(OPENSSL_SYS_WINDOWS)
237 # if defined(WSAEWOULDBLOCK)
243 # ifdef WSAEWOULDBLOCK
244 # if WSAEWOULDBLOCK != EWOULDBLOCK
252 # if defined(ENOTCONN)
261 # if EWOULDBLOCK != EAGAIN
284 #endif /* #ifndef OPENSSL_NO_SOCK */