GNUnet  0.11.x
Data Structures | Enumerations | Functions
Strings library

Strings and string handling functions, including malloc and string tokenizing. More...

Data Structures

struct  GNUNET_STRINGS_PortPolicy
 
struct  GNUNET_STRINGS_IPv4NetworkPolicy
 IPV4 network in CIDR notation. More...
 
struct  GNUNET_STRINGS_IPv6NetworkPolicy
 network in CIDR notation for IPV6. More...
 

Enumerations

enum  GNUNET_STRINGS_FilenameCheck { GNUNET_STRINGS_CHECK_EXISTS = 0x00000001, GNUNET_STRINGS_CHECK_IS_DIRECTORY = 0x00000002, GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004, GNUNET_STRINGS_CHECK_IS_ABSOLUTE = 0x00000008 }
 Flags for what we should check a file for. More...
 

Functions

int GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, unsigned long long *size)
 Convert a given fancy human-readable size to bytes. More...
 
int GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, struct GNUNET_TIME_Relative *rtime)
 Convert a given fancy human-readable time to our internal representation. More...
 
char * GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
 Convert a given filesize into a fancy human-readable format. More...
 
char * GNUNET_STRINGS_conv (const char *input, size_t len, const char *input_charset, const char *output_charset)
 Convert the len characters long character sequence given in input that is in the given input charset to a string in given output charset. More...
 
char * GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
 Convert the len characters long character sequence given in input that is in the given charset to UTF-8. More...
 
char * GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
 Convert the len bytes-long UTF-8 string given in input to the given charset. More...
 
void GNUNET_STRINGS_utf8_tolower (const char *input, char *output)
 Convert the utf-8 input string to lower case. More...
 
void GNUNET_STRINGS_utf8_toupper (const char *input, char *output)
 Convert the utf-8 input string to upper case. More...
 
char * GNUNET_STRINGS_filename_expand (const char *fil)
 Complete filename (a la shell) from abbrevition. More...
 
size_t GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count,...)
 Fill a buffer of the given size with count 0-terminated strings (given as varargs). More...
 
unsigned int GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, unsigned int count,...)
 Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count (varargs) of type "const char**" to the locations of the respective strings in the buffer. More...
 
const char * GNUNET_STRINGS_get_short_name (const char *filename)
 "man basename" Returns a pointer to a part of filename (allocates nothing)! More...
 
char * GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t out_size)
 Convert binary data to ASCII encoding using CrockfordBase32. More...
 
char * GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size)
 Return the base32crockford encoding of the given buffer. More...
 
int GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, void *out, size_t out_size)
 Convert CrockfordBase32 encoding back to data. More...
 
size_t GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
 Encode into Base64. More...
 
size_t GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **output)
 Decode from Base64. More...
 
char * GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids, unsigned int num_pids)
 Convert a peer path to a human-readable string. More...
 
int GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part, const char **path_part)
 Parse a path that might be an URI. More...
 
int GNUNET_STRINGS_path_is_absolute (const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
 Check whether filename is absolute or not, and if it's an URI. More...
 
int GNUNET_STRINGS_check_filename (const char *filename, enum GNUNET_STRINGS_FilenameCheck checks)
 Perform checks on filename. More...
 
int GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, uint16_t addrlen, struct sockaddr_in6 *r_buf)
 Tries to convert zt_addr string to an IPv6 address. More...
 
int GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen, struct sockaddr_in *r_buf)
 Tries to convert zt_addr string to an IPv4 address. More...
 
size_t GNUNET_STRINGS_parse_socket_addr (const char *addr, uint8_t *af, struct sockaddr **sa)
 Parse an address given as a string into a struct sockaddr. More...
 
int GNUNET_STRINGS_to_address_ip (const char *addr, uint16_t addrlen, struct sockaddr_storage *r_buf)
 Tries to convert addr string to an IP (v4 or v6) address. More...
 
int GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv)
 Returns utf-8 encoded arguments. More...
 
size_t GNUNET_strlcpy (char *dst, const char *src, size_t n)
 Like strlcpy but portable. More...
 
struct GNUNET_STRINGS_IPv4NetworkPolicyGNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
 Parse an IPv4 network policy. More...
 
struct GNUNET_STRINGS_IPv6NetworkPolicyGNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
 Parse an IPv6 network policy. More...
 

Detailed Description

Strings and string handling functions, including malloc and string tokenizing.

Enumeration Type Documentation

◆ GNUNET_STRINGS_FilenameCheck

Flags for what we should check a file for.

Enumerator
GNUNET_STRINGS_CHECK_EXISTS 

Check that it exists.

GNUNET_STRINGS_CHECK_IS_DIRECTORY 

Check that it is a directory.

GNUNET_STRINGS_CHECK_IS_LINK 

Check that it is a link.

GNUNET_STRINGS_CHECK_IS_ABSOLUTE 

Check that the path is an absolute path.

Definition at line 421 of file gnunet_strings_lib.h.

422 {
426  GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
427 
432 
436  GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
437 
442 };
Check that the path is an absolute path.
Check that it exists.
Check that it is a link.
Check that it is a directory.

Function Documentation

◆ GNUNET_STRINGS_fancy_size_to_bytes()

int GNUNET_STRINGS_fancy_size_to_bytes ( const char *  fancy_size,
unsigned long long *  size 
)

Convert a given fancy human-readable size to bytes.

Parameters
fancy_sizehuman readable string (i.e. 1 MB)
sizeset to the size in bytes
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 319 of file strings.c.

References convert_with_table().

Referenced by GNUNET_ATS_solvers_load_quotas(), GNUNET_CONFIGURATION_get_value_size(), parse_quota(), and print_quotas().

