1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2013 Intel Corporation. All rights reserved.
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "dhcp-internal.h"
29 int dhcp_option_append(uint8_t options[], size_t size, size_t *offset,
30 uint8_t code, size_t optlen, const void *optval) {
34 if (code != DHCP_OPTION_END)
35 /* always make sure there is space for an END option */
42 if (size - *offset < 1)
45 options[*offset] = code;
50 if (size - *offset < optlen + 2)
55 options[*offset] = code;
56 options[*offset + 1] = optlen;
57 memcpy(&options[*offset + 2], optval, optlen);
59 *offset += optlen + 2;
67 static int parse_options(const uint8_t options[], size_t buflen, uint8_t *overload,
68 uint8_t *message_type, dhcp_option_cb_t cb,
73 while (offset < buflen) {
74 switch (options[offset]) {
83 case DHCP_OPTION_MESSAGE_TYPE:
84 if (buflen < offset + 3)
87 len = options[++offset];
92 *message_type = options[++offset];
100 case DHCP_OPTION_OVERLOAD:
101 if (buflen < offset + 3)
104 len = options[++offset];
109 *overload = options[++offset];
118 if (buflen < offset + 3)
121 code = options[offset];
122 len = options[++offset];
124 if (buflen < ++offset + len)
128 cb(code, len, &options[offset], user_data);
142 int dhcp_option_parse(DHCPMessage *message, size_t len,
143 dhcp_option_cb_t cb, void *user_data) {
144 uint8_t overload = 0;
145 uint8_t message_type = 0;
151 if (len < sizeof(DHCPMessage))
154 len -= sizeof(DHCPMessage);
156 r = parse_options(message->options, len, &overload, &message_type,
161 if (overload & DHCP_OVERLOAD_FILE) {
162 r = parse_options(message->file, sizeof(message->file),
163 NULL, &message_type, cb, user_data);
168 if (overload & DHCP_OVERLOAD_SNAME) {
169 r = parse_options(message->sname, sizeof(message->sname),
170 NULL, &message_type, cb, user_data);