network: introduce GetLinkByName and GetLinkByIndex bus methods
This commit is contained in:
parent
37d577c895
commit
071712b281
|
@ -1,6 +1,9 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include <net/if.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "bus-common-errors.h"
|
||||
#include "bus-util.h"
|
||||
#include "networkd-link-bus.h"
|
||||
#include "networkd-link.h"
|
||||
|
@ -47,6 +50,72 @@ static int method_list_links(sd_bus_message *message, void *userdata, sd_bus_err
|
|||
return sd_bus_send(NULL, reply, NULL);
|
||||
}
|
||||
|
||||
static int method_get_link_by_name(sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
Manager *manager = userdata;
|
||||
const char *name;
|
||||
int index, r;
|
||||
Link *link;
|
||||
|
||||
r = sd_bus_message_read(message, "s", &name);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
index = if_nametoindex(name);
|
||||
if (index <= 0)
|
||||
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name);
|
||||
|
||||
link = hashmap_get(manager->links, INT_TO_PTR(index));
|
||||
if (!link)
|
||||
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %s not known", name);
|
||||
|
||||
r = sd_bus_message_new_method_return(message, &reply);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
path = link_bus_path(link);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sd_bus_message_append(reply, "io", link->ifindex, empty_to_root(path));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return sd_bus_send(NULL, reply, NULL);
|
||||
}
|
||||
|
||||
static int method_get_link_by_index(sd_bus_message *message, void *userdata, sd_bus_error *error) {
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
Manager *manager = userdata;
|
||||
int32_t index;
|
||||
Link *link;
|
||||
int r;
|
||||
|
||||
r = sd_bus_message_read(message, "i", &index);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
link = hashmap_get(manager->links, INT_TO_PTR((int) index));
|
||||
if (!link)
|
||||
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %" PRIi32 " not known", index);
|
||||
|
||||
r = sd_bus_message_new_method_return(message, &reply);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
path = link_bus_path(link);
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sd_bus_message_append(reply, "so", link->ifname, empty_to_root(path));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return sd_bus_send(NULL, reply, NULL);
|
||||
}
|
||||
|
||||
const sd_bus_vtable manager_vtable[] = {
|
||||
SD_BUS_VTABLE_START(0),
|
||||
|
||||
|
@ -55,6 +124,8 @@ const sd_bus_vtable manager_vtable[] = {
|
|||
SD_BUS_PROPERTY("AddressState", "s", property_get_address_state, offsetof(Manager, address_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
|
||||
|
||||
SD_BUS_METHOD("ListLinks", NULL, "a(iso)", method_list_links, SD_BUS_VTABLE_UNPRIVILEGED),
|
||||
SD_BUS_METHOD("GetLinkByName", "s", "io", method_get_link_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
|
||||
SD_BUS_METHOD("GetLinkByIndex", "i", "so", method_get_link_by_index, SD_BUS_VTABLE_UNPRIVILEGED),
|
||||
|
||||
SD_BUS_VTABLE_END
|
||||
};
|
||||
|
|
|
@ -40,6 +40,14 @@
|
|||
send_interface="org.freedesktop.network1.Manager"
|
||||
send_member="ListLinks"/>
|
||||
|
||||
<allow send_destination="org.freedesktop.network1"
|
||||
send_interface="org.freedesktop.network1.Manager"
|
||||
send_member="GetLinkByName"/>
|
||||
|
||||
<allow send_destination="org.freedesktop.network1"
|
||||
send_interface="org.freedesktop.network1.Manager"
|
||||
send_member="GetLinkByIndex"/>
|
||||
|
||||
<allow receive_sender="org.freedesktop.network1"/>
|
||||
</policy>
|
||||
|
||||
|
|
Loading…
Reference in a new issue