summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2006-02-19 13:04:18 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2006-02-19 13:04:18 +0000
commit6214255bbb56558f6062aa6c33ae31d32c38f3e5 (patch)
tree4f3aa2ddc152fa23cfe83f5ccb981a8d2d6ac6e2
parenta1f2f9faaed0d2829602d98940eecc3bf2f7c949 (diff)
downloadbtpd-6214255bbb56558f6062aa6c33ae31d32c38f3e5.tar.gz
btpd-6214255bbb56558f6062aa6c33ae31d32c38f3e5.zip
Safer code for net_read32 and net_write32. It may have been possible for them
to cause failure on some architectures because of unaligned fetch/write of
integers.

-rw-r--r--btpd/net.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/btpd/net.c b/btpd/net.c
index a1eac6b..0c63776 100644
--- a/btpd/net.c
+++ b/btpd/net.c
@@ -128,13 +128,19 @@ net_active(struct torrent *tp)
 void
 net_write32(void *buf, uint32_t num)
 {
-    *(uint32_t *)buf = htonl(num);
+    uint8_t *p = buf;
+    *p = (num >> 24) & 0xff;
+    *(p + 1) = (num >> 16) & 0xff;
+    *(p + 2) = (num >> 8) & 0xff;
+    *(p + 3) = num & 0xff;
 }
 
 uint32_t
 net_read32(const void *buf)
 {
-    return ntohl(*(uint32_t *)buf);
+    const uint8_t *p = buf;
+    return (uint32_t)*p << 24 | (uint32_t)*(p + 1) << 16
+        | (uint16_t)*(p + 2) << 8 | *(p + 3);
 }
 
 static unsigned long