OpenVPN
Functions
argv.c File Reference
#include "syshead.h"
#include "argv.h"
#include "integer.h"
#include "env_set.h"
#include "options.h"
Include dependency graph for argv.c:

Go to the source code of this file.

Functions

static void argv_extend (struct argv *a, const size_t newcap)
 Resizes the list of arguments struct argv can carry. More...
 
static void argv_init (struct argv *a)
 Initialise an already allocated struct argv. More...
 
struct argv argv_new (void)
 Allocates a new struct argv and ensures it is initialised. More...
 
void argv_free (struct argv *a)
 Frees all memory allocations allocated by the struct argv related functions. More...
 
static void argv_reset (struct argv *a)
 Resets the struct argv to an initial state. More...
 
static void argv_grow (struct argv *a, const size_t add)
 Extends an existing struct argv to carry minimum 'add' number of new arguments. More...
 
static void argv_append (struct argv *a, char *str)
 Appends a string to to the list of arguments stored in a struct argv This will ensure the list size in struct argv has the needed capacity to store the value. More...
 
static struct argv argv_clone (const struct argv *source, const size_t headroom)
 Clones a struct argv with all the contents to a new allocated struct argv. More...
 
struct argv argv_insert_head (const struct argv *a, const char *head)
 Inserts an argument string in front of all other argument slots. More...
 
const char * argv_str (const struct argv *a, struct gc_arena *gc, const unsigned int flags)
 Generate a single string with all the arguments in a struct argv concatenated. More...
 
void argv_msg (const int msglev, const struct argv *a)
 Write the arguments stored in a struct argv via the msg() command. More...
 
void argv_msg_prefix (const int msglev, const struct argv *a, const char *prefix)
 Similar to argv_msg() but prefixes the messages being written with a given string. More...
 
static char * argv_prep_format (const char *format, const char delim, size_t *count, struct gc_arena *gc)
 Prepares argv format string for further processing. More...
 
static bool argv_printf_arglist (struct argv *argres, const char *format, va_list arglist)
 Create a struct argv based on a format string. More...
 
bool argv_printf (struct argv *argres, const char *format,...)
 printf() variant which populates a struct argv. More...
 
bool argv_printf_cat (struct argv *argres, const char *format,...)
 printf() inspired argv concatenation. More...
 
void argv_parse_cmd (struct argv *argres, const char *cmdstr)
 Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot. More...
 

Function Documentation

◆ argv_append()

static void argv_append ( struct argv a,
char *  str 
)
static

Appends a string to to the list of arguments stored in a struct argv This will ensure the list size in struct argv has the needed capacity to store the value.

Parameters
*astruct argv where to append the new string value
<em>strPointer to string to append. The provided string *MUST have been malloc()ed or NULL.

Definition at line 158 of file argv.c.

References argv::argc, argv::argv, and argv_grow().

Referenced by argv_clone(), argv_parse_cmd(), and argv_printf_arglist().

◆ argv_clone()

static struct argv argv_clone ( const struct argv source,
const size_t  headroom 
)
static

Clones a struct argv with all the contents to a new allocated struct argv.

If 'headroom' is larger than 0, it will create a head-room in front of the values being copied from the source input.

Parameters
*sourceValid pointer to the source struct argv to clone. It may be NULL.
headroomNumber of slots to leave empty in front of the slots copied from the source.
Returns
Returns a new struct argv containing a copy of the source struct argv, with the given headroom in front of the copy.

Definition at line 180 of file argv.c.

References argv::argc, argv::argv, argv_append(), argv_init(), argv::gc, and string_alloc().

Referenced by argv_insert_head().

◆ argv_extend()

static void argv_extend ( struct argv a,
const size_t  newcap 
)
static

Resizes the list of arguments struct argv can carry.

This resize operation will only increase the size, never decrease the size.

Parameters
*aValid pointer to a struct argv to resize
newcapsize_t with the new size of the argument list.

Definition at line 49 of file argv.c.

References ALLOC_ARRAY_CLEAR_GC, argv::argc, argv::argv, argv::capacity, and argv::gc.

Referenced by argv_grow(), and argv_init().

◆ argv_free()

void argv_free ( struct argv a)

Frees all memory allocations allocated by the struct argv related functions.

Parameters
*aValid pointer to a struct argv to release memory from

Definition at line 102 of file argv.c.

References argv::gc, and gc_free().

