int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
const void *packet, size_t len);
-int dhcp_option_append(uint8_t options[], size_t size, size_t *offset,
+int dhcp_option_append(DHCPMessage *message, size_t size, size_t *offset, uint8_t overload,
uint8_t code, size_t optlen, const void *optval);
typedef int (*dhcp_option_cb_t)(uint8_t code, uint8_t len,
dhcp_option_cb_t cb, void *user_data);
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid, uint8_t type,
- uint8_t options[], size_t optlen, size_t *optoffset);
+ size_t optlen, size_t *optoffset);
uint16_t dhcp_packet_checksum(void *buf, size_t len);
#include "dhcp-internal.h"
-int dhcp_option_append(uint8_t options[], size_t size, size_t *offset,
- uint8_t code, size_t optlen, const void *optval) {
+static int option_append(uint8_t options[], size_t size, size_t *offset,
+ uint8_t code, size_t optlen, const void *optval) {
assert(options);
assert(offset);
case DHCP_OPTION_PAD:
case DHCP_OPTION_END:
- if (size - *offset < 1)
+ if (size < *offset + 1)
return -ENOBUFS;
options[*offset] = code;
break;
default:
- if (size - *offset < optlen + 2)
+ if (size < *offset + optlen + 2)
return -ENOBUFS;
- assert(optval);
-
options[*offset] = code;
options[*offset + 1] = optlen;
- memcpy(&options[*offset + 2], optval, optlen);
+
+ if (optlen) {
+ assert(optval);
+
+ memcpy(&options[*offset + 2], optval, optlen);
+ }
*offset += optlen + 2;
return 0;
}
+int dhcp_option_append(DHCPMessage *message, size_t size, size_t *offset,
+ uint8_t overload,
+ uint8_t code, size_t optlen, const void *optval) {
+ size_t file_offset = 0, sname_offset =0;
+ bool file, sname;
+ int r;
+
+ assert(message);
+ assert(offset);
+
+ file = overload & DHCP_OVERLOAD_FILE;
+ sname = overload & DHCP_OVERLOAD_SNAME;
+
+ if (*offset < size) {
+ /* still space in the options array */
+ r = option_append(message->options, size, offset, code, optlen, optval);
+ if (r >= 0)
+ return 0;
+ else if (r == -ENOBUFS && (file || sname)) {
+ /* did not fit, but we have more buffers to try
+ close the options array and move the offset to its end */
+ r = option_append(message->options, size, offset, DHCP_OPTION_END, 0, NULL);
+ if (r < 0)
+ return r;
+
+ *offset = size;
+ } else
+ return r;
+ }
+
+ if (overload & DHCP_OVERLOAD_FILE) {
+ file_offset = *offset - size;
+
+ if (file_offset < sizeof(message->file)) {
+ /* still space in the 'file' array */
+ r = option_append(message->file, sizeof(message->file), &file_offset, code, optlen, optval);
+ if (r >= 0) {
+ *offset = size + file_offset;
+ return 0;
+ } else if (r == -ENOBUFS && sname) {
+ /* did not fit, but we have more buffers to try
+ close the file array and move the offset to its end */
+ r = option_append(message->options, size, offset, DHCP_OPTION_END, 0, NULL);
+ if (r < 0)
+ return r;
+
+ *offset = size + sizeof(message->file);
+ } else
+ return r;
+ }
+ }
+
+ if (overload & DHCP_OVERLOAD_SNAME) {
+ sname_offset = *offset - size - (file ? sizeof(message->file) : 0);
+
+ if (sname_offset < sizeof(message->sname)) {
+ /* still space in the 'sname' array */
+ r = option_append(message->sname, sizeof(message->sname), &sname_offset, code, optlen, optval);
+ if (r >= 0) {
+ *offset = size + (file ? sizeof(message->file) : 0) + sname_offset;
+ return 0;
+ } else {
+ /* no space, or other error, give up */
+ return r;
+ }
+ }
+ }
+
+ return -ENOBUFS;
+}
+
static int parse_options(const uint8_t options[], size_t buflen, uint8_t *overload,
uint8_t *message_type, dhcp_option_cb_t cb,
void *user_data) {
#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
- uint8_t type, uint8_t options[], size_t optlen, size_t *optoffset) {
+ uint8_t type, size_t optlen, size_t *optoffset) {
size_t offset = 0;
int r;
message->xid = htobe32(xid);
message->magic = htobe32(DHCP_MAGIC_COOKIE);
- r = dhcp_option_append(message->options, optlen, &offset,
- DHCP_OPTION_MESSAGE_TYPE, 1, &type);
+ r = dhcp_option_append(message, optlen, &offset, 0,
+ DHCP_OPTION_MESSAGE_TYPE, 1, &type);
if (r < 0)
return r;
assert(type == DHCP_DISCOVER || type == DHCP_REQUEST);
r = dhcp_message_init(message, BOOTREQUEST, client->xid, type,
- message->options, optlen, optoffset);
+ optlen, optoffset);
if (r < 0)
return r;
/* Some DHCP servers will refuse to issue an DHCP lease if the Client
Identifier option is not set */
- r = dhcp_option_append(message->options, optlen, optoffset,
+ r = dhcp_option_append(message, optlen, optoffset, 0,
DHCP_OPTION_CLIENT_IDENTIFIER,
sizeof(client->client_id), &client->client_id);
if (r < 0)
it MUST include that list in any subsequent DHCPREQUEST
messages.
*/
- r = dhcp_option_append(message->options, optlen, optoffset,
+ r = dhcp_option_append(message, optlen, optoffset, 0,
DHCP_OPTION_PARAMETER_REQUEST_LIST,
client->req_opts_size, client->req_opts);
if (r < 0)
*/
max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
DHCP_MIN_OPTIONS_SIZE);
- r = dhcp_option_append(message->options, optlen, optoffset,
+ r = dhcp_option_append(message, optlen, optoffset, 0,
DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
2, &max_size);
if (r < 0)
option to suggest the lease time it would like.
*/
if (client->last_addr != INADDR_ANY) {
- r = dhcp_option_append(discover->dhcp.options, optlen, &optoffset,
+ r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
DHCP_OPTION_REQUESTED_IP_ADDRESS,
4, &client->last_addr);
if (r < 0)
return r;
}
- r = dhcp_option_append(discover->dhcp.options, optlen, &optoffset,
+ r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
DHCP_OPTION_END, 0, NULL);
/* We currently ignore:
filled in with the yiaddr value from the chosen DHCPOFFER.
*/
- r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
+ r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
DHCP_OPTION_SERVER_IDENTIFIER,
4, &client->lease->server_address);
if (r < 0)
return r;
- r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
+ r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
DHCP_OPTION_REQUESTED_IP_ADDRESS,
4, &client->lease->address);
if (r < 0)
option MUST be filled in with client’s notion of its previously
assigned address. ’ciaddr’ MUST be zero.
*/
- r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
+ r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
DHCP_OPTION_REQUESTED_IP_ADDRESS,
4, &client->last_addr);
if (r < 0)
return -EINVAL;
}
- r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
+ r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
DHCP_OPTION_END, 0, NULL);
if (r < 0)
return r;
message = malloc0(len);
assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678,
- DHCP_DISCOVER, message->options, optlen, &optoffset) >= 0);
+ DHCP_DISCOVER, optlen, &optoffset) >= 0);
assert_se(message->xid == htobe32(0x12345678));
assert_se(message->op == BOOTREQUEST);
printf("DHCP type %s\n", dhcp_type(res));
}
-static uint8_t result[64] = {
- 'A', 'B', 'C', 'D',
-};
-
static uint8_t options[64] = {
'A', 'B', 'C', 'D',
160, 2, 0x11, 0x12,
static void test_option_set(void)
{
+ DHCPMessage *result;
size_t offset = 0, len, pos;
unsigned i;
- assert_se(dhcp_option_append(result, 0, &offset, DHCP_OPTION_PAD,
- 0, NULL) == -ENOBUFS);
+ result = malloc0(sizeof(DHCPMessage) + 11);
+ assert_se(result);
+
+ result->options[0] = 'A';
+ result->options[1] = 'B';
+ result->options[2] = 'C';
+ result->options[3] = 'D';
+
+ assert_se(dhcp_option_append(result, 0, &offset, 0, DHCP_OPTION_PAD,
+ 0, NULL) == -ENOBUFS);
assert_se(offset == 0);
offset = 4;
- assert_se(dhcp_option_append(result, 1, &offset, DHCP_OPTION_PAD,
- 0, NULL) >= 0);
+ assert_se(dhcp_option_append(result, 5, &offset, 0, DHCP_OPTION_PAD,
+ 0, NULL) == -ENOBUFS);
+ assert_se(offset == 4);
+ assert_se(dhcp_option_append(result, 6, &offset, 0, DHCP_OPTION_PAD,
+ 0, NULL) >= 0);
assert_se(offset == 5);
offset = pos = 4;
- len = 60;
- while (pos < 64 && options[pos] != DHCP_OPTION_END) {
- offset = pos;
-
- assert_se(dhcp_option_append(result, len, &offset,
+ len = 11;
+ while (pos < len && options[pos] != DHCP_OPTION_END) {
+ assert_se(dhcp_option_append(result, len, &offset, DHCP_OVERLOAD_SNAME,
options[pos],
options[pos + 1],
&options[pos + 2]) >= 0);
else
pos += 2 + options[pos + 1];
- assert_se(offset == pos);
+ if (pos < len)
+ assert_se(offset == pos);
}
- for (i = 0; i < pos; i++) {
+ for (i = 0; i < 9; i++) {
if (verbose)
- printf("%2d: 0x%02x(0x%02x)\n", i, result[i],
+ printf("%2d: 0x%02x(0x%02x) (options)\n", i, result->options[i],
options[i]);
- assert_se(result[i] == options[i]);
+ assert_se(result->options[i] == options[i]);
+ }
+
+ if (verbose)
+ printf("%2d: 0x%02x(0x%02x) (options)\n", 9, result->options[9],
+ DHCP_OPTION_END);
+
+ assert_se(result->options[9] == DHCP_OPTION_END);
+
+ if (verbose)
+ printf("%2d: 0x%02x(0x%02x) (options)\n", 10, result->options[10],
+ DHCP_OPTION_PAD);
+
+ assert_se(result->options[10] == DHCP_OPTION_PAD);
+
+ for (i = 0; i < pos - 8; i++) {
+ if (verbose)
+ printf("%2d: 0x%02x(0x%02x) (sname)\n", i, result->sname[i],
+ options[i + 9]);
+ assert_se(result->sname[i] == options[i + 9]);
}
if (verbose)