Commit Graph

49 Commits

Author SHA1 Message Date
Thomas Haller
c3808550eb shared: change nm_utils_strbuf_seek_end() handling truncated strings
Ok, I changed my mind.

The new behavior seems to make more sense to me. Not that it matters,
because we always use nm_utils_strbuf*() API with buffers that we expect
to be large enough to contain the result. And when truncation occurs,
we usually don't care much about it. That is, there is no code that
uses nm_utils_strbuf*() API and handles string truncation in particular.
2018-09-07 18:13:10 +02:00
Thomas Haller
0a8248af10 shared: add nm_utils_strbuf_seek_end() helper 2018-09-07 11:24:17 +02:00
Thomas Haller
f38cbce653 shared: add nm_utils_gbytes_equal_mem() util 2018-08-30 11:17:09 +02:00
Thomas Haller
57c371e32f shared: add nm_utils_buf_utf8safe_escape() util
We already have nm_utils_str_utf8safe_escape() to convert a
NUL termianted string to an UTF-8 string. nm_utils_str_utf8safe_escape()
operates under the assumption, that the input strig is already valid UTF-8
and returns the input string verbatim. That way, in the common expected
cases, the string just looks like a regular UTF-8 string.
However, in case there are invalid UTF-8 sequences (or a backslash
escape characters), the function will use backslash escaping to encode
the input string as a valid UTF-8 sequence. Note that the escaped
sequence, can be reverted to the original non-UTF-8 string via
unescape.
An example, where this is useful are file names or interface names.
Which are not in a defined encoding, but NUL terminated and commonly ASCII or
UTF-8 encoded.

Extend this, to also handle not NUL terminated buffers. The same
applies, except that the process cannot be reverted via g_strcompress()
-- because the NUL character cannot be unescaped.

This will be useful to escape a Wi-Fi SSID. Commonly we expect the SSID
to be in UTF-8/ASCII encoding and we want to print it verbatim. Only
if that is not the case, we fallback to backslash escaping. However, the
orginal value can be fully recovered via unescape(). The difference
between an SSID and a filename is, that the former can contain '\0'
bytes.
2018-08-22 10:49:34 +02:00
Thomas Haller
c06a55958b shared: add nm_utils_gbytes_to_variant_ay() util 2018-08-22 10:49:30 +02:00
Beniamino Galvani
6a51d393b2 shared: add @allow_escaping argument to @nm_utils_strsplit_set 2018-08-11 09:41:07 +02:00
Beniamino Galvani
e0bbaf6a39 shared: add space escape functions 2018-08-11 09:41:07 +02:00
Thomas Haller
a587d32467 shared: move nm_utils_ptrarray_find_binary_search() to shared utils 2018-08-10 10:38:19 +02:00
Thomas Haller
d32da2daaa shared: move nm_utils_array_find_binary_search() to shared utils 2018-08-10 10:38:19 +02:00
Thomas Haller
b5bdfdc773 shared: add nm_utils_hash_table_equal() util
Add utility function to compare the content of two
hash tables for equality.
2018-08-10 10:38:19 +02:00
Thomas Haller
e1c7a2b5d0 all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.

    $ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
    587
    $ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
    21114

One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during

  g_object_set (obj, PROPERTY, (gint) value, NULL);

However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.

Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).

A simple style guide is instead: don't use these typedefs.

No manual actions, I only ran the bash script:

  FILES=($(git ls-files '*.[hc]'))
  sed -i \
      -e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
      -e 's/\<g\(char\|short\|int\|long\|float\|double\)\>  /\1   /g' \
      -e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
      "${FILES[@]}"
2018-07-11 12:02:06 +02:00
Thomas Haller
f11bb3d93d shared: minor cleanup of nm_utils_get_start_time_for_pid() 2018-05-26 20:11:04 +02:00
Thomas Haller
7ba3f4f35b shared: add nm_utils_dbus_path_cmp() helper
At various places we sort our D-Bus paths. For example,
server sorts them before exporting them on D-Bus.