Referenced by add_route(), add_route_ipv6(), argv_insert_head__empty_argv__head_only(), argv_insert_head__non_empty_argv__head_added(), argv_parse_cmd__command_and_extra_options__argc_correct(), argv_parse_cmd__command_string__argc_correct(), argv_printf__combined_path_with_spaces__argc_correct(), argv_printf__embedded_format_directive__replaced_in_output(), argv_printf__empty_parameter__argc_correct(), argv_printf__group_sep_in_arg__fail_no_ouput(), argv_printf__long_args__data_correct(), argv_printf__multiple_spaces_in_format__parsed_as_one(), argv_printf_cat__multiple_spaces_in_format__parsed_as_one(), argv_printf_cat__used_twice__argc_correct(), argv_str__empty_argv__empty_output(), argv_str__multiple_argv__correct_output(), check_cmd_access(), cleanup(), delete_route(), delete_route_ipv6(), do_dns_domain_wmic(), do_ifconfig_ipv4(), do_ifconfig_ipv6(), do_route(), init(), ipconfig_register_dns(), learn_address_script(), link_socket_connection_initiated(), multi_client_connect_call_plugin_v1(), multi_client_connect_call_script(), multi_client_disconnect_script(), netsh_delete_address_dns(), netsh_enable_dhcp(), netsh_ifconfig(), netsh_ifconfig_options(), netsh_set_dns6_servers(), plugin_call_item(), run_up_down(), set_lladdr(), tls_crypt_v2_verify_metadata(), undo_ifconfig_ipv4(), undo_ifconfig_ipv6(), verify_cert_call_command(), verify_cert_call_plugin(), verify_crresponse_script(), and verify_user_pass_script().

◆ argv_grow()

static void argv_grow ( struct argv a,
const size_t  add 
)
static

Extends an existing struct argv to carry minimum 'add' number of new arguments.

This builds on argv_extend(), which ensures the new size will only be higher than the current capacity.

The new size is also calculated based on the result of adjust_power_of_2(). This approach ensures that the list does grow bulks and only when the current limit is reached.

Parameters
*aValid pointer to the struct argv to extend
addsize_t with the number of elements to add.

Definition at line 141 of file argv.c.

References adjust_power_of_2(), argv::argc, argv_extend(), and ASSERT.

Referenced by argv_append().

◆ argv_init()

static void argv_init ( struct argv a)
static

Initialise an already allocated struct argv.

It is expected that the input argument is a valid pointer.

Parameters
*aPointer to a struct argv to initialise

Definition at line 72 of file argv.c.

References argv::argc, argv::argv, argv_extend(), argv::capacity, argv::gc, and gc_new().

Referenced by argv_clone(), and argv_new().

◆ argv_insert_head()

struct argv argv_insert_head ( const struct argv a,
const char *  head 
)

Inserts an argument string in front of all other argument slots.

Parameters
*aValid pointer to the struct argv to insert the argument into
*headPointer to the char * string with the argument to insert
Returns
Returns a new struct argv with the inserted argument in front

Definition at line 208 of file argv.c.

References argv_clone(), and string_alloc().

Referenced by argv_insert_head__empty_argv__head_only(), argv_insert_head__non_empty_argv__head_added(), and plugin_call_item().

◆ argv_msg()

void argv_msg ( const int  msglev,
const struct argv a 
)

Write the arguments stored in a struct argv via the msg() command.

Parameters
msglevInteger with the message level used by msg().
*aValid pointer to the struct argv with the arguments to write.

Definition at line 243 of file argv.c.

References argv_str(), gc_free(), gc_new(), and msg.

Referenced by add_route(), add_route_ipv6(), delete_route(), delete_route_ipv6(), do_ifconfig_ipv4(), do_ifconfig_ipv6(), ipconfig_register_dns(), run_up_down(), set_lladdr(), undo_ifconfig_ipv4(), and undo_ifconfig_ipv6().

◆ argv_msg_prefix()

void argv_msg_prefix ( const int  msglev,
const struct argv a,
const char *  prefix 
)

Similar to argv_msg() but prefixes the messages being written with a given string.

Parameters
msglevInteger with the message level used by msg().
*aValid pointer to the struct argv with the arguments to write
*prefixValid char * pointer to the prefix string

Definition at line 260 of file argv.c.

References argv_str(), gc_free(), gc_new(), and msg.

Referenced by exec_command(), tls_crypt_v2_verify_metadata(), and verify_cert_call_command().

◆ argv_new()

