Debian Codegraph @

src/network/networkctl-link-info-json.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-dhcp-client-id.h"
4 #include "sd-json.h"
5
6 #include "iovec-util.h"
7 #include "json-util.h"
8 #include "networkctl-link-info.h"
9 #include "networkctl-link-info-json.h"
10 #include "networkctl-util.h"
11
12 static int acquire_link_bitrates(LinkInfo *link) {
13 int r;
14
15 assert(link);
16
17 sd_json_variant *v;
18 r = json_variant_find_object(link->description, STRV_MAKE("Interface", "BitRates"), &v);
19 if (r == -ENODATA)
20 return 0;
21 if (r < 0)
22 return r;
23
24 static const sd_json_dispatch_field dispatch_table[] = {
25 { "TxBitRate", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LinkInfo, tx_bitrate), SD_JSON_MANDATORY },
26 { "RxBitRate", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LinkInfo, rx_bitrate), SD_JSON_MANDATORY },
27 {}
28 };
29
30 r = sd_json_dispatch(v, dispatch_table,
31 SD_JSON_LOG | SD_JSON_WARNING | SD_JSON_ALLOW_EXTENSIONS,
32 link);
33 if (r < 0)
34 return r;
35
36 link->has_bitrates = true;
37 return 0;
38 }
39
40 static int acquire_link_dhcp_client(LinkInfo *link) {
41 int r;
42
43 assert(link);
44
45 sd_json_variant *v;
46 r = json_variant_find_object(link->description, STRV_MAKE("Interface", "DHCPv4Client", "ClientIdentifier"), &v);
47 if (r == -ENODATA)
48 return 0;
49 if (r < 0)
50 return r;
51
52 _cleanup_(iovec_done) struct iovec iov = {};
53 r = json_dispatch_byte_array_iovec("ClientIdentifier", v, /* flags= */ 0, &iov);
54 if (r < 0)
55 return r;
56
57 return sd_dhcp_client_id_set_raw(&link->dhcp_client_id, iov.iov_base, iov.iov_len);
58 }
59
60 static int acquire_link_dhcp_message(LinkInfo *link) {
61 int r;
62
63 assert(link);
64
65 sd_json_variant *v;
66 r = json_variant_find_object(link->description, STRV_MAKE("Interface", "DHCPv4Client", "Lease", "Message"), &v);
67 if (r == -ENODATA)
68 return 0;
69 if (r < 0)
70 return r;
71
72 return dhcp_message_parse_json(v, &link->dhcp_message);
73 }
74
75 int link_info_parse_description(LinkInfo *link, sd_varlink *vl) {
76 int r;
77
78 assert(link);
79
80 if (!vl)
81 return 0;
82
83 r = acquire_link_description(vl, link->ifindex, &link->description);
84 if (r < 0)
85 return r;
86
87 (void) acquire_link_bitrates(link);
88 (void) acquire_link_dhcp_client(link);
89 (void) acquire_link_dhcp_message(link);
90
91 return 0;
92 }