Server knows well, that a lot of these paths are build
by attaching an incrementing number as last component.
It looks nicer to sort by this number, instead of strictly
lexical with strcmp().

Note that this handles the cases correctly where paths have
different prefixes, or where they don't end with a number.
2018-05-14 17:53:03 +02:00
Thomas Haller
3b0d50475b shared: add nm_utils_dbus_path_get_last_component() helper 2018-05-14 17:53:03 +02:00
Thomas Haller
dc138da420 shared: add nm_utils_user_data_pack() and unpack helpers 2018-04-24 09:03:39 +02:00
Thomas Haller
bc1b15cf05 shared: move cmp functions to nm-shared-utils.c
For one, these functions are not often needed. No need to define them in the
"nm-macros-internal.h" header, which is included everywhere. Move them to
"nm-shared-utils.h", which must be explicitly included.

Also, these functions are usually not called directly, but by passing their
function pointer to a sort function or similar. There is no point in having
defined in the header file.
2018-04-19 09:36:41 +02:00
Thomas Haller
be9a5ab308 shared: add nm_utils_strv_sort() helper 2018-04-18 07:55:15 +02:00
Thomas Haller
e5e8f86c3d shared: move nm_utils_get_start_time_for_pid() to shared/nm-utils
We will also use it in nmcli later. It will be needed when we replace
polkit_unix_process_new_for_owner(). Which is still far down the road.
2018-04-16 16:03:14 +02:00
Thomas Haller
938d9a82cf shared: add nm_utils_hash_keys_to_array() helper 2018-03-27 09:58:00 +02:00
Thomas Haller
d5c212b145 shared: add nm_utils_strv_make_deep_copied() helper
At several places we create strv arrays where the
strings themself are not deep-copied.

This helper function iterates over such an "const char **"
array, clones the strings, and updates the strv array
inplace to be a "char **" strv array.

This helper function is to reduce code duplication.
2017-12-18 12:10:11 +01:00
Thomas Haller
fe7b4641d6 shared: add nm_utils_strdict_get_keys() helper
At various places we get the (string) keys of a GHashTable.
Add a helper function that does that, including an argument
for optional sorting.

The helper function is there to get reduce code duplication.
2017-12-18 11:56:54 +01:00
Thomas Haller
097bd72e2e shared: add nm_utils_named_values_from_str_dict()
NMUtilsNamedValue's purpose is precisely to create
a list and sort by entires.

Add nm_utils_named_values_from_str_dict() as helper
function to do that.
2017-12-08 18:47:37 +01:00
Thomas Haller
de92e88161 shared: fix nm_utils_strsplit_set() to increase buffer size exponentially 2017-11-29 16:26:28 +01:00
Thomas Haller
5befde7d7d shared: add nm_g_object_set_property_*() helper 2017-11-23 18:43:48 +01:00
Francesco Giudici
85cc5db7a4 shared: trivial: add space before comment close 2017-11-09 17:17:00 +01:00
Thomas Haller
281d2d9fad shared: split random and hash utils
"nm-utils/nm-shared-utils.h" shall contain utility function without other
dependencies. It is intended to be used by other projects as-is.

nm_utils_random_bytes() requires getrandom() and a HAVE_GETRANDOM configure
check. That makes it more cumbersome to re-use "nm-shared-utils.h", in
cases where you don't care about nm_utils_random_bytes().

Split nm_utils_random_bytes() out to a separate file.

Same for hash utils, which depend on nm_utils_random_bytes(). Also, hash
utils will eventually be extended to use siphash24.
2017-10-17 20:02:59 +02:00
Thomas Haller
c978b9dfe5 core: randomize hash seed with a global seed
This makes hashing non-deterministic with the aim to
make it harder to exploit hash collisions.

