Commit Graph

103 Commits

Author SHA1 Message Date
Thomas Haller
acf1cf61cf shared: add _nm_utils_strv_cmp_n() and _nm_utils_strv_equal() 2019-03-24 09:21:13 +01:00
Thomas Haller
53b747fff5 all: move nm_utils_hexstr2bin*() to shared
libnm exposes simplified variants of hexstr2bin in its public API. I
think that was a mistake, because libnm should provide NetworkManager
specific utils. It should not provide such string functions.

However, nmcli used to need this, so it was added to libnm.

The better approach is to add it to our internally shared static
library, so that all interested components can make use of it.
2019-02-22 14:04:13 +01:00
Thomas Haller
98e6abb5a0 shared: add nm_ip_addr_set_from_untrusted() helper
Will be used later. The point is to set an IP address from
unvalidated/untrusted input (that is, the data length might
not match the address-family).

Will be used later when parsing netlink attributes.
2019-02-22 09:58:09 +01:00
Beniamino Galvani
b5efcf08f4 all: move nm_utils_bin2hexstr_full() to shared
reuse++
2019-02-21 09:36:17 +01:00
Thomas Haller
47123e493a shared: add nm_ip4_addr_is_localhost() util 2019-02-19 16:18:57 +01:00
Thomas Haller
c9244d28ae shared: add nm_g_type_find_implementing_class_for_property() helper
A helper method, only useful for printf debugging -- and thus
unused in the source-tree.

It is relatively cumbersome to lookup the GType that implements
a property. For example, for NMDeviceBond.driver, it should return
NMDevice (which implements the "driver" property).
2019-02-19 09:33:05 +01:00
Thomas Haller
d83d5f1da2 shared: use nm_strerror_native_r() in lower layers
Subsequent calls to nm_strerror_native() overwrite the previous
buffer. That is potentially dangerious. At least functions in
shared/nm-utils (which are lower-layer utilities) should not do
that and instead use a stack-local buffer. That is because these
functions should not make assumptions about the way they are called.

On the other end, nmcli passing the return-value of nm_strerror_native()
to g_print() is clearly OK because the higher layers are in control of
when the call nm_strerror_native() -- by relying that lower layers don't
interfere.
2019-02-12 08:50:28 +01:00
Thomas Haller
a4fb6ddfca all: replace g_strerror() calls with nm_strerror_native() 2019-02-12 08:50:28 +01:00
Thomas Haller
67130e6706 shared: cleanup separation and transition between errno and nmerr numbers
The native error numbers (from <errno.h>) and our nmerr extention on top
of them are almost the same. But there are peculiarities.

Both errno and nmerr must be positive values. That is because some API
(systemd) like to return negative error codes. So, a positive errno and
its negative counter part indicate the same error. We need normalization
functions that make an error number positive (these are nm_errno() and
nm_errno_native()).

This means, G_MININT needs special treatment, because it cannot be
represented as a positive integer. Also, zero needs special
treatment, because we want to encode an error, and zero already encodes
no-error. Take care of these special cases.

On top of that, nmerr reserves a range within native error numbers for
NetworkManager specific failure codes. So we need to transition from native
numbers to nmerr numbers via nm_errno_from_native().

Take better care of some special cases and clean them up.

Also add NM_ERRNO_NATIVE() macro. While nm_errno_native() coerces a
value in the suitable range, NM_ERRNO_NATIVE() asserts that the number
is already positive (and returns it as-is). It's use is only for
asserting and implicitly documenting the requirements we have on the
number passed to it.
2019-02-12 08:50:28 +01:00
Thomas Haller
61e76e97dc shared: add nm_utils_gettid() and NM_ASSERT_ON_MAIN_THREAD() 2019-02-05 08:18:07 +01:00
Rafael Fontenelle
d81e10942f all: fix misspellings
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/64
2019-01-24 17:19:44 +01:00
Thomas Haller
744e11dc0d shared: add "struct in_addr" union member to NMIPAddr struct
NMIPAddr is a union of IPv4 and IPv6 addresses.

