summary refs log tree commit diff
path: root/src/libnm-glib-aux/nm-shared-utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnm-glib-aux/nm-shared-utils.c')
-rw-r--r--src/libnm-glib-aux/nm-shared-utils.c99
1 files changed, 56 insertions, 43 deletions
diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c
index 563b7cc2..ad99a6b9 100644
--- a/src/libnm-glib-aux/nm-shared-utils.c
+++ b/src/libnm-glib-aux/nm-shared-utils.c
@@ -870,7 +870,12 @@ nm_utils_to_string_buffer_init(char **buf, gsize *len)
 {
     if (!*buf) {
         *buf = _nm_utils_to_string_buffer;
-        *len = sizeof(_nm_utils_to_string_buffer);
+        *len = NM_UTILS_TO_STRING_BUFFER_SIZE;
+
+        /* We no longer want to support callers to omit the buffer
+         * and fallback to the global buffer. Callers should be fixed
+         * to always provide a valid buffer. */
+        g_return_if_reached();
     }
 }
 
@@ -945,20 +950,6 @@ nm_utils_flags2str(const NMUtilsFlags2StrDesc *descs,
 
 /*****************************************************************************/
 
-/**
- * _nm_utils_ip4_prefix_to_netmask:
- * @prefix: a CIDR prefix
- *
- * Returns: the netmask represented by the prefix, in network byte order
- **/
-guint32
-_nm_utils_ip4_prefix_to_netmask(guint32 prefix)
-{
-    return prefix < 32 ? ~htonl(0xFFFFFFFFu >> prefix) : 0xFFFFFFFFu;
-}
-
-/*****************************************************************************/
-
 guint32
 _nm_utils_ip4_get_default_prefix0(in_addr_t ip)
 {
@@ -1130,6 +1121,7 @@ nm_utils_parse_inaddr_bin_full(int         addr_family,
 
 #if NM_MORE_ASSERTS > 10
     if (addr_family == AF_INET) {
+        NM_PRAGMA_WARNING_DISABLE_DANGLING_POINTER
         gs_free_error GError *error = NULL;
         in_addr_t             a;
 
@@ -1145,6 +1137,7 @@ nm_utils_parse_inaddr_bin_full(int         addr_family,
                     error->message);
         }
         nm_assert(addrbin.addr4 == a);
+        NM_PRAGMA_WARNING_REENABLE
     }
 #endif
 
@@ -2958,8 +2951,8 @@ _str_buf_append_c_escape_octal(NMStrBuf *strbuf, char ch)
  *
  * Returns: the unescaped buffer of length @out_len. If @str is %NULL, this returns %NULL
  *   and sets @out_len to 0. Otherwise, a non-%NULL binary buffer is returned with
- *   @out_len bytes. Note that the binary buffer is guaranteed to be NUL terminated. That
- *   is @result[@out_len] is NUL.
+ *   @out_len bytes. Note that the binary buffer is guaranteed to be NUL terminated
+ *   (@result[@out_len] is NUL).
  *   Note that the result is binary, and may have embedded NUL characters and non-UTF-8.
  *   If the function can avoid cloning the input string, it will return a pointer inside
  *   the input @str. For example, if there is no backslash, no cloning is necessary. In that
@@ -3121,11 +3114,14 @@ nm_utils_buf_utf8safe_unescape(const char             *str,
  * invalid UTF-8 sequences, and other (depending on @flags).
  *
  * Returns: the escaped input buffer, as valid UTF-8. If no escaping
- *   is necessary, it returns the input @buf. Otherwise, an allocated
- *   string @to_free is returned which must be freed by the caller
- *   with g_free. The escaping can be reverted by g_strcompress().
- *   There are cases where this function can return NULL:
- *   - if @buflen is 0
+ *   is necessary and @buflen is negative, it returns the input @buf
+ *   that can be interpreted as NUL terminated UTF-8 string.
+ *   Otherwise, an allocated string @to_free is returned which must be freed
+ *   by the caller with g_free().
+ *   The escaping can be reverted by nm_utils_buf_utf8safe_unescape()
+ *   (or, if in the absence of NUL characters, with g_strcompress()).
+ *   There are cases where this function returns %NULL:
+ *   - if @buflen is 0.
  *   - if @buflen is negative and @buf is NULL.
  **/
 const char *
@@ -4689,14 +4685,20 @@ _nm_utils_invoke_on_idle_start(gboolean                    use_timeout,
     }
 
     if (use_timeout) {
+        /* We use G_PRIORITY_DEFAULT_IDLE both for the with/without timeout
+         * case. The reason is not strong, but it seems right that the caller
+         * requests a lower priority than G_PRIORITY_DEFAULT. That is unlike
+         * what g_timeout_add() would do. */
         source = nm_g_timeout_source_new(timeout_msec,
-                                         G_PRIORITY_DEFAULT,
+                                         G_PRIORITY_DEFAULT_IDLE,
                                          _nm_utils_invoke_on_idle_cb_idle,
                                          data,
                                          NULL);
     } else {
-        source =
-            nm_g_idle_source_new(G_PRIORITY_DEFAULT, _nm_utils_invoke_on_idle_cb_idle, data, NULL);
+        source = nm_g_idle_source_new(G_PRIORITY_DEFAULT_IDLE,
+                                      _nm_utils_invoke_on_idle_cb_idle,
+                                      data,
+                                      NULL);
     }
 
     /* use the current thread default context. */
@@ -4840,6 +4842,27 @@ nm_utils_bin2hexstr_full(gconstpointer addr,
     return out0;
 }
 
+char *
+_nm_utils_bin2hexstr(gconstpointer src, gsize len, int final_len)
+{
+    char *result;
+    gsize buflen = (len * 2) + 1;
+
+    nm_assert(src);
+    nm_assert(len > 0 && (buflen - 1) / 2 == len);
+    nm_assert(final_len < 0 || (gsize) final_len < buflen);
+
+    result = g_malloc(buflen);
+
+    nm_utils_bin2hexstr_full(src, len, '\0', FALSE, result);
+
+    /* Cut converted key off at the correct length for this cipher type */
+    if (final_len >= 0 && (gsize) final_len < buflen)
+        result[final_len] = '\0';
+
+    return result;
+}
+
 guint8 *
 nm_utils_hexstr2bin_full(const char *hexstr,
                          gboolean    allow_0x_prefix,
@@ -6259,7 +6282,7 @@ _nm_utils_ssid_to_string_gbytes(GBytes *ssid)
 /*****************************************************************************/
 
 gconstpointer
-nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen)
+nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint32 plen)
 {
     g_return_val_if_fail(dst, NULL);
 
@@ -6283,21 +6306,10 @@ nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer
     return dst;
 }
 
-/* nm_utils_ip4_address_clear_host_address:
- * @addr: source ip6 address
- * @plen: prefix length of network
- *
- * returns: the input address, with the host address set to 0.
- */
-in_addr_t
-nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint8 plen)
-{
-    return addr & _nm_utils_ip4_prefix_to_netmask(plen);
-}
-
 /* nm_utils_ip6_address_clear_host_address:
  * @dst: destination output buffer, will contain the network part of the @src address
- * @src: source ip6 address
+ * @src: source ip6 address. If NULL, this does an in-place update of @dst.
+ *   Also, @src and @dst are allowed to be the same pointers.
  * @plen: prefix length of network
  *
  * Note: this function is self assignment safe, to update @src inplace, set both
@@ -6306,7 +6318,7 @@ nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint8 plen)
 const struct in6_addr *
 nm_utils_ip6_address_clear_host_address(struct in6_addr       *dst,
                                         const struct in6_addr *src,
-                                        guint8                 plen)
+                                        guint32                plen)
 {
     g_return_val_if_fail(plen <= 128, NULL);
     g_return_val_if_fail(dst, NULL);
@@ -6335,14 +6347,15 @@ nm_utils_ip6_address_clear_host_address(struct in6_addr       *dst,
 int
 nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a,
                                      const struct in6_addr *addr_b,
-                                     guint8                 plen)
+                                     guint32                plen)
 {
     int    nbytes;
     guint8 va, vb, m;
 
-    if (plen >= 128)
+    if (plen >= 128) {
+        nm_assert(plen == 128);
         NM_CMP_DIRECT_MEMCMP(addr_a, addr_b, sizeof(struct in6_addr));
-    else {
+    } else {
         nbytes = plen / 8;
         if (nbytes)
             NM_CMP_DIRECT_MEMCMP(addr_a, addr_b, nbytes);