Non-deterministic also means that for unit testing
we will get different values on each run. But since we
shall never assign any meaning to these hash values
nor rely on them being stable between restarts (or
upgrades), that doesn't hurt.
2017-10-13 12:47:55 +02:00
Thomas Haller
b01a453ca2 core: add nm_utils_random_bytes() and use getrandom()
Add a new function nm_utils_random_bytes().

This function now preferably uses getrandom() syscall if it is
available.

As fallback, it always tries to fill the buffer from /dev/urandom.
If it cannot, as last fallback it uses GRand, which cannot fail.
Hence, the function always sets some (pseudo) random bytes.

It also returns FALSE if the obtained bytes are possibly not good
randomness.
2017-10-13 12:47:55 +02:00
Thomas Haller
93ea7a5905 shared: move nm_utils_fd_*() from src/ to shared/nm-utils/
The functions are general purpose and independent from NetworkManager core.
Move them to "shared/nm-utils/" so they can be used independently.
2017-10-13 12:47:55 +02:00
Beniamino Galvani
d554f535af shared: fix gtk-doc build warning
Fixes the following:

 shared/nm-utils/nm-shared-utils.c:136: Warning: NetworkManager: GTK-Doc comment block end token "*/" should not be preceded by comment text:
 * Returns: the input buffer with the quoted string. */
2017-10-13 12:10:02 +02:00
Thomas Haller
a31f1706e5 shared: guarantee output argument of nm_utils_parse_inaddr_bin() is only set on success
The documentation of inet_pton() is not clear about what happens
when parsing fails. Guarantee that the output is only modified
on success.
2017-10-06 11:08:39 +02:00
Thomas Haller
a0aec7efea shared: pass addr_family as first argument to nm_utils_parse_inaddr*()
The addr_family should be the first argument. It mirrors inet_pton()
and is just nicer.

Also, rename the argument from "family" to "addr_family".
2017-10-06 11:08:39 +02:00
Thomas Haller
f1009bcde3 shared: add nm_strquote() util
We already have nm_strquote_a(). That is useful, but uses alloca(), hence it
is ill suited to be called from a macro, inside a loop, or from a function
that should be inlined.

Instead, add nm_strquote() that has the same purpose but writes to a provided
string buffer.
2017-10-06 11:08:39 +02:00
Thomas Haller
1348b7c83a shared: move utils NM_UTILS_ENUM2STR*, NM_UTILS_FLAGS2STR*, and nm_utils_to_string_buffer_init()
These are just simple utility functions, not tied to NetworkManager
core. Move them to shared/nm-utils, so that they may be reused
otherwise.
2017-10-05 11:50:31 +02:00
Thomas Haller
daa4604c12 shared: add nm_utils_strsplit_set() helper
A replacement for g_strsplit_set(). While g_strsplit_set()
does (n+1) malloc and n slice allocations, this needs
roughtly (O(log(n))) mallocs.

Another difference from g_strsplit_set() is that this function
treats multiple delimiters as one (and thus never returns empty
words). While I can see that sometimes you may want to keep empty
words (like parsing a CSV file and preserve empty cells), we usually
use this function for splitting user input. In such case, we want
to treat multiple delimiters as one.
2017-09-18 20:14:09 +02:00
Thomas Haller
f6a727685e shared: add nm_utils_parse_inaddr_prefix_bin() helper 2017-09-18 20:14:09 +02:00
Thomas Haller
dd0617547d shared: add NMIPAddr struct 2017-09-18 20:14:09 +02:00
Thomas Haller
19716df23d shared: add nm_g_object_class_find_property_from_gtype() util
And relax the type for nm_auto_unref_gtypeclass macro. Like
g_type_class_unref() itself, you usually don't use it with a GTypeClass
base class, but some subtype like GObjectClass.
2017-09-07 18:10:06 +02:00
Thomas Haller
506fca65b3 tui: guess the prefix length (netmask) of private IPv4 addresses and routes based on network class
For RFC1918 private IPv4addresses, guess a better prefix length for
addresses and routes.

