1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-07 01:27:11 +03:00

sd-rtnl: introduce read ether and ip address functions

This patch introduces reading ethernet address and IPV4/IPv6
as well which is based on table based look up.

[tomegun: rename read_ether() to read_ether_addr() to match the append function.]
This commit is contained in:
Susant Sahani 2014-03-07 20:38:02 +05:30 committed by Tom Gundersen
parent b2cdc6664e
commit 4e9e7f1836
3 changed files with 74 additions and 0 deletions

View File

@ -876,6 +876,51 @@ int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *
return 0;
}
int sd_rtnl_message_read_ether_addr(sd_rtnl_message *m, unsigned short type, struct ether_addr *data) {
int r;
void *attr_data;
assert_return(data, -EINVAL);
r = rtnl_message_read_internal(m, type, &attr_data);
if(r < 0)
return r;
memcpy(data, attr_data, sizeof(struct ether_addr));
return 0;
}
int sd_rtnl_message_read_in_addr(sd_rtnl_message *m, unsigned short type, struct in_addr *data) {
int r;
void *attr_data;
assert_return(data, -EINVAL);
r = rtnl_message_read_internal(m, type, &attr_data);
if(r < 0)
return r;
memcpy(data, attr_data, sizeof(struct in_addr));
return 0;
}
int sd_rtnl_message_read_in6_addr(sd_rtnl_message *m, unsigned short type, struct in6_addr *data) {
int r;
void *attr_data;
assert_return(data, -EINVAL);
r = rtnl_message_read_internal(m, type, &attr_data);
if(r < 0)
return r;
memcpy(data, attr_data, sizeof(struct in6_addr));
return 0;
}
int sd_rtnl_message_exit_container(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
assert_return(m->sealed, -EINVAL);

View File

@ -68,6 +68,7 @@ static void test_link_get(sd_rtnl *rtnl, int ifindex) {
uint16_t type;
uint8_t u8_data;
uint32_t u32_data;
struct ether_addr eth_data;
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
assert_se(m);
@ -139,11 +140,35 @@ static void test_link_get(sd_rtnl *rtnl, int ifindex) {
assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);
assert_se(sd_rtnl_message_read_ether_addr(r, IFLA_ADDRESS, &eth_data) == 0);
assert_se(sd_rtnl_flush(rtnl) >= 0);
assert_se((m = sd_rtnl_message_unref(m)) == NULL);
assert_se((r = sd_rtnl_message_unref(r)) == NULL);
}
static void test_address_get(sd_rtnl *rtnl, int ifindex) {
sd_rtnl_message *m;
sd_rtnl_message *r;
struct in_addr in_data;
assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0);
assert_se(m);
assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);
assert_se(sd_rtnl_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0);
assert_se(sd_rtnl_message_read_in_addr(r, IFA_ADDRESS, &in_data) == 0);
assert_se(sd_rtnl_message_read_in_addr(r, IFA_LABEL, &in_data) == 0);
assert_se(sd_rtnl_message_read_in_addr(r, IFA_CACHEINFO, &in_data) == 0);
assert_se(sd_rtnl_flush(rtnl) >= 0);
assert_se((m = sd_rtnl_message_unref(m)) == NULL);
assert_se((r = sd_rtnl_message_unref(r)) == NULL);
}
static void test_route(void) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *req;
struct in_addr addr;
@ -441,6 +466,7 @@ int main(void) {
assert_se((r = sd_rtnl_message_unref(r)) == NULL);
test_link_get(rtnl, if_loopback);
test_address_get(rtnl, if_loopback);
assert_se(sd_rtnl_flush(rtnl) >= 0);
assert_se((m = sd_rtnl_message_unref(m)) == NULL);

View File

@ -107,6 +107,9 @@ int sd_rtnl_message_read_string(sd_rtnl_message *m, unsigned short type, char **
int sd_rtnl_message_read_u8(sd_rtnl_message *m, unsigned short type, uint8_t *data);
int sd_rtnl_message_read_u16(sd_rtnl_message *m, unsigned short type, uint16_t *data);
int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *data);
int sd_rtnl_message_read_ether_addr(sd_rtnl_message *m, unsigned short type, struct ether_addr *data);
int sd_rtnl_message_read_in_addr(sd_rtnl_message *m, unsigned short type, struct in_addr *data);
int sd_rtnl_message_read_in6_addr(sd_rtnl_message *m, unsigned short type, struct in6_addr *data);
int sd_rtnl_message_exit_container(sd_rtnl_message *m);
int sd_rtnl_message_rewind(sd_rtnl_message *m);