1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef OPENSSL_HEADER_CRYPTO_BIO_INTERNAL_H
16 #define OPENSSL_HEADER_CRYPTO_BIO_INTERNAL_H
17 
18 #include <openssl/bio.h>
19 
20 #include <openssl/ex_data.h>
21 
22 #include "../internal.h"
23 
24 #if !defined(OPENSSL_NO_SOCK)
25 #if !defined(OPENSSL_WINDOWS)
26 #if defined(OPENSSL_PNACL)
27 // newlib uses u_short in socket.h without defining it.
28 typedef unsigned short u_short;
29 #endif
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #else
33 #include <winsock2.h>
34 typedef int socklen_t;
35 #endif
36 #endif  // !OPENSSL_NO_SOCK
37 
38 #if defined(__cplusplus)
39 extern "C" {
40 #endif
41 
42 
43 struct bio_method_st {
44   int type;
45   const char *name;
46   int (*bwrite)(BIO *, const char *, int);
47   int (*bread)(BIO *, char *, int);
48   int (*bgets)(BIO *, char *, int);
49   long (*ctrl)(BIO *, int, long, void *);
50   int (*create)(BIO *);
51   int (*destroy)(BIO *);
52   long (*callback_ctrl)(BIO *, int, BIO_info_cb *);
53 };
54 
55 struct bio_st {
56   const BIO_METHOD *method;
57   CRYPTO_EX_DATA ex_data;
58 
59   // TODO(crbug.com/412269080): |init| and |shutdown| could be bitfields, or
60   // integrated into |flags|, to save memory.
61 
62   // init is non-zero if this |BIO| has been initialised.
63   int init;
64   // shutdown is often used by specific |BIO_METHOD|s to determine whether
65   // they own some underlying resource. This flag can often be controlled by
66   // |BIO_set_close|. For example, whether an fd BIO closes the underlying fd
67   // when it, itself, is closed.
68   int shutdown;
69   int flags;
70   int retry_reason;
71   // num is a BIO-specific value. For example, in fd BIOs it's used to store a
72   // file descriptor.
73   int num;
74   CRYPTO_refcount_t references;
75   void *ptr;
76   // next_bio points to the next |BIO| in a chain. This |BIO| owns a reference
77   // to |next_bio|.
78   BIO *next_bio;  // used by filter BIOs
79   uint64_t num_read, num_write;
80 };
81 
82 #if !defined(OPENSSL_NO_SOCK)
83 
84 // bio_ip_and_port_to_socket_and_addr creates a socket and fills in |*out_addr|
85 // and |*out_addr_length| with the correct values for connecting to |hostname|
86 // on |port_str|. It returns one on success or zero on error.
87 int bio_ip_and_port_to_socket_and_addr(int *out_sock,
88                                        struct sockaddr_storage *out_addr,
89                                        socklen_t *out_addr_length,
90                                        const char *hostname,
91                                        const char *port_str);
92 
93 // bio_socket_nbio sets whether |sock| is non-blocking. It returns one on
94 // success and zero otherwise.
95 int bio_socket_nbio(int sock, int on);
96 
97 // bio_clear_socket_error clears the last system socket error.
98 //
99 // TODO(fork): remove all callers of this.
100 void bio_clear_socket_error(void);
101 
102 // bio_sock_error returns the last socket error on |sock|.
103 int bio_sock_error(int sock);
104 
105 // bio_socket_should_retry returns non-zero if |return_value| indicates an error
106 // and the last socket error indicates that it's non-fatal.
107 int bio_socket_should_retry(int return_value);
108 
109 #endif  // !OPENSSL_NO_SOCK
110 
111 // bio_errno_should_retry returns non-zero if |return_value| indicates an error
112 // and |errno| indicates that it's non-fatal.
113 int bio_errno_should_retry(int return_value);
114 
115 
116 #if defined(__cplusplus)
117 }  // extern C
118 #endif
119 
120 #endif  // OPENSSL_HEADER_CRYPTO_BIO_INTERNAL_H
121