struct argv argv_new ( void  )

Allocates a new struct argv and ensures it is initialised.

Note that it does not return a pointer, but a struct argv directly.

Returns
Returns an initialised and empty struct argv.

Definition at line 88 of file argv.c.

References argv_init().

Referenced by add_route(), add_route_ipv6(), argv_insert_head__empty_argv__head_only(), argv_insert_head__non_empty_argv__head_added(), argv_parse_cmd__command_and_extra_options__argc_correct(), argv_parse_cmd__command_string__argc_correct(), argv_printf__combined_path_with_spaces__argc_correct(), argv_printf__embedded_format_directive__replaced_in_output(), argv_printf__empty_parameter__argc_correct(), argv_printf__group_sep_in_arg__fail_no_ouput(), argv_printf__long_args__data_correct(), argv_printf__multiple_spaces_in_format__parsed_as_one(), argv_printf_cat__multiple_spaces_in_format__parsed_as_one(), argv_printf_cat__used_twice__argc_correct(), argv_str__empty_argv__empty_output(), argv_str__multiple_argv__correct_output(), check_cmd_access(), cleanup(), delete_route(), delete_route_ipv6(), do_dns_domain_wmic(), do_ifconfig_ipv4(), do_ifconfig_ipv6(), do_route(), init(), ipconfig_register_dns(), learn_address_script(), link_socket_connection_initiated(), multi_client_connect_call_plugin_v1(), multi_client_connect_call_script(), multi_client_disconnect_script(), netsh_delete_address_dns(), netsh_enable_dhcp(), netsh_ifconfig(), netsh_ifconfig_options(), netsh_set_dns6_servers(), run_up_down(), set_lladdr(), tls_crypt_v2_verify_metadata(), undo_ifconfig_ipv4(), undo_ifconfig_ipv6(), verify_cert_call_command(), verify_cert_call_plugin(), verify_crresponse_script(), and verify_user_pass_script().

◆ argv_parse_cmd()

void argv_parse_cmd ( struct argv argres,
const char *  cmdstr 
)

Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot.

@params *argres Valid pointer to a struct argv where the parsed result will be found. @params *cmdstr Char * based string to parse

Definition at line 483 of file argv.c.

References argv_append(), argv_reset(), D_ARGV_PARSE_CMD, argv::gc, MAX_PARMS, parse_line(), and string_alloc().

Referenced by argv_parse_cmd__command_and_extra_options__argc_correct(), argv_parse_cmd__command_string__argc_correct(), check_cmd_access(), do_route(), ipchange_fmt(), learn_address_script(), multi_client_connect_call_script(), multi_client_disconnect_script(), run_up_down(), tls_crypt_v2_verify_metadata(), verify_cert_call_command(), verify_crresponse_script(), and verify_user_pass_script().

◆ argv_prep_format()

static char* argv_prep_format ( const char *  format,
const char  delim,
size_t *  count,
struct gc_arena gc 
)
static

Prepares argv format string for further processing.

Individual argument must be separated by space. Ignores leading and trailing spaces. Consecutive spaces count as one. Returns prepared format string, with space replaced by delim and adds the number of arguments to the count parameter.

Parameters
*formatPointer to a the format string to process
delimChar with the delimiter to use
*countsize_t pointer used to return the number of tokens (argument slots) found in the format string.
*gcPointer to a gc_arena managed buffer.
Returns
Returns a parsed format string (char *), together with the number of tokens parts found (via *count). The result string is allocated within the gc_arena managed buffer. If the gc_arena pointer is NULL, the returned string must be explicitly free()d to avoid memory leaks.

Definition at line 288 of file argv.c.

References http-client::f, and gc_malloc().

Referenced by argv_printf_arglist().

◆ argv_printf()

bool argv_printf ( struct argv argres,
const char *  format,
  ... 
)

printf() variant which populates a struct argv.

It processes the format string with the provided arguments. For each space separator found in the format string, a new argument will be added to the resulting struct argv.

This will always reset and ensure the result is based on a pristine struct argv.

Parameters
*argresValid pointer to a struct argv where the result will be put.
*formatprintf() compliant (char *) format string.
Returns
Returns true if the parsing was successful. See argv_printf_arglist() for more details. The parsed result will be put into argres.

Definition at line 440 of file argv.c.

References argv_printf_arglist(), and argv_reset().

