| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
#include "sd-netlink.h" |
| 5 |
#include "sd-varlink.h" |
| 6 |
|
| 7 |
#include "dhcp-message-dump.h" |
| 8 |
#include "log.h" |
| 9 |
#include "networkctl.h" |
| 10 |
#include "networkctl-dhcp-lease.h" |
| 11 |
#include "networkctl-link-info.h" |
| 12 |
#include "networkctl-util.h" |
| 13 |
#include "strv.h" |
| 14 |
|
| 15 |
int verb_dhcp_lease(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 16 |
int r; |
| 17 |
|
| 18 |
/* networkctl dhcp-lease INTERFACE [CODE[:FORMAT] ...] */ |
| 19 |
assert(argc >= 2); |
| 20 |
|
| 21 |
pager_open(arg_pager_flags); |
| 22 |
|
| 23 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 24 |
r = sd_netlink_open(&rtnl); |
| 25 |
if (r < 0) |
| 26 |
return log_error_errno(r, "Failed to connect to netlink: %m"); |
| 27 |
|
| 28 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL; |
| 29 |
r = varlink_connect_networkd(&vl); |
| 30 |
if (r < 0) |
| 31 |
return r; |
| 32 |
|
| 33 |
const char *ifname = argv[1]; |
| 34 |
|
| 35 |
_cleanup_(link_info_array_freep) LinkInfo *link = NULL; |
| 36 |
r = acquire_link_info(vl, rtnl, STRV_MAKE(ifname), &link); |
| 37 |
if (r < 0) |
| 38 |
return r; |
| 39 |
if (r == 0) |
| 40 |
return -EINVAL; /* already logged in acquire_link_info(). */ |
| 41 |
if (r > 1) |
| 42 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 43 |
"Interface name '%s' matches multiple interfaces.", ifname); |
| 44 |
|
| 45 |
if (!link->dhcp_message) |
| 46 |
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), |
| 47 |
"Interface '%s' does not have DHCPv4 lease.", link->name); |
| 48 |
|
| 49 |
if (sd_json_format_enabled(arg_json_format_flags)) { |
| 50 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 51 |
r = dhcp_message_build_json(link->dhcp_message, &v); |
| 52 |
if (r < 0) |
| 53 |
return log_error_errno(r, "Failed to build JSON variant from DHCP message: %m"); |
| 54 |
|
| 55 |
r = sd_json_variant_dump(v, arg_json_format_flags, /* f= */ NULL, /* prefix= */ NULL); |
| 56 |
if (r < 0) |
| 57 |
return log_error_errno(r, "Failed to dump JSON variant: %m"); |
| 58 |
|
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
DumpDHCPMessageFlag flags = 0; |
| 63 |
SET_FLAG(flags, DUMP_DHCP_MESSAGE_LEGEND, arg_legend); |
| 64 |
SET_FLAG(flags, DUMP_DHCP_MESSAGE_FULL, arg_full); |
| 65 |
|
| 66 |
return dump_dhcp_message(link->dhcp_message, strv_skip(argv, 2), flags); |
| 67 |
} |