Commit Graph

182 Commits

Author SHA1 Message Date
Thomas Haller
f3146de41b libnm: avoid unnecessary copies accessing NMIPRoute's attributes
We want to support large number of routes. Reduce the number
of copies, by adding internal accessor functions.

Also, work around a complaint from coverity:

  46. NetworkManager-1.9.2/libnm-core/nm-utils.c:1987:
  dereference: Dereferencing a null pointer "names".
2017-10-30 14:12:41 +01:00
Thomas Haller
5f882e8e8f libnm: reject colon in nm_utils_is_valid_iface_name()
Since kernel commit a4176a9391868bfa87705bcd2e3b49e9b9dd2996 (net:
reject creation of netdev names with colons), kernel rejects any
colons in the interface name.

Since kernel could get away with tightening up the check, we can
too.

The user anyway can not choose arbitrary interface names, like
"all", "default", "bonding_masters" are all going to fail one
way or another.
2017-10-23 14:59:52 +02:00
Beniamino Galvani
0a7b08968d libnm-core: normalize "tx_hash" when comparing team config
teamd adds the "tx_hash" property for "lacp" and "loadbalance" runners
when not present. Do the same so that our original configuration
matches with the one reported by teamd.

https://bugzilla.redhat.com/show_bug.cgi?id=1497333
2017-10-19 14:31:07 +02:00
Thomas Haller
2f56de7492 all: add helper functions for nm_hash_update*()
By using a macro, we don't cast all the types to guint. Instead,
we use their native types directly. Hence, we don't need
nm_hash_update_uint64() nor nm_hash_update_ptr().
Also, for types smaller then guint like char, we save hashing
the all zero bytes.
2017-10-18 13:29:22 +02:00
Thomas Haller
0e9e35e309 all: refactor hashing by introducing NMHashState
The privious NM_HASH_* macros directly operated on a guint value
and were thus close to the actual implementation.

Replace them by adding a NMHashState struct and accessors to
update the hash state. This hides the implementation better
and would allow us to carry more state. For example, we could
switch to siphash24() transparently.

For now, we still do a form basically djb2 hashing, albeit with
differing start seed.

Also add nm_hash_str() and nm_str_hash():

- nm_hash_str() is our own string hashing implementation

- nm_str_hash() is our own string implementation, but with a
  GHashFunc signature, suitable to pass it to g_hash_table_new().
  Also, it has this name in order to remind you of g_str_hash(),
  which it is replacing.
2017-10-18 13:05:00 +02: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
4a2798434e core: introduce NM_HASH_INIT() to initialize hash seed
Introduce a NM_HASH_INIT() function. It makes the places
where we initialize a hash with a certain seed visually clear.

Also, move them from "shared/nm-utils/nm-shared-utils.h" to
"shared/nm-utils/nm-macros-internal.h". We might want to
have NM_HASH_INIT() non-inline (hence, define it in the
source file).
2017-10-13 12:47:55 +02:00
Thomas Haller
d06c46b80f libnm: make index variable i unsigned for iterating array
GArray's and GPtrArray's plen argument is unsigned. The index variable
to iterate the list, should not have a smaller range (or different data type).

