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.
int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
const void *packet, size_t len);
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 **buf, size_t *buflen, uint8_t code,
- size_t optlen, const void *optval);
+int dhcp_option_append(uint8_t options[], size_t size, size_t *offset,
+ uint8_t code, size_t optlen, const void *optval);
typedef int (*dhcp_option_cb_t)(uint8_t code, uint8_t len,
const uint8_t *option, void *user_data);
typedef int (*dhcp_option_cb_t)(uint8_t code, uint8_t len,
const uint8_t *option, void *user_data);
dhcp_option_cb_t cb, void *user_data);
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid, uint8_t type,
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 **opt, size_t *optlen);
+ uint8_t options[], size_t optlen, size_t *optoffset);
uint16_t dhcp_packet_checksum(void *buf, size_t len);
uint16_t dhcp_packet_checksum(void *buf, size_t len);
#include "dhcp-internal.h"
#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:
switch (code) {
case DHCP_OPTION_PAD:
case DHCP_OPTION_END:
+ if (size - *offset < 1)
- (*buf)[0] = code;
- *buf += 1;
- *buflen -= 1;
+ options[*offset] = code;
+ *offset += 1;
- if (*buflen < optlen + 2)
+ if (size - *offset < optlen + 2)
- if (!optval)
- return -EINVAL;
- (*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);
{
uint8_t overload = 0;
uint8_t message_type = 0;
{
uint8_t overload = 0;
uint8_t message_type = 0;
- uint8_t *opt = (uint8_t *)(message + 1);
len -= sizeof(DHCPMessage);
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;
cb, user_data);
if (res < 0)
return res;
#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
- uint8_t type, uint8_t **opt, size_t *optlen) {
- int err;
+ uint8_t type, uint8_t options[], size_t optlen, size_t *optoffset) {
+ size_t offset = 0;
+ int r;
assert(op == BOOTREQUEST || op == BOOTREPLY);
assert(op == BOOTREQUEST || op == BOOTREPLY);
message->xid = htobe32(xid);
message->magic = htobe32(DHCP_MAGIC_COOKIE);
message->xid = htobe32(xid);
message->magic = htobe32(DHCP_MAGIC_COOKIE);
- *opt = (uint8_t *)(message + 1);
+ r = dhcp_option_append(message->options, optlen, &offset,
+ DHCP_OPTION_MESSAGE_TYPE, 1, &type);
+ if (r < 0)
+ return r;
- err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
- &type);
- if (err < 0)
- return err;
uint8_t sname[64];
uint8_t file[128];
be32_t magic;
uint8_t sname[64];
uint8_t file[128];
be32_t magic;
} _packed_;
typedef struct DHCPMessage DHCPMessage;
} _packed_;
typedef struct DHCPMessage DHCPMessage;
}
static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
}
static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
- uint8_t type, uint8_t **opt, size_t *optlen) {
+ uint8_t type, size_t optlen, size_t *optoffset) {
be16_t max_size;
int r;
assert(client);
assert(client->secs);
assert(message);
be16_t max_size;
int r;
assert(client);
assert(client->secs);
assert(message);
- assert(opt);
- assert(optlen);
assert(type == DHCP_DISCOVER || type == DHCP_REQUEST);
assert(type == DHCP_DISCOVER || type == DHCP_REQUEST);
- r = dhcp_message_init(message, BOOTREQUEST, client->xid, type, opt,
- optlen);
+ r = dhcp_message_init(message, BOOTREQUEST, client->xid, type,
+ message->options, optlen, optoffset);
/* Some DHCP servers will refuse to issue an DHCP lease if the Client
Identifier option is not set */
/* Some DHCP servers will refuse to issue an DHCP lease if the Client
Identifier option is not set */
- r = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
+ r = dhcp_option_append(message->options, optlen, optoffset,
+ DHCP_OPTION_CLIENT_IDENTIFIER,
sizeof(client->client_id), &client->client_id);
if (r < 0)
return r;
sizeof(client->client_id), &client->client_id);
if (r < 0)
return r;
it MUST include that list in any subsequent DHCPREQUEST
messages.
*/
it MUST include that list in any subsequent DHCPREQUEST
messages.
*/
- r = dhcp_option_append(opt, optlen,
+ r = dhcp_option_append(message->options, optlen, optoffset,
DHCP_OPTION_PARAMETER_REQUEST_LIST,
DHCP_OPTION_PARAMETER_REQUEST_LIST,
- client->req_opts_size,
- client->req_opts);
+ client->req_opts_size, client->req_opts);
*/
max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
DHCP_MIN_OPTIONS_SIZE);
*/
max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
DHCP_MIN_OPTIONS_SIZE);
- r = dhcp_option_append(opt, optlen,
+ r = dhcp_option_append(message->options, optlen, optoffset,
DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
2, &max_size);
if (r < 0)
DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
2, &max_size);
if (r < 0)
static int client_send_discover(sd_dhcp_client *client) {
_cleanup_free_ DHCPPacket *discover = NULL;
static int client_send_discover(sd_dhcp_client *client) {
_cleanup_free_ DHCPPacket *discover = NULL;
- size_t optlen, len;
- uint8_t *opt;
+ size_t optoffset, optlen, len;
return -ENOMEM;
r = client_message_init(client, &discover->dhcp, DHCP_DISCOVER,
return -ENOMEM;
r = client_message_init(client, &discover->dhcp, DHCP_DISCOVER,
option to suggest the lease time it would like.
*/
if (client->last_addr != INADDR_ANY) {
option to suggest the lease time it would like.
*/
if (client->last_addr != INADDR_ANY) {
- r = dhcp_option_append(&opt, &optlen,
- DHCP_OPTION_REQUESTED_IP_ADDRESS,
- 4, &client->last_addr);
+ r = dhcp_option_append(discover->dhcp.options, optlen, &optoffset,
+ DHCP_OPTION_REQUESTED_IP_ADDRESS,
+ 4, &client->last_addr);
- r = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
- if (r < 0)
- return r;
+ r = dhcp_option_append(discover->dhcp.options, optlen, &optoffset,
+ DHCP_OPTION_END, 0, NULL);
/* We currently ignore:
The client SHOULD wait a random time between one and ten seconds to
desynchronize the use of DHCP at startup.
*/
/* We currently ignore:
The client SHOULD wait a random time between one and ten seconds to
desynchronize the use of DHCP at startup.
*/
- r = dhcp_client_send_raw(client, discover, len - optlen);
+ r = dhcp_client_send_raw(client, discover, sizeof(DHCPPacket) + optoffset);
static int client_send_request(sd_dhcp_client *client) {
_cleanup_free_ DHCPPacket *request;
static int client_send_request(sd_dhcp_client *client) {
_cleanup_free_ DHCPPacket *request;
- size_t optlen, len;
- uint8_t *opt;
+ size_t optoffset, optlen, len;
int r;
optlen = DHCP_MIN_OPTIONS_SIZE;
int r;
optlen = DHCP_MIN_OPTIONS_SIZE;
if (!request)
return -ENOMEM;
if (!request)
return -ENOMEM;
- r = client_message_init(client, &request->dhcp, DHCP_REQUEST, &opt,
- &optlen);
+ r = client_message_init(client, &request->dhcp, DHCP_REQUEST,
+ optlen, &optoffset);
filled in with the yiaddr value from the chosen DHCPOFFER.
*/
filled in with the yiaddr value from the chosen DHCPOFFER.
*/
- r = dhcp_option_append(&opt, &optlen,
+ r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
DHCP_OPTION_SERVER_IDENTIFIER,
4, &client->lease->server_address);
if (r < 0)
return r;
DHCP_OPTION_SERVER_IDENTIFIER,
4, &client->lease->server_address);
if (r < 0)
return r;
- r = dhcp_option_append(&opt, &optlen,
+ r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
DHCP_OPTION_REQUESTED_IP_ADDRESS,
4, &client->lease->address);
if (r < 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.
*/
option MUST be filled in with client’s notion of its previously
assigned address. ’ciaddr’ MUST be zero.
*/
- r = dhcp_option_append(&opt, &optlen,
+ r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
DHCP_OPTION_REQUESTED_IP_ADDRESS,
4, &client->last_addr);
if (r < 0)
DHCP_OPTION_REQUESTED_IP_ADDRESS,
4, &client->last_addr);
if (r < 0)
- r = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
+ r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
+ DHCP_OPTION_END, 0, NULL);
client->lease->server_address,
DHCP_PORT_SERVER,
&request->dhcp,
client->lease->server_address,
DHCP_PORT_SERVER,
&request->dhcp,
- len - optlen - DHCP_IP_UDP_SIZE);
+ sizeof(DHCPMessage) + optoffset);
- r = dhcp_client_send_raw(client, request, len - optlen);
+ r = dhcp_client_send_raw(client, request, sizeof(DHCPPacket) + optoffset);
static void test_message_init(void)
{
_cleanup_free_ DHCPMessage *message = NULL;
static void test_message_init(void)
{
_cleanup_free_ DHCPMessage *message = NULL;
+ size_t optlen = 3, optoffset;
size_t len = sizeof(DHCPMessage) + optlen;
size_t len = sizeof(DHCPMessage) + optlen;
- opt = (uint8_t *)(message + 1);
-
assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678,
assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678,
- DHCP_DISCOVER, &opt, &optlen) >= 0);
+ DHCP_DISCOVER, message->options, optlen, &optoffset) >= 0);
assert_se(message->xid == htobe32(0x12345678));
assert_se(message->op == BOOTREQUEST);
assert_se(message->xid == htobe32(0x12345678));
assert_se(message->op == BOOTREQUEST);
{
DHCPMessage *message;
size_t len = sizeof(DHCPMessage) + optlen;
{
DHCPMessage *message;
size_t len = sizeof(DHCPMessage) + optlen;
- opt = (uint8_t *)(message + 1);
- memcpy(opt, options, optlen);
+ memcpy(&message->options, options, optlen);
if (file && filelen <= 128)
memcpy(&message->file, file, filelen);
if (file && filelen <= 128)
memcpy(&message->file, file, filelen);
static void test_option_set(void)
{
static void test_option_set(void)
{
- size_t len, oldlen;
- int pos, i;
- uint8_t *opt;
-
- assert_se(dhcp_option_append(NULL, NULL, 0, 0, NULL) == -EINVAL);
-
- len = 0;
- opt = &result[0];
- assert_se(dhcp_option_append(&opt, NULL, 0, 0, NULL) == -EINVAL);
- assert_se(opt == &result[0] && len == 0);
+ size_t offset = 0, len, pos;
+ unsigned i;
- assert_se(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD,
+ assert_se(dhcp_option_append(result, 0, &offset, DHCP_OPTION_PAD,
- assert_se(opt == &result[0] && len == 0);
+ assert_se(offset == 0);
- opt = &result[4];
- len = 1;
- assert_se(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD,
+ offset = 4;
+ assert_se(dhcp_option_append(result, 1, &offset, DHCP_OPTION_PAD,
- assert_se(opt == &result[5] && len == 0);
+ assert_se(offset == 5);
len = 60;
while (pos < 64 && options[pos] != DHCP_OPTION_END) {
len = 60;
while (pos < 64 && options[pos] != DHCP_OPTION_END) {
- opt = &result[pos];
- oldlen = len;
- assert_se(dhcp_option_append(&opt, &len, options[pos],
- options[pos + 1],
- &options[pos + 2]) >= 0);
+ assert_se(dhcp_option_append(result, len, &offset,
+ options[pos],
+ options[pos + 1],
+ &options[pos + 2]) >= 0);
- if (options[pos] == DHCP_OPTION_PAD) {
- assert_se(opt == &result[pos + 1]);
- assert_se(len == oldlen - 1);
+ if (options[pos] == DHCP_OPTION_PAD)
- } else {
- assert_se(opt == &result[pos + 2 + options[pos + 1]]);
- assert_se(len == oldlen - 2 - options[pos + 1]);
pos += 2 + options[pos + 1];
pos += 2 + options[pos + 1];
+
+ assert_se(offset == pos);
}
for (i = 0; i < pos; i++) {
}
for (i = 0; i < pos; i++) {