nmtui is an interactive program. It makes sense to be a bit smarter
about what the user probably meant.

It would be nice if nmtui would update the entry field immediately when
the cursor leaves the field, to show the guessed prefix length. However,
that is not easily possible, so lets to that another time.

For IPv6 addresses, default to /64 instead of /128.

https://bugzilla.redhat.com/show_bug.cgi?id=1474295
2017-09-05 18:44:04 +02:00
Thomas Haller
b434d7d436 shared: add nm_utils_ip_is_site_local() 2017-09-05 18:44:04 +02:00
Thomas Haller
7319fdd74a shared: add _nm_utils_ip4_*() utils functions
(cherry picked from commit e9c0b1851b88cef8a0e74d9e8701e0ef548e092a)
2017-09-05 16:19:57 +02:00
Thomas Haller
df6d27b33a shared: add nm_utils_str_utf8safe_*() API to sanitize UTF-8 strings
Use C-style backslash escaping to sanitize non-UTF-8 strings.
The functions are compatible with glib's g_strcompress() and
g_strescape().

The difference is only that g_strescape() escapes all non-printable,
non ASCII character as well, while nm_utils_str_utf8safe_escape()
-- depending on the flags -- preserves valid UTF-8 sequence except
backslash.

The flags allow to optionally escape ASCII control characters and
all non-ASCII (valid UTF-8) characters. But the option to preserve
valid UTF-8 (non-ASCII) characters verbatim, is what distinguishes
from g_strescape().
2017-05-19 09:46:08 +02:00
Thomas Haller
7861ce7560 shared: add NM_PTRARRAY_EMPTY() util 2017-04-12 11:24:03 +02:00
Thomas Haller
d1c6d64e6a shared: move _nm_utils_strv_cleanup() to shared utils 2017-04-05 16:53:06 +02:00
Thomas Haller
7d6b8bab70 all: use NM_CACHED_QUARK_FCN() instead of G_DEFINE_QUARK()
I think NM_CACHED_QUARK_FCN() is better because:

- the implementation is in our hand, meaning it is clear that
  putting a "static" before NM_CACHED_QUARK_FCN() is guaranteed to
  work -- without relying on G_DEFINE_QUARK() to be defined in a way
  that this works (in fact, we currently never do that and instead
  make all functions non-static).
- it does not construct function names by appending "_quark".
  Thus you can grep for the entire function name and finding
  the place where it is implemented.
- same with the stings, where the new macro doesn't stringify the
  argument, which is less surpising. Again, now you can grep
  for the string including the double quoting.

(yes, I really use grep to understand the source-code)
2017-02-10 14:33:52 +01:00
Thomas Haller
a165907554 shared: add nm_utils_strv_find_first() helper
Make _nm_utils_strv_find_first() accessible outside of libnm-core
by copying it from "libnm-core/nm-core-internal.h" (and rename it).
2017-02-04 17:54:49 +01:00
Thomas Haller
f779c51f87 shared: move nm_utils_strbuf_*() helper to shared/nm-utils 2017-01-05 11:13:17 +01:00
Thomas Haller
5789bfa5d9 shared: minor cleanup in _nm_utils_ascii_str_to_int64() using const pointer 2016-11-02 12:04:49 +01:00
Thomas Haller
4b288136e1 shared: move shared files to subdirectory "shared/nm-utils/"
The "shared" directory contains files that are possibly used by all components
of NetworkManager repository.

Some of these files are even copied as-is to other projects (VPN plugins, nm-applet)
and used there without modification. Move those files to a separate directory.
By moving them to a common directory, it is clearer that they belong
together. Also, you can easier compare the copied versions to their
original via

  $ diff -r ./shared/nm-utils/ /path/to/nm-vpn-plugin/shared/nm-utils/
2016-06-16 10:45:53 +02:00