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 "alloc-util.h"
30 #include "dhcp-internal.h"
32 static int option_append(uint8_t options
[], size_t size
, size_t *offset
,
33 uint8_t code
, size_t optlen
, const void *optval
) {
37 if (code
!= DHCP_OPTION_END
)
38 /* always make sure there is space for an END option */
45 if (size
< *offset
+ 1)
48 options
[*offset
] = code
;
53 if (size
< *offset
+ optlen
+ 2)
56 options
[*offset
] = code
;
57 options
[*offset
+ 1] = optlen
;
62 memcpy(&options
[*offset
+ 2], optval
, optlen
);
65 *offset
+= optlen
+ 2;
73 int dhcp_option_append(DHCPMessage
*message
, size_t size
, size_t *offset
,
75 uint8_t code
, size_t optlen
, const void *optval
) {
76 size_t file_offset
= 0, sname_offset
=0;
83 file
= overload
& DHCP_OVERLOAD_FILE
;
84 sname
= overload
& DHCP_OVERLOAD_SNAME
;
87 /* still space in the options array */
88 r
= option_append(message
->options
, size
, offset
, code
, optlen
, optval
);
91 else if (r
== -ENOBUFS
&& (file
|| sname
)) {
92 /* did not fit, but we have more buffers to try
93 close the options array and move the offset to its end */
94 r
= option_append(message
->options
, size
, offset
, DHCP_OPTION_END
, 0, NULL
);
103 if (overload
& DHCP_OVERLOAD_FILE
) {
104 file_offset
= *offset
- size
;
106 if (file_offset
< sizeof(message
->file
)) {
107 /* still space in the 'file' array */
108 r
= option_append(message
->file
, sizeof(message
->file
), &file_offset
, code
, optlen
, optval
);
110 *offset
= size
+ file_offset
;
112 } else if (r
== -ENOBUFS
&& sname
) {
113 /* did not fit, but we have more buffers to try
114 close the file array and move the offset to its end */
115 r
= option_append(message
->options
, size
, offset
, DHCP_OPTION_END
, 0, NULL
);
119 *offset
= size
+ sizeof(message
->file
);
125 if (overload
& DHCP_OVERLOAD_SNAME
) {
126 sname_offset
= *offset
- size
- (file
? sizeof(message
->file
) : 0);
128 if (sname_offset
< sizeof(message
->sname
)) {
129 /* still space in the 'sname' array */
130 r
= option_append(message
->sname
, sizeof(message
->sname
), &sname_offset
, code
, optlen
, optval
);
132 *offset
= size
+ (file
? sizeof(message
->file
) : 0) + sname_offset
;
135 /* no space, or other error, give up */
144 static int parse_options(const uint8_t options
[], size_t buflen
, uint8_t *overload
,
145 uint8_t *message_type
, char **error_message
, dhcp_option_cb_t cb
,
148 const uint8_t *option
;
151 while (offset
< buflen
) {
152 code
= options
[offset
++];
155 case DHCP_OPTION_PAD
:
158 case DHCP_OPTION_END
:
162 if (buflen
< offset
+ 1)
165 len
= options
[offset
++];
167 if (buflen
< offset
+ len
)
170 option
= &options
[offset
];
173 case DHCP_OPTION_MESSAGE_TYPE
:
178 *message_type
= *option
;
182 case DHCP_OPTION_ERROR_MESSAGE
:
187 _cleanup_free_
char *string
= NULL
;
189 /* Accept a trailing NUL byte */
190 if (memchr(option
, 0, len
- 1))
193 string
= strndup((const char *) option
, len
);
197 if (!ascii_is_valid(string
))
200 free(*error_message
);
201 *error_message
= string
;
206 case DHCP_OPTION_OVERLOAD
:
217 cb(code
, len
, option
, userdata
);
231 int dhcp_option_parse(DHCPMessage
*message
, size_t len
, dhcp_option_cb_t cb
, void *userdata
, char **_error_message
) {
232 _cleanup_free_
char *error_message
= NULL
;
233 uint8_t overload
= 0;
234 uint8_t message_type
= 0;
240 if (len
< sizeof(DHCPMessage
))
243 len
-= sizeof(DHCPMessage
);
245 r
= parse_options(message
->options
, len
, &overload
, &message_type
, &error_message
, cb
, userdata
);
249 if (overload
& DHCP_OVERLOAD_FILE
) {
250 r
= parse_options(message
->file
, sizeof(message
->file
), NULL
, &message_type
, &error_message
, cb
, userdata
);
255 if (overload
& DHCP_OVERLOAD_SNAME
) {
256 r
= parse_options(message
->sname
, sizeof(message
->sname
), NULL
, &message_type
, &error_message
, cb
, userdata
);
261 if (message_type
== 0)
264 if (_error_message
&& IN_SET(message_type
, DHCP_NAK
, DHCP_DECLINE
)) {
265 *_error_message
= error_message
;
266 error_message
= NULL
;