Skip to content

Commit 039ebe6

Browse files
committed
sd-dhcp-client/networkd: add domainname support
1 parent bcbca82 commit 039ebe6

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

src/libsystemd/sd-dhcp-client.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct DHCPLease {
4444
struct in_addr *dns;
4545
size_t dns_size;
4646
uint16_t mtu;
47+
char *domainname;
4748
char *hostname;
4849
};
4950

@@ -237,6 +238,32 @@ int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t
237238
return 0;
238239
}
239240

241+
int sd_dhcp_client_get_domainname(sd_dhcp_client *client, const char **domainname) {
242+
assert_return(client, -EINVAL);
243+
assert_return(domainname, -EINVAL);
244+
245+
switch (client->state) {
246+
case DHCP_STATE_INIT:
247+
case DHCP_STATE_SELECTING:
248+
case DHCP_STATE_INIT_REBOOT:
249+
case DHCP_STATE_REBOOTING:
250+
case DHCP_STATE_REQUESTING:
251+
return -EADDRNOTAVAIL;
252+
253+
case DHCP_STATE_BOUND:
254+
case DHCP_STATE_RENEWING:
255+
case DHCP_STATE_REBINDING:
256+
if (client->lease->domainname)
257+
*domainname = client->lease->domainname;
258+
else
259+
return -ENOENT;
260+
261+
break;
262+
}
263+
264+
return 0;
265+
}
266+
240267
int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname) {
241268
assert_return(client, -EINVAL);
242269
assert_return(hostname, -EINVAL);
@@ -336,6 +363,7 @@ static void lease_free(DHCPLease *lease) {
336363
return;
337364

338365
free(lease->hostname);
366+
free(lease->domainname);
339367
free(lease->dns);
340368
free(lease);
341369
}
@@ -832,6 +860,14 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
832860

833861
break;
834862

863+
case DHCP_OPTION_DOMAIN_NAME:
864+
if (len >= 1) {
865+
free(lease->domainname);
866+
lease->domainname = strndup((const char *)option, len);
867+
}
868+
869+
break;
870+
835871
case DHCP_OPTION_HOST_NAME:
836872
if (len >= 1) {
837873
free(lease->hostname);

src/network/networkd-gperf.gperf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ Route.Destination, config_parse_destination, 0, 0
3333
DHCPv4.UseDNS, config_parse_bool, 0, offsetof(Network, dhcp_dns)
3434
DHCPv4.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_mtu)
3535
DHCPv4.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_hostname)
36+
DHCPv4.UseDomainName, config_parse_bool, 0, offsetof(Network, dhcp_domainname)
3637
Bridge.Description, config_parse_string, 0, offsetof(Bridge, description)
3738
Bridge.Name, config_parse_ifname, 0, offsetof(Bridge, name)

src/network/networkd-manager.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ int manager_update_resolv_conf(Manager *m) {
325325
Link *link;
326326
Iterator i;
327327
unsigned count = 0;
328+
const char *domainname = NULL;
328329
int r;
329330

330331
assert(m);
@@ -350,12 +351,20 @@ int manager_update_resolv_conf(Manager *m) {
350351
struct in_addr *nameservers;
351352
size_t nameservers_size;
352353

353-
r = sd_dhcp_client_get_dns(link->dhcp, &nameservers, &nameservers_size);
354-
if (r >= 0) {
355-
unsigned j;
354+
if (link->network->dhcp_dns) {
355+
r = sd_dhcp_client_get_dns(link->dhcp, &nameservers, &nameservers_size);
356+
if (r >= 0) {
357+
unsigned j;
356358

357-
for (j = 0; j < nameservers_size; j++)
358-
append_dns(f, &nameservers[j], AF_INET, &count);
359+
for (j = 0; j < nameservers_size; j++)
360+
append_dns(f, &nameservers[j], AF_INET, &count);
361+
}
362+
}
363+
364+
if (link->network->dhcp_domainname && !domainname) {
365+
r = sd_dhcp_client_get_domainname(link->dhcp, &domainname);
366+
if (r >= 0)
367+
fprintf(f, "domain %s\n", domainname);
359368
}
360369
}
361370
}

src/network/networkd-network.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static int network_load_one(Manager *manager, const char *filename) {
6868
network->dhcp_dns = true;
6969
network->dhcp_mtu = true;
7070
network->dhcp_hostname = true;
71+
network->dhcp_domainname = true;
7172

7273
r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup,
7374
(void*) network_gperf_lookup, false, false, network);

src/network/networkd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct Network {
8989
bool dhcp_dns;
9090
bool dhcp_mtu;
9191
bool dhcp_hostname;
92+
bool dhcp_domainname;
9293

9394
LIST_HEAD(Address, static_addresses);
9495
LIST_HEAD(Route, static_routes);

src/systemd/sd-dhcp-client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ int sd_dhcp_client_prefixlen(const struct in_addr *addr);
5656
int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr);
5757
int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size);
5858
int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu);
59+
int sd_dhcp_client_get_domainname(sd_dhcp_client *client, const char **domainname);
5960
int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname);
6061

6162
int sd_dhcp_client_stop(sd_dhcp_client *client);

0 commit comments

Comments
 (0)