Also, assert against negative idx argument.
2017-09-27 18:58:53 +02:00
Thomas Haller
121321542e libnm: fix uninitialized variable in get_system_encodings()
CC       libnm-core/libnm_core_libnm_core_la-nm-utils.lo
  libnm-core/nm-utils.c:210:6: error: variable 'encodings' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
          if (lang) {
              ^~~~
  libnm-core/nm-utils.c:220:7: note: uninitialized use occurs here
          if (!encodings) {
               ^~~~~~~~~
  libnm-core/nm-utils.c:210:2: note: remove the 'if' if its condition is always true
          if (lang) {
          ^~~~~~~~~~
  libnm-core/nm-utils.c:198:30: note: initialize the variable 'encodings' to silence this warning
          const char *const *encodings;
                                      ^
                                       = NULL

Fixes: 28a0627481
2017-09-13 08:17:30 +02:00
Beniamino Galvani
789f8a6b51 libnm-core: also consider LC_ALL and LC_CTYPE to guess SSID charset
Also consider LC_ALL and LC_CTYPE environment variables, in addition
to LANG, to determine the charset used for converting SSIDs to UTF-8.

https://bugzilla.gnome.org/show_bug.cgi?id=784415
2017-09-12 23:46:51 +02:00
Beniamino Galvani
28a0627481 libnm-core: cache the current character encoding
Instead of performing the parsing of environment variables and the
lookup for every string, cache the selected encoding.
2017-09-12 23:46:49 +02:00
Thomas Haller
5c42cdb287 all: use _nm_utils_ip4_*() utils functions 2017-09-05 18:44:04 +02:00
Thomas Haller
f0adca00f3 core: fix creating lower-case MAC address with nm_utils_hwaddr_ntoa_buf()
There is only one caller at the moment, and he passes TRUE anyway.
2017-07-25 15:17:50 +02:00
Thomas Haller
45a58c3811 trivial: fix whitespace 2017-07-17 12:49:58 +02:00
Thomas Haller
3f76b5b7eb core: use NM_HASH_COMBINE() function 2017-07-05 14:22:10 +02:00
Thomas Haller
a973eacb3b libnm: add enum for setting priorities
Using plain numbers make it cumbersome to grep for
setting types by priority.

The only downside is, that with the enum values it
is no longer obvious which value has higher or lower
priority.

Also, introduce NM_SETTING_PRIORITY_INVALID. This is what
_nm_setting_type_get_base_type_priority() returns. For the moment
it still has the same numerical value 0 as before. Later, that
shall be distinct from NM_SETTING_PRIORITY_CONNECTION.
2017-06-07 09:07:17 +02:00
Lubomir Rintel
c0419257e7 all: reject duplicate keys in JSON
Teamd is not happy about them and would fail anyway. Worse even, if we
json_loads() such a JSON, which is precisely what happens when we inject the
"hwaddr" key, we turn bad JSON into a good one in a lossy matter. Not good.

https://bugzilla.redhat.com/show_bug.cgi?id=1455130
2017-06-01 13:30:21 +02:00
Lubomir Rintel
2c1a178f5b core: negotiate the best base setting
When the two base settings are present, use one of higher priority.

This will pick the "bridge" setting when both "bridge" and "bluetooth" are
present for a Bluetooth NAP connection.
2017-05-31 20:15:24 +02:00
Thomas Haller
9800b1dccc libnm: move public nm_utils_enum_*() functions back to libnm-core
Commit a8730c51c8 moved the enum
utils from libnm-core to shared/nm-utils.

However, three of those functions are part of public API in libnm.
So, when statically linking against "shared/nm-utils/nm-enum-utils.c"
and dynamically linking against libnm.so, those symbols are present
twice and cause a linker failure.

Fix that by moving the public API back to libnm-core.

Fixes: a8730c51c8
2017-04-05 18:38:31 +02:00
Thomas Haller
d1c6d64e6a shared: move _nm_utils_strv_cleanup() to shared utils 2017-04-05 16:53:06 +02:00
Thomas Haller
a8730c51c8 libnm: move enum utils to new shared file shared/nm-utils/nm-enum-utils.h
libnm contains the public function nm_utils_enum_from_str() et al.
The function is not flexible enough for nmcli's usecase. So, I would
need another public function like nm_utils_enum_from_str_full() that
has an extended API.

That was already required previously for ifcfg-rh writer, but in that
case I could just add it as internal API as libnm-core is linked statically
with NetworkManager.

I don't want to commit to a public API for an utility function. So move
the code instead to the shared directory, so that nmcli may link
statically against it and use the internal API.
2017-03-30 13:09:58 +02:00
Thomas Haller
e26a81cf35 libnm: handler numeric values more gracefully in nm_utils_enum_from_str()
nm_utils_enum_to_str() may also output numeric values if there is no
corresponding "nick" for the enum/flag value.

For enums the value is in decimal and for flags the value is hexadecimal
(with a "0x" prefix).

The same was already supported by nm_utils_enum_from_str() when reading
the value. However, previously, reading a flag would only support hex
numbers and reading a enum would only support decimal numbers.

Extend that, to allow passing numbers in any base. For nm_utils_enum_to_str()
also make sure to never output nicks that may be misinterpreted as
numbers.
2017-03-30 13:09:55 +02:00
Thomas Haller
112f09cf4b libnm: return zero flags value from nm_utils_enum_to_str()
It is not uncommon that a flags type has also the value 0 mapped,
for example to "unknown" or "none".

In that case, we should not return an empty string, but instead
that zero value.

Also, flags actually have an unsigned type. That isn't a real
problem to cast it to a signed int. But be more careful about
it and use unsigned while handling unsigned values and only
cast to int once.
2017-03-30 13:09:54 +02:00
Thomas Haller
163e193286 libnm: fix type annotation for nm_utils_enum_from_str() 2017-03-25 17:12:27 +01:00
Beniamino Galvani
93b3a478bb libnm-core: add attribute parsing/format helpers
Various libnm objects (addresses, routes) carry an hash table of
attributes represented as GVariants indexed by name. Add common
routines to convert to and from a string representation.

To parse a string, a knowledge of the supported attributes (and their
types) is needed: we represent it as an opaque type
NMVariantAttributeSpec that callers must query to the library for the
specific object type and pass to the parse function.
2017-03-06 15:20:25 +01:00
Lubomir Rintel
61722fdeb1 libnm-core/trivial: fix a typo in doc comment 2017-02-25 22:17:46 +01:00
Thomas Haller
1525b44714 utils: support unknown numeric values in nm_utils_enum_to_str() and nm_utils_enum_from_str()
- for nm_utils_enum_to_str(), whenever encounter a numeric value
  that has no expression as enum/flag, encode the value numerically.
  For enums, encode it as decimal. For flags, encode it as hexadecimal
  (with 0x prefix).
  Also check that an existing value_nick cannot be wrongly interpreted
  as a integer, and if they would, encode them instead as integers only.

- Likewise, in nm_utils_enum_from_str() accept numerical values
  and for nm_utils_enum_get_values() return enum nicks that look
  like numeric values in their numeric form only.

- In nm_utils_enum_from_str(), don't use g_strsplit(), but clone the
  string only once and manipulate it inplace.

- Accept '\n' and '\r' as additional delimiters for flags.

- For consistency, also return an err_token for enum types. If the caller
  doesn't care about that, he should simply not pass the out-argument.
2017-02-20 13:45:32 +01:00
Beniamino Galvani
8950d18362 libnm-core: add _nm_utils_enum_to_str_full()
Unfortunately nm_utils_enum_to_str() doesn't allow to specify the
separator between enum values. Since the function is public API and
can't be modified now, add a new internal function which accepts the
separator as argument.
2017-02-20 13:45:32 +01:00
Beniamino Galvani
4d3ec516bd libnm-core: add missing transfer annotation for nm_utils_enum_to_str() 2017-02-20 13:44:08 +01:00
Thomas Haller
7c6c8f0d8b all: cleanup switch fall-through comments for -Wimplicit-fallthrough warning
The -Wimplicit-fallthrough=3 warning is quite flexible of accepting
a fall-through warning.

Some comments were missing or not detected correctly.

Thereby, also change all other comments to follow the exact
same pattern.
2017-02-06 16:45:20 +01:00
Thomas Haller
11bc3f191e all: use nm_utils_strv_find_first() from shared/nm-utils 2017-02-04 17:55:30 +01:00
Beniamino Galvani
660bb1a48f libnm-core: return NULL from _nm_utils_slist_to_strv for empty lists
The function is used, among others, in the get_property() of many
objects to return a boxed strv from a list. The default value for a
boxed strv property is NULL, but _nm_utils_slist_to_strv() returns a
pointer to an array with zero elements when the list is empty.

Change the function to return NULL if the input list is empty.
2017-02-02 13:45:27 +01:00
Lubomir Rintel
f9be6540b0 libnm-core: deprecate nm_utils_iface_valid_name()
We don't use it internally and a better version exists. Not like we
could drop it or anything.
2017-01-06 15:11:56 +01:00
Lubomir Rintel
ac0563d784 libnm-core: add nm_utils_is_valid_iface_name()
Version of nm_utils_iface_valid_name() with error reporting.
2017-01-06 15:11:56 +01:00
Thomas Haller
16aeac5c3e libnm: add nm_utils_version() function to query libnm version at runtime
if (nm_utils_version () < NM_ENCODE_VERSION (1, 5, 0))
        g_error ("Requires at least version 1.5.0");
2016-12-20 16:51:58 +01:00
Jiří Klimeš
3803739808 libnm-core: (trivial) fix a typo 2016-12-15 15:31:56 +01:00
Thomas Haller
eb77d4ed28 libnm-core/utils: remove unused code from nm_utils_hwaddr_matches()
Makes coverity complain.
2016-12-13 18:49:34 +01:00
Thomas Haller
55fb25f59d libnm: force unsigned integer comparisong in nm_utils_bin2hexstr()
Not really a big issue, without the case the intergers
would be compared as ssize_t, which is large enough for
final_len, but possibly not for buflen.
2016-11-28 10:21:24 +01:00
Thomas Haller
3aa41c6e17 libnm: merge hwaddr_aton() and nm_utils_hexstr2bin()
Have nm_utils_hexstr2bin() take over the allocated buffer
via g_bytes_new_take().
2016-11-28 10:06:27 +01:00
Thomas Haller
d7e535aee5 libnm/trivial: rename internal function _bin2str() 2016-11-28 08:50:35 +01:00
Thomas Haller
4cc2f8906c libnm: drop _bin2str() util
It was only used by two places, inline it.
2016-11-27 13:57:13 +01:00
Thomas Haller
542d1c257d libnm: implement nm_utils_bin2hexstr() via _bin2str_buf() 2016-11-27 13:54:23 +01:00
Thomas Haller
5ea2fc776c libnm/trivial: move code 2016-11-27 13:54:23 +01:00
Thomas Haller
34eb8d513f libnm/trivial: move hexstr<>bin conversion functions
nm_utils_hexstr2bin() is quite similar to hwaddr_aton(), and
nm_utils_bin2hexstr() is similar to _bin2str_buf().

Move them close to each other. Maybe one day they should be
consolidated. But their API is slightly different, so the
consolidation would require some effort.

For now, just move them close to each other, so that it's
clearer that two similar (but distinct) functions exists.
2016-11-13 16:01:18 +01:00
Thomas Haller
e36f6a7e52 libnm: add nm_utils_uuid_generate_buf() util 2016-11-09 12:07:33 +01:00
Thomas Haller
37bf643213 libnm: don't initialize buffer for uuid_unparse_lower() with zeros 2016-11-09 12:07:33 +01:00
Thomas Haller
e5fe5a4c03 libnm-core/utils: update hwaddr utilities
_nm_utils_hwaddr_length() did a validation of the string
and returned the length of the address. In all cases where
we were interested in that, we also either want to validate
the address, get the address in binary form, or canonicalize
the address.

We can avoid these duplicate checks, by using _nm_utils_hwaddr_aton()
which both does the parsing and returning the length.
2016-10-28 16:28:29 +02:00
Thomas Haller
0e47b327dc libnm: move backported symbols from libnm-core to libnm
Backported symbols only make sense for libnm itself, not for
libnm-core which is statically linked with NetworkManager and
nm-ifcace-helper. Declaring the symbols in libnm-core, means
that NetworkManager binary also contains them, although there
are not used.

Move them to libnm.
2016-10-13 21:33:33 +02:00
Thomas Haller
a83eb773ce all: modify line separator comments to be 80 chars wide
sed 's#^/\*\{5\}\*\+/$#/*****************************************************************************/#' $(git grep -l '\*\{5\}' | grep '\.[hc]$') -i
2016-10-03 12:01:15 +02:00
Pellaeon Lin
4d7cf9a16d libnm: fix typo g_size to gsize
Fixes: 32f78ae6c3

https://bugzilla.gnome.org/show_bug.cgi?id=772062
2016-09-27 18:08:13 +02:00