util: introduce helper functions to read in_addr from bus message
This commit is contained in:
parent
e293e755d5
commit
976009897b
50
src/shared/bus-message-util.c
Normal file
50
src/shared/bus-message-util.c
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||||
|
|
||||||
|
#include "bus-message-util.h"
|
||||||
|
|
||||||
|
int bus_message_read_family(sd_bus_message *message, sd_bus_error *error, int *ret) {
|
||||||
|
int family, r;
|
||||||
|
|
||||||
|
assert(message);
|
||||||
|
assert(ret);
|
||||||
|
|
||||||
|
assert_cc(sizeof(int) == sizeof(int32_t));
|
||||||
|
|
||||||
|
r = sd_bus_message_read(message, "i", &family);
|
||||||
|
if (r < 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
|
if (!IN_SET(family, AF_INET, AF_INET6))
|
||||||
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
|
||||||
|
|
||||||
|
*ret = family;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bus_message_read_in_addr_auto(sd_bus_message *message, sd_bus_error *error, int *ret_family, union in_addr_union *ret_addr) {
|
||||||
|
int family, r;
|
||||||
|
const void *d;
|
||||||
|
size_t sz;
|
||||||
|
|
||||||
|
assert(message);
|
||||||
|
|
||||||
|
r = sd_bus_message_read(message, "i", &family);
|
||||||
|
if (r < 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
|
r = sd_bus_message_read_array(message, 'y', &d, &sz);
|
||||||
|
if (r < 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
|
if (!IN_SET(family, AF_INET, AF_INET6))
|
||||||
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
|
||||||
|
|
||||||
|
if (sz != FAMILY_ADDRESS_SIZE(family))
|
||||||
|
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
|
||||||
|
|
||||||
|
if (ret_family)
|
||||||
|
*ret_family = family;
|
||||||
|
if (ret_addr)
|
||||||
|
memcpy(ret_addr, d, sz);
|
||||||
|
return 0;
|
||||||
|
}
|
9
src/shared/bus-message-util.h
Normal file
9
src/shared/bus-message-util.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "sd-bus.h"
|
||||||
|
|
||||||
|
#include "in-addr-util.h"
|
||||||
|
|
||||||
|
int bus_message_read_family(sd_bus_message *message, sd_bus_error *error, int *ret);
|
||||||
|
int bus_message_read_in_addr_auto(sd_bus_message *message, sd_bus_error *error, int *ret_family, union in_addr_union *ret_addr);
|
|
@ -35,6 +35,8 @@ shared_sources = files('''
|
||||||
bus-log-control-api.h
|
bus-log-control-api.h
|
||||||
bus-map-properties.c
|
bus-map-properties.c
|
||||||
bus-map-properties.h
|
bus-map-properties.h
|
||||||
|
bus-message-util.c
|
||||||
|
bus-message-util.h
|
||||||
bus-object.c
|
bus-object.c
|
||||||
bus-object.h
|
bus-object.h
|
||||||
bus-polkit.c
|
bus-polkit.c
|
||||||
|
|
Loading…
Reference in a new issue