diff options
author | Tom Gundersen <teg@jklm.no> | 2014-05-20 11:04:50 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-05-20 23:31:22 +0200 |
commit | 20b958bf157dfb2f521b191ef7158035bcaa3003 (patch) | |
tree | d5964a60f26aaa1aeb4b0771a1f2a2b0f0efed10 /src/libsystemd-network/test-dhcp-option.c | |
parent | ece6e766cf89c8ec82ad135969dedf16cd7c1ee8 (diff) |
sd-dhcp: refactor dhcp_option_append
Store a pointer to the options in the DHCPMessage struct, and pass
this together with an offset around, rather than a uint8_t**.
This avoids us having to (re)compute the pointer; and changes
dhcp_option_append from adjusting both the pointer to the next
option and the remaining size of the options, to just adjusting
the current offset.
This makes the code a bit simpler to follow IMHO, but there should
be no functional change.
Diffstat (limited to 'src/libsystemd-network/test-dhcp-option.c')
-rw-r--r-- | src/libsystemd-network/test-dhcp-option.c | 58 |
1 files changed, 21 insertions, 37 deletions
diff --git a/src/libsystemd-network/test-dhcp-option.c b/src/libsystemd-network/test-dhcp-option.c index 35db8c1c0a..eaf6a535e6 100644 --- a/src/libsystemd-network/test-dhcp-option.c +++ b/src/libsystemd-network/test-dhcp-option.c @@ -85,16 +85,14 @@ static void test_invalid_buffer_length(void) static void test_message_init(void) { _cleanup_free_ DHCPMessage *message = NULL; - size_t optlen = 3; + size_t optlen = 3, optoffset; size_t len = sizeof(DHCPMessage) + optlen; - uint8_t *opt, *magic; + uint8_t *magic; message = malloc0(len); - opt = (uint8_t *)(message + 1); - assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678, - DHCP_DISCOVER, &opt, &optlen) >= 0); + DHCP_DISCOVER, message->options, optlen, &optoffset) >= 0); assert_se(message->xid == htobe32(0x12345678)); assert_se(message->op == BOOTREQUEST); @@ -115,13 +113,11 @@ static DHCPMessage *create_message(uint8_t *options, uint16_t optlen, { DHCPMessage *message; size_t len = sizeof(DHCPMessage) + optlen; - uint8_t *opt; message = malloc0(len); - opt = (uint8_t *)(message + 1); if (options && optlen) - memcpy(opt, options, optlen); + memcpy(&message->options, options, optlen); if (file && filelen <= 128) memcpy(&message->file, file, filelen); @@ -308,46 +304,34 @@ static uint8_t options[64] = { static void test_option_set(void) { - size_t len, oldlen; - int pos, i; - uint8_t *opt; - - assert_se(dhcp_option_append(NULL, NULL, 0, 0, NULL) == -EINVAL); - - len = 0; - opt = &result[0]; - assert_se(dhcp_option_append(&opt, NULL, 0, 0, NULL) == -EINVAL); - assert_se(opt == &result[0] && len == 0); + size_t offset = 0, len, pos; + unsigned i; - assert_se(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD, + assert_se(dhcp_option_append(result, 0, &offset, DHCP_OPTION_PAD, 0, NULL) == -ENOBUFS); - assert_se(opt == &result[0] && len == 0); + assert_se(offset == 0); - opt = &result[4]; - len = 1; - assert_se(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD, + offset = 4; + assert_se(dhcp_option_append(result, 1, &offset, DHCP_OPTION_PAD, 0, NULL) >= 0); - assert_se(opt == &result[5] && len == 0); + assert_se(offset == 5); - pos = 4; + offset = pos = 4; len = 60; while (pos < 64 && options[pos] != DHCP_OPTION_END) { - opt = &result[pos]; - oldlen = len; + offset = pos; - assert_se(dhcp_option_append(&opt, &len, options[pos], - options[pos + 1], - &options[pos + 2]) >= 0); + assert_se(dhcp_option_append(result, len, &offset, + options[pos], + options[pos + 1], + &options[pos + 2]) >= 0); - if (options[pos] == DHCP_OPTION_PAD) { - assert_se(opt == &result[pos + 1]); - assert_se(len == oldlen - 1); + if (options[pos] == DHCP_OPTION_PAD) pos++; - } else { - assert_se(opt == &result[pos + 2 + options[pos + 1]]); - assert_se(len == oldlen - 2 - options[pos + 1]); + else pos += 2 + options[pos + 1]; - } + + assert_se(offset == pos); } for (i = 0; i < pos; i++) { |