]> git.decadent.org.uk Git - nfs-utils.git/commitdiff
nfs-utils: make private cookie to hex conversion a library routine
authorJeff Layton <jlayton@redhat.com>
Tue, 12 Jan 2010 00:52:47 +0000 (19:52 -0500)
committerSteve Dickson <steved@redhat.com>
Tue, 12 Jan 2010 10:52:55 +0000 (05:52 -0500)
Signed-off-by: Jeff Layton <jlayton@redhat.com>
Signed-off-by: Steve Dickson <steved@redhat.com>
support/include/nsm.h
support/nsm/file.c

index 7554493953533272b3db5c79078417108970e2c4..28314d138288f01ba79b4471c3a81ce1bbab4199 100644 (file)
@@ -60,5 +60,7 @@ extern _Bool  nsm_insert_monitored_host(const char *hostname,
                        const struct sockaddr *sap, const struct mon *m);
 extern void    nsm_delete_monitored_host(const char *hostname);
 extern void    nsm_delete_notified_host(const char *hostname);
+extern size_t  nsm_priv_to_hex(const char *priv, char *buf,
+                               const size_t buflen);
 
 #endif /* !NFS_UTILS_SUPPORT_NSM_H */
index 4dfc235dce5bc4f28562145420f236cc07123603..fc3241a1a4d9a10d128d22f3d352dcbc2445863d 100644 (file)
@@ -517,6 +517,33 @@ nsm_retire_monitored_hosts(void)
        return count;
 }
 
+/*
+ * nsm_priv_to_hex - convert a NSM private cookie to a hex string.
+ *
+ * @priv: buffer holding the binary NSM private cookie
+ * @buf: output buffer for NULL terminated hex string
+ * @buflen: size of output buffer
+ *
+ * Returns the length of the resulting string or 0 on error
+ */
+size_t
+nsm_priv_to_hex(const char *priv, char *buf, const size_t buflen)
+{
+       int i, len;
+       size_t remaining = buflen;
+
+       for (i = 0; i < SM_PRIV_SIZE; i++) {
+               len = snprintf(buf, remaining, "%02x",
+                               (unsigned int)(0xff & priv[i]));
+               if (error_check(len, remaining))
+                       return 0;
+               buf += len;
+               remaining -= (size_t)len;
+       }
+
+       return buflen - remaining;
+}
+
 /*
  * Returns the length in bytes of the created record.
  */
@@ -526,8 +553,8 @@ nsm_create_monitor_record(char *buf, const size_t buflen,
                const struct sockaddr *sap, const struct mon *m)
 {
        const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
-       size_t remaining = buflen;
-       int i, len;
+       size_t hexlen, remaining = buflen;
+       int len;
 
        len = snprintf(buf, remaining, "%08x %08x %08x %08x ",
                        (unsigned int)sin->sin_addr.s_addr,
@@ -539,14 +566,11 @@ nsm_create_monitor_record(char *buf, const size_t buflen,
        buf += len;
        remaining -= (size_t)len;
 
-       for (i = 0; i < SM_PRIV_SIZE; i++) {
-               len = snprintf(buf, remaining, "%02x",
-                               (unsigned int)(0xff & m->priv[i]));
-               if (error_check(len, remaining))
-                       return 0;
-               buf += len;
-               remaining -= (size_t)len;
-       }
+       hexlen = nsm_priv_to_hex(m->priv, buf, remaining);
+       if (hexlen == 0)
+               return 0;
+       buf += hexlen;
+       remaining -= hexlen;
 
        len = snprintf(buf, remaining, " %s %s\n",
                        m->mon_id.mon_name, m->mon_id.my_id.my_name);