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 **buf, size_t *buflen, uint8_t code,
30 size_t optlen, const void *optval)
48 if (*buflen < optlen + 2)
56 memcpy(&(*buf)[2], optval, optlen);
59 *buflen -= (optlen + 2);
67 static int parse_options(const uint8_t *buf, size_t buflen, uint8_t *overload,
68 uint8_t *message_type, dhcp_option_cb_t cb,
71 const uint8_t *code = buf;
84 case DHCP_OPTION_MESSAGE_TYPE:
94 *message_type = *(len + 1);
100 case DHCP_OPTION_OVERLOAD:
110 *overload = *(len + 1);
122 if (buflen < (size_t)*len + 2)
127 cb(*code, *len, len + 1, user_data);
141 int dhcp_option_parse(DHCPMessage *message, size_t len,
142 dhcp_option_cb_t cb, void *user_data)
144 uint8_t overload = 0;
145 uint8_t message_type = 0;
146 uint8_t *opt = (uint8_t *)(message + 1);
152 if (len < sizeof(DHCPMessage) + 4)
155 len -= sizeof(DHCPMessage) + 4;
157 if (opt[0] != 0x63 && opt[1] != 0x82 && opt[2] != 0x53 &&
161 res = parse_options(&opt[4], len, &overload, &message_type,
166 if (overload & DHCP_OVERLOAD_FILE) {
167 res = parse_options(message->file, sizeof(message->file),
168 NULL, &message_type, cb, user_data);
173 if (overload & DHCP_OVERLOAD_SNAME) {
174 res = parse_options(message->sname, sizeof(message->sname),
175 NULL, &message_type, cb, user_data);