OpenVPN
Macros | Functions
buffer.c File Reference
#include "syshead.h"
#include "common.h"
#include "buffer.h"
#include "error.h"
#include "mtu.h"
#include "misc.h"
#include "memdbg.h"
#include <wchar.h>
Include dependency graph for buffer.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define buf_debug_line(buf)   0
 
#define buf_debug_file(buf)   "[UNDEF]"
 

Functions

size_t array_mult_safe (const size_t m1, const size_t m2, const size_t extra)
 
void buf_size_error (const size_t size)
 
struct buffer alloc_buf (size_t size)
 
struct buffer alloc_buf_gc (size_t size, struct gc_arena *gc)
 
struct buffer clone_buf (const struct buffer *buf)
 
void buf_clear (struct buffer *buf)
 
bool buf_assign (struct buffer *dest, const struct buffer *src)
 
void free_buf (struct buffer *buf)
 
static void free_buf_gc (struct buffer *buf, struct gc_arena *gc)
 
struct buffer buf_sub (struct buffer *buf, int size, bool prepend)
 
bool buf_printf (struct buffer *buf, const char *format,...)
 
bool buf_puts (struct buffer *buf, const char *str)
 
bool openvpn_snprintf (char *str, size_t size, const char *format,...)
 
bool openvpn_swprintf (wchar_t *const str, const size_t size, const wchar_t *const format,...)
 
void buf_catrunc (struct buffer *buf, const char *str)
 
void convert_to_one_line (struct buffer *buf)
 
bool buffer_write_file (const char *filename, const struct buffer *buf)
 Write buffer contents to file. More...
 
void * gc_malloc (size_t size, bool clear, struct gc_arena *a)
 
void x_gc_free (struct gc_arena *a)
 
void x_gc_freespecial (struct gc_arena *a)
 
void gc_addspecial (void *addr, void(free_function)(void *), struct gc_arena *a)
 
void gc_transfer (struct gc_arena *dest, struct gc_arena *src)
 
