[systemd-commits] 3 commits - .gitignore Makefile.am src/libsystemd-network src/resolve src/shared src/test
Tom Gundersen
tomegun at kemper.freedesktop.org
Sat Nov 1 07:38:43 PDT 2014
.gitignore | 1
Makefile.am | 8 ++
src/libsystemd-network/dhcp6-option.c | 43 +++++++++------
src/resolve/resolved-dns-packet.c | 18 ++----
src/shared/unaligned.h | 56 ++++++++++++++++++++
src/test/test-unaligned.c | 92 ++++++++++++++++++++++++++++++++++
6 files changed, 188 insertions(+), 30 deletions(-)
New commits:
commit 4903a73c37d444261c0a6b362e1c27e58100ea72
Author: Tom Gundersen <teg at jklm.no>
Date: Sat Nov 1 14:32:28 2014 +0100
dhcp6: use unaligned read/write helpers
diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c
index e6a3177..a46b6e3 100644
--- a/src/libsystemd-network/dhcp6-option.c
+++ b/src/libsystemd-network/dhcp6-option.c
@@ -24,31 +24,37 @@
#include <string.h>
#include "sparse-endian.h"
+#include "unaligned.h"
#include "util.h"
#include "dhcp6-internal.h"
#include "dhcp6-protocol.h"
-#define DHCP6_OPTION_HDR_LEN 4
#define DHCP6_OPTION_IA_NA_LEN 12
#define DHCP6_OPTION_IA_TA_LEN 4
+typedef struct DHCP6Option {
+ be16_t code;
+ be16_t len;
+ uint8_t data[];
+} DHCP6Option;
+
static int option_append_hdr(uint8_t **buf, size_t *buflen, uint16_t optcode,
size_t optlen) {
+ DHCP6Option *option = (DHCP6Option*) *buf; /* unaligned! */
+
assert_return(buf, -EINVAL);
assert_return(*buf, -EINVAL);
assert_return(buflen, -EINVAL);
- if (optlen > 0xffff || *buflen < optlen + DHCP6_OPTION_HDR_LEN)
+ if (optlen > 0xffff || *buflen < optlen + sizeof(DHCP6Option))
return -ENOBUFS;
- (*buf)[0] = optcode >> 8;
- (*buf)[1] = optcode & 0xff;
- (*buf)[2] = optlen >> 8;
- (*buf)[3] = optlen & 0xff;
+ unaligned_write_be16(&option->code, optcode);
+ unaligned_write_be16(&option->len, (uint16_t) optlen);
- *buf += DHCP6_OPTION_HDR_LEN;
- *buflen -= DHCP6_OPTION_HDR_LEN;
+ *buf += sizeof(DHCP6Option);
+ *buflen -= sizeof(DHCP6Option);
return 0;
}
@@ -100,8 +106,8 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
ia_hdr = *buf;
ia_buflen = *buflen;
- *buf += DHCP6_OPTION_HDR_LEN;
- *buflen -= DHCP6_OPTION_HDR_LEN;
+ *buf += sizeof(DHCP6Option);
+ *buflen -= sizeof(DHCP6Option);
memcpy(*buf, &ia->id, len);
@@ -119,7 +125,7 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
*buf += sizeof(addr->iaaddr);
*buflen -= sizeof(addr->iaaddr);
- ia_addrlen += DHCP6_OPTION_HDR_LEN + sizeof(addr->iaaddr);
+ ia_addrlen += sizeof(DHCP6Option) + sizeof(addr->iaaddr);
}
r = option_append_hdr(&ia_hdr, &ia_buflen, ia->type, len + ia_addrlen);
@@ -130,23 +136,24 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
}
-static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *opt,
+static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *optcode,
size_t *optlen) {
+ DHCP6Option *option = (DHCP6Option*) *buf; /* unaligned! */
uint16_t len;
assert_return(buf, -EINVAL);
- assert_return(opt, -EINVAL);
+ assert_return(optcode, -EINVAL);
assert_return(optlen, -EINVAL);
- if (*buflen < 4)
+ if (*buflen < sizeof(DHCP6Option))
return -ENOMSG;
- len = (*buf)[2] << 8 | (*buf)[3];
+ len = unaligned_read_be16(&option->len);
if (len > *buflen)
return -ENOMSG;
- *opt = (*buf)[0] << 8 | (*buf)[1];
+ *optcode = unaligned_read_be16(&option->code);
*optlen = len;
*buf += 4;
@@ -190,7 +197,7 @@ int dhcp6_option_parse_ia(uint8_t **buf, size_t *buflen, uint16_t iatype,
switch (iatype) {
case DHCP6_OPTION_IA_NA:
- if (*buflen < DHCP6_OPTION_IA_NA_LEN + DHCP6_OPTION_HDR_LEN +
+ if (*buflen < DHCP6_OPTION_IA_NA_LEN + sizeof(DHCP6Option) +
sizeof(addr->iaaddr)) {
r = -ENOBUFS;
goto error;
@@ -212,7 +219,7 @@ int dhcp6_option_parse_ia(uint8_t **buf, size_t *buflen, uint16_t iatype,
break;
case DHCP6_OPTION_IA_TA:
- if (*buflen < DHCP6_OPTION_IA_TA_LEN + DHCP6_OPTION_HDR_LEN +
+ if (*buflen < DHCP6_OPTION_IA_TA_LEN + sizeof(DHCP6Option) +
sizeof(addr->iaaddr)) {
r = -ENOBUFS;
goto error;
commit 725ca0e53a5a1e4093c494623253a2f312c02bc0
Author: Tom Gundersen <teg at jklm.no>
Date: Sat Nov 1 14:07:42 2014 +0100
resolved: dns-packet - use unaligned read/write helpers
diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c
index 7375f77..e5d07b3 100644
--- a/src/resolve/resolved-dns-packet.c
+++ b/src/resolve/resolved-dns-packet.c
@@ -22,6 +22,7 @@
#include "utf8.h"
#include "util.h"
#include "strv.h"
+#include "unaligned.h"
#include "resolved-dns-domain.h"
#include "resolved-dns-packet.h"
@@ -311,8 +312,7 @@ int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start) {
if (r < 0)
return r;
- ((uint8_t*) d)[0] = (uint8_t) (v >> 8);
- ((uint8_t*) d)[1] = (uint8_t) v;
+ unaligned_write_be16(d, v);
return 0;
}
@@ -327,10 +327,7 @@ int dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start) {
if (r < 0)
return r;
- ((uint8_t*) d)[0] = (uint8_t) (v >> 24);
- ((uint8_t*) d)[1] = (uint8_t) (v >> 16);
- ((uint8_t*) d)[2] = (uint8_t) (v >> 8);
- ((uint8_t*) d)[3] = (uint8_t) v;
+ unaligned_write_be32(d, v);
return 0;
}
@@ -793,8 +790,8 @@ int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start) {
if (r < 0)
return r;
- *ret = (((uint16_t) ((uint8_t*) d)[0]) << 8) |
- ((uint16_t) ((uint8_t*) d)[1]);
+ *ret = unaligned_read_be16(d);
+
return 0;
}
@@ -808,10 +805,7 @@ int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start) {
if (r < 0)
return r;
- *ret = (((uint32_t) ((uint8_t*) d)[0]) << 24) |
- (((uint32_t) ((uint8_t*) d)[1]) << 16) |
- (((uint32_t) ((uint8_t*) d)[2]) << 8) |
- ((uint32_t) ((uint8_t*) d)[3]);
+ *ret = unaligned_read_be32(d);
return 0;
}
commit f089257d7b573dcff409a5fb0b140fa5c6e4ca84
Author: Tom Gundersen <teg at jklm.no>
Date: Sat Nov 1 14:04:17 2014 +0100
shared: add helpers for unaligend BE read/write
diff --git a/.gitignore b/.gitignore
index 23522ed..2e2c64e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -199,6 +199,7 @@
/test-libudev
/test-libudev-sym*
/test-list
+/test-unaligned
/test-locale-util
/test-log
/test-login
diff --git a/Makefile.am b/Makefile.am
index 3f1ace0..ea4da0b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -746,6 +746,7 @@ libsystemd_shared_la_SOURCES = \
src/shared/securebits.h \
src/shared/special.h \
src/shared/list.h \
+ src/shared/unaligned.h \
src/shared/macro.h \
src/shared/def.h \
src/shared/sparse-endian.h \
@@ -1347,6 +1348,7 @@ tests += \
test-hashmap \
test-set \
test-list \
+ test-unaligned \
test-tables \
test-device-nodes \
test-xml \
@@ -1625,6 +1627,12 @@ test_xml_LDADD = \
test_list_SOURCES = \
src/test/test-list.c
+test_unaligned_LDADD = \
+ libsystemd-core.la
+
+test_unaligned_SOURCES = \
+ src/test/test-unaligned.c
+
test_list_LDADD = \
libsystemd-core.la
diff --git a/src/shared/unaligned.h b/src/shared/unaligned.h
new file mode 100644
index 0000000..b194d71
--- /dev/null
+++ b/src/shared/unaligned.h
@@ -0,0 +1,56 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Tom Gundersen
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdint.h>
+
+#include "sparse-endian.h"
+
+static inline uint16_t unaligned_read_be16(const be16_t *u) {
+ return (((uint16_t) ((uint8_t*) u)[0]) << 8) |
+ ((uint16_t) ((uint8_t*) u)[1]);
+}
+
+static inline uint32_t unaligned_read_be32(const be32_t *u) {
+ return (((uint32_t) unaligned_read_be16((be16_t*) u)) << 16) |
+ ((uint32_t) unaligned_read_be16((be16_t*) u + 1));
+}
+
+static inline uint64_t unaligned_read_be64(const be64_t *u) {
+ return (((uint64_t) unaligned_read_be32((be32_t*) u)) << 32) |
+ ((uint64_t) unaligned_read_be32((be32_t*) u + 1));
+}
+
+static inline void unaligned_write_be16(be16_t *u, uint16_t a) {
+ ((uint8_t*) u)[0] = (uint8_t) (a >> 8);
+ ((uint8_t*) u)[1] = (uint8_t) a;
+}
+
+static inline void unaligned_write_be32(be32_t *u, uint32_t a) {
+ unaligned_write_be16((be16_t*) u, (uint16_t) (a >> 16));
+ unaligned_write_be16((be16_t*) u + 1, (uint16_t) a);
+}
+
+static inline void unaligned_write_be64(be64_t *u, uint64_t a) {
+ unaligned_write_be32((be32_t*) u, (uint32_t) (a >> 32));
+ unaligned_write_be32((be32_t*) u + 1, (uint32_t) a);
+}
diff --git a/src/test/test-unaligned.c b/src/test/test-unaligned.c
new file mode 100644
index 0000000..52693cd
--- /dev/null
+++ b/src/test/test-unaligned.c
@@ -0,0 +1,92 @@
+/***
+ This file is part of systemd
+
+ Copyright 2014 Tom Gundersen
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "unaligned.h"
+#include "util.h"
+
+static uint8_t data[] = {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+};
+
+int main(int argc, const char *argv[]) {
+ uint8_t scratch[16];
+
+ assert_se(unaligned_read_be16((be16_t*)&data[0]) == 0x0001);
+ assert_se(unaligned_read_be16((be16_t*)&data[1]) == 0x0102);
+
+ assert_se(unaligned_read_be32((be32_t*)&data[0]) == 0x00010203);
+ assert_se(unaligned_read_be32((be32_t*)&data[1]) == 0x01020304);
+ assert_se(unaligned_read_be32((be32_t*)&data[2]) == 0x02030405);
+ assert_se(unaligned_read_be32((be32_t*)&data[3]) == 0x03040506);
+
+ assert_se(unaligned_read_be64((be64_t*)&data[0]) == 0x0001020304050607);
+ assert_se(unaligned_read_be64((be64_t*)&data[1]) == 0x0102030405060708);
+ assert_se(unaligned_read_be64((be64_t*)&data[2]) == 0x0203040506070809);
+ assert_se(unaligned_read_be64((be64_t*)&data[3]) == 0x030405060708090a);
+ assert_se(unaligned_read_be64((be64_t*)&data[4]) == 0x0405060708090a0b);
+ assert_se(unaligned_read_be64((be64_t*)&data[5]) == 0x05060708090a0b0c);
+ assert_se(unaligned_read_be64((be64_t*)&data[6]) == 0x060708090a0b0c0d);
+ assert_se(unaligned_read_be64((be64_t*)&data[7]) == 0x0708090a0b0c0d0e);
+
+ zero(scratch);
+ unaligned_write_be16((uint16_t*)&scratch[0], 0x0001);
+ assert_se(memcmp(&scratch[0], &data[0], sizeof(uint16_t)) == 0);
+ zero(scratch);
+ unaligned_write_be16((uint16_t*)&scratch[1], 0x0102);
+ assert_se(memcmp(&scratch[1], &data[1], sizeof(uint16_t)) == 0);
+
+ zero(scratch);
+ unaligned_write_be32((be32_t*)&scratch[0], 0x00010203);
+ assert_se(memcmp(&scratch[0], &data[0], sizeof(uint32_t)) == 0);
+ zero(scratch);
+ unaligned_write_be32((be32_t*)&scratch[1], 0x01020304);
+ assert_se(memcmp(&scratch[1], &data[1], sizeof(uint32_t)) == 0);
+ zero(scratch);
+ unaligned_write_be32((be32_t*)&scratch[2], 0x02030405);
+ assert_se(memcmp(&scratch[2], &data[2], sizeof(uint32_t)) == 0);
+ zero(scratch);
+ unaligned_write_be32((be32_t*)&scratch[3], 0x03040506);
+ assert_se(memcmp(&scratch[3], &data[3], sizeof(uint32_t)) == 0);
+
+ zero(scratch);
+ unaligned_write_be64((be64_t*)&scratch[0], 0x0001020304050607);
+ assert_se(memcmp(&scratch[0], &data[0], sizeof(uint64_t)) == 0);
+ zero(scratch);
+ unaligned_write_be64((be64_t*)&scratch[1], 0x0102030405060708);
+ assert_se(memcmp(&scratch[1], &data[1], sizeof(uint64_t)) == 0);
+ zero(scratch);
+ unaligned_write_be64((be64_t*)&scratch[2], 0x0203040506070809);
+ assert_se(memcmp(&scratch[2], &data[2], sizeof(uint64_t)) == 0);
+ zero(scratch);
+ unaligned_write_be64((be64_t*)&scratch[3], 0x030405060708090a);
+ assert_se(memcmp(&scratch[3], &data[3], sizeof(uint64_t)) == 0);
+ zero(scratch);
+ unaligned_write_be64((be64_t*)&scratch[4], 0x0405060708090a0b);
+ assert_se(memcmp(&scratch[4], &data[4], sizeof(uint64_t)) == 0);
+ zero(scratch);
+ unaligned_write_be64((be64_t*)&scratch[5], 0x05060708090a0b0c);
+ assert_se(memcmp(&scratch[5], &data[5], sizeof(uint64_t)) == 0);
+ zero(scratch);
+ unaligned_write_be64((be64_t*)&scratch[6], 0x060708090a0b0c0d);
+ assert_se(memcmp(&scratch[6], &data[6], sizeof(uint64_t)) == 0);
+ zero(scratch);
+ unaligned_write_be64((be64_t*)&scratch[7], 0x0708090a0b0c0d0e);
+ assert_se(memcmp(&scratch[7], &data[7], sizeof(uint64_t)) == 0);
+}
More information about the systemd-commits
mailing list