summaryrefslogtreecommitdiff
path: root/src/libsystemd-network/dhcp-option.c
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-05-20 11:04:50 +0200
committerTom Gundersen <teg@jklm.no>2014-05-20 23:31:22 +0200
commit20b958bf157dfb2f521b191ef7158035bcaa3003 (patch)
treed5964a60f26aaa1aeb4b0771a1f2a2b0f0efed10 /src/libsystemd-network/dhcp-option.c
parentece6e766cf89c8ec82ad135969dedf16cd7c1ee8 (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/dhcp-option.c')
-rw-r--r--src/libsystemd-network/dhcp-option.c33
1 files changed, 14 insertions, 19 deletions
diff --git a/src/libsystemd-network/dhcp-option.c b/src/libsystemd-network/dhcp-option.c
index 1b92e8616b..7bf8812676 100644
--- a/src/libsystemd-network/dhcp-option.c
+++ b/src/libsystemd-network/dhcp-option.c
@@ -26,37 +26,33 @@
#include "dhcp-internal.h"
-int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
- size_t optlen, const void *optval)
-{
- if (!buf || !buflen)
- return -EINVAL;
+int dhcp_option_append(uint8_t options[], size_t size, size_t *offset,
+ uint8_t code, size_t optlen, const void *optval) {
+ assert(options);
+ assert(offset);
switch (code) {
case DHCP_OPTION_PAD:
case DHCP_OPTION_END:
- if (*buflen < 1)
+ if (size - *offset < 1)
return -ENOBUFS;
- (*buf)[0] = code;
- *buf += 1;
- *buflen -= 1;
+ options[*offset] = code;
+ *offset += 1;
break;
default:
- if (*buflen < optlen + 2)
+ if (size - *offset < optlen + 2)
return -ENOBUFS;
- if (!optval)
- return -EINVAL;
+ assert(optval);
- (*buf)[0] = code;
- (*buf)[1] = optlen;
- memcpy(&(*buf)[2], optval, optlen);
+ options[*offset] = code;
+ options[*offset + 1] = optlen;
+ memcpy(&options[*offset + 2], optval, optlen);
- *buf += optlen + 2;
- *buflen -= (optlen + 2);
+ *offset += optlen + 2;
break;
}
@@ -143,7 +139,6 @@ int dhcp_option_parse(DHCPMessage *message, size_t len,
{
uint8_t overload = 0;
uint8_t message_type = 0;
- uint8_t *opt = (uint8_t *)(message + 1);
int res;
if (!message)
@@ -154,7 +149,7 @@ int dhcp_option_parse(DHCPMessage *message, size_t len,
len -= sizeof(DHCPMessage);
- res = parse_options(opt, len, &overload, &message_type,
+ res = parse_options(message->options, len, &overload, &message_type,
cb, user_data);
if (res < 0)
return res;