Optimized BIO mem read - without reallocation
[openssl.git] / crypto / bio / bio_lcl.h
1 #define USE_SOCKETS
2 #include "e_os.h"
3
4 /* BEGIN BIO_ADDRINFO/BIO_ADDR stuff. */
5
6 #ifndef OPENSSL_NO_SOCK
7 /*
8  * Throughout this file and b_addr.c, the existence of the macro
9  * AI_PASSIVE is used to detect the availability of struct addrinfo,
10  * getnameinfo() and getaddrinfo().  If that macro doesn't exist,
11  * we use our own implementation instead.
12  */
13
14 /*
15  * It's imperative that these macros get defined before openssl/bio.h gets
16  * included.  Otherwise, the AI_PASSIVE hack will not work properly.
17  * For clarity, we check for internal/cryptlib.h since it's a common header
18  * that also includes bio.h.
19  */
20 # ifdef HEADER_CRYPTLIB_H
21 #  error internal/cryptlib.h included before bio_lcl.h
22 # endif
23 # ifdef HEADER_BIO_H
24 #  error openssl/bio.h included before bio_lcl.h
25 # endif
26
27 /*
28  * Undefine AF_UNIX on systems that define it but don't support it.
29  */
30 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VMS)
31 #  undef AF_UNIX
32 # endif
33
34 # ifdef AI_PASSIVE
35
36 /*
37  * There's a bug in VMS C header file netdb.h, where struct addrinfo
38  * always is the P32 variant, but the functions that handle that structure,
39  * such as getaddrinfo() and freeaddrinfo() adapt to the initial pointer
40  * size.  The easiest workaround is to force struct addrinfo to be the
41  * 64-bit variant when compiling in P64 mode.
42  */
43 #  if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE == 64
44 #   define addrinfo __addrinfo64
45 #  endif
46
47 #  define bio_addrinfo_st addrinfo
48 #  define bai_family      ai_family
49 #  define bai_socktype    ai_socktype
50 #  define bai_protocol    ai_protocol
51 #  define bai_addrlen     ai_addrlen
52 #  define bai_addr        ai_addr
53 #  define bai_next        ai_next
54 # else
55 struct bio_addrinfo_st {
56     int bai_family;
57     int bai_socktype;
58     int bai_protocol;
59     size_t bai_addrlen;
60     struct sockaddr *bai_addr;
61     struct bio_addrinfo_st *bai_next;
62 };
63 # endif
64
65 union bio_addr_st {
66     struct sockaddr sa;
67 # ifdef AF_INET6
68     struct sockaddr_in6 s_in6;
69 # endif
70     struct sockaddr_in s_in;
71 # ifdef AF_UNIX
72     struct sockaddr_un s_un;
73 # endif
74 };
75 #endif
76
77 /* END BIO_ADDRINFO/BIO_ADDR stuff. */
78
79 /* BIO_BUF_MEM  */
80 struct bio_buf_mem_st {
81     struct buf_mem_st *buf;   /* allocated buffer */
82     struct buf_mem_st *readp; /* read pointer */
83 };
84
85 #include "internal/cryptlib.h"
86 #include <internal/bio.h>
87
88 typedef struct bio_f_buffer_ctx_struct {
89     /*-
90      * Buffers are setup like this:
91      *
92      * <---------------------- size ----------------------->
93      * +---------------------------------------------------+
94      * | consumed | remaining          | free space        |
95      * +---------------------------------------------------+
96      * <-- off --><------- len ------->
97      */
98     /*- BIO *bio; *//*
99      * this is now in the BIO struct
100      */
101     int ibuf_size;              /* how big is the input buffer */
102     int obuf_size;              /* how big is the output buffer */
103     char *ibuf;                 /* the char array */
104     int ibuf_len;               /* how many bytes are in it */
105     int ibuf_off;               /* write/read offset */
106     char *obuf;                 /* the char array */
107     int obuf_len;               /* how many bytes are in it */
108     int obuf_off;               /* write/read offset */
109 } BIO_F_BUFFER_CTX;
110
111 struct bio_st {
112     const BIO_METHOD *method;
113     /* bio, mode, argp, argi, argl, ret */
114     long (*callback) (struct bio_st *, int, const char *, int, long, long);
115     char *cb_arg;               /* first argument for the callback */
116     int init;
117     int shutdown;
118     int flags;                  /* extra storage */
119     int retry_reason;
120     int num;
121     void *ptr;
122     struct bio_st *next_bio;    /* used by filter BIOs */
123     struct bio_st *prev_bio;    /* used by filter BIOs */
124     int references;
125     uint64_t num_read;
126     uint64_t num_write;
127     CRYPTO_EX_DATA ex_data;
128     CRYPTO_RWLOCK *lock;
129 };
130
131 #ifndef OPENSSL_NO_SOCK
132 # ifdef OPENSSL_SYS_VMS
133 typedef unsigned int socklen_t;
134 # endif
135
136 int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa);
137 const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap);
138 struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap);
139 socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap);
140 socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai);
141 const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai);
142 #endif
143
144 #if BIO_FLAGS_UPLINK==0
145 /* Shortcut UPLINK calls on most platforms... */
146 # define UP_stdin        stdin
147 # define UP_stdout       stdout
148 # define UP_stderr       stderr
149 # define UP_fprintf      fprintf
150 # define UP_fgets        fgets
151 # define UP_fread        fread
152 # define UP_fwrite       fwrite
153 # undef  UP_fsetmod
154 # define UP_feof         feof
155 # define UP_fclose       fclose
156
157 # define UP_fopen        fopen
158 # define UP_fseek        fseek
159 # define UP_ftell        ftell
160 # define UP_fflush       fflush
161 # define UP_ferror       ferror
162 # ifdef _WIN32
163 #  define UP_fileno       _fileno
164 #  define UP_open         _open
165 #  define UP_read         _read
166 #  define UP_write        _write
167 #  define UP_lseek        _lseek
168 #  define UP_close        _close
169 # else
170 #  define UP_fileno       fileno
171 #  define UP_open         open
172 #  define UP_read         read
173 #  define UP_write        write
174 #  define UP_lseek        lseek
175 #  define UP_close        close
176 # endif
177
178 #endif
179