cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
stream_int.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * Internal STREAM Header File *
4 * Copyright Peter Gutmann 1993-2007 *
5 * *
6 ****************************************************************************/
7 
8 #ifndef _STREAM_INT_DEFINED
9 
10 #define _STREAM_INT_DEFINED
11 
12 #if defined( INC_ALL )
13  #include "stream.h"
14 #else
15  #include "io/stream.h"
16 #endif /* Compiler-specific includes */
17 
18 /****************************************************************************
19 * *
20 * Stream Constants *
21 * *
22 ****************************************************************************/
23 
24 /* The stream types */
25 
26 typedef enum {
27  STREAM_TYPE_NONE, /* No stream type */
28  STREAM_TYPE_NULL, /* Null stream (/dev/nul) */
29  STREAM_TYPE_MEMORY, /* Memory stream */
30  STREAM_TYPE_FILE, /* File stream */
31  STREAM_TYPE_NETWORK, /* Network stream */
32  STREAM_TYPE_LAST /* Last possible stream type */
33  } STREAM_TYPE;
34 
35 /* General-purpose stream flags. These are:
36 
37  FLAG_DIRTY: Stream buffer contains data that needs to be committed to
38  backing store.
39 
40  FLAG_PARTIALREAD: Used for network reads to handle timeouts and for file
41  streams when we don't know the full extent of a file stream. When
42  this is set and we ask for a read of n bytes and there isn't
43  sufficient data present in the file to satisfy the request the
44  stream code returns 0...n bytes rather than an underflow error.
45 
46  FLAG_PARTIALWRITE: Used for network streams when performing bulk data
47  transfers, in this case the write may time out and can be restarted
48  later rather than returning a timeout error
49 
50  FLAG_READONLY: Stream is read-only */
51 
52 #define STREAM_FLAG_READONLY 0x0001 /* Read-only stream */
53 #define STREAM_FLAG_PARTIALREAD 0x0002 /* Allow read of less than req.amount */
54 #define STREAM_FLAG_PARTIALWRITE 0x0004 /* Allow write of less than req.amount */
55 #define STREAM_FLAG_DIRTY 0x0008 /* Stream contains un-committed data */
56 #define STREAM_FLAG_MASK 0x000F /* Mask for general-purpose flags */
57 
58 /* Memory stream flags. These are:
59 
60  MFLAG_VFILE: The underlying OS doesn't support conventional file I/O (it
61  may only support, for example, access to fixed blocks of flash
62  memory) so this is a memory stream emulating a file stream */
63 
64 #define STREAM_MFLAG_VFILE 0x0010 /* File stream emulated via mem.stream */
65 #define STREAM_MFLAG_MASK ( 0x0010 | STREAM_FLAG_MASK )
66  /* Mask for memory-only flags */
67 
68 /* File stream flags. These are:
69 
70  FFLAG_BUFFERSET: Used to indicate that the stream has an I/O buffer
71  associated with it. A stream can be opened without a buffer, but to
72  read/write data it needs to have a buffer associated with it. Since
73  this can be of variable size and sometimes isn't required at all,
74  it's created on-demand rather than always being present, and its
75  presence is indicated by this flag.
76 
77  FFLAG_EOF: The underlying file has reached EOF, no further data can be
78  read once the current buffer is emptied.
79 
80  FFLAG_MMAPPED: This is a memory-mapped file stream, used in conjunction
81  with MFLAG_VFILE virtual file streams.
82 
83  FFLAG_POSCHANGED: The position in the underlying file has changed,
84  requiring the file buffer to be refilled from the new position
85  before data can be read from it */
86 
87 #define STREAM_FFLAG_BUFFERSET 0x0080 /* Stream has associated buffer */
88 #define STREAM_FFLAG_EOF 0x0100 /* EOF reached on stream */
89 #define STREAM_FFLAG_POSCHANGED 0x0200 /* File stream position has changed */
90 #define STREAM_FFLAG_POSCHANGED_NOSKIP 0x0400 /* New stream pos.is in following block */
91 #define STREAM_FFLAG_MMAPPED 0x0800 /* File stream is memory-mapped */
92 #define STREAM_FFLAG_MASK ( 0x0F80 | STREAM_FLAG_MASK )
93  /* Mask for file-only flags */
94 
95 /* Network stream flags. Since there are quite a number of these and they're
96  only required for the network-specific stream functionality, we give them
97  their own flags variable instead of using the overall stream flags. These
98  are:
99 
100  NFLAG_ENCAPS: The protocol is running over a lower encapsulation layer
101  that provides additional packet control information, typically
102  packet size and flow control information. If this flag is set then
103  the lower-level read code overrides some error handling that
104  normally takes place at a higher level. For example if a read of n
105  bytes is requested and the encapsulation layer reports that only m
106  bytes, m < n is present, this isn't treated as a read/timeout error.
107 
108  NFLAG_FIRSTREADOK: The first data read from the stream succeeded. This
109  is used to detect problems due to buggy firewall software, see the
110  comments in io/tcp.c for details.
111 
112  NFLAG_HTTP10: This is an HTTP 1.0 (rather than 1.1) HTTP stream.
113 
114  NFLAG_HTTPPROXY/NFLAG_HTTPTUNNEL: HTTP proxy control flags.
115 
116  NFLAG_HTTPGET/NFLAG_HTTPPOST: HTTP allowed-actions flags.
117 
118  NFLAG_ISSERVER: The stream is a server stream (default is client).
119 
120  NFLAG_LASTMSG: This is the last message in the exchange, after which any
121  high-level shutdown (for example at the HTTP level) can be
122  performed.
123 
124  NFLAG_USERSOCKET: The network socket was supplied by the user rather
125  than being created by cryptlib, so some actions such as socket
126  shutdown should be skipped */
127 
128 #define STREAM_NFLAG_ISSERVER 0x0001 /* Stream is server rather than client */
129 #define STREAM_NFLAG_USERSOCKET 0x0002 /* Network socket was supplied by user */
130 #define STREAM_NFLAG_HTTP10 0x0004 /* HTTP 1.0 stream */
131 #define STREAM_NFLAG_HTTPPROXY 0x0008 /* Use HTTP proxy format for requests */
132 #define STREAM_NFLAG_HTTPTUNNEL 0x0010 /* Use HTTP proxy tunnel for connect */
133 #define STREAM_NFLAG_HTTPGET 0x0020 /* Allow HTTP GET */
134 #define STREAM_NFLAG_HTTPPOST 0x0040 /* Allow HTTP POST */
135 #define STREAM_NFLAG_LASTMSG 0x0080 /* Last message in exchange */
136 #define STREAM_NFLAG_ENCAPS 0x0100 /* Network transport is encapsulated */
137 #define STREAM_NFLAG_FIRSTREADOK 0x0200 /* First data read succeeded */
138 #define STREAM_NFLAG_HTTPREQMASK ( STREAM_NFLAG_HTTPGET | STREAM_NFLAG_HTTPPOST )
139  /* Mask for permitted HTTP req.types */
140 
141 /* Network transport-specific flags. These are:
142 
143  FLAG_FLUSH: Used in writes to buffered streams to force a flush of data in
144  the stream buffers.
145 
146  FLAG_BLOCKING/FLAG_NONBLOCKING: Used to override the stream default
147  behaviour on reads and writes and force blocking/nonblocking I/O */
148 
149 #define TRANSPORT_FLAG_NONE 0x00 /* No transport flag */
150 #define TRANSPORT_FLAG_FLUSH 0x01 /* Flush data on write */
151 #define TRANSPORT_FLAG_NONBLOCKING 0x02 /* Explicitly perform nonblocking read */
152 #define TRANSPORT_FLAG_BLOCKING 0x04 /* Explicitly perform blocking read */
153 #define TRANSPORT_FLAG_MAX 0x07 /* Maximum possible flag value */
154 
155 /* The size of the memory buffer used for virtual file streams, which are
156  used in CONFIG_NO_STDIO environments to store data before it's committed
157  to backing storage */
158 
159 #if defined( __MVS__ ) || defined( __VMCMS__ ) || \
160  defined( __IBM4758__ ) || defined( __TESTIO__ )
161  #define VIRTUAL_FILE_STREAM
162 #endif /* Nonstandard I/O environments */
163 
164 #define STREAM_VFILE_BUFSIZE 16384
165 
166 /****************************************************************************
167 * *
168 * Stream Structures *
169 * *
170 ****************************************************************************/
171 
172 #ifdef USE_TCP
173 
174 /* The network-stream specific information stored as part of the STREAM
175  data type */
176 
177 typedef struct NS {
178  /* General information for the network stream. For a server the
179  listenSocket is the (possibly shared) common socket that the server
180  is listening on, the netSocket is the ephemeral socket used for
181  communications */
182  STREAM_PROTOCOL_TYPE protocol;/* Network protocol type */
183  int nFlags; /* Network-specific flags */
184  int netSocket, listenSocket;/* Network socket */
185  CRYPT_SESSION iTransportSession;/* cryptlib session as transport layer */
186 
187  /* Network timeout information. The timeout value depends on whether
188  the stream is in the connect/handshake phase or the data transfer
189  phase. The handshake phase is logically treated as part of the
190  connect phase even though from the stream point of view it's part of
191  the data transfer phase. Initially the stream timeout is set to the
192  connect timeout and the saved timeout is set to the data transfer
193  timeout. Once the connect/handshake has completed, the stream
194  timeout is set to the saved data transfer timeout and the saved
195  timeout is cleared */
196  int timeout, savedTimeout; /* Network comms timeout */
197 
198  /* Network streams require separate read/write buffers for packet
199  assembly/disassembly so we provide a write buffer alongside the
200  generic stream read buffer */
201  BUFFER( writeBufSize, writeBufEnd ) \
202  BYTE *writeBuffer; /* Write buffer */
203  int writeBufSize; /* Total size of buffer */
204  int writeBufEnd; /* Last buffer position with valid data */
205 
206  /* General network-related information. The server FQDN is held in
207  dynamically-allocated storage, the optional path for HTTP is a pointer
208  into the host string at the appropriate location */
209  BUFFER_FIXED( hostLen ) \
210  char *host;
211  int hostLen;
213  char *path;
214  int pathLen;
215  int port; /* Host name, path on host, and port */
216  BUFFER( CRYPT_MAX_TEXTSIZE / 2, clientAddressLen ) \
217  char clientAddress[ ( CRYPT_MAX_TEXTSIZE / 2 ) + 4 ];
218  int clientAddressLen; /* Client IP address (dotted-decimal) */
219  int clientPort; /* Client port */
220 
221  /* If a network error condition is fatal we set the persistentStatus
222  value. This is checked by the higher-level stream code and copied
223  to to stream persistent status if required */
224  int persistentStatus;
225 
226  /* Last-error information returned from lower-level code */
228 
229  /* Network stream access functions. The general read and write
230  functions are for the higher-level network access routines such as
231  HTTP and CMP I/O, the transport I/O functions are for transport-level
232  I/O that sits below the general I/O. Finally, there's an
233  intermediate function that adds speculative read-ahead buffering to
234  the transport-level read to improve performance for higher-level
235  protocols like HTTP that have to read a byte at a time in some
236  places */
238  BOOLEAN ( *sanityCheckFunction )( IN const struct ST *stream );
239  CHECK_RETVAL_FNPTR STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
240  int ( *readFunction )( INOUT struct ST *stream,
241  OUT_BUFFER( maxLength, *length ) void *buffer,
242  IN_LENGTH const int maxLength,
243  OUT_LENGTH_Z int *length );
244  CHECK_RETVAL_FNPTR STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
245  int ( *writeFunction )( INOUT struct ST *stream,
246  IN_BUFFER( length ) const void *buffer,
247  IN_LENGTH const int maxLength,
248  OUT_LENGTH_Z int *length );
250  int ( *transportConnectFunction )( INOUT struct NS *netStream,
251  IN_BUFFER_OPT( length ) const char *hostName,
252  IN_LENGTH_DNS_Z const int hostNameLen,
253  IN_PORT const int port );
254  STDC_NONNULL_ARG( ( 1 ) ) \
255  void ( *transportDisconnectFunction )( INOUT struct NS *netStream,
256  const BOOLEAN fullDisconnect );
257  CHECK_RETVAL_FNPTR STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
258  int ( *transportReadFunction )( INOUT struct ST *stream,
260  IN_LENGTH const int maxLength,
261  OUT_LENGTH_Z int *length,
262  IN_FLAGS_Z( TRANSPORT ) \
263  const int flags );
264  CHECK_RETVAL_FNPTR STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
265  int ( *transportWriteFunction )( INOUT struct ST *stream,
266  IN_BUFFER( length ) const BYTE *buffer,
267  IN_LENGTH const int maxLength,
268  OUT_LENGTH_Z int *length,
269  IN_FLAGS_Z( TRANSPORT ) \
270  const int flags );
272  BOOLEAN ( *transportOKFunction )( void );
274  int ( *transportCheckFunction )( INOUT struct NS *netStream );
275  CHECK_RETVAL_FNPTR STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
276  int ( *bufferedTransportReadFunction )( INOUT struct ST *stream,
278  BYTE *buffer,
279  IN_LENGTH const int maxLength,
280  OUT_LENGTH_Z int *length,
281  IN_FLAGS_Z( TRANSPORT ) \
282  const int flags );
283  CHECK_RETVAL_FNPTR STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
284  int ( *bufferedTransportWriteFunction )( INOUT struct ST *stream,
285  IN_BUFFER( length ) const BYTE *buffer,
286  IN_LENGTH const int maxLength,
287  OUT_LENGTH_Z int *length,
288  IN_FLAGS_Z( TRANSPORT ) \
289  const int flags );
290 
291  /* Variable-length storage for the stream buffers */
293  } NET_STREAM_INFO;
294 #else
295 
296 typedef void *NET_STREAM_INFO; /* Dummy for function prototypes */
297 
298 #endif /* USE_TCP */
299 
300 /****************************************************************************
301 * *
302 * Stream Function Prototypes *
303 * *
304 ****************************************************************************/
305 
306 /* Stream query functions to determine whether a stream is a memory-mapped
307  file stream or a virtual file stream. The memory-mapped stream check is
308  used when we can eliminate extra buffer allocation if all data is
309  available in memory. The virtual file stream check is used where the
310  low-level access routines have converted a file on a CONFIG_NO_STDIO
311  system to a memory stream that acts like a file stream */
312 
313 #define sIsMemMappedStream( stream ) \
314  ( ( ( stream )->type == STREAM_TYPE_FILE ) && \
315  ( ( stream )->flags & STREAM_FFLAG_MMAPPED ) )
316 #define sIsVirtualFileStream( stream ) \
317  ( ( ( stream )->type == STREAM_TYPE_MEMORY ) && \
318  ( ( stream )->flags & STREAM_MFLAG_VFILE ) )
319 
320 /* Network URL processing functions in net_url.c */
321 
322 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
323 int parseURL( OUT URL_INFO *urlInfo,
324  IN_BUFFER( urlLen ) const char *url,
326  IN_PORT_OPT const int defaultPort,
327  IN_ENUM( URL_TYPE ) const URL_TYPE urlTypeHint,
329 
330 /* Network proxy functions in net_proxy.c */
331 
333 int connectViaSocksProxy( INOUT STREAM *stream );
334 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
335 int connectViaHttpProxy( INOUT STREAM *stream, INOUT ERROR_INFO *errorInfo );
336 #if defined( __WIN32__ )
337 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3, 4 ) ) \
338  int findProxyUrl( OUT_BUFFER( proxyMaxLen, *proxyLen ) char *proxy,
339  IN_LENGTH_DNS const int proxyMaxLen,
340  OUT_LENGTH_DNS_Z int *proxyLen,
341  IN_BUFFER( urlLen ) const char *url,
342  IN_LENGTH_DNS const int urlLen );
343 #else
344  #define findProxyUrl( proxy, proxyMaxLen, proxyLen, url, urlLen ) CRYPT_ERROR_NOTFOUND
345 #endif /* Win32 */
346 
347 /* Network access mapping functions */
348 
349 STDC_NONNULL_ARG( ( 1 ) ) \
350 void setAccessMethodTCP( INOUT NET_STREAM_INFO *netStream );
351 STDC_NONNULL_ARG( ( 1 ) ) \
352 void setStreamLayerHTTP( INOUT NET_STREAM_INFO *netStream );
353 STDC_NONNULL_ARG( ( 1 ) ) \
354 void setStreamLayerCMP( INOUT NET_STREAM_INFO *netStream );
355 STDC_NONNULL_ARG( ( 1 ) ) \
356 void setStreamLayerDirect( INOUT NET_STREAM_INFO *netStream );
357 STDC_NONNULL_ARG( ( 1 ) ) \
358 void setStreamLayerBuffering( INOUT NET_STREAM_INFO *netStream,
360 #if 0 /* See comment in net_trans.c */
361 STDC_NONNULL_ARG( ( 1 ) ) \
363 #else
364 #define setAccessMethodTransportSession( netStream )
365 #endif /* 0 */
366 
367 #endif /* _STREAM_INT_DEFINED */