Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 // Written in NSPR style to also be suitable for adding to the NSS demo suite
      5 
      6 #ifndef __MEMIO_H
      7 #define __MEMIO_H
      8 #pragma once
      9 
     10 #include <stddef.h>
     11 
     12 #ifdef __cplusplus
     13 extern "C" {
     14 #endif
     15 
     16 #include "prio.h"
     17 
     18 /* Opaque structure.  Really just a more typesafe alias for PRFilePrivate. */
     19 struct memio_Private;
     20 typedef struct memio_Private memio_Private;
     21 
     22 /*----------------------------------------------------------------------
     23  NSPR I/O layer that terminates in a pair of circular buffers
     24  rather than talking to the real network.
     25  To use this with NSS:
     26  1) call memio_CreateIOLayer to create a fake NSPR socket
     27  2) call SSL_ImportFD to ssl-ify the socket
     28  3) Do your own networking calls to set up a TCP connection
     29  4) call memio_SetPeerName to tell NSS about the other end of the connection
     30  5) While at the same time doing plaintext nonblocking NSPR I/O as
     31     usual to the nspr file descriptor returned by SSL_ImportFD,
     32     your app must shuttle encrypted data between
     33     the real network and memio's network buffers.
     34     memio_GetReadParams/memio_PutReadResult
     35     are the hooks you need to pump data into memio's input buffer,
     36     and memio_GetWriteParams/memio_PutWriteResult
     37     are the hooks you need to pump data out of memio's output buffer.
     38 ----------------------------------------------------------------------*/
     39 
     40 /* Create the I/O layer and its two circular buffers. */
     41 PRFileDesc *memio_CreateIOLayer(int bufsize);
     42 
     43 /* Must call before trying to make an ssl connection */
     44 void memio_SetPeerName(PRFileDesc *fd, const PRNetAddr *peername);
     45 
     46 /* Return a private pointer needed by the following
     47  * four functions.  (We could have passed a PRFileDesc to
     48  * them, but that would be slower.  Better for the caller
     49  * to grab the pointer once and cache it.
     50  * This may be a premature optimization.)
     51  */
     52 memio_Private *memio_GetSecret(PRFileDesc *fd);
     53 
     54 /* Ask memio where to put bytes from the network, and how many it can handle.
     55  * Returns bytes available to write, or 0 if none available.
     56  * Puts current buffer position into *buf.
     57  */
     58 int memio_GetReadParams(memio_Private *secret, char **buf);
     59 
     60 /* Tell memio how many bytes were read from the network.
     61  * If bytes_read is 0, causes EOF to be reported to
     62  * NSS after it reads the last byte from the circular buffer.
     63  * If bytes_read is < 0, it is treated as an NSPR error code.
     64  * See nspr/pr/src/md/unix/unix_errors.c for how to
     65  * map from Unix errors to NSPR error codes.
     66  * On EWOULDBLOCK or the equivalent, don't call this function.
     67  */
     68 void memio_PutReadResult(memio_Private *secret, int bytes_read);
     69 
     70 /* Ask memio what data it has to send to the network.
     71  * Returns up to two buffers of data by writing the positions and lengths into
     72  * |buf1|, |len1| and |buf2|, |len2|.
     73  */
     74 void memio_GetWriteParams(memio_Private *secret,
     75                           const char **buf1, unsigned int *len1,
     76                           const char **buf2, unsigned int *len2);
     77 
     78 /* Tell memio how many bytes were sent to the network.
     79  * If bytes_written is < 0, it is treated as an NSPR error code.
     80  * See nspr/pr/src/md/unix/unix_errors.c for how to
     81  * map from Unix errors to NSPR error codes.
     82  * On EWOULDBLOCK or the equivalent, don't call this function.
     83  */
     84 void memio_PutWriteResult(memio_Private *secret, int bytes_written);
     85 
     86 
     87 #ifdef __cplusplus
     88 }
     89 #endif
     90 
     91 #endif
     92