static int ctx_buffer_append( ctx_buffer *buf, const unsigned char * data, size_t len ) { int ret; if( len > (size_t) INT_MAX ) return( -1 ); if( len > sizeof( buf->data ) ) { mbedtls_printf( " ! buffer size %u too large (max %u)\n", (unsigned) len, (unsigned) sizeof( buf->data ) ); return( -1 ); } if( sizeof( buf->data ) - buf->len < len ) { if( ( ret = ctx_buffer_flush( buf ) ) <= 0 ) return( ret ); } memcpy( buf->data + buf->len, data, len ); buf->len += len; if( ++buf->num_datagrams == 1 ) buf->packet_lifetime = ellapsed_time(); return( (int) len ); }
static int ctx_buffer_flush( ctx_buffer *buf ) { int ret; mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n", ellapsed_time(), buf->description, (unsigned) buf->len, buf->num_datagrams, ellapsed_time() - buf->packet_lifetime ); ret = mbedtls_net_send( buf->ctx, buf->data, buf->len ); buf->len = 0; buf->num_datagrams = 0; return( ret ); }
/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ void print_packet( const packet *p, const char *why ) { if( why == NULL ) mbedtls_printf( " %05lu %s %s (%u bytes)\n", ellapsed_time(), p->way, p->type, p->len ); else mbedtls_printf( " %s %s (%u bytes): %s\n", p->way, p->type, p->len, why ); fflush( stdout ); }
/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ void print_packet( const packet *p, const char *why ) { #if defined(MBEDTLS_TIMING_C) if( why == NULL ) mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n", ellapsed_time(), p->way, p->type, p->len ); else mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n", ellapsed_time(), p->way, p->type, p->len, why ); #else if( why == NULL ) mbedtls_printf( " dispatch %s %s (%u bytes)\n", p->way, p->type, p->len ); else mbedtls_printf( " dispatch %s %s (%u bytes): %s\n", p->way, p->type, p->len, why ); #endif fflush( stdout ); }
static unsigned ctx_buffer_time_remaining( ctx_buffer *buf ) { unsigned const cur_time = ellapsed_time(); if( buf->num_datagrams == 0 ) return( (unsigned) -1 ); if( cur_time - buf->packet_lifetime >= opt.pack ) return( 0 ); return( opt.pack - ( cur_time - buf->packet_lifetime ) ); }