char * format_hex_ex (const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
 
void buf_rmtail (struct buffer *buf, uint8_t remove)
 
void buf_null_terminate (struct buffer *buf)
 
void buf_chomp (struct buffer *buf)
 
const char * skip_leading_whitespace (const char *str)
 
void string_null_terminate (char *str, int len, int capacity)
 
void chomp (char *str)
 
void rm_trailing_chars (char *str, const char *what_to_delete)
 
char * string_alloc (const char *str, struct gc_arena *gc)
 
void string_clear (char *str)
 
int string_array_len (const char **array)
 
char * print_argv (const char **p, struct gc_arena *gc, const unsigned int flags)
 
struct buffer string_alloc_buf (const char *str, struct gc_arena *gc)
 
bool buf_string_match_head_str (const struct buffer *src, const char *match)
 
bool buf_string_compare_advance (struct buffer *src, const char *match)
 
int buf_substring_len (const struct buffer *buf, int delim)
 
bool buf_parse (struct buffer *buf, const int delim, char *line, const int size)
 
const char * np (const char *str)
 
bool char_class (const unsigned char c, const unsigned int flags)
 
static bool char_inc_exc (const char c, const unsigned int inclusive, const unsigned int exclusive)
 
bool string_class (const char *str, const unsigned int inclusive, const unsigned int exclusive)
 
bool string_mod (char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
 
const char * string_mod_const (const char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace, struct gc_arena *gc)
 
void string_replace_leading (char *str, const char match, const char replace)
 
struct buffer_listbuffer_list_new (const int max_size)
 Allocate an empty buffer list of capacity max_size. More...
 
void buffer_list_free (struct buffer_list *ol)
 Frees a buffer list and all the buffers in it. More...
 
bool buffer_list_defined (const struct buffer_list *ol)
 Checks if the list is valid and non-empty. More...
 
void buffer_list_reset (struct buffer_list *ol)
 Empty the list ol and frees all the contained buffers. More...
 
void buffer_list_push (struct buffer_list *ol, const char *str)
 Allocates and appends a new buffer containing str as data to ol. More...
 
struct buffer_entrybuffer_list_push_data (struct buffer_list *ol, const void *data, size_t size)
 Allocates and appends a new buffer containing data of length size. More...
 
struct bufferbuffer_list_peek (struct buffer_list *ol)
 Retrieve the head buffer. More...
 
void buffer_list_aggregate_separator (struct buffer_list *bl, const size_t max_len, const char *sep)
 Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len . More...
 
void buffer_list_aggregate (struct buffer_list *bl, const size_t max)
 Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len . More...
 
void buffer_list_pop (struct buffer_list *ol)
 
void buffer_list_advance (struct buffer_list *ol, int n)
 
struct buffer_listbuffer_list_file (const char *fn, int max_line_len)
 
struct buffer buffer_read_from_file (const char *filename, struct gc_arena *gc)
 buffer_read_from_file - copy the content of a file into a buffer More...
 

Macro Definition Documentation

◆ buf_debug_file

#define buf_debug_file (   buf)    "[UNDEF]"

Definition at line 159 of file buffer.c.

Referenced by clone_buf(), and string_replace_leading().

◆ buf_debug_line

#define buf_debug_line (   buf)    0

Definition at line 158 of file buffer.c.

Referenced by clone_buf(), and string_replace_leading().

Function Documentation

◆ alloc_buf()

struct buffer alloc_buf ( size_t  size)

◆ alloc_buf_gc()

struct buffer alloc_buf_gc ( size_t  size,
struct gc_arena gc 
)

Definition at line 90 of file buffer.c.

References buf_size_error(), buf_size_valid(), buffer::capacity, buffer::data, gc_malloc(), buffer::len, and buffer::offset.

Referenced by add_option(), add_route_ipv6(), buffer_read_from_file(), ce_management_query_proxy(), ce_management_query_remote(), check_file_access_chroot(), check_incoming_control_channel_dowork(), construct_name_value(), crypto_pem_encode(), delete_route_ipv6(), foreign_option(), fork_dhcp_action(), fork_register_dns_action(), format_common_name(), format_hex_ex(), format_ip_addr_string(), format_route_entry(), frame_print(), get_device_guid(), get_unspecified_device_guid(), get_user_pass_cr(), hostname_randomize(), ifconfig_options_string(), ifconfig_pool_read(), ipset2ascii_all(), link_socket_connection_initiated(), log_entry_print(), management_hold(), management_query_multiline(), management_query_user_pass(), mroute_addr_print_ex(), mroute_helper_regenerate(), msg_flags_string(), multi_instance_string(), netsh_get_id(), ntlm_phase_1(), options_string_version(), options_warning_safe_ml(), packet_id_net_print(), packet_id_persist_print(), platform_gen_path(), plugin_mask_string(), print_argv(), print_default_gateway(), print_in6_addr(), print_in_addr_t(), print_key_id(), print_link_socket_actual_ex(), print_netmask(), print_opt_route(), print_opt_route_gateway(), print_opt_route_gateway_dhcp(), print_opt_topology(), print_sockaddr_ex(), print_str(), print_str_int(), proto2ascii_all(), protocol_dump(), push_peer_info(), read_control_auth(), reliable_ack_print(), reliable_print_ids(), route_quota_exceeded(), route_string(), send_auth_failed(), send_push_reply(), setenv_format_indexed_name(), setenv_route(), setenv_route_addr(), setenv_route_ipv6(), socket_stat(), strerror_win32(), system_error_message(), tap_win_getinfo(), test_buffer_free_gc_one(), test_buffer_free_gc_two(), test_crypto(), test_tls_crypt_v2_setup(), time_string(), tls_crypt_v2_wrap_client_key(), tls_crypt_v2_wrap_unwrap_dst_too_small(), tls_crypt_v2_wrap_unwrap_max_metadata(), tls_crypt_v2_wrap_unwrap_no_metadata(), tls_crypt_v2_write_client_key_file(), translate_mtu_discover_type_name(), tun_stat(), tv_string(), username_password_as_base64(), wide_cmd_line(), win32_version_string(), window_title_generate(), write_key_file(), x509_get_sha1_fingerprint(), and x509_get_sha256_fingerprint().

◆ array_mult_safe()

size_t array_mult_safe ( const size_t  m1,
const size_t  m2,
const size_t  extra 
)

Definition at line 43 of file buffer.c.

References M_FATAL, msg, and unlikely.

◆ buf_assign()

bool buf_assign ( struct buffer dest,
const struct buffer src 
)

Definition at line 175 of file buffer.c.

References BLEN, BPTR, buf_init, buf_write(), and buffer::offset.

Referenced by buffer_turnover().

◆ buf_catrunc()

void buf_catrunc ( struct buffer buf,
const char *  str 
)

◆ buf_chomp()

void buf_chomp ( struct buffer buf)

Definition at line 593 of file buffer.c.

References BLAST, buf_inc_len(), buf_null_terminate(), CC_CRLF, CC_NULL, and char_class().

Referenced by command_line_get(), and secure_memzero().

◆ buf_clear()

void buf_clear ( struct buffer buf)

◆ buf_null_terminate()

void buf_null_terminate ( struct buffer buf)

◆ buf_parse()

bool buf_parse ( struct buffer buf,
const int  delim,
char *  line,
const int  size 
)

◆ buf_printf()

bool buf_printf ( struct buffer buf,
const char *  format,
  ... 
)

Definition at line 245 of file buffer.c.

References BEND, buf_defined(), buf_forward_capacity(), buffer::capacity, buffer::data, buffer::len, and vsnprintf.

Referenced by add_option(), add_route_ipv6(), ce_management_query_proxy(), ce_management_query_remote(), check_file_access_chroot(), check_inline_file(), construct_name_value(), delete_route_ipv6(), foreign_option(), fork_dhcp_action(), fork_register_dns_action(), format_common_name(), format_hex_ex(), format_ip_addr_string(), format_route_entry(), frame_print(), get_device_guid(), get_unspecified_device_guid(), get_user_pass_cr(), hostname_randomize(), ifconfig_options_string(), ipset2ascii_all(), link_socket_connection_initiated(), log_entry_print(), management_hold(), management_query_multiline(), management_query_user_pass(), mroute_addr_print_ex(), mroute_helper_regenerate(), msg_flags_string(), multi_instance_string(), ntlm_phase_1(), options_string(), options_warning_safe_ml(), packet_id_net_print(), packet_id_persist_print(), platform_gen_path(), plugin_mask_string(), print_argv(), print_default_gateway(), print_in6_addr(), print_in_addr_t(), print_key_id(), print_link_socket_actual_ex(), print_netmask(), print_opt_route(), print_opt_route_gateway(), print_opt_route_gateway_dhcp(), print_opt_topology(), print_sockaddr_ex(), print_str(), print_str_int(), proto2ascii_all(), protocol_dump(), push_peer_info(), read_inline_file(), reliable_ack_print(), reliable_print_ids(), route_string(), secure_memzero(), send_auth_failed(), send_push_options(), send_push_reply(), setenv_format_indexed_name(), setenv_route(), setenv_route_addr(), setenv_route_ipv6(), setenv_str_safe(), socket_stat(), strerror_win32(), system_error_message(), time_string(), translate_mtu_discover_type_name(), tun_stat(), tv_string(), username_password_as_base64(), wide_cmd_line(), win32_version_string(), window_title_generate(), and write_key_file().

◆ buf_puts()

bool buf_puts ( struct buffer buf,
const char *  str 
)

◆ buf_rmtail()

void buf_rmtail ( struct buffer buf,
uint8_t  remove 
)

Definition at line 557 of file buffer.c.

References BLAST, and buffer::len.

Referenced by secure_memzero(), time_string(), and translate_mtu_discover_type_name().

◆ buf_size_error()

void buf_size_error ( const size_t  size)

Definition at line 55 of file buffer.c.

References M_FATAL, and msg.

Referenced by alloc_buf(), alloc_buf_gc(), buf_set_read(), and buf_set_write().

◆ buf_string_compare_advance()

bool buf_string_compare_advance ( struct buffer src,
const char *  match 
)

◆ buf_string_match_head_str()

bool buf_string_match_head_str ( const struct buffer src,
const char *  match 
)

◆ buf_sub()

struct buffer buf_sub ( struct buffer buf,
int  size,
bool  prepend 
)

◆ buf_substring_len()

int buf_substring_len ( const struct buffer buf,
int  delim 
)

Definition at line 843 of file buffer.c.

References buf_read_u8().

Referenced by buf_string_match_head(), and command_line_get().

◆ buffer_list_advance()

void buffer_list_advance ( struct buffer_list ol,
int  n 
)

Definition at line 1365 of file buffer.c.

References ASSERT, BLEN, buffer_entry::buf, buf_advance(), buffer_list_pop(), and buffer_list::head.

Referenced by init_static(), and man_write().

◆ buffer_list_aggregate()

void buffer_list_aggregate ( struct buffer_list bl,
const size_t  max 
)

Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .

All the aggregated buffers are removed from the list and replaced by the new one, followed by any additional (non-aggregated) data.

Parameters
blthe list of buffer to aggregate
maxthe maximum length of the aggregated buffer

Definition at line 1342 of file buffer.c.

References buffer_list_aggregate_separator().

Referenced by init_static(), man_write(), and management_query_multiline_flatten().

◆ buffer_list_aggregate_separator()

void buffer_list_aggregate_separator ( struct buffer_list bl,
const size_t  max_len,
const char *  sep 
)

Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .

sep is written after each copied buffer (also after the last one). All the aggregated buffers are removed from the list and replaced by the new one, followed by any additional (non-aggregated) data. Nothing happens if max_len is not enough to aggregate at least 2 buffers.

Parameters
blthe list of buffer to aggregate
max_lenthe maximum length of the aggregated buffer
septhe separator to put between buffers during aggregation

Definition at line 1296 of file buffer.c.

References alloc_buf(), ALLOC_OBJ_CLEAR, BLEN, buffer_entry::buf, buf_copy(), buf_write(), http-client::f, free, free_buf(), buffer_list::head, buffer_entry::next, buffer_list::size, and buffer_list::tail.

Referenced by buffer_list_aggregate(), management_query_multiline_flatten_newline(), test_buffer_list_aggregate_separator_all(), test_buffer_list_aggregate_separator_empty(), test_buffer_list_aggregate_separator_emptybuffers(), test_buffer_list_aggregate_separator_noop(), test_buffer_list_aggregate_separator_nosep(), test_buffer_list_aggregate_separator_two(), and test_buffer_list_aggregate_separator_zerolen().

◆ buffer_list_defined()

bool buffer_list_defined ( const struct buffer_list ol)

Checks if the list is valid and non-empty.

Parameters
olthe list to check
Returns
true iff ol is not NULL and contains at least one buffer

Definition at line 1221 of file buffer.c.

References buffer_list::head.

Referenced by man_update_io_state(), management_query_multiline_flatten(), and management_query_multiline_flatten_newline().

◆ buffer_list_file()

struct buffer_list* buffer_list_file ( const char *  fn,
int  max_line_len 
)

Definition at line 1379 of file buffer.c.

References buffer_list_new(), buffer_list_push(), free, malloc, and platform_fopen().

◆ buffer_list_free()

void buffer_list_free ( struct buffer_list ol)

◆ buffer_list_new()

struct buffer_list* buffer_list_new ( const int  max_size)

Allocate an empty buffer list of capacity max_size.

Parameters
max_sizethe capacity of the list to allocate
Returns
the new list

Definition at line 1201 of file buffer.c.

References ALLOC_OBJ_CLEAR, buffer_list::max_size, and buffer_list::size.

Referenced by buffer_list_file(), in_extra_reset(), init_static(), man_connection_init(), test_buffer_list_setup(), and tls_send_payload().

◆ buffer_list_peek()

struct buffer* buffer_list_peek ( struct buffer_list ol)

◆ buffer_list_pop()

void buffer_list_pop ( struct buffer_list ol)

◆ buffer_list_push()

void buffer_list_push ( struct buffer_list ol,
const char *  str 
)

Allocates and appends a new buffer containing str as data to ol.

Parameters
olthe list to append the new buffer to
strthe string to copy into the new buffer

Definition at line 1242 of file buffer.c.

References buffer_entry::buf, buffer_list_push_data(), and buffer::len.

Referenced by buffer_list_file(), init_static(), man_output_list_push_str(), man_read(), test_buffer_list_full(), and test_buffer_list_setup().

◆ buffer_list_push_data()

struct buffer_entry* buffer_list_push_data ( struct buffer_list ol,
const void *  data,
size_t  size 
)

Allocates and appends a new buffer containing data of length size.

Parameters
olthe list to append the new buffer to
datathe data to copy into the new buffer
sizethe length of data to copy into the buffer
Returns
the new buffer

Definition at line 1256 of file buffer.c.

References alloc_buf(), ALLOC_OBJ_CLEAR, ASSERT, buffer_entry::buf, buffer::data, buffer_list::head, buffer::len, buffer_list::max_size, buffer_entry::next, buffer_list::size, and buffer_list::tail.

Referenced by buffer_list_push(), test_buffer_list_setup(), and tls_send_payload().

◆ buffer_list_reset()

void buffer_list_reset ( struct buffer_list ol)

Empty the list ol and frees all the contained buffers.

Parameters
olthe list to reset

Definition at line 1227 of file buffer.c.

References buffer_entry::buf, free, free_buf(), buffer_list::head, buffer_entry::next, buffer_list::size, and buffer_list::tail.

Referenced by buffer_list_free(), man_new_connection_post(), man_read(), and man_reset_client_socket().

◆ buffer_read_from_file()

struct buffer buffer_read_from_file ( const char *  filename,
struct gc_arena gc 
)

buffer_read_from_file - copy the content of a file into a buffer

Parameters
filepath to the file to read
gcthe garbage collector to use when allocating the buffer. It is passed to alloc_buf_gc() and therefore can be NULL.
Returns
the buffer storing the file content or an invalid buffer in case of error

Definition at line 1402 of file buffer.c.

References alloc_buf_gc(), ASSERT, BPTR, buf_inc_len(), buf_null_terminate(), free_buf_gc(), platform_fopen(), platform_stat(), and ssize_t.

Referenced by options_postprocess_mutate_ce(), read_key_file(), and tls_crypt_v2_read_keyfile().

◆ buffer_write_file()

bool buffer_write_file ( const char *  filename,
const struct buffer buf 
)

Write buffer contents to file.

Parameters
filenameThe filename to write the buffer to.
bufThe buffer to write to the file.
Returns
true on success, false otherwise.

Definition at line 372 of file buffer.c.

References BLEN, BPTR, M_ERRNO, msg, platform_open(), S_IRUSR, S_IWUSR, and write.

Referenced by secure_memzero(), tls_crypt_v2_verify_metadata(), tls_crypt_v2_write_client_key_file(), tls_crypt_v2_write_server_key_file(), and write_key_file().

◆ char_class()

bool char_class ( const unsigned char  c,
const unsigned int  flags 
)

◆ char_inc_exc()

static bool char_inc_exc ( const char  c,
const unsigned int  inclusive,
const unsigned int  exclusive 
)
inlinestatic

Definition at line 1056 of file buffer.c.

References char_class().

Referenced by string_class(), and string_mod().

◆ chomp()

void chomp ( char *  str)

◆ clone_buf()

struct buffer clone_buf ( const struct buffer buf)

◆ convert_to_one_line()

void convert_to_one_line ( struct buffer buf)

Definition at line 357 of file buffer.c.

References BLEN, BPTR, and buffer::len.

Referenced by secure_memzero().

◆ format_hex_ex()

char* format_hex_ex ( const uint8_t data,
int  size,
int  maxoutput,
unsigned int  space_break_flags,
const char *  separator,
struct gc_arena gc 
)

◆ free_buf()

void free_buf ( struct buffer buf)

◆ free_buf_gc()

static void free_buf_gc ( struct buffer buf,
struct gc_arena gc 
)
static

◆ gc_addspecial()

void gc_addspecial ( void *  addr,
void(free_function)(void *)  ,
struct gc_arena a 
)

◆ gc_malloc()

void* gc_malloc ( size_t  size,
bool  clear,
struct gc_arena a 
)

◆ gc_transfer()

void gc_transfer ( struct gc_arena dest,
struct gc_arena src 
)

Definition at line 499 of file buffer.c.

References gc_arena::list, and gc_entry::next.

Referenced by add_option().

◆ np()

const char* np ( const char *  str)

Definition at line 900 of file buffer.c.

Referenced by buf_string_match_head().

◆ openvpn_snprintf()

bool openvpn_snprintf ( char *  str,
size_t  size,
const char *  format,
  ... 
)

◆ openvpn_swprintf()

bool openvpn_swprintf ( wchar_t *const  str,
const size_t  size,
const wchar_t *const  format,
  ... 
)

◆ print_argv()

char* print_argv ( const char **  p,
struct gc_arena gc,
const unsigned int  flags 
)

Definition at line 757 of file buffer.c.

References alloc_buf_gc(), BSTR, buf_printf(), and PA_BRACKET.

Referenced by argv_str(), plugin_open_item(), plugin_option_list_print(), and push_options().

◆ rm_trailing_chars()

void rm_trailing_chars ( char *  str,
const char *  what_to_delete 
)

Definition at line 662 of file buffer.c.

References buffer::len.

Referenced by chomp(), and secure_memzero().

◆ skip_leading_whitespace()

const char* skip_leading_whitespace ( const char *  str)

Definition at line 618 of file buffer.c.

Referenced by make_inline_array(), and secure_memzero().

◆ string_alloc()

char* string_alloc ( const char *  str,
struct gc_arena gc 
)

◆ string_alloc_buf()

struct buffer string_alloc_buf ( const char *  str,
struct gc_arena gc 
)

Definition at line 792 of file buffer.c.

References ASSERT, buf_set_read(), buffer::len, and string_alloc().

Referenced by options_warning_extract_parm1().

◆ string_array_len()

int string_array_len ( const char **  array)

◆ string_class()

bool string_class ( const char *  str,
const unsigned int  inclusive,
const unsigned int  exclusive 
)

Definition at line 1062 of file buffer.c.

References ASSERT, and char_inc_exc().

Referenced by dns_addr_safe(), openvpn_inet_aton(), push_option_ex(), and wide_cmd_line().

◆ string_clear()

void string_clear ( char *  str)

◆ string_mod()

bool string_mod ( char *  str,
const unsigned int  inclusive,
const unsigned int  exclusive,
const char  replace 
)

◆ string_mod_const()

const char* string_mod_const ( const char *  str,
const unsigned int  inclusive,
const unsigned int  exclusive,
const char  replace,
struct gc_arena gc 
)

Definition at line 1113 of file buffer.c.

References string_alloc(), and string_mod().

Referenced by platform_gen_path(), safe_print(), and setenv_str_ex().

◆ string_null_terminate()

void string_null_terminate ( char *  str,
int  len,
int  capacity 
)

Definition at line 636 of file buffer.c.

References ASSERT, and buffer::len.

Referenced by get_console_input_win32(), and secure_memzero().

◆ string_replace_leading()

void string_replace_leading ( char *  str,
const char  match,
const char  replace 
)

◆ x_gc_free()

void x_gc_free ( struct gc_arena a)

Definition at line 442 of file buffer.c.

References free, gc_arena::list, and gc_entry::next.

Referenced by gc_free().

◆ x_gc_freespecial()

void x_gc_freespecial ( struct gc_arena a)