A lot of our internal API handles IPv4 as in_addr_t / guint32 / be32_t
types, as such the union field "addr4" is just a plain number. Possibly
the internal API should be all refactored to prefer "struct in_addr"
instead, but that is yet to be done.

Anyway, at a few places we will need also access to the IPv4 address in form of
a `struct in_addr`. Add an alias for that.

I am not too happy about the resulting naming. It would be nicer to have

    struct in_addr  addr4;
    struct in6_addr addr6;
    in_addr_t       s_addr4;

but for now, don't do such renaming.
2019-01-22 16:30:23 +01:00
Thomas Haller
035c4ad45d shared: suppress -Wstringop-truncation warning in nm_strndup_a()
The compiler is too smart for nm_strndup_a(). The code is correct,
suppress "-Wstringop-truncation" warning.
2019-01-22 16:30:23 +01:00
Thomas Haller
e7e0100062 shared: fix generic selection of integers in nm_strdup_int()
This fixes a test error, which aims to convert "unsigned long int" type,
but the generic type may not have been covered.

Don't select based on the gint32-like typedefs, but on the basic C
integer types.

Fixes: 8c2d58b237

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/issues/108
2019-01-16 15:45:43 +01:00
Thomas Haller
8c2d58b237 shared/tests: add test for nm_strdup_int() macro 2019-01-15 09:52:01 +01:00
Thomas Haller
3263cab596 all: add static assertion for maximumg alloca() allocated buffer
Add a compile time check that the buffer that we allocate on the stack
is reasonably small.
2019-01-15 09:52:01 +01:00
Thomas Haller
694533f529 shared: add nm_utils_strbuf_append_bin() helper
Add a version of nm_utils_strbuf_append_*() that does not care
about NUL terminate strings, but accept any binary data. That makes
it useful for writing a binary buffer.
2019-01-14 16:40:39 +01:00
Thomas Haller
fce3243f12 shared/trivial: rename nm_utils_mem_all_zero() to nm_utils_memeqzero()
in systemd/systemd, systemd/casync, and rustyrussel/ccan (github) this
function is called "memeqzero()". Rename, to use a more popular name.
2019-01-09 16:46:41 +01:00
Thomas Haller
af67b4520f shared: refactor nm_utils_mem_all_zero() to use memcmp() 2019-01-09 16:46:41 +01:00
Thomas Haller
aab3e14883 shared: add nm_utils_getpagesize() and use it in netlink code
Since we already cached the result of getpagesize() in a static variable (at
two places), move the code to nm-shared-utils, so it is reusable.

