Tor  0.4.7.0-alpha-dev
Data Structures | Macros | Functions | Variables
nss_countbytes.c File Reference

A PRFileDesc layer to let us count the number of bytes bytes actually written on a PRFileDesc. More...

#include "orconfig.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/tls/nss_countbytes.h"
#include <stdlib.h>
#include <string.h>
#include <prio.h>

Go to the source code of this file.

Data Structures

struct  tor_nss_bytecounts_t
 

Macros

#define INC_READ(fd, n)
 
#define INC_WRITTEN(fd, n)
 

Functions

static PRStatus countbytes_close_fn (PRFileDesc *fd)
 
static PRInt32 countbytes_read_fn (PRFileDesc *fd, void *buf, PRInt32 amount)
 
static PRInt32 countbytes_write_fn (PRFileDesc *fd, const void *buf, PRInt32 amount)
 
static PRInt32 countbytes_writev_fn (PRFileDesc *fd, const PRIOVec *iov, PRInt32 size, PRIntervalTime timeout)
 
static PRInt32 countbytes_send_fn (PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout)
 
static PRInt32 countbytes_recv_fn (PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout)
 
void tor_nss_countbytes_init (void)
 
static tor_nss_bytecounts_tget_counts (PRFileDesc *fd)
 
PRFileDesc * tor_wrap_prfiledesc_with_byte_counter (PRFileDesc *stack)
 
int tor_get_prfiledesc_byte_counts (PRFileDesc *fd, uint64_t *n_read_out, uint64_t *n_written_out)
 

Variables

static bool countbytes_initialized = false
 
static PRDescIdentity countbytes_layer_id = PR_INVALID_IO_LAYER
 
static PRIOMethods countbytes_methods
 
static PRStatus(* default_close_fn )(PRFileDesc *fd)
 

Detailed Description

A PRFileDesc layer to let us count the number of bytes bytes actually written on a PRFileDesc.

Definition in file nss_countbytes.c.

Macro Definition Documentation

◆ INC_READ

#define INC_READ (   fd,
 
)
Value:
get_counts(fd)->n_read += (n); \
STMT_END
static tor_nss_bytecounts_t * get_counts(PRFileDesc *fd)

Helper: increment the read-count of an fd by n.

Definition at line 92 of file nss_countbytes.c.

◆ INC_WRITTEN

#define INC_WRITTEN (   fd,
 
)
Value:
get_counts(fd)->n_written += (n); \
STMT_END

Helper: increment the write-count of an fd by n.

Definition at line 97 of file nss_countbytes.c.

Function Documentation

◆ countbytes_close_fn()

static PRStatus countbytes_close_fn ( PRFileDesc *  fd)
static

Implementation for PR_Close: frees the 'secret' field, then passes control to the default close function

Definition at line 104 of file nss_countbytes.c.

Referenced by tor_nss_countbytes_init().

◆ countbytes_read_fn()

static PRInt32 countbytes_read_fn ( PRFileDesc *  fd,
void *  buf,
PRInt32  amount 
)
static

Implementation for PR_Read: Calls the lower-level read function, and records what it said.

Definition at line 118 of file nss_countbytes.c.

Referenced by tor_nss_countbytes_init().

◆ countbytes_recv_fn()

static PRInt32 countbytes_recv_fn ( PRFileDesc *  fd,
void *  buf,
PRInt32  amount,
PRIntn  flags,
PRIntervalTime  timeout 
)
static

Implementation for PR_Recv: Calls the lower-level recv function, and records what it said.

Definition at line 173 of file nss_countbytes.c.

Referenced by tor_nss_countbytes_init().

◆ countbytes_send_fn()

static PRInt32 countbytes_send_fn ( PRFileDesc *  fd,
const void *  buf,
PRInt32  amount,
PRIntn  flags,
PRIntervalTime  timeout 
)
static

Implementation for PR_Send: Calls the lower-level send function, and records what it said.

Definition at line 158 of file nss_countbytes.c.

Referenced by tor_nss_countbytes_init().

◆ countbytes_write_fn()

static PRInt32 countbytes_write_fn ( PRFileDesc *  fd,
const void *  buf,
PRInt32  amount 
)
static

Implementation for PR_Write: Calls the lower-level write function, and records what it said.

Definition at line 131 of file nss_countbytes.c.

Referenced by tor_nss_countbytes_init().

◆ countbytes_writev_fn()

static PRInt32 countbytes_writev_fn ( PRFileDesc *  fd,
const PRIOVec *  iov,
PRInt32  size,
PRIntervalTime  timeout 
)
static

Implementation for PR_Writev: Calls the lower-level writev function, and records what it said.

Definition at line 144 of file nss_countbytes.c.

Referenced by tor_nss_countbytes_init().

◆ get_counts()

static tor_nss_bytecounts_t* get_counts ( PRFileDesc *  fd)
static

Return the tor_nss_bytecounts_t object for a given IO layer. Asserts that the IO layer is in fact a layer created by this module.

Definition at line 85 of file nss_countbytes.c.

◆ tor_get_prfiledesc_byte_counts()

int tor_get_prfiledesc_byte_counts ( PRFileDesc *  fd,
uint64_t *  n_read_out,
uint64_t *  n_written_out 
)

Given a PRFileDesc returned by tor_wrap_prfiledesc_with_byte_counter(), or another PRFileDesc wrapping that PRFileDesc, set the provided pointers to the number of bytes read and written on the descriptor since it was created.

Return 0 on success, -1 on failure.

Definition at line 225 of file nss_countbytes.c.

Referenced by tor_tls_get_n_raw_bytes().

◆ tor_nss_countbytes_init()

void tor_nss_countbytes_init ( void  )

Initialize this module, if it is not already initialized.

Definition at line 57 of file nss_countbytes.c.

Referenced by tor_tls_init().

◆ tor_wrap_prfiledesc_with_byte_counter()

PRFileDesc* tor_wrap_prfiledesc_with_byte_counter ( PRFileDesc *  stack)

Wrap a PRFileDesc from NSPR with a new PRFileDesc that will count the total number of bytes read and written. Return the new PRFileDesc.

This function takes ownership of its input.

Definition at line 193 of file nss_countbytes.c.

Referenced by tor_tls_new().

Variable Documentation

◆ countbytes_initialized

bool countbytes_initialized = false
static

Boolean: have we initialized this module

Definition at line 22 of file nss_countbytes.c.

Referenced by tor_nss_countbytes_init().

◆ countbytes_layer_id

PRDescIdentity countbytes_layer_id = PR_INVALID_IO_LAYER
static

Integer to identity this layer.

Definition at line 25 of file nss_countbytes.c.

Referenced by get_counts(), and tor_nss_countbytes_init().

◆ countbytes_methods

PRIOMethods countbytes_methods
static

Table of methods for this layer.

Definition at line 28 of file nss_countbytes.c.

Referenced by tor_nss_countbytes_init().

◆ default_close_fn

PRStatus(* default_close_fn) (PRFileDesc *fd) ( PRFileDesc *  fd)
static

Default close function provided by NSPR. We use this to help implement our own close function.

Definition at line 32 of file nss_countbytes.c.

Referenced by countbytes_close_fn(), and tor_nss_countbytes_init().