Skip to content

Commit 461dbb2

Browse files
committed
dhcp: remove struct sd_dhcp_raw_option
sd_dhcp_raw_option and sd_dhcp_option are essentially equivalent.
1 parent d8b736b commit 461dbb2

File tree

12 files changed

+109
-127
lines changed

12 files changed

+109
-127
lines changed

src/libsystemd-network/dhcp-internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
#include "dhcp-protocol.h"
1616
#include "socket-util.h"
1717

18+
typedef struct sd_dhcp_option {
19+
unsigned n_ref;
20+
21+
uint8_t option;
22+
void *data;
23+
size_t length;
24+
} sd_dhcp_option;
25+
26+
extern const struct hash_ops dhcp_option_hash_ops;
27+
1828
int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link,
1929
uint32_t xid, const uint8_t *mac_addr,
2030
size_t mac_addr_len, uint16_t arp_type,

src/libsystemd-network/dhcp-option.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static int option_append(uint8_t options[], size_t size, size_t *offset,
7979
break;
8080
case SD_DHCP_OPTION_VENDOR_SPECIFIC: {
8181
OrderedHashmap *s = (OrderedHashmap *) optval;
82-
struct sd_dhcp_raw_option *p;
82+
struct sd_dhcp_option *p;
8383
size_t l = 0;
8484
Iterator i;
8585

@@ -95,7 +95,7 @@ static int option_append(uint8_t options[], size_t size, size_t *offset,
9595
*offset += 2;
9696

9797
ORDERED_HASHMAP_FOREACH(p, s, i) {
98-
options[*offset] = p->type;
98+
options[*offset] = p->option;
9999
options[*offset + 1] = p->length;
100100
memcpy(&options[*offset + 2], p->data, p->length);
101101
*offset += 2 + p->length;
@@ -315,3 +315,43 @@ int dhcp_option_parse(DHCPMessage *message, size_t len, dhcp_option_callback_t c
315315

316316
return message_type;
317317
}
318+
319+
static sd_dhcp_option* dhcp_option_free(sd_dhcp_option *i) {
320+
if (!i)
321+
return NULL;
322+
323+
free(i->data);
324+
return mfree(i);
325+
}
326+
327+
int sd_dhcp_option_new(uint8_t option, const void *data, size_t length, sd_dhcp_option **ret) {
328+
assert_return(ret, -EINVAL);
329+
assert_return(length == 0 || data, -EINVAL);
330+
331+
_cleanup_free_ void *q = memdup(data, length);
332+
if (!q)
333+
return -ENOMEM;
334+
335+
sd_dhcp_option *p = new(sd_dhcp_option, 1);
336+
if (!p)
337+
return -ENOMEM;
338+
339+
*p = (sd_dhcp_option) {
340+
.n_ref = 1,
341+
.option = option,
342+
.length = length,
343+
.data = TAKE_PTR(q),
344+
};
345+
346+
*ret = TAKE_PTR(p);
347+
return 0;
348+
}
349+
350+
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_option, sd_dhcp_option, dhcp_option_free);
351+
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
352+
dhcp_option_hash_ops,
353+
void,
354+
trivial_hash_func,
355+
trivial_compare_func,
356+
sd_dhcp_option,
357+
sd_dhcp_option_unref);

src/libsystemd-network/dhcp-server-internal.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ typedef struct DHCPLease {
3737
usec_t expiration;
3838
} DHCPLease;
3939

40-
struct sd_dhcp_raw_option {
41-
unsigned n_ref;
42-
43-
uint8_t type;
44-
uint8_t length;
45-
46-
void *data;
47-
};
48-
4940
struct sd_dhcp_server {
5041
unsigned n_ref;
5142

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

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
#define RESTART_AFTER_NAK_MIN_USEC (1 * USEC_PER_SEC)
3535
#define RESTART_AFTER_NAK_MAX_USEC (30 * USEC_PER_MINUTE)
3636

37-
struct sd_dhcp_option {
38-
unsigned n_ref;
39-
40-
uint8_t option;
41-
void *data;
42-
size_t length;
43-
};
44-
4537
struct sd_dhcp_client {
4638
unsigned n_ref;
4739

@@ -538,46 +530,6 @@ int sd_dhcp_client_set_max_attempts(sd_dhcp_client *client, uint64_t max_attempt
538530
return 0;
539531
}
540532

541-
static sd_dhcp_option* dhcp_option_free(sd_dhcp_option *i) {
542-
if (!i)
543-
return NULL;
544-
545-
free(i->data);
546-
return mfree(i);
547-
}
548-
549-
int sd_dhcp_option_new(uint8_t option, void *data, size_t length, sd_dhcp_option **ret) {
550-
assert_return(ret, -EINVAL);
551-
assert_return(length == 0 || data, -EINVAL);
552-
553-
_cleanup_free_ void *q = memdup(data, length);
554-
if (!q)
555-
return -ENOMEM;
556-
557-
sd_dhcp_option *p = new(sd_dhcp_option, 1);
558-
if (!p)
559-
return -ENOMEM;
560-
561-
*p = (sd_dhcp_option) {
562-
.n_ref = 1,
563-
.option = option,
564-
.length = length,
565-
.data = TAKE_PTR(q),
566-
};
567-
568-
*ret = TAKE_PTR(p);
569-
return 0;
570-
}
571-
572-
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_option, sd_dhcp_option, dhcp_option_free);
573-
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
574-
dhcp_option_hash_ops,
575-
void,
576-
trivial_hash_func,
577-
trivial_compare_func,
578-
sd_dhcp_option,
579-
sd_dhcp_option_unref);
580-
581533
int sd_dhcp_client_set_dhcp_option(sd_dhcp_client *client, sd_dhcp_option *v) {
582534
int r;
583535

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

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -127,46 +127,6 @@ int client_id_compare_func(const DHCPClientId *a, const DHCPClientId *b) {
127127
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(dhcp_lease_hash_ops, DHCPClientId, client_id_hash_func, client_id_compare_func,
128128
DHCPLease, dhcp_lease_free);
129129

130-
static sd_dhcp_raw_option* raw_option_free(sd_dhcp_raw_option *i) {
131-
if (!i)
132-
return NULL;
133-
134-
free(i->data);
135-
return mfree(i);
136-
}
137-
138-
_public_ int sd_dhcp_raw_option_new(uint8_t type, char *data, size_t length, sd_dhcp_raw_option **ret) {
139-
_cleanup_(sd_dhcp_raw_option_unrefp) sd_dhcp_raw_option *p = NULL;
140-
141-
assert_return(ret, -EINVAL);
142-
143-
p = new(sd_dhcp_raw_option, 1);
144-
if (!p)
145-
return -ENOMEM;
146-
147-
*p = (sd_dhcp_raw_option) {
148-
.n_ref = 1,
149-
.data = memdup(data, length),
150-
.length = length,
151-
.type = type,
152-
};
153-
154-
if (!p->data)
155-
return -ENOMEM;
156-
157-
*ret = TAKE_PTR(p);
158-
return 0;
159-
}
160-
161-
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_raw_option, sd_dhcp_raw_option, raw_option_free);
162-
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
163-
dhcp_raw_options_hash_ops,
164-
void,
165-
trivial_hash_func,
166-
trivial_compare_func,
167-
sd_dhcp_raw_option,
168-
sd_dhcp_raw_option_unref);
169-
170130
static sd_dhcp_server *dhcp_server_free(sd_dhcp_server *server) {
171131
assert(server);
172132

@@ -1222,21 +1182,21 @@ int sd_dhcp_server_set_emit_router(sd_dhcp_server *server, int enabled) {
12221182
return 1;
12231183
}
12241184

1225-
int sd_dhcp_server_add_raw_option(sd_dhcp_server *server, sd_dhcp_raw_option *v) {
1185+
int sd_dhcp_server_add_option(sd_dhcp_server *server, sd_dhcp_option *v) {
12261186
int r;
12271187

12281188
assert_return(server, -EINVAL);
12291189
assert_return(v, -EINVAL);
12301190

1231-
r = ordered_hashmap_ensure_allocated(&server->raw_option, &dhcp_raw_options_hash_ops);
1191+
r = ordered_hashmap_ensure_allocated(&server->raw_option, &dhcp_option_hash_ops);
12321192
if (r < 0)
12331193
return -ENOMEM;
12341194

12351195
r = ordered_hashmap_put(server->raw_option, v, v);
12361196
if (r < 0)
12371197
return r;
12381198

1239-
sd_dhcp_raw_option_ref(v);
1199+
sd_dhcp_option_ref(v);
12401200

12411201
return 1;
12421202
}

src/network/networkd-dhcp-server.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "sd-dhcp-server.h"
44

5+
#include "dhcp-internal.h"
56
#include "escape.h"
67
#include "networkd-dhcp-server.h"
78
#include "networkd-link.h"
@@ -192,7 +193,7 @@ static int link_push_uplink_sip_to_dhcp_server(Link *link, sd_dhcp_server *s) {
192193

193194
int dhcp4_server_configure(Link *link) {
194195
bool acquired_uplink = false;
195-
sd_dhcp_raw_option *p;
196+
sd_dhcp_option *p;
196197
Link *uplink = NULL;
197198
Address *address;
198199
Iterator i;
@@ -305,8 +306,8 @@ int dhcp4_server_configure(Link *link) {
305306
return r;
306307
}
307308

308-
ORDERED_HASHMAP_FOREACH(p, link->network->dhcp_server_raw_options, i) {
309-
r = sd_dhcp_server_add_raw_option(link->dhcp_server, p);
309+
ORDERED_HASHMAP_FOREACH(p, link->network->dhcp_server_options, i) {
310+
r = sd_dhcp_server_add_option(link->dhcp_server, p);
310311
if (r == -EEXIST)
311312
continue;
312313
if (r < 0)
@@ -492,7 +493,7 @@ int config_parse_dhcp_server_option_data(
492493
void *data,
493494
void *userdata) {
494495

495-
_cleanup_(sd_dhcp_raw_option_unrefp) sd_dhcp_raw_option *opt = NULL, *old = NULL;
496+
_cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *opt = NULL, *old = NULL;
496497
_cleanup_free_ char *word = NULL, *q = NULL;
497498
union in_addr_union addr;
498499
DHCPOptionDataType type;
@@ -512,7 +513,7 @@ int config_parse_dhcp_server_option_data(
512513
assert(data);
513514

514515
if (isempty(rvalue)) {
515-
network->dhcp_server_raw_options = ordered_hashmap_free(network->dhcp_server_raw_options);
516+
network->dhcp_server_options = ordered_hashmap_free(network->dhcp_server_options);
516517
return 0;
517518
}
518519

@@ -619,20 +620,20 @@ int config_parse_dhcp_server_option_data(
619620
return -EINVAL;
620621
}
621622

622-
r = sd_dhcp_raw_option_new(u, udata, sz, &opt);
623+
r = sd_dhcp_option_new(u, udata, sz, &opt);
623624
if (r < 0) {
624625
log_syntax(unit, LOG_ERR, filename, line, r,
625626
"Failed to store DHCP send raw option '%s', ignoring assignment: %m", rvalue);
626627
return 0;
627628
}
628629

629-
r = ordered_hashmap_ensure_allocated(&network->dhcp_server_raw_options, &dhcp_raw_options_hash_ops);
630+
r = ordered_hashmap_ensure_allocated(&network->dhcp_server_options, &dhcp_option_hash_ops);
630631
if (r < 0)
631632
return log_oom();
632633

633634
/* Overwrite existing option */
634-
old = ordered_hashmap_remove(network->dhcp_server_raw_options, UINT_TO_PTR(u));
635-
r = ordered_hashmap_put(network->dhcp_server_raw_options, UINT_TO_PTR(u), opt);
635+
old = ordered_hashmap_remove(network->dhcp_server_options, UINT_TO_PTR(u));
636+
r = ordered_hashmap_put(network->dhcp_server_options, UINT_TO_PTR(u), opt);
636637
if (r < 0) {
637638
log_syntax(unit, LOG_ERR, filename, line, r,
638639
"Failed to store DHCP server send raw option '%s'", rvalue);

src/network/networkd-network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ static Network *network_free(Network *network) {
687687
set_free_free(network->dnssec_negative_trust_anchors);
688688

689689
ordered_hashmap_free(network->dhcp_send_options);
690-
ordered_hashmap_free(network->dhcp_server_raw_options);
690+
ordered_hashmap_free(network->dhcp_server_options);
691691

692692
return mfree(network);
693693
}

src/network/networkd-network.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct Network {
115115
Set *dhcp_black_listed_ip;
116116
Set *dhcp_request_options;
117117
OrderedHashmap *dhcp_send_options;
118-
OrderedHashmap *dhcp_server_raw_options;
118+
OrderedHashmap *dhcp_server_options;
119119

120120
/* DHCPv6 Client support*/
121121
bool dhcp6_use_dns;

src/systemd/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ _not_installed_headers = '''
2222
sd-dhcp6-lease.h
2323
sd-dhcp-client.h
2424
sd-dhcp-lease.h
25+
sd-dhcp-option.h
2526
sd-dhcp-server.h
2627
sd-ipv4acd.h
2728
sd-ipv4ll.h

src/systemd/sd-dhcp-client.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <stdbool.h>
2727

2828
#include "sd-dhcp-lease.h"
29+
#include "sd-dhcp-option.h"
2930
#include "sd-event.h"
3031

3132
#include "_sd-common.h"
@@ -99,7 +100,6 @@ enum {
99100
};
100101

101102
typedef struct sd_dhcp_client sd_dhcp_client;
102-
typedef struct sd_dhcp_option sd_dhcp_option;
103103

104104
typedef int (*sd_dhcp_client_callback_t)(sd_dhcp_client *client, int event, void *userdata);
105105
int sd_dhcp_client_set_callback(
@@ -179,9 +179,6 @@ int sd_dhcp_client_set_service_type(
179179
sd_dhcp_client *client,
180180
int type);
181181

182-
int sd_dhcp_option_new(uint8_t option, void *data, size_t length, sd_dhcp_option **ret);
183-
sd_dhcp_option* sd_dhcp_option_ref(sd_dhcp_option *i);
184-
sd_dhcp_option* sd_dhcp_option_unref(sd_dhcp_option *i);
185182
int sd_dhcp_client_set_dhcp_option(sd_dhcp_client *client, sd_dhcp_option *v);
186183

187184
int sd_dhcp_client_stop(sd_dhcp_client *client);
@@ -204,7 +201,6 @@ int sd_dhcp_client_detach_event(sd_dhcp_client *client);
204201
sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client);
205202

206203
_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp_client, sd_dhcp_client_unref);
207-
_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_dhcp_option, sd_dhcp_option_unref);
208204

209205
_SD_END_DECLARATIONS;
210206

0 commit comments

Comments
 (0)