Also, use sysconf() instead of getpagesize(), like suggested by `man
getpagesize`.
2019-01-09 16:46:41 +01:00
Thomas Haller
6ae04654f7 shared: avoid compiler warning in nm_strndup_a()
Using strncpy() in the macro directly can result in a compiler warning.
We don't want to replace this with memcpy(), because strncpy() aborts
on the first NUL and fills the rest with NUL. Since nm_strndup_a() is a
replacement for g_strndup(), we want to do that here as well.

    In file included from ../shared/nm-default.h:294,
                     from ../libnm-core/nm-utils.c:22:
    ../libnm-core/nm-utils.c: In function nm_sock_addr_endpoint_new:
    ../shared/nm-utils/nm-shared-utils.h:281:4: error: strncpy output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation]
        strncpy (_s, _str, _len); \
        ^~~~~~~~~~~~~~~~~~~~~~~~
    ../libnm-core/nm-utils.c:154:26: note: in expansion of macro nm_strndup_a
      host = _parse_endpoint (nm_strndup_a (200, endpoint, l_endpoint - 1, &host_clone),
                              ^~~~~~~~~~~~
    ../libnm-core/nm-utils.c:152:15: note: length computed here
      l_endpoint = strlen (endpoint) + 1;
                   ^~~~~~~~~~~~~~~~~
2019-01-09 16:46:41 +01:00
Thomas Haller
33bf73f252 shared: add typed nm_g_object_set_property*() helpers
Add helper wrappers around nm_g_object_set_property() that take a
native value, construct a GValue of the according type, and call
nm_g_object_set_property().
2019-01-07 10:09:10 +01:00
Thomas Haller
f9f022b659 shared: move nm_errno() function to nm-errno.h
No other changes (yet).
2018-12-27 21:33:59 +01:00
Thomas Haller
b7fc328a66 shared: add nm_utils_error_is_notfound() helper
Inspired by bolt's bolt_err_notfound() in "bolt-error.c".
2018-12-12 08:22:12 +01:00
Thomas Haller
e60f6ee6d6 shared: allow AF_UNSPEC for nm_utils_addr_family_to_char()
It just makes sense, that our to-char function can also handle AF_UNSPEC.
Unclear which character to return in this case, but "IPvX" seems suitable.
2018-12-11 09:23:47 +01:00
Thomas Haller
c54a2ed82f shared: add nm_strv_ptrarray_*() helpers
These are simple macros/functions that append a heap-allocated string to
a GPtrArray. It is intended for a GPtrArray which takes ownership of the
strings (meaning, it has a g_free() GDestroyNotify).
2018-12-03 12:28:45 +01:00
Thomas Haller
3746845204 shared: add nm_utils_checksum_get_digest*() helper
The GChecksum API is cumbersome to use.

For example, g_checksum_get_digest() requires a length input/output
argument. At the same time, GChecksum does not allow you to query its
checksum-type nor the desired digest-length. When you have a GChecksum
at hand, you must always know the digest-length you are going to use.
So, the length parameter is only good for asserting.

Add a macro to make that more convenient.

Benefits: it's less lines of code, and we always do all the asserts
that are due.
2018-11-13 18:30:03 +01:00
Thomas Haller
c0d292d255 shared: add _nm_utils_ascii_str_to_uint64() helper 2018-10-17 16:22:34 +02:00
Thomas Haller
bd21a63e2f shared: add nm_utils_invoke_on_idle() helper 2018-10-17 13:03:50 +02:00
Thomas Haller
72b4541771 libnm-core: fix int comparisons in team setting 2018-10-07 13:57:11 +02:00
Thomas Haller
ba491a6674 shared: add nm_strndup_a() helper 2018-10-04 10:58:50 +02:00
Thomas Haller
0b3197a3fd shared: let nm_utils_parse_inaddr_bin() return the detected address family
As we accept addr_family %AF_UNSPEC to detect the address family,
we also need to return it. Just returning the binary address without
the address family makes no sense.
2018-09-17 14:52:54 +02:00
Thomas Haller
99380fbcea shared: add nm_errno() and nm_utils_error_set_errno() helper
(cherry picked from commit 4186ddb58b)
2018-09-12 10:40:07 +02:00
Thomas Haller
0a8248af10 shared: add nm_utils_strbuf_seek_end() helper 2018-09-07 11:24:17 +02:00
Thomas Haller
0feeeaac63 shared: add nm_utils_mem_all_zero() 2018-09-07 11:24:17 +02:00
Thomas Haller
1fb8fbbc99 shared: add nm_memdup() as replacement for g_memdup()
I think g_memdup() is dangerous for integer overflow. There
is no need for accepting this danger, just use our own nm_memdup()
which does not have this flaw.
2018-09-07 11:24:17 +02:00
Thomas Haller
c5c0ffdfd0 shared: add nm_gbytes_equal0() helper
Like g_bytes_equal(), except that it accepts %NULL arguments.
2018-09-04 07:38:30 +02:00
Thomas Haller
4c4c85eab0 shared: add _NM_INT_NOT_NEGATIVE() helper 2018-09-04 07:38:30 +02:00
Thomas Haller
5bb8e2fa4d shared: add nm_utils_hexchar_to_int() 2018-09-04 07:38:30 +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
246b747540 shared: add special error codes to NM_UTILS_ERROR
Will be used next.
2018-07-24 09:39:09 +02:00
Thomas Haller
0ee782d03e shared: add nm_utils_error_set*() helper macros
Add helper macros to type less.
2018-07-24 09:39:09 +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