about summary refs log tree commit homepage
path: root/nameinfo.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-04-16 20:26:57 +0000
committerEric Wong <normalperson@yhbt.net>2013-04-16 20:27:42 +0000
commit29342bcd9864e4aabb9e6febef8748a5f51ac944 (patch)
tree4e639d4832f748a5a1ccf86eb4b80f4b3174275e /nameinfo.c
parent88d34b4686a650dba89674aa302ab13c78e8cef0 (diff)
downloadcmogstored-29342bcd9864e4aabb9e6febef8748a5f51ac944.tar.gz
This will allow us to more easily handle error reporting for
IPv6 addresses and allow for consistent formatting of
stringified IP addresses.
Diffstat (limited to 'nameinfo.c')
-rw-r--r--nameinfo.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/nameinfo.c b/nameinfo.c
new file mode 100644
index 0000000..3a086ae
--- /dev/null
+++ b/nameinfo.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2012-2013, Eric Wong <normalperson@yhbt.net>
+ * License: GPLv3 or later (see COPYING for details)
+ */
+#include "cmogstored.h"
+
+/*
+ * small wrapper around getnameinfo(3), this only handles numeric types
+ * for IPv4 and IPv6 and uses the compact mog_ni structure to reduce
+ * stack usage in error reporting.
+ *
+ * returns the return value of getnameinfo(3)
+ */
+int mog_nameinfo(union mog_sockaddr *any, socklen_t len, struct mog_ni *ni)
+{
+        char *hostptr = ni->ni_host;
+        size_t hostlen = sizeof(ni->ni_host) - (sizeof("[]") - 1);
+
+        char *servptr = ni->ni_serv + 1; /* offset for ':' */
+        size_t servlen = sizeof(ni->ni_serv) - 1; /* offset for ':' */
+
+        struct sockaddr *sa = mog_sockaddr_sa(any);
+        int flags = NI_NUMERICHOST | NI_NUMERICSERV;
+        int rc;
+
+        rc = getnameinfo(sa, len, hostptr, hostlen, servptr, servlen, flags);
+
+        /* terminate serv string on error */
+        ni->ni_serv[0] = rc == 0 ? ':' : 0;
+
+        return rc;
+}