[Openvpn-devel,v3] buffer: Add buf_read_u64() and buf_write_u64()

Message ID 20260714092552.18685-1-gert@greenie.muc.de
State New
Headers
Series [Openvpn-devel,v3] buffer: Add buf_read_u64() and buf_write_u64() |

Commit Message

Gert Doering July 14, 2026, 9:25 a.m. UTC
  From: Lev Stipakov <lev@openvpn.net>

Add 64-bit big-endian buffer accessors alongside the existing 8/16/32-bit
helpers, using htonll()/ntohll() for the byte-order conversion.

buf_read_u64() reports success via a bool out-parameter, mirroring
buf_read_u32().

Change-Id: Ic677f43fc3eb0052f8e80d9a7f098d34ad03dfe1
Signed-off-by: Lev Stipakov <lev@openvpn.net>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1740
---

This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1740
This mail reflects revision 3 of this Change.

Acked-by according to Gerrit (reflected above):
  

Comments

Gert Doering July 14, 2026, 10:51 a.m. UTC | #1
This is basically "not doing anything yet", but preparing infrastructure
for future work.  So it's hard to test beyond "does it compile" (it does,
on all platforms, including 32bit NetBSD).  "htonll()" seems to be generally
somewhat under-documented (my BSDs claim "no manual entry" and it's
not really defined in the system includes either) - since this is 
inlines, I tested replacing one of the u32 calls "so it's inlined" and
it still compiles...  magic stuff.

Your patch has been applied to the master branch.

commit e8d39061007b0a29f78cecce72a0269d6dd3ecde
Author: Lev Stipakov
Date:   Tue Jul 14 11:25:46 2026 +0200

     buffer: Add buf_read_u64() and buf_write_u64()

     Signed-off-by: Lev Stipakov <lev@openvpn.net>
     Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
     Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1740
     Message-Id: <20260714092552.18685-1-gert@greenie.muc.de>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg37578.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering
  

Patch

diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h
index fcc923b..1db9367 100644
--- a/src/openvpn/buffer.h
+++ b/src/openvpn/buffer.h
@@ -25,6 +25,7 @@ 
 
 #include "basic.h"
 #include "error.h"
+#include "integer.h"
 
 #define BUF_SIZE_MAX 1000000
 
@@ -702,6 +703,13 @@ 
 }
 
 static inline bool
+buf_write_u64(struct buffer *dest, uint64_t data)
+{
+    uint64_t u64 = htonll(data);
+    return buf_write(dest, &u64, sizeof(uint64_t));
+}
+
+static inline bool
 buf_copy(struct buffer *dest, const struct buffer *src)
 {
     return buf_write(dest, BPTR(src), BLENZ(src));
@@ -827,6 +835,30 @@ 
     }
 }
 
+/* Read a 64-bit big-endian value (see buf_write_u64()). Sets *good to indicate
+ * success, like buf_read_u32(). */
+static inline uint64_t
+buf_read_u64(struct buffer *buf, bool *good)
+{
+    uint64_t ret;
+    if (!buf_read(buf, &ret, sizeof(uint64_t)))
+    {
+        if (good)
+        {
+            *good = false;
+        }
+        return 0;
+    }
+    else
+    {
+        if (good)
+        {
+            *good = true;
+        }
+        return ntohll(ret);
+    }
+}
+
 /** Return true if buffer contents are equal */
 static inline bool
 buf_equal(const struct buffer *a, const struct buffer *b)