Skip to content

Commit

Permalink
Merge pull request #658 from ssahani/json
Browse files Browse the repository at this point in the history
JSON: Show DHCP DUID
  • Loading branch information
ssahani authored Jan 12, 2024
2 parents 13657ef + 3dce08c commit 9a47c60
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 7 deletions.
81 changes: 80 additions & 1 deletion src/json/network-link-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1622,8 +1622,9 @@ int json_fill_address(bool ipv4, Link *l, json_object *jn, json_object *jobj) {
}

int json_fill_one_link(IfNameIndex *p, bool ipv4, json_object *jn, json_object **ret) {
_auto_cleanup_ char *setup_state = NULL, *tz = NULL, *network = NULL, *dhcp4_duid_type = NULL,
*dhcp6_duid_type = NULL, *dhcp4_duid_data = NULL, *dhcp6_duid_data = NULL, *iaid = NULL;
_cleanup_(json_object_putp) json_object *jobj = NULL, *jdns = NULL, *jntp = NULL;
_auto_cleanup_ char *setup_state = NULL, *tz = NULL, *network = NULL;
_cleanup_(addresses_freep) Addresses *addr = NULL;
_cleanup_(routes_freep) Routes *route = NULL;
_cleanup_(link_freep) Link *l = NULL;
Expand Down Expand Up @@ -1741,6 +1742,84 @@ int json_fill_one_link(IfNameIndex *p, bool ipv4, json_object *jn, json_object
steal_ptr(js);
}

r = manager_acquire_link_dhcp_client_iaid(p, DHCP_CLIENT_IPV4, &iaid);
if (r >= 0) {
_cleanup_(json_object_putp) json_object *js = NULL;

js = json_object_new_string(iaid);
if (!js)
return log_oom();

json_object_object_add(jobj, "DHCPv4IAID", js);
steal_ptr(js);
}

r = manager_acquire_link_dhcp_client_iaid(p, DHCP_CLIENT_IPV6, &iaid);
if (r >= 0) {
_cleanup_(json_object_putp) json_object *js = NULL;

js = json_object_new_string(iaid);
if (!js)
return log_oom();

json_object_object_add(jobj, "DHCPv6IAID", js);
steal_ptr(js);
}

r = manager_acquire_link_dhcp_client_duid(p, DHCP_CLIENT_IPV4, &dhcp4_duid_type, &dhcp4_duid_data);
if (r >= 0) {
_cleanup_(json_object_putp) json_object *js = NULL, *j = NULL;

j = json_object_new_object();
if (!j)
return log_oom();

js = json_object_new_string(dhcp4_duid_type);
if (!js)
return log_oom();

json_object_object_add(j, "DUIDType", js);
steal_ptr(js);


js = json_object_new_string(dhcp4_duid_data);
if (!js)
return log_oom();

json_object_object_add(j, "DUIDRawData", js);
steal_ptr(js);

json_object_object_add(jobj, "DHCPv4DUID", j);
steal_ptr(j);
}

r = manager_acquire_link_dhcp_client_duid(p, DHCP_CLIENT_IPV6, &dhcp6_duid_type, &dhcp6_duid_data);
if (r >= 0) {
_cleanup_(json_object_putp) json_object *js = NULL, *j = NULL;

j = json_object_new_object();
if (!j)
return log_oom();

js = json_object_new_string(dhcp6_duid_type);
if (!js)
return log_oom();

json_object_object_add(j, "DUIDType", js);
steal_ptr(js);


js = json_object_new_string(dhcp6_duid_data);
if (!js)
return log_oom();

json_object_object_add(j, "DUIDRawData", js);
steal_ptr(js);

json_object_object_add(jobj, "DHCPv6DUID", j);
steal_ptr(j);
}

if (ret)
*ret = steal_ptr(jobj);
else
Expand Down
21 changes: 20 additions & 1 deletion src/manager/ctl-display.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ static void display_alterative_names(gpointer data, gpointer user_data) {
static int list_one_link(int argc, char *argv[]) {
_auto_cleanup_ char *setup_state = NULL, *operational_state = NULL, *address_state = NULL, *ipv4_state = NULL,
*ipv6_state = NULL, *required_for_online = NULL, *device_activation_policy = NULL, *tz = NULL, *network = NULL,
*online_state = NULL, *link = NULL, *dhcp4_identifier = NULL, *dhcp6_duid = NULL, *dhcp6_iaid = NULL, *iaid = NULL;
*online_state = NULL, *link = NULL, *dhcp4_identifier = NULL, *dhcp6_duid = NULL, *dhcp6_iaid = NULL, *iaid = NULL,
*dhcp4_duid_type = NULL, *dhcp6_duid_type = NULL, *dhcp4_duid_data = NULL, *dhcp6_duid_data = NULL;
_auto_cleanup_strv_ char **dns = NULL, **ntp = NULL, **search_domains = NULL, **route_domains = NULL;
const char *operational_state_color, *setup_set_color;
_cleanup_(json_object_putp) json_object *jn = NULL;
Expand Down Expand Up @@ -757,6 +758,22 @@ static int list_one_link(int argc, char *argv[]) {
printf("%s\n", iaid);
}

r = manager_acquire_link_dhcp_client_duid(p, DHCP_CLIENT_IPV4, &dhcp4_duid_type, &dhcp4_duid_data);
if (r >= 0) {
display(arg_beautify, ansi_color_bold_cyan(), " DHCPv4 DUIDType: ");
printf("%s ", dhcp4_duid_type);
display(arg_beautify, ansi_color_bold_cyan(), "DUIDRawData: ");
printf("%s\n", dhcp4_duid_data);
}

r = manager_acquire_link_dhcp_client_duid(p, DHCP_CLIENT_IPV6, &dhcp6_duid_type, &dhcp6_duid_data);
if (r >= 0) {
display(arg_beautify, ansi_color_bold_cyan(), " DHCPv6 DUIDType: ");
printf("%s ", dhcp6_duid_type);
display(arg_beautify, ansi_color_bold_cyan(), "DUIDRawData: ");
printf("%s\n", dhcp6_duid_data);
}

r = network_parse_link_dhcp4_client_id(p->ifindex, &dhcp4_identifier);
if (r >= 0) {
_auto_cleanup_ IfNameIndex *ifn = NULL;
Expand Down Expand Up @@ -797,6 +814,8 @@ static int list_one_link(int argc, char *argv[]) {
printf("%s\n", dhcp6_duid);
}



if (log_enabled())
display_network_logs(l->ifindex, l->name);

Expand Down
2 changes: 1 addition & 1 deletion src/manager/network-config-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ _public_ int ncm_link_set_dhcp_client_duid(int argc, char *argv[]) {
kind = r;

continue;
} else if (str_eq_fold(argv[i], "duid")) {
} else if (str_eq_fold(argv[i], "duid") || str_eq_fold(argv[i], "type")) {
int k = -1;

parse_next_arg(argv, argc, i);
Expand Down
2 changes: 1 addition & 1 deletion src/manager/network-manager-ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static int help(void) {
"\n\t\t\t\t use-domains-ipv4 [BOOLEAN] use-domains-ipv6 [BOOLEAN] send-release-ipv4 [BOOLEAN] send-release-ipv6 [BOOLEAN] Configures DHCP client.\n"
" set-dhcp4-cid dev [DEVICE] id [IDENTIFIER {mac|duid|duid-only} Configures device DHCPv4 identifier.\n"
" set-dhcp-iaid dev [DEVICE] family|f iaid [IAID] Configures the DHCP Identity Association Identifier (IAID)\n"
" set-dhcp-duid dev|system [DEVICE] family|f [ipv4|ipv6|4|6] duid [DUID {link-layer-time|vendor|link-layer|uuid|0…65535}] data [RAWDATA]"
" set-dhcp-duid dev|system [DEVICE] family|f [ipv4|ipv6|4|6] type [DUIDType {link-layer-time|vendor|link-layer|uuid|0…65535}] data [RAWDATA]"
"\n\t\t\t\t Sets DUID of DHCPv4 or DHCPv6 Client.\n"
" set-link-state dev [DEVICE] [STATE {up|down}] Configures device State.\n"
" show-addr dev [DEVICE] [family|f ipv4|ipv6|4|6] Show device addresses\n"
Expand Down
29 changes: 29 additions & 0 deletions src/manager/network-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,35 @@ int manager_acquire_link_dhcp_client_iaid(const IfNameIndex *ifidx, const DHCPCl
return 0;
}

int manager_acquire_link_dhcp_client_duid(const IfNameIndex *ifidx, const DHCPClient kind, char **duid_kind, char **raw_data) {
_auto_cleanup_ char *network = NULL, *v = NULL;
int r;

assert(ifidx);

r = network_parse_link_network_file(ifidx->ifindex, &network);
if (r < 0)
return r;

r = parse_config_file(network, kind == DHCP_CLIENT_IPV4 ? "DHCPv4" : "DHCPv6", "DUIDType", &v);
if (r < 0)
return r;

*duid_kind = strdup(v);
if(!*duid_kind)
return log_oom();

r = parse_config_file(network, kind == DHCP_CLIENT_IPV4 ? "DHCPv4" : "DHCPv6", "DUIDRawData", &v);
if (r < 0)
return r;

*raw_data = strdup(v);
if(!*raw_data)
return log_oom();

return 0;
}

int manager_set_link_dhcp_client_duid(const IfNameIndex *ifidx,
const char *duid,
const char *raw_data,
Expand Down
3 changes: 3 additions & 0 deletions src/manager/network-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ bool manager_link_has_static_address(const IfNameIndex *ifidx);
int manager_set_link_dhcp_client_iaid(const IfNameIndex *ifidx, DHCPClient kind, const char *iaid);
int manager_acquire_link_dhcp_client_iaid(const IfNameIndex *ifidx, DHCPClient kind, char **iaid);


int manager_acquire_link_dhcp_client_duid(const IfNameIndex *ifidx, const DHCPClient kind, char **duid_kind, char **raw_data);

int manager_set_link_dhcp_client_duid(const IfNameIndex *ifidx,
const char *duid,
const char *raw_data,
Expand Down
6 changes: 3 additions & 3 deletions tests/cmocka/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ static void test_set_dhcp6_duid_custom(void **state) {
_cleanup_(key_file_freep) KeyFile *key_file = NULL;
int r;

assert_true(system("nmctl set-dhcp-duid dev test99 f 6 duid 1234 data 00:00:ab:11:f9:2a:c2:77:29:f9:5c:00") >= 0);
assert_true(system("nmctl set-dhcp-duid dev test99 f 6 type 1234 data 00:00:ab:11:f9:2a:c2:77:29:f9:5c:00") >= 0);

r = parse_key_file("/etc/systemd/network/10-test99.network", &key_file);
assert_true(r >= 0);
Expand All @@ -453,7 +453,7 @@ static void test_set_dhcp6_duid(void **state) {
_cleanup_(key_file_freep) KeyFile *key_file = NULL;
int r;

assert_true(system("nmctl set-dhcp-duid dev test99 f 6 duid vendor data 00:00:ab:11:f9:2a:c2:77:29:f9:5c:00") >= 0);
assert_true(system("nmctl set-dhcp-duid dev test99 f 6 type vendor data 00:00:ab:11:f9:2a:c2:77:29:f9:5c:00") >= 0);

r = parse_key_file("/etc/systemd/network/10-test99.network", &key_file);
assert_true(r >= 0);
Expand All @@ -469,7 +469,7 @@ static void test_set_dhcp4_duid(void **state) {
_cleanup_(key_file_freep) KeyFile *key_file = NULL;
int r;

assert_true(system("nmctl set-dhcp-duid dev test99 f 4 duid vendor data 00:00:ab:11:f9:2a:c2:77:29:f9:5c:00") >= 0);
assert_true(system("nmctl set-dhcp-duid dev test99 f 4 type vendor data 00:00:ab:11:f9:2a:c2:77:29:f9:5c:00") >= 0);

r = parse_key_file("/etc/systemd/network/10-test99.network", &key_file);
assert_true(r >= 0);
Expand Down

0 comments on commit 9a47c60

Please sign in to comment.