summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2013-10-28 11:54:22 +0100
committerThomas Haller <thaller@redhat.com>2013-12-09 17:21:21 +0100
commit41f8114359404ae2cf14724d84eccfc378e9317c (patch)
tree8ecd3b67c09c105cfe294c00aea3eab39a2d99dd
parentd40a499c01395f8e479cdaab80aee7a42b0caffa (diff)
libnm-util: add nm_utils_inet[46]_ntop functions
https://bugzilla.gnome.org/show_bug.cgi?id=711684 Signed-off-by: Thomas Haller <thaller@redhat.com>
-rw-r--r--libnm-util/libnm-util.ver2
-rw-r--r--libnm-util/nm-utils.c55
-rw-r--r--libnm-util/nm-utils.h10
3 files changed, 67 insertions, 0 deletions
diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver
index bcaa4260ae..3797e2beae 100644
--- a/libnm-util/libnm-util.ver
+++ b/libnm-util/libnm-util.ver
@@ -593,6 +593,8 @@ global:
nm_utils_hwaddr_type;
nm_utils_hwaddr_valid;
nm_utils_iface_valid_name;
+ nm_utils_inet4_ntop;
+ nm_utils_inet6_ntop;
nm_utils_init;
nm_utils_ip4_addresses_from_gvalue;
nm_utils_ip4_addresses_to_gvalue;
diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c
index e9c2593fab..f40c5b245a 100644
--- a/libnm-util/nm-utils.c
+++ b/libnm-util/nm-utils.c
@@ -2273,3 +2273,58 @@ nm_utils_is_uuid (const char *str)
return FALSE;
}
+
+static char _nm_utils_inet_ntop_buffer[NM_UTILS_INET_ADDRSTRLEN];
+
+/**
+ * nm_utils_inet4_ntop:
+ * @inaddr: the address that should be converted to string.
+ * @dst: the destination buffer, it must contain at least %INET_ADDRSTRLEN
+ * or %NM_UTILS_INET_ADDRSTRLEN characters. If set to %NULL, it will return
+ * a pointer to an internal, static buffer (shared with nm_utils_inet6_ntop()).
+ * Beware, that the internal buffer will be overwritten with ever new call
+ * of nm_utils_inet4_ntop() or nm_utils_inet6_ntop() that does not provied it's
+ * own @dst buffer. Also, using the internal buffer is not thread safe. When
+ * in doubt, pass your own @dst buffer to avoid these issues.
+ *
+ * Wrapper for inet_ntop.
+ *
+ * Returns: the input buffer @dst, or a pointer to an
+ * internal, static buffer. This function cannot fail.
+ *
+ * Since: 0.9.10
+ **/
+const char *
+nm_utils_inet4_ntop (in_addr_t inaddr, char *dst)
+{
+ return inet_ntop (AF_INET, &inaddr, dst ? dst : _nm_utils_inet_ntop_buffer,
+ INET_ADDRSTRLEN);
+}
+
+/**
+ * nm_utils_inet6_ntop:
+ * @in6addr: the address that should be converted to string.
+ * @dst: the destination buffer, it must contain at least %INET6_ADDRSTRLEN
+ * or %NM_UTILS_INET_ADDRSTRLEN characters. If set to %NULL, it will return
+ * a pointer to an internal, static buffer (shared with nm_utils_inet4_ntop()).
+ * Beware, that the internal buffer will be overwritten with ever new call
+ * of nm_utils_inet4_ntop() or nm_utils_inet6_ntop() that does not provied it's
+ * own @dst buffer. Also, using the internal buffer is not thread safe. When
+ * in doubt, pass your own @dst buffer to avoid these issues.
+ *
+ * Wrapper for inet_ntop.
+ *
+ * Returns: the input buffer @dst, or a pointer to an
+ * internal, static buffer. %NULL is not allowed as @in6addr,
+ * otherwise, this function cannot fail.
+ *
+ * Since: 0.9.10
+ **/
+const char *
+nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst)
+{
+ g_return_val_if_fail (in6addr, NULL);
+ return inet_ntop (AF_INET6, in6addr, dst ? dst : _nm_utils_inet_ntop_buffer,
+ INET6_ADDRSTRLEN);
+}
+
diff --git a/libnm-util/nm-utils.h b/libnm-util/nm-utils.h
index 25f4a5609e..f6fdf36f99 100644
--- a/libnm-util/nm-utils.h
+++ b/libnm-util/nm-utils.h
@@ -154,6 +154,16 @@ gboolean nm_utils_iface_valid_name(const char *name);
gboolean nm_utils_is_uuid (const char *str);
+/**
+ * NM_UTILS_INET_ADDRSTRLEN:
+ *
+ * Defines the minimal length for a char buffer that is suitable as @dst argument
+ * for both nm_utils_inet4_ntop() and nm_utils_inet6_ntop().
+ **/
+#define NM_UTILS_INET_ADDRSTRLEN INET6_ADDRSTRLEN
+const char *nm_utils_inet4_ntop (in_addr_t inaddr, char *dst);
+const char *nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst);
+
G_END_DECLS
#endif /* NM_UTILS_H */