static void test_link_configure(sd_rtnl *rtnl, int ifindex) { _cleanup_rtnl_message_unref_ sd_rtnl_message *message; uint16_t type; const char *mac = "98:fe:94:3f:c6:18", *name = "test"; unsigned int mtu = 1450; void *data; /* we'd really like to test NEWLINK, but let's not mess with the running kernel */ assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0); assert_se(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0); assert_se(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0); assert_se(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0); assert_se(sd_rtnl_call(rtnl, message, 0, NULL) == 1); assert_se(sd_rtnl_message_read(message, &type, &data) > 0); assert_se(type == IFLA_IFNAME); assert_se(streq(name, (char *) data)); assert_se(sd_rtnl_message_read(message, &type, &data) > 0); assert_se(type == IFLA_ADDRESS); assert_se(streq(mac, ether_ntoa(data))); assert_se(sd_rtnl_message_read(message, &type, &data) > 0); assert_se(type == IFLA_MTU); assert_se(mtu == *(unsigned int *) data); }
int main(void) { sd_rtnl *rtnl; sd_rtnl_message *m; sd_rtnl_message *r; void *data; int if_loopback; uint16_t type; test_match(); test_multiple(); test_route(); test_container(); assert_se(sd_rtnl_open(&rtnl, 0) >= 0); assert_se(rtnl); if_loopback = (int) if_nametoindex("lo"); assert_se(if_loopback > 0); test_async(if_loopback); test_pipe(if_loopback); test_event_loop(if_loopback); test_link_configure(rtnl, if_loopback); assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, if_loopback) >= 0); assert_se(m); assert_se(sd_rtnl_message_get_type(m, &type) >= 0); assert_se(type == RTM_GETLINK); assert_se(sd_rtnl_message_read(m, &type, &data) == -EPERM); assert_se(sd_rtnl_call(rtnl, m, 0, &r) == 1); assert_se(sd_rtnl_message_get_type(r, &type) >= 0); assert_se(type == RTM_NEWLINK); assert_se(sd_rtnl_message_read(m, &type, &data) == 0); assert_se((r = sd_rtnl_message_unref(r)) == NULL); assert_se(sd_rtnl_call(rtnl, m, -1, &r) == -EPERM); assert_se((m = sd_rtnl_message_unref(m)) == NULL); 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); assert_se((r = sd_rtnl_message_unref(r)) == NULL); assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL); return EXIT_SUCCESS; }
static void test_route(void) { _cleanup_rtnl_message_unref_ sd_rtnl_message *req; struct in_addr addr; uint32_t index = 2; uint16_t type; void *data; uint32_t u32_data; int r; struct rtmsg *rtm; r = sd_rtnl_message_new_route(NULL, &req, RTM_NEWROUTE, AF_INET); if (r < 0) { log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r)); return; } addr.s_addr = htonl(INADDR_LOOPBACK); r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &addr); if (r < 0) { log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r)); return; } r = sd_rtnl_message_append_u32(req, RTA_OIF, index); if (r < 0) { log_error("Could not append RTA_OIF attribute: %s", strerror(-r)); return; } assert_se(rtnl_message_seal(NULL, req) >= 0); assert_se(sd_rtnl_message_read(req, &type, &data) > 0); assert_se(type == RTA_GATEWAY); assert_se(((struct in_addr *)data)->s_addr == addr.s_addr); assert_se(sd_rtnl_message_read(req, &type, &data) > 0); assert_se(type == RTA_OIF); assert_se(*(uint32_t *) data == index); rtm = NLMSG_DATA(req->hdr); r = rtnl_message_parse(req, &req->rta_offset_tb, &req->rta_tb_size, RTA_MAX, RTM_RTA(rtm), RTM_PAYLOAD(req->hdr)); assert_se(sd_rtnl_message_read_u32(req, RTA_GATEWAY, &u32_data) == 0); assert_se(sd_rtnl_message_read_u32(req, RTA_OIF, &u32_data) == 0); assert_se((req = sd_rtnl_message_unref(req)) == NULL); }
static int link_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { void *data; uint16_t type; char *ifname = userdata; assert_se(rtnl); assert_se(m); log_info("got link info about %s", ifname); free(ifname); while (sd_rtnl_message_read(m, &type, &data) > 0) { switch (type) { // case IFLA_MTU: // assert_se(*(unsigned int *) data == 65536); // break; // case IFLA_QDISC: // assert_se(streq((char *) data, "noqueue")); // break; case IFLA_IFNAME: assert_se(streq((char *) data, "lo")); break; } } return 1; }
static void test_container(void) { _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL; uint16_t type; uint32_t u32_data; void *data; int r; struct ifinfomsg *ifi; assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0); assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0); assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -ENOTSUP); assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0); assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) >= 0); assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) == -ENOTSUP); assert_se(sd_rtnl_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0); assert_se(sd_rtnl_message_close_container(m) >= 0); assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0); assert_se(sd_rtnl_message_close_container(m) >= 0); assert_se(sd_rtnl_message_close_container(m) == -EINVAL); assert_se(rtnl_message_seal(NULL, m) >= 0); assert_se(sd_rtnl_message_read(m, &type, &data) >= 0); assert_se(type == IFLA_LINKINFO); assert_se(data == NULL); /* assert_se(sd_rtnl_message_read(m, &type, &data) >= 0); assert_se(type == IFLA_INFO_KIND); assert_se(streq("kind", (char *)data)); assert_se(sd_rtnl_message_read(m, &type, &data) >= 0); assert_se(type == IFLA_INFO_DATA); assert_se(data == NULL); assert_se(sd_rtnl_message_read(m, &type, &data) >= 0); assert_se(type == IFLA_VLAN_ID); assert_se(*(uint16_t *)data == 100); assert_se(sd_rtnl_message_read(m, &type, &data) == 0); assert_se(sd_rtnl_message_exit_container(m) >= 0); assert_se(sd_rtnl_message_read(m, &type, &data) >= 0); assert_se(type == IFLA_INFO_KIND); assert_se(streq("kind", (char *)data)); assert_se(sd_rtnl_message_read(m, &type, &data) == 0); assert_se(sd_rtnl_message_exit_container(m) >= 0); */ ifi = NLMSG_DATA(m->hdr); r = rtnl_message_parse(m, &m->rta_offset_tb, &m->rta_tb_size, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(m->hdr)); if(r < 0) return; assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) == 0); assert_se(sd_rtnl_message_exit_container(m) == -EINVAL); }
static void test_route(void) { _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req; uint32_t addr = htonl(INADDR_LOOPBACK); uint32_t index = 2; uint16_t type; void *data; int r; r = sd_rtnl_message_route_new(RTM_NEWROUTE, AF_INET, 0, 0, 0, RT_TABLE_MAIN, RT_SCOPE_UNIVERSE, RTPROT_BOOT, RTN_UNICAST, 0, &req); if (r < 0) { log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r)); return; } r = sd_rtnl_message_append(req, RTA_GATEWAY, &addr); if (r < 0) { log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r)); return; } r = sd_rtnl_message_append(req, RTA_OIF, &index); if (r < 0) { log_error("Could not append RTA_OIF attribute: %s", strerror(-r)); return; } assert(sd_rtnl_message_read(req, &type, &data) > 0); assert(type == RTA_GATEWAY); assert(*(uint32_t *) data == addr); assert(sd_rtnl_message_read(req, &type, &data) > 0); assert(type == RTA_OIF); assert(*(uint32_t *) data == index); }
static void test_container(void) { _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL; uint16_t type; void *data; assert(sd_rtnl_message_link_new(RTM_NEWLINK, 0, 0, 0, &m) >= 0); assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0); assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -EINVAL); assert(sd_rtnl_message_append(m, IFLA_INFO_KIND, "kind") >= 0); assert(sd_rtnl_message_close_container(m) >= 0); assert(sd_rtnl_message_close_container(m) == -EINVAL); assert(sd_rtnl_message_read(m, &type, &data) == -EINVAL); /* TODO: add support for entering containers assert(sd_rtnl_message_read(m, &type, &data) > 0); assert(type == IFLA_INFO_KIND); assert(streq("kind", (char *) data)); assert(sd_rtnl_message_read(m, &type, &data) == 0); */ }
static void test_link_get(sd_rtnl *rtnl, int ifindex) { sd_rtnl_message *m; sd_rtnl_message *r; unsigned int mtu = 1500; unsigned int *mtu_reply; void *data; char *str_data; 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); /* u8 test cases */ assert_se(sd_rtnl_message_append_u8(m, IFLA_CARRIER, 0) >= 0); assert_se(sd_rtnl_message_append_u8(m, IFLA_OPERSTATE, 0) >= 0); assert_se(sd_rtnl_message_append_u8(m, IFLA_LINKMODE, 0) >= 0); /* u32 test cases */ assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, mtu) >= 0); assert_se(sd_rtnl_message_append_u32(m, IFLA_GROUP, 0) >= 0); assert_se(sd_rtnl_message_append_u32(m, IFLA_TXQLEN, 0) >= 0); assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_TX_QUEUES, 0) >= 0); assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_RX_QUEUES, 0) >= 0); assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1); /* u8 read back */ assert_se(sd_rtnl_message_read(m, &type, &data) == 1); assert_se(type == IFLA_CARRIER); assert_se(sd_rtnl_message_read(m, &type, &data) == 1); assert_se(type == IFLA_OPERSTATE); assert_se(sd_rtnl_message_read(m, &type, &data) == 1); assert_se(type == IFLA_LINKMODE); /* u32 read back */ assert_se(sd_rtnl_message_read(m, &type, (void **) &mtu_reply) == 1); assert_se(type == IFLA_MTU); assert_se(*mtu_reply == mtu); assert_se(sd_rtnl_message_read(m, &type, &data) == 1); assert_se(type == IFLA_GROUP); assert_se(sd_rtnl_message_read(m, &type, &data) == 1); assert_se(type == IFLA_TXQLEN); assert_se(sd_rtnl_message_read(m, &type, &data) == 1); assert_se(type == IFLA_NUM_TX_QUEUES); assert_se(sd_rtnl_message_read(m, &type, &data) == 1); assert_se(type == IFLA_NUM_RX_QUEUES); while (sd_rtnl_message_read(r, &type, &data) > 0) { switch (type) { // case IFLA_MTU: // assert_se(*(unsigned int *) data == 65536); // break; // case IFLA_QDISC: // assert_se(streq((char *) data, "noqueue")); // break; case IFLA_IFNAME: assert_se(streq((char *) data, "lo")); break; } } assert_se(sd_rtnl_message_read_string(r, IFLA_IFNAME, &str_data) == 0); assert_se(sd_rtnl_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0); assert_se(sd_rtnl_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0); assert_se(sd_rtnl_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0); assert_se(sd_rtnl_message_read_u32(r, IFLA_MTU, &u32_data) == 0); assert_se(sd_rtnl_message_read_u32(r, IFLA_GROUP, &u32_data) == 0); assert_se(sd_rtnl_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0); 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, ð_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); }
int main(void) { sd_rtnl *rtnl; sd_rtnl_message *m; sd_rtnl_message *r; void *data; int if_loopback; uint16_t type; unsigned int mtu = 0; unsigned int *mtu_reply; test_multiple(); test_route(); test_container(); assert(sd_rtnl_open(0, &rtnl) >= 0); assert(rtnl); if_loopback = (int) if_nametoindex("lo"); assert(if_loopback > 0); test_async(if_loopback); test_pipe(if_loopback); test_event_loop(if_loopback); test_link_configure(rtnl, if_loopback); assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0); assert(m); assert(sd_rtnl_message_get_type(m, &type) >= 0); assert(type == RTM_GETLINK); assert(sd_rtnl_message_read(m, &type, &data) == 0); assert(sd_rtnl_call(rtnl, m, 0, &r) == 1); assert(sd_rtnl_message_get_type(r, &type) >= 0); assert(type == RTM_NEWLINK); assert(sd_rtnl_message_read(m, &type, &data) == 0); assert((r = sd_rtnl_message_unref(r)) == NULL); assert(sd_rtnl_call(rtnl, m, -1, &r) == -EPERM); assert((m = sd_rtnl_message_unref(m)) == NULL); assert((r = sd_rtnl_message_unref(r)) == NULL); assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0); assert(m); assert(sd_rtnl_message_append(m, IFLA_MTU, &mtu) >= 0); assert(sd_rtnl_message_read(m, &type, (void **) &mtu_reply) == 1); assert(type == IFLA_MTU); assert(*mtu_reply == 0); assert(sd_rtnl_message_read(m, &type, &data) == 0); assert(sd_rtnl_call(rtnl, m, -1, &r) == 1); while (sd_rtnl_message_read(r, &type, &data) > 0) { switch (type) { // case IFLA_MTU: // assert(*(unsigned int *) data == 65536); // break; // case IFLA_QDISC: // assert(streq((char *) data, "noqueue")); // break; case IFLA_IFNAME: assert(streq((char *) data, "lo")); break; } } assert(sd_rtnl_flush(rtnl) >= 0); assert((m = sd_rtnl_message_unref(m)) == NULL); assert((r = sd_rtnl_message_unref(r)) == NULL); assert((rtnl = sd_rtnl_unref(rtnl)) == NULL); return EXIT_SUCCESS; }