Referenced by add_route(), add_route_ipv6(), argv_insert_head__non_empty_argv__head_added(), argv_printf__combined_path_with_spaces__argc_correct(), argv_printf__embedded_format_directive__replaced_in_output(), argv_printf__empty_parameter__argc_correct(), argv_printf__group_sep_in_arg__fail_no_ouput(), argv_printf__long_args__data_correct(), argv_printf__multiple_spaces_in_format__parsed_as_one(), argv_printf_cat__multiple_spaces_in_format__parsed_as_one(), argv_printf_cat__used_twice__argc_correct(), argv_str__multiple_argv__correct_output(), cleanup(), delete_route(), delete_route_ipv6(), do_dns_domain_wmic(), do_ifconfig_ipv4(), do_ifconfig_ipv6(), init(), ipchange_fmt(), ipconfig_register_dns(), learn_address_script(), multi_client_connect_call_plugin_v1(), netsh_delete_address_dns(), netsh_enable_dhcp(), netsh_ifconfig(), netsh_ifconfig_options(), netsh_set_dns6_servers(), run_up_down(), set_lladdr(), undo_ifconfig_ipv4(), undo_ifconfig_ipv6(), and verify_cert_call_plugin().

◆ argv_printf_arglist()

static bool argv_printf_arglist ( struct argv argres,
const char *  format,
va_list  arglist 
)
static

Create a struct argv based on a format string.

Instead of parsing the format string ourselves place delimiters via argv_prep_format() before we let libc's printf() do the parsing. Then split the resulting string at the injected delimiters.

Parameters
*argresValid pointer to a struct argv where the resulting parsed arguments, based on the format string.
<em>formatChar string with a printf() compliant format string
arglistA va_list with the arguments to be consumed by the format string
Returns
Returns true if the parsing and processing was successfully. If the resulting number of arguments does not match the expected number of arguments (based on the format string), it is considered a failure, which returns false. This can happen if the ASCII Group Separator (GS - 0x1D) is put into the arguments list or format string.

Definition at line 349 of file argv.c.

References argv::argc, argv_append(), argv_prep_format(), argv_reset(), http-client::f, argv::gc, and gc_malloc().

Referenced by argv_printf(), and argv_printf_cat().

◆ argv_printf_cat()

bool argv_printf_cat ( struct argv argres,
const char *  format,
  ... 
)

printf() inspired argv concatenation.

Adds arguments to an existing struct argv and populets the argument slots based on the printf() based format string.

Parameters
*argresValid pointer to a struct argv where the result will be put.
*formatprintf() compliant (char *) format string.
Returns
Returns true if the parsing was successful. See argv_printf_arglist() for more details. The parsed result will be put into argres.

Definition at line 464 of file argv.c.

References argv_printf_arglist().

Referenced by add_route(), add_route_ipv6(), argv_parse_cmd__command_and_extra_options__argc_correct(), argv_printf_cat__multiple_spaces_in_format__parsed_as_one(), argv_printf_cat__used_twice__argc_correct(), argv_str__multiple_argv__correct_output(), delete_route_ipv6(), ipchange_fmt(), learn_address_script(), multi_client_connect_call_script(), netsh_ifconfig_options(), netsh_set_dns6_servers(), run_up_down(), verify_cert_call_command(), verify_crresponse_script(), and verify_user_pass_script().

◆ argv_reset()

static void argv_reset ( struct argv a)
static

Resets the struct argv to an initial state.

No memory buffers will be released by this call.

Parameters
*aValid pointer to a struct argv to resize

Definition at line 114 of file argv.c.

References argv::argc, and argv::argv.

Referenced by argv_parse_cmd(), argv_printf(), and argv_printf_arglist().

◆ argv_str()

const char* argv_str ( const struct argv a,
struct gc_arena gc,
const unsigned int  flags 
)

Generate a single string with all the arguments in a struct argv concatenated.

Parameters
*aValid pointer to the struct argv with the arguments to list
*gcPointer to a struct gc_arena managed buffer
flagsFlags passed to the print_argv() function.
Returns
Returns a string generated by print_argv() with all the arguments concatenated. If the argument count is 0, it will return an empty string. The return string is allocated in the gc_arena managed buffer. If the gc_arena pointer is NULL, the returned string must be free()d explicitly to avoid memory leaks.

Definition at line 231 of file argv.c.

References argv::argv, argv::gc, and print_argv().

Referenced by argv_msg(), argv_msg_prefix(), argv_str__empty_argv__empty_output(), and argv_str__multiple_argv__correct_output().