321 {
322  static const struct ConversionTable table[] =
323  { { "B", 1 },
324  { "KiB", 1024 },
325  { "kB", 1000 },
326  { "MiB", 1024 * 1024 },
327  { "MB", 1000 * 1000 },
328  { "GiB", 1024 * 1024 * 1024 },
329  { "GB", 1000 * 1000 * 1000 },
330  { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
331  { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
332  { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
333  { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL },
334  { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
335  { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
336  { NULL, 0 } };
337 
338  return convert_with_table (fancy_size, table, size);
339 }
static int convert_with_table(const char *input, const struct ConversionTable *table, unsigned long long *output)
Convert a string of the form "4 X 5 Y" into a numeric value by interpreting "X" and "Y" as units and ...
Definition: strings.c:260
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition: peer.c:55
Unit conversion table entry for 'convert_with_table'.
Definition: strings.c:234
static unsigned int size
Size of the "table".
Definition: peer.c:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_fancy_time_to_relative()

int GNUNET_STRINGS_fancy_time_to_relative ( const char *  fancy_time,
struct GNUNET_TIME_Relative rtime 
)

Convert a given fancy human-readable time to our internal representation.

Parameters
fancy_timehuman readable string (i.e. 1 minute)
rtimeset to the relative time
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 351 of file strings.c.

References convert_with_table(), GNUNET_OK, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_Relative::rel_value_us, and ret.

Referenced by GNUNET_CONFIGURATION_get_value_time(), main(), parse_expiration(), parse_record(), and set_relative_time().

353 {
354  static const struct ConversionTable table[] =
355  { { "us", 1 },
356  { "ms", 1000 },
357  { "s", 1000 * 1000LL },
358  { "second", 1000 * 1000LL },
359  { "seconds", 1000 * 1000LL },
360  { "\"", 1000 * 1000LL },
361  { "m", 60 * 1000 * 1000LL },
362  { "min", 60 * 1000 * 1000LL },
363  { "minute", 60 * 1000 * 1000LL },
364  { "minutes", 60 * 1000 * 1000LL },
365  { "'", 60 * 1000 * 1000LL },
366  { "h", 60 * 60 * 1000 * 1000LL },
367  { "hour", 60 * 60 * 1000 * 1000LL },
368  { "hours", 60 * 60 * 1000 * 1000LL },
369  { "d", 24 * 60 * 60 * 1000LL * 1000LL },
370  { "day", 24 * 60 * 60 * 1000LL * 1000LL },
371  { "days", 24 * 60 * 60 * 1000LL * 1000LL },
372  { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
373  { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
374  { "year", 31536000000000LL /* year */ },
375  { "years", 31536000000000LL /* year */ },
376  { "a", 31536000000000LL /* year */ },
377  { NULL, 0 } };
378  int ret;
379  unsigned long long val;
380 
381  if (0 == strcasecmp ("forever", fancy_time))
382  {
384  return GNUNET_OK;
385  }
386  ret = convert_with_table (fancy_time, table, &val);
387  rtime->rel_value_us = (uint64_t) val;
388  return ret;
389 }
uint64_t rel_value_us
The actual value.
static int convert_with_table(const char *input, const struct ConversionTable *table, unsigned long long *output)
Convert a string of the form "4 X 5 Y" into a numeric value by interpreting "X" and "Y" as units and ...
Definition: strings.c:260
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition: peer.c:55
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
Unit conversion table entry for 'convert_with_table'.
Definition: strings.c:234
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_byte_size_fancy()

char* GNUNET_STRINGS_byte_size_fancy ( unsigned long long  size)

Convert a given filesize into a fancy human-readable format.

Parameters
sizenumber of bytes
Returns
fancy representation of the size (possibly rounded) for humans

Definition at line 175 of file strings.c.

References _, GNUNET_malloc, GNUNET_snprintf(), and ret.

Referenced by progress_cb(), and publish_inspector().

176 {
177  const char *unit = _ (/* size unit */ "b");
178  char *ret;
179 
180  if (size > 5 * 1024)
181  {
182  size = size / 1024;
183  unit = "KiB";
184  if (size > 5 * 1024)
185  {
186  size = size / 1024;
187  unit = "MiB";
188  if (size > 5 * 1024)
189  {
190  size = size / 1024;
191  unit = "GiB";
192  if (size > 5 * 1024)
193  {
194  size = size / 1024;
195  unit = "TiB";
196  }
197  }
198  }
199  }
200  ret = GNUNET_malloc (32);
201  GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
202  return ret;
203 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_conv()

char* GNUNET_STRINGS_conv ( const char *  input,
size_t  len,
const char *  input_charset,
const char *  output_charset 
)

Convert the len characters long character sequence given in input that is in the given input charset to a string in given output charset.

Parameters
inputinput string
lennumber of bytes in input
input_charsetcharacter set used for input
output_charsetdesired character set for the return value
Returns
the converted string (0-terminated), if conversion fails, a copy of the orignal string is returned.

Definition at line 447 of file strings.c.

References _, GNUNET_ERROR_TYPE_WARNING, GNUNET_malloc, GNUNET_memcpy, len, LOG, LOG_STRERROR, and ret.

Referenced by GNUNET_STRINGS_from_utf8(), and GNUNET_STRINGS_to_utf8().

451 {
452  char *ret;
453  uint8_t *u8_string;
454  char *encoded_string;
455  size_t u8_string_length;
456  size_t encoded_string_length;
457 
458  u8_string = u8_conv_from_encoding (input_charset,
459  iconveh_error,
460  input,
461  len,
462  NULL,
463  NULL,
464  &u8_string_length);
465  if (NULL == u8_string)
466  {
467  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
468  goto fail;
469  }
470  if (0 == strcmp (output_charset, "UTF-8"))
471  {
472  ret = GNUNET_malloc (u8_string_length + 1);
473  GNUNET_memcpy (ret, u8_string, u8_string_length);
474  ret[u8_string_length] = '\0';
475  free (u8_string);
476  return ret;
477  }
478  encoded_string = u8_conv_to_encoding (output_charset,
479  iconveh_error,
480  u8_string,
481  u8_string_length,
482  NULL,
483  NULL,
484  &encoded_string_length);
485  free (u8_string);
486  if (NULL == encoded_string)
487  {
488  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
489  goto fail;
490  }
491  ret = GNUNET_malloc (encoded_string_length + 1);
492  GNUNET_memcpy (ret, encoded_string, encoded_string_length);
493  ret[encoded_string_length] = '\0';
494  free (encoded_string);
495  return ret;
496 fail:
498  _ ("Character sets requested were `%s'->`%s'\n"),
499  "UTF-8",
500  output_charset);
501  ret = GNUNET_malloc (len + 1);
502  GNUNET_memcpy (ret, input, len);
503  ret[len] = '\0';
504  return ret;
505 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define LOG(kind,...)
Definition: strings.c:37
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:39
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_utf8()

char* GNUNET_STRINGS_to_utf8 ( const char *  input,
size_t  len,
const char *  charset 
)

Convert the len characters long character sequence given in input that is in the given charset to UTF-8.

Parameters
inputthe input string (not necessarily 0-terminated)
lenthe number of bytes in the input
charsetcharacter set to convert from
Returns
the converted string (0-terminated)
Parameters
inputthe input string (not necessarily 0-terminated)
lenthe number of bytes in the input
charsetcharacter set to convert from
Returns
the converted string (0-terminated), if conversion fails, a copy of the orignal string is returned.

Definition at line 521 of file strings.c.

References GNUNET_STRINGS_conv().

522 {
523  return GNUNET_STRINGS_conv (input, len, charset, "UTF-8");
524 }
char * GNUNET_STRINGS_conv(const char *input, size_t len, const char *input_charset, const char *output_charset)
Convert the len characters long character sequence given in input that is in the given input charset ...
Definition: strings.c:447
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:

◆ GNUNET_STRINGS_from_utf8()

char* GNUNET_STRINGS_from_utf8 ( const char *  input,
size_t  len,
const char *  charset 
)

Convert the len bytes-long UTF-8 string given in input to the given charset.

Parameters
inputthe input string (not necessarily 0-terminated)
lenthe number of bytes in the input
charsetcharacter set to convert to
Returns
the converted string (0-terminated), if conversion fails, a copy of the orignal string is returned.

Definition at line 539 of file strings.c.

References GNUNET_STRINGS_conv().

540 {
541  return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
542 }
char * GNUNET_STRINGS_conv(const char *input, size_t len, const char *input_charset, const char *output_charset)
Convert the len characters long character sequence given in input that is in the given input charset ...
Definition: strings.c:447
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:

◆ GNUNET_STRINGS_utf8_tolower()

void GNUNET_STRINGS_utf8_tolower ( const char *  input,
char *  output 
)

Convert the utf-8 input string to lower case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer

Convert the utf-8 input string to lower case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer

Definition at line 553 of file strings.c.

References GNUNET_memcpy, and len.

Referenced by ego_create(), GNUNET_GNSRECORD_string_to_lowercase(), GNUNET_RECLAIM_ATTRIBUTE_claim_new(), handle_collect(), handle_create_message(), handle_delete_message(), handle_get_default_message(), handle_lookup(), handle_rename_message(), handle_set_default_message(), handle_verify(), header_iterator(), and run_with_zone_pkey().

554 {
555  uint8_t *tmp_in;
556  size_t len;
557 
558  tmp_in = u8_tolower ((uint8_t *) input,
559  strlen ((char *) input),
560  NULL,
561  UNINORM_NFD,
562  NULL,
563  &len);
564  GNUNET_memcpy (output, tmp_in, len);
565  output[len] = '\0';
566  free (tmp_in);
567 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_STRINGS_utf8_toupper()

void GNUNET_STRINGS_utf8_toupper ( const char *  input,
char *  output 
)

Convert the utf-8 input string to upper case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer

Convert the utf-8 input string to upper case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer

Definition at line 578 of file strings.c.

References GNUNET_memcpy, and len.

Referenced by client_configure_plugin(), GNUNET_CRYPTO_hash_from_string2(), GNUNET_HOSTLIST_client_start(), and load_op_add_address().

579 {
580  uint8_t *tmp_in;
581  size_t len;
582 
583  tmp_in = u8_toupper ((uint8_t *) input,
584  strlen ((char *) input),
585  NULL,
586  UNINORM_NFD,
587  NULL,
588  &len);
589  GNUNET_memcpy (output, tmp_in, len);
590  output[len] = '\0';
591  free (tmp_in);
592 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_STRINGS_filename_expand()

char* GNUNET_STRINGS_filename_expand ( const char *  fil)

Complete filename (a la shell) from abbrevition.

Parameters
filthe name of the file, may contain ~/ or be relative to the current directory
Returns
the full file name, NULL is returned on error
Parameters
filthe name of the file, may contain ~/ or be relative to the current directory
Returns
the full file name, NULL is returned on error

Definition at line 603 of file strings.c.

References _, DIR_SEPARATOR, DIR_SEPARATOR_STR, getenv(), GNUNET_asprintf(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_malloc, GNUNET_strdup, len, LOG, and LOG_STRERROR.

Referenced by check_client_index_start(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_parse(), GNUNET_CONFIGURATION_write(), GNUNET_DISK_directory_create(), GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_directory_scan(), GNUNET_DISK_file_open(), GNUNET_DISK_file_test(), GNUNET_FS_directory_scan_start(), GNUNET_FS_make_file_reader_context_(), GNUNET_log_setup(), handle_client_index_start(), hash_for_index_cb(), print_option(), and set_filename().

604 {
605  char *buffer;
606  size_t len;
607  char *fm;
608  const char *fil_ptr;
609 
610  if (fil == NULL)
611  return NULL;
612 
613  if (fil[0] == DIR_SEPARATOR)
614  /* absolute path, just copy */
615  return GNUNET_strdup (fil);
616  if (fil[0] == '~')
617  {
618  fm = getenv ("HOME");
619  if (fm == NULL)
620  {
622  _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
623  return NULL;
624  }
625  fm = GNUNET_strdup (fm);
626  /* do not copy '~' */
627  fil_ptr = fil + 1;
628 
629  /* skip over dir seperator to be consistent */
630  if (fil_ptr[0] == DIR_SEPARATOR)
631  fil_ptr++;
632  }
633  else
634  {
635  /* relative path */
636  fil_ptr = fil;
637  len = 512;
638  fm = NULL;
639  while (1)
640  {
641  buffer = GNUNET_malloc (len);
642  if (getcwd (buffer, len) != NULL)
643  {
644  fm = buffer;
645  break;
646  }
647  if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
648  {
649  len *= 2;
650  GNUNET_free (buffer);
651  continue;
652  }
653  GNUNET_free (buffer);
654  break;
655  }
656  if (fm == NULL)
657  {
659  buffer = getenv ("PWD"); /* alternative */
660  if (buffer != NULL)
661  fm = GNUNET_strdup (buffer);
662  }
663  if (fm == NULL)
664  fm = GNUNET_strdup ("./"); /* give up */
665  }
666  GNUNET_asprintf (&buffer,
667  "%s%s%s",
668  fm,
669  (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? ""
671  fil_ptr);
672  GNUNET_free (fm);
673  return buffer;
674 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define DIR_SEPARATOR_STR
Definition: platform.h:168
#define LOG(kind,...)
Definition: strings.c:37
#define DIR_SEPARATOR
Definition: platform.h:167
char * getenv()
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:39
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_buffer_fill()

size_t GNUNET_STRINGS_buffer_fill ( char *  buffer,
size_t  size,
unsigned int  count,
  ... 
)

Fill a buffer of the given size with count 0-terminated strings (given as varargs).

If "buffer" is NULL, only compute the amount of space required (sum of "strlen(arg)+1").

Unlike using "snprintf" with "%s", this function will add 0-terminators after each string. The "GNUNET_string_buffer_tokenize" function can be used to parse the buffer back into individual strings.

Parameters
bufferthe buffer to fill with strings, can be NULL in which case only the necessary amount of space will be calculated
sizenumber of bytes available in buffer
countnumber of strings that follow
...count 0-terminated strings to copy to buffer
Returns
number of bytes written to the buffer (or number of bytes that would have been written)

If "buffer" is NULL, only compute the amount of space required (sum of "strlen(arg)+1").

Unlike using "snprintf" with "%s", this function will add 0-terminators after each string. The #GNUNET_string_buffer_tokenize() function can be used to parse the buffer back into individual strings.

Parameters
bufferthe buffer to fill with strings, can be NULL in which case only the necessary amount of space will be calculated
sizenumber of bytes available in buffer
countnumber of strings that follow
...count 0-terminated strings to copy to buffer
Returns
number of bytes written to the buffer (or number of bytes that would have been written)

Definition at line 65 of file strings.c.

References GNUNET_assert, and GNUNET_memcpy.

Referenced by save(), transmit(), transmit_get(), transmit_set(), and transmit_watch().

66 {
67  size_t needed;
68  size_t slen;
69  const char *s;
70  va_list ap;
71 
72  needed = 0;
73  va_start (ap, count);
74  while (count > 0)
75  {
76  s = va_arg (ap, const char *);
77 
78  slen = strlen (s) + 1;
79  if (buffer != NULL)
80  {
81  GNUNET_assert (needed + slen <= size);
82  GNUNET_memcpy (&buffer[needed], s, slen);
83  }
84  needed += slen;
85  count--;
86  }
87  va_end (ap);
88  return needed;
89 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static unsigned int size
Size of the "table".
Definition: peer.c:67
Here is the caller graph for this function:

◆ GNUNET_STRINGS_buffer_tokenize()

unsigned int GNUNET_STRINGS_buffer_tokenize ( const char *  buffer,
size_t  size,
unsigned int  count,
  ... 
)

Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count (varargs) of type "const char**" to the locations of the respective strings in the buffer.

Parameters
bufferthe buffer to parse
sizesize of the buffer
countnumber of strings to locate
...pointers to where to store the strings
Returns
offset of the character after the last 0-termination in the buffer, or 0 on error.
Parameters
bufferthe buffer to parse
sizesize of the buffer
countnumber of strings to locate
Returns
offset of the character after the last 0-termination in the buffer, or 0 on error.

Definition at line 135 of file strings.c.

References start.

Referenced by check_get(), check_set(), check_statistics_value(), check_watch(), handle_get(), handle_set(), handle_statistics_value(), handle_watch(), and process_sblock().

139 {
140  unsigned int start;
141  unsigned int needed;
142  const char **r;
143  va_list ap;
144 
145  needed = 0;
146  va_start (ap, count);
147  while (count > 0)
148  {
149  r = va_arg (ap, const char **);
150 
151  start = needed;
152  while ((needed < size) && (buffer[needed] != '\0'))
153  needed++;
154  if (needed == size)
155  {
156  va_end (ap);
157  return 0; /* error */
158  }
159  *r = &buffer[start];
160  needed++; /* skip 0-termination */
161  count--;
162  }
163  va_end (ap);
164  return needed;
165 }
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
static unsigned int size
Size of the "table".
Definition: peer.c:67
Here is the caller graph for this function:

◆ GNUNET_STRINGS_get_short_name()

const char* GNUNET_STRINGS_get_short_name ( const char *  filename)

"man basename" Returns a pointer to a part of filename (allocates nothing)!

Parameters
filenamefilename to extract basename from
Returns
short (base) name of the file (that is, everything following the last directory separator in filename. If filename ends with a directory separator, the result will be a zero-length string. If filename has no directory separators, the result is filename itself.

Definition at line 775 of file strings.c.

References DIR_SEPARATOR_STR, and filename.

Referenced by expand_tree(), iter_check_config(), and iter_testbed_path().

776 {
777  const char *short_fn = filename;
778  const char *ss;
779 
780  while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
781  short_fn = 1 + ss;
782  return short_fn;
783 }
#define DIR_SEPARATOR_STR
Definition: platform.h:168
static char * filename
Here is the caller graph for this function:

◆ GNUNET_STRINGS_data_to_string()

char* GNUNET_STRINGS_data_to_string ( const void *  data,
size_t  size,
char *  out,
size_t  out_size 
)

Convert binary data to ASCII encoding using CrockfordBase32.

Does not append 0-terminator, but returns a pointer to the place where it should be placed, if needed.

Parameters
datadata to encode
sizesize of data (in bytes)
outbuffer to fill
out_sizesize of the buffer. Must be large enough to hold ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
Returns
pointer to the next byte in 'out' or NULL on error.

Convert binary data to ASCII encoding using CrockfordBase32.

Returns a pointer to the byte after the last byte in the string, that is where the 0-terminator was placed if there was room.

Parameters
datadata to encode
sizesize of data (in bytes)
outbuffer to fill
out_sizesize of the buffer. Must be large enough to hold (size * 8 + 4) / 5 bytes
Returns
pointer to the next byte in out or NULL on error.

32 characters for encoding

Definition at line 856 of file strings.c.

References data, GNUNET_assert, and GNUNET_break.

Referenced by auth_key_to_string(), GNUNET_CRYPTO_ecdsa_private_key_to_string(), GNUNET_CRYPTO_ecdsa_public_key_to_string(), GNUNET_CRYPTO_eddsa_private_key_to_string(), GNUNET_CRYPTO_eddsa_public_key_to_string(), GNUNET_CRYPTO_hash_to_enc(), GNUNET_GNSRECORD_z2s(), GNUNET_sh2s(), GNUNET_STRINGS_data_to_string_alloc(), GNUNET_uuid2s(), output_message(), print_examples_ecdh(), secret_ready_cb(), uri_loc_to_string(), and uri_sks_to_string().

860 {
864  static char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
865  unsigned int wpos;
866  unsigned int rpos;
867  unsigned int bits;
868  unsigned int vbit;
869  const unsigned char *udata;
870 
871  udata = data;
872  if (out_size < (size * 8 + 4) / 5)
873  {
874  GNUNET_break (0);
875  return NULL;
876  }
877  vbit = 0;
878  wpos = 0;
879  rpos = 0;
880  bits = 0;
881  while ((rpos < size) || (vbit > 0))
882  {
883  if ((rpos < size) && (vbit < 5))
884  {
885  bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
886  vbit += 8;
887  }
888  if (vbit < 5)
889  {
890  bits <<= (5 - vbit); /* zero-padding */
891  GNUNET_assert (vbit == ((size * 8) % 5));
892  vbit = 5;
893  }
894  if (wpos >= out_size)
895  {
896  GNUNET_break (0);
897  return NULL;
898  }
899  out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
900  vbit -= 5;
901  }
902  GNUNET_assert (0 == vbit);
903  if (wpos < out_size)
904  out[wpos] = '\0';
905  return &out[wpos];
906 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static unsigned int size
Size of the "table".
Definition: peer.c:67
uint32_t data
The data value.
Here is the caller graph for this function:

◆ GNUNET_STRINGS_data_to_string_alloc()

char* GNUNET_STRINGS_data_to_string_alloc ( const void *  buf,
size_t  size 
)

Return the base32crockford encoding of the given buffer.

The returned string will be freshly allocated, and must be free'd with GNUNET_free().

Parameters
bufbuffer with data
sizesize of the buffer buf
Returns
freshly allocated, null-terminated string

The returned string will be freshly allocated, and must be free'd with GNUNET_free().

Parameters
bufferwith data
sizesize of the buffer
Returns
freshly allocated, null-terminated string

Definition at line 920 of file strings.c.

References end, GNUNET_free, GNUNET_malloc, GNUNET_STRINGS_data_to_string(), and len.

Referenced by attr_collect(), attr_store_task(), conversation_value_to_string(), GNUNET_CURL_job_add2(), GNUNET_JSON_from_data(), handle_attribute_delete_message(), issue_ticket(), iter_cb(), lookup_authz_cb(), move_attr_finished(), move_attrs(), OIDC_id_token_new(), oidc_ticket_issue_cb(), process_attrs(), process_record(), RECLAIM_TICKETS_consume(), RECLAIM_TICKETS_revoke(), rvk_move_attr_cb(), ticket_collect(), ticket_issue_cb(), ticket_iter(), and value_to_string().

921 {
922  char *str_buf;
923  size_t len = size * 8;
924  char *end;
925 
926  if (len % 5 > 0)
927  len += 5 - len % 5;
928  len /= 5;
929  str_buf = GNUNET_malloc (len + 1);
930  end = GNUNET_STRINGS_data_to_string (buf, size, str_buf, len);
931  if (NULL == end)
932  {
933  GNUNET_free (str_buf);
934  return NULL;
935  }
936  *end = '\0';
937  return str_buf;
938 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using Crockford Base32 encoding.
Definition: strings.c:856
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_string_to_data()

int GNUNET_STRINGS_string_to_data ( const char *  enc,
size_t  enclen,
void *  out,
size_t  out_size 
)

Convert CrockfordBase32 encoding back to data.

out_size must match exactly the size of the data before it was encoded.

Parameters
encthe encoding
enclennumber of characters in enc (without 0-terminator, which can be missing)
outlocation where to store the decoded data
out_sizesize of the output buffer out
Returns
GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding

Convert CrockfordBase32 encoding back to data.

out_size must match exactly the size of the data before it was encoded.

Parameters
encthe encoding
enclennumber of characters in enc (without 0-terminator, which can be missing)
outlocation where to store the decoded data
out_sizesize of the output buffer out
Returns
GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding

Definition at line 952 of file strings.c.

References getValue__(), GNUNET_break, GNUNET_OK, GNUNET_SYSERR, and ret.

Referenced by check_authorization(), conversation_string_to_value(), delete_attribute_cont(), GNUNET_CONFIGURATION_get_data(), GNUNET_CRYPTO_ecdsa_public_key_from_string(), GNUNET_CRYPTO_eddsa_private_key_from_string(), GNUNET_CRYPTO_eddsa_public_key_from_string(), GNUNET_CRYPTO_hash_from_string2(), GNUNET_HELLO_parse_uri(), lookup_redirect_uri_result(), parse_attr(), parse_fixed_data(), parse_rsa_public_key(), parse_rsa_signature(), parse_ticket(), parse_variable_data(), read_service_conf(), run(), set_base32(), start_process(), string_to_auth_key(), string_to_value(), uri_loc_parse(), and uri_sks_parse().

956 {
957  unsigned int rpos;
958  unsigned int wpos;
959  unsigned int bits;
960  unsigned int vbit;
961  int ret;
962  int shift;
963  unsigned char *uout;
964  unsigned int encoded_len = out_size * 8;
965 
966  if (0 == enclen)
967  {
968  if (0 == out_size)
969  return GNUNET_OK;
970  return GNUNET_SYSERR;
971  }
972  uout = out;
973  wpos = out_size;
974  rpos = enclen;
975  if ((encoded_len % 5) > 0)
976  {
977  vbit = encoded_len % 5; /* padding! */
978  shift = 5 - vbit;
979  bits = (ret = getValue__ (enc[--rpos])) >> shift;
980  }
981  else
982  {
983  vbit = 5;
984  shift = 0;
985  bits = (ret = getValue__ (enc[--rpos]));
986  }
987  if ((encoded_len + shift) / 5 != enclen)
988  return GNUNET_SYSERR;
989  if (-1 == ret)
990  return GNUNET_SYSERR;
991  while (wpos > 0)
992  {
993  if (0 == rpos)
994  {
995  GNUNET_break (0);
996  return GNUNET_SYSERR;
997  }
998  bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
999  if (-1 == ret)
1000  return GNUNET_SYSERR;
1001  vbit += 5;
1002  if (vbit >= 8)
1003  {
1004  uout[--wpos] = (unsigned char) bits;
1005  bits >>= 8;
1006  vbit -= 8;
1007  }
1008  }
1009  if ((0 != rpos) || (0 != vbit))
1010  return GNUNET_SYSERR;
1011  return GNUNET_OK;
1012 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static unsigned int getValue__(unsigned char a)
Get the decoded value corresponding to a character according to Crockford Base32 encoding.
Definition: strings.c:794
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static OpusEncoder * enc
OPUS encoder.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_base64_encode()

size_t GNUNET_STRINGS_base64_encode ( const void *  in,
size_t  len,
char **  output 
)

Encode into Base64.

Parameters
datathe data to encode
lenthe length of the input
outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output
Parameters
inthe data to encode
lenthe length of the input
outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output

Definition at line 1855 of file strings.c.

References cvt, data, FILLCHAR, GNUNET_malloc, len, and ret.

Referenced by api_send(), base64_and_urlencode(), base64url_encode(), dns_value_to_string(), GNUNET_ABD_delegate_to_string(), GNUNET_HELLO_sign_address(), OIDC_access_token_new(), OIDC_id_token_new(), and store_and_free_entries().

1856 {
1857  const char *data = in;
1858  size_t ret;
1859  char *opt;
1860 
1861  ret = 0;
1862  opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1863  for (size_t i = 0; i < len; ++i)
1864  {
1865  char c;
1866 
1867  c = (data[i] >> 2) & 0x3f;
1868  opt[ret++] = cvt[(int) c];
1869  c = (data[i] << 4) & 0x3f;
1870  if (++i < len)
1871  c |= (data[i] >> 4) & 0x0f;
1872  opt[ret++] = cvt[(int) c];
1873  if (i < len)
1874  {
1875  c = (data[i] << 2) & 0x3f;
1876  if (++i < len)
1877  c |= (data[i] >> 6) & 0x03;
1878  opt[ret++] = cvt[(int) c];
1879  }
1880  else
1881  {
1882  ++i;
1883  opt[ret++] = FILLCHAR;
1884  }
1885  if (i < len)
1886  {
1887  c = data[i] & 0x3f;
1888  opt[ret++] = cvt[(int) c];
1889  }
1890  else
1891  {
1892  opt[ret++] = FILLCHAR;
1893  }
1894  }
1895  *output = opt;
1896  return ret;
1897 }
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1839
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char * cvt
Definition: strings.c:1840
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_STRINGS_base64_decode()

size_t GNUNET_STRINGS_base64_decode ( const char *  data,
size_t  len,
void **  out 
)

Decode from Base64.

Parameters
datathe data to encode
lenthe length of the input
[out]outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output
Parameters
datathe data to encode
lenthe length of the input
outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output

Definition at line 1920 of file strings.c.

References CHECK_CRLF, cvtfind, END, FILLCHAR, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_malloc, len, and ret.

Referenced by check_authorization(), database_setup(), dns_string_to_value(), GNUNET_ABD_delegate_from_string(), GNUNET_HELLO_extract_address(), listenAndDistribute(), and OIDC_parse_authz_code().

1921 {
1922  char *output;
1923  size_t ret = 0;
1924 
1925 #define CHECK_CRLF \
1926  while (data[i] == '\r' || data[i] == '\n') \
1927  { \
1928  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \
1929  "ignoring CR/LF\n"); \
1930  i++; \
1931  if (i >= len) \
1932  goto END; \
1933  }
1934 
1935  output = GNUNET_malloc ((len * 3 / 4) + 8);
1937  "base64_decode decoding len=%d\n",
1938  (int) len);
1939  for (size_t i = 0; i < len; ++i)
1940  {
1941  char c;
1942  char c1;
1943 
1944  CHECK_CRLF;
1945  if (FILLCHAR == data[i])
1946  break;
1947  c = (char) cvtfind (data[i]);
1948  ++i;
1949  CHECK_CRLF;
1950  c1 = (char) cvtfind (data[i]);
1951  c = (c << 2) | ((c1 >> 4) & 0x3);
1952  output[ret++] = c;
1953  if (++i < len)
1954  {
1955  CHECK_CRLF;
1956  c = data[i];
1957  if (FILLCHAR == c)
1958  break;
1959  c = (char) cvtfind (c);
1960  c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1961  output[ret++] = c1;
1962  }
1963  if (++i < len)
1964  {
1965  CHECK_CRLF;
1966  c1 = data[i];
1967  if (FILLCHAR == c1)
1968  break;
1969 
1970  c1 = (char) cvtfind (c1);
1971  c = ((c << 6) & 0xc0) | c1;
1972  output[ret++] = c;
1973  }
1974  }
1975 END:
1976  *out = output;
1977  return ret;
1978 }
#define CHECK_CRLF
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1839
#define cvtfind(a)
Definition: strings.c:1900
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
We&#39;re done processing.
#define GNUNET_log(kind,...)
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_STRINGS_pp2s()

char* GNUNET_STRINGS_pp2s ( const struct GNUNET_PeerIdentity pids,
unsigned int  num_pids 
)

Convert a peer path to a human-readable string.

Parameters
pidsarray of PIDs to convert to a string
num_pidslength of the pids array
Returns
string representing the array of pids

Definition at line 100 of file strings.c.

References buf, GNUNET_i2s(), GNUNET_malloc, and GNUNET_snprintf().

Referenced by handle_dht_p2p_put(), handle_dht_p2p_result(), handle_local_result(), and process_client_result().

102 {
103  char *buf;
104  size_t off;
105  size_t plen = num_pids * 5 + 1;
106 
107  off = 0;
108  buf = GNUNET_malloc (plen);
109  for (unsigned int i = 0; i < num_pids; i++)
110  {
111  off += GNUNET_snprintf (&buf[off],
112  plen - off,
113  "%s%s",
114  GNUNET_i2s (&pids[i]),
115  (i == num_pids - 1) ? "" : "-");
116  }
117  return buf;
118 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
static char buf[2048]
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_uri()

int GNUNET_STRINGS_parse_uri ( const char *  path,
char **  scheme_part,
const char **  path_part 
)

Parse a path that might be an URI.

Parameters
pathpath to parse. Must be NULL-terminated.
[out]scheme_partpointer to a string that represents the URI scheme will be stored. Can be NULL. The string is allocated by the function, and should be freed by GNUNET_free() when it is no longer needed.
path_parta pointer to 'const char *' where a pointer to the path part of the URI will be stored. Can be NULL. Points to the same block of memory as path, and thus must not be freed. Might point to '\0', if path part is zero-length.
Returns
GNUNET_YES if it's an URI, GNUNET_NO otherwise. If 'path' is not an URI, '* scheme_part' and '*path_part' will remain unchanged (if they weren't NULL).
Parameters
pathpath to parse. Must be NULL-terminated.
scheme_parta pointer to 'char *' where a pointer to a string that represents the URI scheme will be stored. Can be NULL. The string is allocated by the function, and should be freed by GNUNET_free() when it is no longer needed.
path_parta pointer to 'const char *' where a pointer to the path part of the URI will be stored. Can be NULL. Points to the same block of memory as 'path', and thus must not be freed. Might point to '\0', if path part is zero-length.
Returns
GNUNET_YES if it's an URI, GNUNET_NO otherwise. If 'path' is not an URI, '* scheme_part' and '*path_part' will remain unchanged (if they weren't NULL).

Definition at line 1032 of file strings.c.

References end, GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, GNUNET_YES, and len.

Referenced by GNUNET_STRINGS_path_is_absolute().

1035 {
1036  size_t len;
1037  size_t i;
1038  int end;
1039  int pp_state = 0;
1040  const char *post_scheme_part = NULL;
1041 
1042  len = strlen (path);
1043  for (end = 0, i = 0; ! end && i < len; i++)
1044  {
1045  switch (pp_state)
1046  {
1047  case 0:
1048  if ((path[i] == ':') && (i > 0))
1049  {
1050  pp_state += 1;
1051  continue;
1052  }
1053  if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
1054  ((path[i] >= 'a') && (path[i] <= 'z') ) ||
1055  ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
1056  (path[i] == '-') || (path[i] == '.')))
1057  end = 1;
1058  break;
1059 
1060  case 1:
1061  case 2:
1062  if (path[i] == '/')
1063  {
1064  pp_state += 1;
1065  continue;
1066  }
1067  end = 1;
1068  break;
1069 
1070  case 3:
1071  post_scheme_part = &path[i];
1072  end = 1;
1073  break;
1074 
1075  default:
1076  end = 1;
1077  }
1078  }
1079  if (post_scheme_part == NULL)
1080  return GNUNET_NO;
1081  if (scheme_part)
1082  {
1083  *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
1084  GNUNET_memcpy (*scheme_part, path, post_scheme_part - path);
1085  (*scheme_part)[post_scheme_part - path] = '\0';
1086  }
1087  if (path_part)
1088  *path_part = post_scheme_part;
1089  return GNUNET_YES;
1090 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_YES
Definition: gnunet_common.h:77
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_STRINGS_path_is_absolute()

int GNUNET_STRINGS_path_is_absolute ( const char *  filename,
int  can_be_uri,
int *  r_is_uri,
char **  r_uri_scheme 
)

Check whether filename is absolute or not, and if it's an URI.

Parameters
filenamefilename to check
can_be_uriGNUNET_YES to check for being URI, GNUNET_NO - to assume it's not URI
r_is_uria pointer to an int that is set to GNUNET_YES if 'filename' is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is not GNUNET_YES, *r_is_uri is set to GNUNET_NO.
r_uri_schemea pointer to a char * that is set to a pointer to URI scheme. The string is allocated by the function, and should be freed with GNUNET_free (). Can be NULL.
Returns
GNUNET_YES if 'filename' is absolute, GNUNET_NO otherwise.

Check whether filename is absolute or not, and if it's an URI.

Parameters
filenamefilename to check
can_be_uriGNUNET_YES to check for being URI, GNUNET_NO - to assume it's not URI
r_is_uria pointer to an int that is set to GNUNET_YES if filename is URI and to GNUNET_NO otherwise. Can be NULL. If can_be_uri is not GNUNET_YES, * r_is_uri is set to GNUNET_NO.
r_uri_schemea pointer to a char * that is set to a pointer to URI scheme. The string is allocated by the function, and should be freed with GNUNET_free(). Can be NULL.
Returns
GNUNET_YES if filename is absolute, GNUNET_NO otherwise.

Definition at line 1108 of file strings.c.

References GNUNET_free_non_null, GNUNET_NO, GNUNET_STRINGS_parse_uri(), GNUNET_YES, and uri.

Referenced by GNUNET_OS_check_helper_binary(), GNUNET_OS_get_libexec_binary_path(), GNUNET_OS_get_suid_binary_path(), and GNUNET_STRINGS_check_filename().

1112 {
1113  const char *post_scheme_path;
1114  int is_uri;
1115  char *uri;
1116  /* consider POSIX paths to be absolute too, even on W32,
1117  * as plibc expansion will fix them for us.
1118  */
1119  if (filename[0] == '/')
1120  return GNUNET_YES;
1121  if (can_be_uri)
1122  {
1123  is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
1124  if (r_is_uri)
1125  *r_is_uri = is_uri;
1126  if (is_uri)
1127  {
1128  if (r_uri_scheme)
1129  *r_uri_scheme = uri;
1130  else
1131  GNUNET_free_non_null (uri);
1132 
1133  return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1134  GNUNET_NO,
1135  NULL,
1136  NULL);
1137  }
1138  }
1139  else
1140  {
1141  if (r_is_uri)
1142  *r_is_uri = GNUNET_NO;
1143  }
1144 
1145  return GNUNET_NO;
1146 }
int GNUNET_STRINGS_parse_uri(const char *path, char **scheme_part, const char **path_part)
Parse a path that might be an URI.
Definition: strings.c:1032
int GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it&#39;s an URI.
Definition: strings.c:1108
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static char * filename
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_check_filename()

int GNUNET_STRINGS_check_filename ( const char *  filename,
enum GNUNET_STRINGS_FilenameCheck  checks 
)

Perform checks on filename.

FIXME: some duplication with "GNUNET_DISK_"-APIs. We should unify those.

Parameters
filenamefile to check
checkschecks to perform
Returns
GNUNET_YES if all checks pass, GNUNET_NO if at least one of them fails, GNUNET_SYSERR when a check can't be performed

Perform checks on filename.

Parameters
filenamefile to check
checkschecks to perform
Returns
GNUNET_YES if all checks pass, GNUNET_NO if at least one of them fails, GNUNET_SYSERR when a check can't be performed

Definition at line 1158 of file strings.c.

References GNUNET_NO, GNUNET_STRINGS_CHECK_EXISTS, GNUNET_STRINGS_CHECK_IS_ABSOLUTE, GNUNET_STRINGS_CHECK_IS_DIRECTORY, GNUNET_STRINGS_CHECK_IS_LINK, GNUNET_STRINGS_path_is_absolute(), GNUNET_SYSERR, GNUNET_YES, and S_ISLNK.

1160 {
1161  struct stat st;
1162 
1163  if ((NULL == filename) || (filename[0] == '\0'))
1164  return GNUNET_SYSERR;
1165  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1166  if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
1167  return GNUNET_NO;
1168  if (0 != (checks
1171  {
1172  if (0 != stat (filename, &st))
1173  {
1174  if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1175  return GNUNET_NO;
1176  else
1177  return GNUNET_SYSERR;
1178  }
1179  }
1180  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1181  if (! S_ISDIR (st.st_mode))
1182  return GNUNET_NO;
1183  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1184  if (! S_ISLNK (st.st_mode))
1185  return GNUNET_NO;
1186  return GNUNET_YES;
1187 }
int GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it&#39;s an URI.
Definition: strings.c:1108
Check that the path is an absolute path.
#define GNUNET_NO
Definition: gnunet_common.h:78
Check that it exists.
static char * filename
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
#define S_ISLNK(m)
Definition: disk.c:61
#define GNUNET_YES
Definition: gnunet_common.h:77
Check that it is a link.
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
Check that it is a directory.
Here is the call graph for this function:

◆ GNUNET_STRINGS_to_address_ipv6()

int GNUNET_STRINGS_to_address_ipv6 ( const char *  zt_addr,
uint16_t  addrlen,
struct sockaddr_in6 *  r_buf 
)

Tries to convert zt_addr string to an IPv6 address.

The string is expected to have the format "[ABCD::01]:80".

Parameters
zt_addr0-terminated string. May be mangled by the function.
addrlenlength of zt_addr (not counting 0-terminator).
r_bufa buffer to fill. Initially gets filled with zeroes, then its sin6_port, sin6_family and sin6_addr are set appropriately.
Returns
GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

The string is expected to have the format "[ABCD::01]:80".

Parameters
zt_addr0-terminated string. May be mangled by the function.
addrlenlength of zt_addr (not counting 0-terminator).
r_bufa buffer to fill. Initially gets filled with zeroes, then its sin6_port, sin6_family and sin6_addr are set appropriately.
Returns
GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1203 of file strings.c.

References _, dummy, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_memcpy, GNUNET_OK, GNUNET_SYSERR, port, and ret.

Referenced by GNUNET_STRINGS_parse_socket_addr(), and GNUNET_STRINGS_to_address_ip().

1206 {
1207  char zbuf[addrlen + 1];
1208  int ret;
1209  char *port_colon;
1210  unsigned int port;
1211  char dummy[2];
1212 
1213  if (addrlen < 6)
1214  return GNUNET_SYSERR;
1215  GNUNET_memcpy (zbuf, zt_addr, addrlen);
1216  if ('[' != zbuf[0])
1217  {
1219  _ ("IPv6 address did not start with `['\n"));
1220  return GNUNET_SYSERR;
1221  }
1222  zbuf[addrlen] = '\0';
1223  port_colon = strrchr (zbuf, ':');
1224  if (NULL == port_colon)
1225  {
1227  _ ("IPv6 address did contain ':' to separate port number\n"));
1228  return GNUNET_SYSERR;
1229  }
1230  if (']' != *(port_colon - 1))
1231  {
1232  GNUNET_log (
1234  _ ("IPv6 address did contain ']' before ':' to separate port number\n"));
1235  return GNUNET_SYSERR;
1236  }
1237  ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1238  if ((1 != ret) || (port > 65535))
1239  {
1240  GNUNET_log (
1242  _ ("IPv6 address did contain a valid port number after the last ':'\n"));
1243  return GNUNET_SYSERR;
1244  }
1245  *(port_colon - 1) = '\0';
1246  memset (r_buf, 0, sizeof(struct sockaddr_in6));
1247  ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1248  if (ret <= 0)
1249  {
1251  _ ("Invalid IPv6 address `%s': %s\n"),
1252  &zbuf[1],
1253  strerror (errno));
1254  return GNUNET_SYSERR;
1255  }
1256  r_buf->sin6_port = htons (port);
1257  r_buf->sin6_family = AF_INET6;
1258 #if HAVE_SOCKADDR_IN_SIN_LEN
1259  r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1260 #endif
1261  return GNUNET_OK;
1262 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:81
#define GNUNET_log(kind,...)
Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_address_ipv4()

int GNUNET_STRINGS_to_address_ipv4 ( const char *  zt_addr,
uint16_t  addrlen,
struct sockaddr_in *  r_buf 
)

Tries to convert zt_addr string to an IPv4 address.

The string is expected to have the format "1.2.3.4:80".

Parameters
zt_addr0-terminated string. May be mangled by the function.
addrlenlength of zt_addr (not counting 0-terminator).
r_bufa buffer to fill.
Returns
GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Tries to convert zt_addr string to an IPv4 address.

The string is expected to have the format "1.2.3.4:80".

Parameters
zt_addr0-terminated string. May be mangled by the function.
addrlenlength of zt_addr (not counting 0-terminator).
r_bufa buffer to fill.
Returns
GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1277 of file strings.c.

References dummy, GNUNET_OK, GNUNET_SYSERR, and port.

Referenced by GNUNET_STRINGS_parse_socket_addr(), and GNUNET_STRINGS_to_address_ip().

1280 {
1281  unsigned int temps[4];
1282  unsigned int port;
1283  unsigned int cnt;
1284  char dummy[2];
1285 
1286  if (addrlen < 9)
1287  return GNUNET_SYSERR;
1288  cnt = sscanf (zt_addr,
1289  "%u.%u.%u.%u:%u%1s",
1290  &temps[0],
1291  &temps[1],
1292  &temps[2],
1293  &temps[3],
1294  &port,
1295  dummy);
1296  if (5 != cnt)
1297  return GNUNET_SYSERR;
1298  for (cnt = 0; cnt < 4; cnt++)
1299  if (temps[cnt] > 0xFF)
1300  return GNUNET_SYSERR;
1301  if (port > 65535)
1302  return GNUNET_SYSERR;
1303  r_buf->sin_family = AF_INET;
1304  r_buf->sin_port = htons (port);
1305  r_buf->sin_addr.s_addr =
1306  htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1307 #if HAVE_SOCKADDR_IN_SIN_LEN
1308  r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1309 #endif
1310  return GNUNET_OK;
1311 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:81
Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_socket_addr()

size_t GNUNET_STRINGS_parse_socket_addr ( const char *  addr,
uint8_t *  af,
struct sockaddr **  sa 
)

Parse an address given as a string into a struct sockaddr.

Parameters
addrthe address
[out]afset to the parsed address family (i.e. AF_INET)
[out]saset to the parsed address
Returns
0 on error, otherwise number of bytes in sa

Definition at line 1350 of file strings.c.

References GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_strdup, GNUNET_STRINGS_to_address_ipv4(), and GNUNET_STRINGS_to_address_ipv6().

Referenced by run().

1353 {
1354  char *cp = GNUNET_strdup (addr);
1355 
1356  *af = AF_UNSPEC;
1357  if ('[' == *addr)
1358  {
1359  /* IPv6 */
1360  *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1361  if (GNUNET_OK !=
1363  strlen (cp),
1364  (struct sockaddr_in6 *) *sa))
1365  {
1366  GNUNET_free (*sa);
1367  *sa = NULL;
1368  GNUNET_free (cp);
1369  return 0;
1370  }
1371  *af = AF_INET6;
1372  GNUNET_free (cp);
1373  return sizeof(struct sockaddr_in6);
1374  }
1375  else
1376  {
1377  /* IPv4 */
1378  *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1379  if (GNUNET_OK !=
1381  strlen (cp),
1382  (struct sockaddr_in *) *sa))
1383  {
1384  GNUNET_free (*sa);
1385  *sa = NULL;
1386  GNUNET_free (cp);
1387  return 0;
1388  }
1389  *af = AF_INET;
1390  GNUNET_free (cp);
1391  return sizeof(struct sockaddr_in);
1392  }
1393 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
int GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, uint16_t addrlen, struct sockaddr_in *r_buf)
Tries to convert &#39;zt_addr&#39; string to an IPv4 address.
Definition: strings.c:1277
int GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, uint16_t addrlen, struct sockaddr_in6 *r_buf)
Tries to convert zt_addr string to an IPv6 address.
Definition: strings.c:1203
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_address_ip()

int GNUNET_STRINGS_to_address_ip ( const char *  addr,
uint16_t  addrlen,
struct sockaddr_storage *  r_buf 
)

Tries to convert addr string to an IP (v4 or v6) address.

Will automatically decide whether to treat 'addr' as v4 or v6 address.

Parameters
addra string, may not be 0-terminated.
addrlennumber of bytes in addr (if addr is 0-terminated, 0-terminator should not be counted towards addrlen).
r_bufa buffer to fill.
Returns
GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Will automatically decide whether to treat 'addr' as v4 or v6 address.

Parameters
addra string, may not be 0-terminated.
addrlennumber of bytes in addr (if addr is 0-terminated, 0-terminator should not be counted towards addrlen).
r_bufa buffer to fill.
Returns
GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1326 of file strings.c.

References GNUNET_STRINGS_to_address_ipv4(), and GNUNET_STRINGS_to_address_ipv6().

Referenced by http_common_socket_from_address(), tcp_plugin_string_to_address(), and udp_string_to_address().

1329 {
1330  if (addr[0] == '[')
1331  return GNUNET_STRINGS_to_address_ipv6 (addr,
1332  addrlen,
1333  (struct sockaddr_in6 *) r_buf);
1334  return GNUNET_STRINGS_to_address_ipv4 (addr,
1335  addrlen,
1336  (struct sockaddr_in *) r_buf);
1337 }
int GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, uint16_t addrlen, struct sockaddr_in *r_buf)
Tries to convert &#39;zt_addr&#39; string to an IPv4 address.
Definition: strings.c:1277
int GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, uint16_t addrlen, struct sockaddr_in6 *r_buf)
Tries to convert zt_addr string to an IPv6 address.
Definition: strings.c:1203
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_get_utf8_args()

int GNUNET_STRINGS_get_utf8_args ( int  argc,
char *const *  argv,
int *  u8argc,
char *const **  u8argv 
)

Returns utf-8 encoded arguments.

Does nothing (returns a copy of argc and argv) on any platform other than W32. Returned argv has u8argv[u8argc] == NULL. Returned argv is a single memory block, and can be freed with a single GNUNET_free() call.

Parameters
argcargc (as given by main())
argvargv (as given by main())
u8argca location to store new argc in (though it's th same as argc)
u8argva location to store new argv in
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Does nothing (returns a copy of argc and argv) on any platform other than W32. Returned argv has u8argv[u8argc] == NULL. Returned argv is a single memory block, and can be freed with a single GNUNET_free() call.

Parameters
argcargc (as given by main())
argvargv (as given by main())
u8argca location to store new argc in (though it's th same as argc)
u8argva location to store new argv in
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Definition at line 1438 of file strings.c.

References _make_continuous_arg_copy(), and GNUNET_OK.

Referenced by main().

1442 {
1443  char *const *new_argv =
1444  (char *const *) _make_continuous_arg_copy (argc, argv);
1445  *u8argv = new_argv;
1446  *u8argc = argc;
1447  return GNUNET_OK;
1448 }
static char *const * _make_continuous_arg_copy(int argc, char *const *argv)
Makes a copy of argv that consists of a single memory chunk that can be freed with a single call to G...
Definition: strings.c:1401
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_strlcpy()

size_t GNUNET_strlcpy ( char *  dst,
const char *  src,
size_t  n 
)

Like strlcpy but portable.

The given string src is copied in full length (until its null byte). The destination buffer is guaranteed to be null-terminated.

to a destination buffer and ensures that the destination string is null-terminated.

Parameters
dstdestination of the copy
srcsource of the copy, must be null-terminated
nthe length of the string to copy, including its terminating null byte
Returns
the length of the string that was copied, excluding the terminating null byte

The given string src is copied until its null byte or until n - 1 bytes have been read. The destination buffer is guaranteed to be null-terminated.

Parameters
dstdestination of the copy (must be n bytes long)
srcsource of the copy (at most n - 1 bytes will be read)
nthe length of the string to copy, including its terminating null byte
Returns
the length of the string that was copied, excluding the terminating null byte

Definition at line 219 of file strings.c.

References GNUNET_assert, and strnlen().

Referenced by add_unixpath(), create_keys(), GCP_2s(), GNUNET_CONNECTION_create_from_connect_to_unixpath(), GNUNET_i2s(), GNUNET_i2s2(), GNUNET_TESTBED_hosts_load_from_file(), handle_add_host(), handle_remote_overlay_connect(), mylog(), regex_split(), tokenizer_cb(), and try_unixpath().

220 {
221  size_t slen;
222 
223  GNUNET_assert (0 != n);
224  slen = strnlen (src, n - 1);
225  memcpy (dst, src, slen);
226  dst[slen] = '\0';
227  return slen;
228 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static size_t strnlen(const char *s, size_t n)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_ipv4_policy()

struct GNUNET_STRINGS_IPv4NetworkPolicy* GNUNET_STRINGS_parse_ipv4_policy ( const char *  routeListX)

Parse an IPv4 network policy.

The argument specifies a list of subnets. The format is (network[/netmask][:[!]SPORT-DPORT];)* (no whitespace, must be terminated with a semicolon). The network must be given in dotted-decimal notation. The netmask can be given in CIDR notation (/16) or in dotted-decimal (/255.255.0.0).

Parameters
routeListXa string specifying the IPv4 subnets
Returns
the converted list, terminated with all zeros; NULL if the synatx is flawed

The argument specifies a list of subnets. The format is (network[/netmask][:SPORT[-DPORT]];)* (no whitespace, must be terminated with a semicolon). The network must be given in dotted-decimal notation. The netmask can be given in CIDR notation (/16) or in dotted-decimal (/255.255.0.0).

Parameters
routeListXa string specifying the IPv4 subnets
Returns
the converted list, terminated with all zeros; NULL if the synatx is flawed

Definition at line 1518 of file strings.c.

References _, dummy, end, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_strdup, len, LOG, GNUNET_STRINGS_IPv4NetworkPolicy::netmask, GNUNET_STRINGS_IPv4NetworkPolicy::network, parse_port_policy(), GNUNET_STRINGS_IPv4NetworkPolicy::pp, and result.

Referenced by GNUNET_TUN_ipv4policy2regex(), and process_acl4().

1519 {
1520  unsigned int count;
1521  unsigned int i;
1522  unsigned int j;
1523  unsigned int len;
1524  int cnt;
1525  unsigned int pos;
1526  unsigned int temps[8];
1527  int slash;
1529  int colon;
1530  int end;
1531  char *routeList;
1532  char dummy[2];
1533 
1534  if (NULL == routeListX)
1535  return NULL;
1536  len = strlen (routeListX);
1537  if (0 == len)
1538  return NULL;
1539  routeList = GNUNET_strdup (routeListX);
1540  count = 0;
1541  for (i = 0; i < len; i++)
1542  if (routeList[i] == ';')
1543  count++;
1544  result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv4NetworkPolicy)
1545  * (count + 1));
1546  i = 0;
1547  pos = 0;
1548  while (i < count)
1549  {
1550  for (colon = pos; ':' != routeList[colon]; colon++)
1551  if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1552  break;
1553  for (end = colon; ';' != routeList[end]; end++)
1554  if ('\0' == routeList[end])
1555  break;
1556  if ('\0' == routeList[end])
1557  break;
1558  routeList[end] = '\0';
1559  if (':' == routeList[colon])
1560  {
1561  routeList[colon] = '\0';
1562  if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1563  break;
1564  }
1565  cnt = sscanf (&routeList[pos],
1566  "%u.%u.%u.%u/%u.%u.%u.%u%1s",
1567  &temps[0],
1568  &temps[1],
1569  &temps[2],
1570  &temps[3],
1571  &temps[4],
1572  &temps[5],
1573  &temps[6],
1574  &temps[7],
1575  dummy);
1576  if (8 == cnt)
1577  {
1578  for (j = 0; j < 8; j++)
1579  if (temps[j] > 0xFF)
1580  {
1582  _ ("Invalid format for IP: `%s'\n"),
1583  &routeList[pos]);
1584  GNUNET_free (result);
1585  GNUNET_free (routeList);
1586  return NULL;
1587  }
1588  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1589  + (temps[2] << 8) + temps[3]);
1590  result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1591  + (temps[6] << 8) + temps[7]);
1592  pos = end + 1;
1593  i++;
1594  continue;
1595  }
1596  /* try second notation */
1597  cnt = sscanf (&routeList[pos],
1598  "%u.%u.%u.%u/%u%1s",
1599  &temps[0],
1600  &temps[1],
1601  &temps[2],
1602  &temps[3],
1603  &slash,
1604  dummy);
1605  if (5 == cnt)
1606  {
1607  for (j = 0; j < 4; j++)
1608  if (temps[j] > 0xFF)
1609  {
1611  _ ("Invalid format for IP: `%s'\n"),
1612  &routeList[pos]);
1613  GNUNET_free (result);
1614  GNUNET_free (routeList);
1615  return NULL;
1616  }
1617  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1618  + (temps[2] << 8) + temps[3]);
1619  if ((slash <= 32) && (slash >= 0))
1620  {
1621  result[i].netmask.s_addr = 0;
1622  while (slash > 0)
1623  {
1624  result[i].netmask.s_addr =
1625  (result[i].netmask.s_addr >> 1) + 0x80000000;
1626  slash--;
1627  }
1628  result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1629  pos = end + 1;
1630  i++;
1631  continue;
1632  }
1633  else
1634  {
1636  _ ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1637  slash);
1638  GNUNET_free (result);
1639  GNUNET_free (routeList);
1640  return NULL; /* error */
1641  }
1642  }
1643  /* try third notation */
1644  slash = 32;
1645  cnt = sscanf (&routeList[pos],
1646  "%u.%u.%u.%u%1s",
1647  &temps[0],
1648  &temps[1],
1649  &temps[2],
1650  &temps[3],
1651  dummy);
1652  if (4 == cnt)
1653  {
1654  for (j = 0; j < 4; j++)
1655  if (temps[j] > 0xFF)
1656  {
1658  _ ("Invalid format for IP: `%s'\n"),
1659  &routeList[pos]);
1660  GNUNET_free (result);
1661  GNUNET_free (routeList);
1662  return NULL;
1663  }
1664  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1665  + (temps[2] << 8) + temps[3]);
1666  result[i].netmask.s_addr = 0;
1667  while (slash > 0)
1668  {
1669  result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
1670  slash--;
1671  }
1672  result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1673  pos = end + 1;
1674  i++;
1675  continue;
1676  }
1678  _ ("Invalid format for IP: `%s'\n"),
1679  &routeList[pos]);
1680  GNUNET_free (result);
1681  GNUNET_free (routeList);
1682  return NULL; /* error */
1683  }
1684  if (pos < strlen (routeList))
1685  {
1687  _ ("Invalid format: `%s'\n"),
1688  &routeListX[pos]);
1689  GNUNET_free (result);
1690  GNUNET_free (routeList);
1691  return NULL; /* oops */
1692  }
1693  GNUNET_free (routeList);
1694  return result; /* ok */
1695 }
static int parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1461
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
struct in_addr netmask
IPv4 netmask.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
struct in_addr network
IPv4 address.
IPV4 network in CIDR notation.
static int result
Global testing status.
#define LOG(kind,...)
Definition: strings.c:37
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_ipv6_policy()

struct GNUNET_STRINGS_IPv6NetworkPolicy* GNUNET_STRINGS_parse_ipv6_policy ( const char *  routeListX)

Parse an IPv6 network policy.

The argument specifies a list of subnets. The format is (network[/netmask[:[!]SPORT[-DPORT]]];)* (no whitespace, must be terminated with a semicolon). The network must be given in colon-hex notation. The netmask must be given in CIDR notation (/16) or can be omitted to specify a single host. Note that the netmask is mandatory if ports are specified.

Parameters
routeListXa string specifying the policy
Returns
the converted list, 0-terminated, NULL if the synatx is flawed

The argument specifies a list of subnets. The format is (network[/netmask[:SPORT[-DPORT]]];)* (no whitespace, must be terminated with a semicolon). The network must be given in colon-hex notation. The netmask must be given in CIDR notation (/16) or can be omitted to specify a single host. Note that the netmask is mandatory if ports are specified.

Parameters
routeListXa string specifying the policy
Returns
the converted list, 0-terminated, NULL if the synatx is flawed

Definition at line 1710 of file strings.c.

References _, dummy, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_strdup, len, LOG, LOG_STRERROR, GNUNET_STRINGS_IPv6NetworkPolicy::netmask, GNUNET_STRINGS_IPv6NetworkPolicy::network, parse_port_policy(), GNUNET_STRINGS_IPv6NetworkPolicy::pp, result, ret, save(), and start.

Referenced by GNUNET_TUN_ipv6policy2regex(), and process_acl6().

1711 {
1712  unsigned int count;
1713  unsigned int i;
1714  unsigned int len;
1715  unsigned int pos;
1716  int start;
1717  int slash;
1718  int ret;
1719  char *routeList;
1721  unsigned int bits;
1722  unsigned int off;
1723  int save;
1724  int colon;
1725  char dummy[2];
1726 
1727  if (NULL == routeListX)
1728  return NULL;
1729  len = strlen (routeListX);
1730  if (0 == len)
1731  return NULL;
1732  routeList = GNUNET_strdup (routeListX);
1733  count = 0;
1734  for (i = 0; i < len; i++)
1735  if (';' == routeList[i])
1736  count++;
1737  if (';' != routeList[len - 1])
1738  {
1740  _ ("Invalid network notation (does not end with ';': `%s')\n"),
1741  routeList);
1742  GNUNET_free (routeList);
1743  return NULL;
1744  }
1745 
1746  result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy)
1747  * (count + 1));
1748  i = 0;
1749  pos = 0;
1750  while (i < count)
1751  {
1752  start = pos;
1753  while (';' != routeList[pos])
1754  pos++;
1755  slash = pos;
1756  while ((slash >= start) && (routeList[slash] != '/'))
1757  slash--;
1758 
1759  if (slash < start)
1760  {
1761  memset (&result[i].netmask, 0xFF, sizeof(struct in6_addr));
1762  slash = pos;
1763  }
1764  else
1765  {
1766  routeList[pos] = '\0';
1767  for (colon = pos; ':' != routeList[colon]; colon--)
1768  if ('/' == routeList[colon])
1769  break;
1770  if (':' == routeList[colon])
1771  {
1772  routeList[colon] = '\0';
1773  if (GNUNET_OK !=
1774  parse_port_policy (&routeList[colon + 1], &result[i].pp))
1775  {
1776  GNUNET_free (result);
1777  GNUNET_free (routeList);
1778  return NULL;
1779  }
1780  }
1781  ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
1782  if (ret <= 0)
1783  {
1784  save = errno;
1785  if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) ||
1786  (bits > 128))
1787  {
1788  if (0 == ret)
1790  _ ("Wrong format `%s' for netmask\n"),
1791  &routeList[slash + 1]);
1792  else
1793  {
1794  errno = save;
1795  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton");
1796  }
1797  GNUNET_free (result);
1798  GNUNET_free (routeList);
1799  return NULL;
1800  }
1801  off = 0;
1802  while (bits > 8)
1803  {
1804  result[i].netmask.s6_addr[off++] = 0xFF;
1805  bits -= 8;
1806  }
1807  while (bits > 0)
1808  {
1809  result[i].netmask.s6_addr[off] =
1810  (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1811  bits--;
1812  }
1813  }
1814  }
1815  routeList[slash] = '\0';
1816  ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
1817  if (ret <= 0)
1818  {
1819  if (0 == ret)
1821  _ ("Wrong format `%s' for network\n"),
1822  &routeList[slash + 1]);
1823  else
1824  LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
1825  GNUNET_free (result);
1826  GNUNET_free (routeList);
1827  return NULL;
1828  }
1829  pos++;
1830  i++;
1831  }
1832  GNUNET_free (routeList);
1833  return result;
1834 }
static int parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1461
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
network in CIDR notation for IPV6.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static void save()
Write persistent statistics to disk.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct in6_addr netmask
IPv6 netmask.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static int result
Global testing status.
#define LOG(kind,...)
Definition: strings.c:37
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:39
struct in6_addr network
IPv6 address.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function: