[PATCH 10/25] site: use unaligned.h's functions, not pointer cast and ntohl

Ian Jackson ijackson at chiark.greenend.org.uk
Sat Jul 20 00:38:54 BST 2013

Switch site.c to using unaligned.h's functions for accessing
multi-byte values inside messages.  There were a few places where this
construction was used:
   something = ntohl(*(uint32_t*)(buf->start + offset));
It is much clearer to use this equivalent construction:
   something = get_uint32(buf->start + offset);

Also the packet's message type was extracted from the message using
get_uint32 and then put through ntohl.  This is, of course, wrong.
Currently all the message type codes are palindromes, so it doesn't
matter in practice.  Fix it anyway.

Signed-off-by: Ian Jackson <ijackson at chiark.greenend.org.uk>
 site.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/site.c b/site.c
index 7857956..8652c9e 100644
--- a/site.c
+++ b/site.c
@@ -850,9 +850,9 @@ static bool_t process_msg0(struct site *st, struct buffer_if *msg0,
 static void dump_packet(struct site *st, struct buffer_if *buf,
 			const struct comm_addr *addr, bool_t incoming)
-    uint32_t dest=ntohl(*(uint32_t *)buf->start);
-    uint32_t source=ntohl(*(uint32_t *)(buf->start+4));
-    uint32_t msgtype=ntohl(*(uint32_t *)(buf->start+8));
+    uint32_t dest=get_uint32(buf->start);
+    uint32_t source=get_uint32(buf->start+4);
+    uint32_t msgtype=get_uint32(buf->start+8);
     if (st->log_events & LOG_DUMP)
 	slilog(st->log,M_DEBUG,"%s: %s: %08x<-%08x: %08x:",
@@ -1258,8 +1258,8 @@ static bool_t site_incoming(void *sst, struct buffer_if *buf,
     if (buf->size < 12) return False;
-    uint32_t msgtype=ntohl(get_uint32(buf->start+8));
-    uint32_t dest=ntohl(*(uint32_t *)buf->start);
+    uint32_t dest=get_uint32(buf->start);
+    uint32_t msgtype=get_uint32(buf->start+8);
     struct msg named_msg;
     if (msgtype==LABEL_MSG1) {

More information about the sgo-software-discuss mailing list