X Tutup
Skip to content

Commit 20b958b

Browse files
committed
sd-dhcp: refactor dhcp_option_append
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.
1 parent ece6e76 commit 20b958b

File tree

6 files changed

+74
-96
lines changed

6 files changed

+74
-96
lines changed

src/libsystemd-network/dhcp-internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
3636
int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
3737
const void *packet, size_t len);
3838

39-
int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
40-
size_t optlen, const void *optval);
39+
int dhcp_option_append(uint8_t options[], size_t size, size_t *offset,
40+
uint8_t code, size_t optlen, const void *optval);
4141

4242
typedef int (*dhcp_option_cb_t)(uint8_t code, uint8_t len,
4343
const uint8_t *option, void *user_data);
@@ -46,7 +46,7 @@ int dhcp_option_parse(DHCPMessage *message, size_t len,
4646
dhcp_option_cb_t cb, void *user_data);
4747

4848
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid, uint8_t type,
49-
uint8_t **opt, size_t *optlen);
49+
uint8_t options[], size_t optlen, size_t *optoffset);
5050

5151
uint16_t dhcp_packet_checksum(void *buf, size_t len);
5252

src/libsystemd-network/dhcp-option.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,33 @@
2626

2727
#include "dhcp-internal.h"
2828

29-
int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
30-
size_t optlen, const void *optval)
31-
{
32-
if (!buf || !buflen)
33-
return -EINVAL;
29+
int dhcp_option_append(uint8_t options[], size_t size, size_t *offset,
30+
uint8_t code, size_t optlen, const void *optval) {
31+
assert(options);
32+
assert(offset);
3433

3534
switch (code) {
3635

3736
case DHCP_OPTION_PAD:
3837
case DHCP_OPTION_END:
39-
if (*buflen < 1)
38+
if (size - *offset < 1)
4039
return -ENOBUFS;
4140

42-
(*buf)[0] = code;
43-
*buf += 1;
44-
*buflen -= 1;
41+
options[*offset] = code;
42+
*offset += 1;
4543
break;
4644

4745
default:
48-
if (*buflen < optlen + 2)
46+
if (size - *offset < optlen + 2)
4947
return -ENOBUFS;
5048

51-
if (!optval)
52-
return -EINVAL;
49+
assert(optval);
5350

54-
(*buf)[0] = code;
55-
(*buf)[1] = optlen;
56-
memcpy(&(*buf)[2], optval, optlen);
51+
options[*offset] = code;
52+
options[*offset + 1] = optlen;
53+
memcpy(&options[*offset + 2], optval, optlen);
5754

58-
*buf += optlen + 2;
59-
*buflen -= (optlen + 2);
55+
*offset += optlen + 2;
6056

6157
break;
6258
}
@@ -143,7 +139,6 @@ int dhcp_option_parse(DHCPMessage *message, size_t len,
143139
{
144140
uint8_t overload = 0;
145141
uint8_t message_type = 0;
146-
uint8_t *opt = (uint8_t *)(message + 1);
147142
int res;
148143

149144
if (!message)
@@ -154,7 +149,7 @@ int dhcp_option_parse(DHCPMessage *message, size_t len,
154149

155150
len -= sizeof(DHCPMessage);
156151

157-
res = parse_options(opt, len, &overload, &message_type,
152+
res = parse_options(message->options, len, &overload, &message_type,
158153
cb, user_data);
159154
if (res < 0)
160155
return res;

src/libsystemd-network/dhcp-packet.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
3939

4040
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
41-
uint8_t type, uint8_t **opt, size_t *optlen) {
42-
int err;
41+
uint8_t type, uint8_t options[], size_t optlen, size_t *optoffset) {
42+
size_t offset = 0;
43+
int r;
4344

4445
assert(op == BOOTREQUEST || op == BOOTREPLY);
4546

@@ -49,12 +50,12 @@ int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
4950
message->xid = htobe32(xid);
5051
message->magic = htobe32(DHCP_MAGIC_COOKIE);
5152

52-
*opt = (uint8_t *)(message + 1);
53+
r = dhcp_option_append(message->options, optlen, &offset,
54+
DHCP_OPTION_MESSAGE_TYPE, 1, &type);
55+
if (r < 0)
56+
return r;
5357

54-
err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
55-
&type);
56-
if (err < 0)
57-
return err;
58+
*optoffset = offset;
5859

5960
return 0;
6061
}

src/libsystemd-network/dhcp-protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct DHCPMessage {
4444
uint8_t sname[64];
4545
uint8_t file[128];
4646
be32_t magic;
47+
uint8_t options[0];
4748
} _packed_;
4849

4950
typedef struct DHCPMessage DHCPMessage;

src/libsystemd-network/sd-dhcp-client.c

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,18 @@ static sd_dhcp_client *client_stop(sd_dhcp_client *client, int error) {
257257
}
258258

259259
static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
260-
uint8_t type, uint8_t **opt, size_t *optlen) {
260+
uint8_t type, size_t optlen, size_t *optoffset) {
261261
be16_t max_size;
262262
int r;
263263

264264
assert(client);
265265
assert(client->secs);
266266
assert(message);
267-
assert(opt);
268-
assert(optlen);
267+
assert(optoffset);
269268
assert(type == DHCP_DISCOVER || type == DHCP_REQUEST);
270269

271-
r = dhcp_message_init(message, BOOTREQUEST, client->xid, type, opt,
272-
optlen);
270+
r = dhcp_message_init(message, BOOTREQUEST, client->xid, type,
271+
message->options, optlen, optoffset);
273272
if (r < 0)
274273
return r;
275274

@@ -285,7 +284,8 @@ static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
285284

286285
/* Some DHCP servers will refuse to issue an DHCP lease if the Client
287286
Identifier option is not set */
288-
r = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
287+
r = dhcp_option_append(message->options, optlen, optoffset,
288+
DHCP_OPTION_CLIENT_IDENTIFIER,
289289
sizeof(client->client_id), &client->client_id);
290290
if (r < 0)
291291
return r;
@@ -299,10 +299,9 @@ static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
299299
it MUST include that list in any subsequent DHCPREQUEST
300300
messages.
301301
*/
302-
r = dhcp_option_append(opt, optlen,
302+
r = dhcp_option_append(message->options, optlen, optoffset,
303303
DHCP_OPTION_PARAMETER_REQUEST_LIST,
304-
client->req_opts_size,
305-
client->req_opts);
304+
client->req_opts_size, client->req_opts);
306305
if (r < 0)
307306
return r;
308307

@@ -316,7 +315,7 @@ static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
316315
*/
317316
max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
318317
DHCP_MIN_OPTIONS_SIZE);
319-
r = dhcp_option_append(opt, optlen,
318+
r = dhcp_option_append(message->options, optlen, optoffset,
320319
DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
321320
2, &max_size);
322321
if (r < 0)
@@ -336,8 +335,7 @@ static int dhcp_client_send_raw(sd_dhcp_client *client, DHCPPacket *packet,
336335

337336
static int client_send_discover(sd_dhcp_client *client) {
338337
_cleanup_free_ DHCPPacket *discover = NULL;
339-
size_t optlen, len;
340-
uint8_t *opt;
338+
size_t optoffset, optlen, len;
341339
usec_t time_now;
342340
int r;
343341

@@ -364,7 +362,7 @@ static int client_send_discover(sd_dhcp_client *client) {
364362
return -ENOMEM;
365363

366364
r = client_message_init(client, &discover->dhcp, DHCP_DISCOVER,
367-
&opt, &optlen);
365+
optlen, &optoffset);
368366
if (r < 0)
369367
return r;
370368

@@ -375,22 +373,21 @@ static int client_send_discover(sd_dhcp_client *client) {
375373
option to suggest the lease time it would like.
376374
*/
377375
if (client->last_addr != INADDR_ANY) {
378-
r = dhcp_option_append(&opt, &optlen,
379-
DHCP_OPTION_REQUESTED_IP_ADDRESS,
380-
4, &client->last_addr);
376+
r = dhcp_option_append(discover->dhcp.options, optlen, &optoffset,
377+
DHCP_OPTION_REQUESTED_IP_ADDRESS,
378+
4, &client->last_addr);
381379
if (r < 0)
382380
return r;
383381
}
384382

385-
r = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
386-
if (r < 0)
387-
return r;
383+
r = dhcp_option_append(discover->dhcp.options, optlen, &optoffset,
384+
DHCP_OPTION_END, 0, NULL);
388385

389386
/* We currently ignore:
390387
The client SHOULD wait a random time between one and ten seconds to
391388
desynchronize the use of DHCP at startup.
392389
*/
393-
r = dhcp_client_send_raw(client, discover, len - optlen);
390+
r = dhcp_client_send_raw(client, discover, sizeof(DHCPPacket) + optoffset);
394391
if (r < 0)
395392
return r;
396393

@@ -401,8 +398,7 @@ static int client_send_discover(sd_dhcp_client *client) {
401398

402399
static int client_send_request(sd_dhcp_client *client) {
403400
_cleanup_free_ DHCPPacket *request;
404-
size_t optlen, len;
405-
uint8_t *opt;
401+
size_t optoffset, optlen, len;
406402
int r;
407403

408404
optlen = DHCP_MIN_OPTIONS_SIZE;
@@ -412,8 +408,8 @@ static int client_send_request(sd_dhcp_client *client) {
412408
if (!request)
413409
return -ENOMEM;
414410

415-
r = client_message_init(client, &request->dhcp, DHCP_REQUEST, &opt,
416-
&optlen);
411+
r = client_message_init(client, &request->dhcp, DHCP_REQUEST,
412+
optlen, &optoffset);
417413
if (r < 0)
418414
return r;
419415

@@ -428,13 +424,13 @@ static int client_send_request(sd_dhcp_client *client) {
428424
filled in with the yiaddr value from the chosen DHCPOFFER.
429425
*/
430426

431-
r = dhcp_option_append(&opt, &optlen,
427+
r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
432428
DHCP_OPTION_SERVER_IDENTIFIER,
433429
4, &client->lease->server_address);
434430
if (r < 0)
435431
return r;
436432

437-
r = dhcp_option_append(&opt, &optlen,
433+
r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
438434
DHCP_OPTION_REQUESTED_IP_ADDRESS,
439435
4, &client->lease->address);
440436
if (r < 0)
@@ -447,7 +443,7 @@ static int client_send_request(sd_dhcp_client *client) {
447443
option MUST be filled in with client’s notion of its previously
448444
assigned address. ’ciaddr’ MUST be zero.
449445
*/
450-
r = dhcp_option_append(&opt, &optlen,
446+
r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
451447
DHCP_OPTION_REQUESTED_IP_ADDRESS,
452448
4, &client->last_addr);
453449
if (r < 0)
@@ -480,7 +476,8 @@ static int client_send_request(sd_dhcp_client *client) {
480476
return -EINVAL;
481477
}
482478

483-
r = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
479+
r = dhcp_option_append(request->dhcp.options, optlen, &optoffset,
480+
DHCP_OPTION_END, 0, NULL);
484481
if (r < 0)
485482
return r;
486483

@@ -489,9 +486,9 @@ static int client_send_request(sd_dhcp_client *client) {
489486
client->lease->server_address,
490487
DHCP_PORT_SERVER,
491488
&request->dhcp,
492-
len - optlen - DHCP_IP_UDP_SIZE);
489+
sizeof(DHCPMessage) + optoffset);
493490
} else {
494-
r = dhcp_client_send_raw(client, request, len - optlen);
491+
r = dhcp_client_send_raw(client, request, sizeof(DHCPPacket) + optoffset);
495492
}
496493
if (r < 0)
497494
return r;

src/libsystemd-network/test-dhcp-option.c

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,14 @@ static void test_invalid_buffer_length(void)
8585
static void test_message_init(void)
8686
{
8787
_cleanup_free_ DHCPMessage *message = NULL;
88-
size_t optlen = 3;
88+
size_t optlen = 3, optoffset;
8989
size_t len = sizeof(DHCPMessage) + optlen;
90-
uint8_t *opt, *magic;
90+
uint8_t *magic;
9191

9292
message = malloc0(len);
9393

94-
opt = (uint8_t *)(message + 1);
95-
9694
assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678,
97-
DHCP_DISCOVER, &opt, &optlen) >= 0);
95+
DHCP_DISCOVER, message->options, optlen, &optoffset) >= 0);
9896

9997
assert_se(message->xid == htobe32(0x12345678));
10098
assert_se(message->op == BOOTREQUEST);
@@ -115,13 +113,11 @@ static DHCPMessage *create_message(uint8_t *options, uint16_t optlen,
115113
{
116114
DHCPMessage *message;
117115
size_t len = sizeof(DHCPMessage) + optlen;
118-
uint8_t *opt;
119116

120117
message = malloc0(len);
121-
opt = (uint8_t *)(message + 1);
122118

123119
if (options && optlen)
124-
memcpy(opt, options, optlen);
120+
memcpy(&message->options, options, optlen);
125121

126122
if (file && filelen <= 128)
127123
memcpy(&message->file, file, filelen);
@@ -308,46 +304,34 @@ static uint8_t options[64] = {
308304

309305
static void test_option_set(void)
310306
{
311-
size_t len, oldlen;
312-
int pos, i;
313-
uint8_t *opt;
314-
315-
assert_se(dhcp_option_append(NULL, NULL, 0, 0, NULL) == -EINVAL);
316-
317-
len = 0;
318-
opt = &result[0];
319-
assert_se(dhcp_option_append(&opt, NULL, 0, 0, NULL) == -EINVAL);
320-
assert_se(opt == &result[0] && len == 0);
307+
size_t offset = 0, len, pos;
308+
unsigned i;
321309

322-
assert_se(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD,
310+
assert_se(dhcp_option_append(result, 0, &offset, DHCP_OPTION_PAD,
323311
0, NULL) == -ENOBUFS);
324-
assert_se(opt == &result[0] && len == 0);
312+
assert_se(offset == 0);
325313

326-
opt = &result[4];
327-
len = 1;
328-
assert_se(dhcp_option_append(&opt, &len, DHCP_OPTION_PAD,
314+
offset = 4;
315+
assert_se(dhcp_option_append(result, 1, &offset, DHCP_OPTION_PAD,
329316
0, NULL) >= 0);
330-
assert_se(opt == &result[5] && len == 0);
317+
assert_se(offset == 5);
331318

332-
pos = 4;
319+
offset = pos = 4;
333320
len = 60;
334321
while (pos < 64 && options[pos] != DHCP_OPTION_END) {
335-
opt = &result[pos];
336-
oldlen = len;
322+
offset = pos;
337323

338-
assert_se(dhcp_option_append(&opt, &len, options[pos],
339-
options[pos + 1],
340-
&options[pos + 2]) >= 0);
324+
assert_se(dhcp_option_append(result, len, &offset,
325+
options[pos],
326+
options[pos + 1],
327+
&options[pos + 2]) >= 0);
341328

342-
if (options[pos] == DHCP_OPTION_PAD) {
343-
assert_se(opt == &result[pos + 1]);
344-
assert_se(len == oldlen - 1);
329+
if (options[pos] == DHCP_OPTION_PAD)
345330
pos++;
346-
} else {
347-
assert_se(opt == &result[pos + 2 + options[pos + 1]]);
348-
assert_se(len == oldlen - 2 - options[pos + 1]);
331+
else
349332
pos += 2 + options[pos + 1];
350-
}
333+
334+
assert_se(offset == pos);
351335
}
352336

353337
for (i = 0; i < pos; i++) {

0 commit comments

Comments
 (0)
X Tutup