From 77edc3a42e2528e72612b2518e28893c0e0782fa Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 15 Aug 2017 20:08:39 +0000 Subject: [PATCH] tests: use ifindex_lo and IFINDEX_LO_STR * tests/netlink_route.c: Do not check for HAVE_IF_INDEXTONAME. (if_nametoindex, IFINDEX_LO): Remove. (test_rtnl_link, test_rtnl_addr, test_rtnl_neigh, test_rtnl_tc, test_rtnl_addrlabel, test_rtnl_mdb): Use ifindex_lo instead of if_nametoindex, use IFINDEX_LO_STR instead of hardcoded string. * tests/netlink_sock_diag.c: Do not check for HAVE_IF_INDEXTONAME. (if_nametoindex, IFINDEX_LO): Remove. (test_inet_diag_sockid, test_inet_diag_req, test_inet_diag_req_v2, test_inet_diag_msg, test_smc_diag_req): Use ifindex_lo instead of if_nametoindex, use IFINDEX_LO_STR instead of hardcoded string. * tests/nlattr_inet_diag_msg.c: Do not check for HAVE_IF_INDEXTONAME. (if_nametoindex, IFINDEX_LO): Remove. (init_inet_diag_msg): Use ifindex_lo instead of if_nametoindex. (print_inet_diag_msg): Use IFINDEX_LO_STR instead of hardcoded string. * tests/nlattr_inet_diag_req_compat.c: Do not check for HAVE_IF_INDEXTONAME. (if_nametoindex, IFINDEX_LO): Remove. (init_inet_diag_req): Use ifindex_lo instead of if_nametoindex. (print_inet_diag_req): Use IFINDEX_LO_STR instead of hardcoded string. * tests/nlattr_inet_diag_req_v2.c: Do not check for HAVE_IF_INDEXTONAME. (if_nametoindex, IFINDEX_LO): Remove. (init_inet_diag_req_v2, print_inet_diag_req_v2, test_inet_diag_bc_dev_cond): Use ifindex_lo instead of if_nametoindex, use IFINDEX_LO_STR instead of hardcoded string. * tests/nlattr_packet_diag_msg.c: Do not check for HAVE_IF_INDEXTONAME. (if_nametoindex, IFINDEX_LO): Remove. (main): Use ifindex_lo instead of if_nametoindex. (print_packet_diag_mclist): Use IFINDEX_LO_STR instead of hardcoded string. --- tests/netlink_route.c | 32 ++++++++++----------------- tests/netlink_sock_diag.c | 34 +++++++++++------------------ tests/nlattr_inet_diag_msg.c | 11 +++------- tests/nlattr_inet_diag_req_compat.c | 10 ++------- tests/nlattr_inet_diag_req_v2.c | 15 +++++-------- tests/nlattr_packet_diag_msg.c | 12 +++------- 6 files changed, 38 insertions(+), 76 deletions(-) diff --git a/tests/netlink_route.c b/tests/netlink_route.c index 68d0fc7a..f9bd0d8e 100644 --- a/tests/netlink_route.c +++ b/tests/netlink_route.c @@ -56,14 +56,6 @@ #endif #include -#ifdef HAVE_IF_INDEXTONAME -/* used to conflict with */ -extern unsigned int if_nametoindex(const char *); -# define IFINDEX_LO (if_nametoindex("lo")) -#else -# define IFINDEX_LO 1 -#endif - #define TEST_NL_ROUTE(fd_, nlh0_, type_, obj_, print_family_, ...) \ do { \ /* family and string */ \ @@ -217,7 +209,7 @@ test_rtnl_link(const int fd) const struct ifinfomsg ifinfo = { .ifi_family = AF_UNIX, .ifi_type = ARPHRD_LOOPBACK, - .ifi_index = IFINDEX_LO, + .ifi_index = ifindex_lo(), .ifi_flags = IFF_UP, .ifi_change = 0xfabcdeba }; @@ -225,7 +217,7 @@ test_rtnl_link(const int fd) TEST_NL_ROUTE(fd, nlh0, RTM_GETLINK, ifinfo, printf("{ifi_family=AF_UNIX"), printf(", ifi_type=ARPHRD_LOOPBACK" - ", ifi_index=if_nametoindex(\"lo\")" + ", ifi_index=" IFINDEX_LO_STR ", ifi_flags=IFF_UP"); PRINT_FIELD_X(", ", ifinfo, ifi_change); printf("}")); @@ -240,7 +232,7 @@ test_rtnl_addr(const int fd) .ifa_prefixlen = 0xde, .ifa_flags = IFA_F_SECONDARY, .ifa_scope = RT_SCOPE_UNIVERSE, - .ifa_index = IFINDEX_LO + .ifa_index = ifindex_lo() }; TEST_NL_ROUTE(fd, nlh0, RTM_GETADDR, msg, @@ -248,7 +240,7 @@ test_rtnl_addr(const int fd) PRINT_FIELD_U(", ", msg, ifa_prefixlen); printf(", ifa_flags=IFA_F_SECONDARY" ", ifa_scope=RT_SCOPE_UNIVERSE" - ", ifa_index=if_nametoindex(\"lo\")"); + ", ifa_index=" IFINDEX_LO_STR); printf("}")); } @@ -313,7 +305,7 @@ test_rtnl_neigh(const int fd) void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct ndmsg msg = { .ndm_family = AF_UNIX, - .ndm_ifindex = IFINDEX_LO, + .ndm_ifindex = ifindex_lo(), .ndm_state = NUD_PERMANENT, .ndm_flags = NTF_PROXY, .ndm_type = NDA_UNSPEC @@ -321,7 +313,7 @@ test_rtnl_neigh(const int fd) TEST_NL_ROUTE(fd, nlh0, RTM_GETNEIGH, msg, printf("{ndm_family=AF_UNIX"), - printf(", ndm_ifindex=if_nametoindex(\"lo\")" + printf(", ndm_ifindex=" IFINDEX_LO_STR ", ndm_state=NUD_PERMANENT" ", ndm_flags=NTF_PROXY" ", ndm_type=NDA_UNSPEC}")); @@ -347,7 +339,7 @@ test_rtnl_tc(const int fd) void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct tcmsg msg = { .tcm_family = AF_UNIX, - .tcm_ifindex = IFINDEX_LO, + .tcm_ifindex = ifindex_lo(), .tcm_handle = 0xfadcdafb, .tcm_parent = 0xafbcadab, .tcm_info = 0xbcaedafa @@ -355,7 +347,7 @@ test_rtnl_tc(const int fd) TEST_NL_ROUTE(fd, nlh0, RTM_GETQDISC, msg, printf("{tcm_family=AF_UNIX"), - printf(", tcm_ifindex=if_nametoindex(\"lo\")"); + printf(", tcm_ifindex=" IFINDEX_LO_STR); PRINT_FIELD_U(", ", msg, tcm_handle); PRINT_FIELD_U(", ", msg, tcm_parent); PRINT_FIELD_U(", ", msg, tcm_info); @@ -385,7 +377,7 @@ test_rtnl_addrlabel(const int fd) .ifal_family = AF_UNIX, .ifal_prefixlen = 0xaf, .ifal_flags = 0xbd, - .ifal_index = IFINDEX_LO, + .ifal_index = ifindex_lo(), .ifal_seq = 0xfadcdafb }; @@ -393,7 +385,7 @@ test_rtnl_addrlabel(const int fd) printf("{ifal_family=AF_UNIX"), PRINT_FIELD_U(", ", msg, ifal_prefixlen); PRINT_FIELD_U(", ", msg, ifal_flags); - printf(", ifal_index=if_nametoindex(\"lo\")"); + printf(", ifal_index=" IFINDEX_LO_STR); PRINT_FIELD_U(", ", msg, ifal_seq); printf("}")); } @@ -438,12 +430,12 @@ test_rtnl_mdb(const int fd) void *const nlh0 = tail_alloc(NLMSG_HDRLEN); const struct br_port_msg msg = { .family = AF_UNIX, - .ifindex = IFINDEX_LO + .ifindex = ifindex_lo() }; TEST_NL_ROUTE(fd, nlh0, RTM_GETMDB, msg, printf("{family=AF_UNIX"), - printf(", ifindex=if_nametoindex(\"lo\")}")); + printf(", ifindex=" IFINDEX_LO_STR "}")); } #endif diff --git a/tests/netlink_sock_diag.c b/tests/netlink_sock_diag.c index 8f935c03..c2ebf129 100644 --- a/tests/netlink_sock_diag.c +++ b/tests/netlink_sock_diag.c @@ -48,14 +48,6 @@ #define SMC_ACTIVE 1 -#ifdef HAVE_IF_INDEXTONAME -/* used to conflict with */ -extern unsigned int if_nametoindex(const char *); -# define IFINDEX_LO (if_nametoindex("lo")) -#else -# define IFINDEX_LO 1 -#endif - #define TEST_SOCK_DIAG(fd_, nlh0_, \ family_, type_, flags_, \ obj_, print_family_, ...) \ @@ -388,7 +380,7 @@ test_inet_diag_sockid(const int fd) .id = { .idiag_sport = 0xfacd, .idiag_dport = 0xdead, - .idiag_if = IFINDEX_LO, + .idiag_if = ifindex_lo(), .idiag_cookie = { 0xdeadbeef, 0xbadc0ded } }, }; @@ -411,7 +403,7 @@ test_inet_diag_sockid(const int fd) ntohs(req.id.idiag_sport), ntohs(req.id.idiag_dport), address, address); - printf(", idiag_if=if_nametoindex(\"lo\")"); + printf(", idiag_if=" IFINDEX_LO_STR); PRINT_FIELD_COOKIE(", ", req.id, idiag_cookie); printf("}}")); @@ -434,7 +426,7 @@ test_inet_diag_sockid(const int fd) ntohs(req.id.idiag_sport), ntohs(req.id.idiag_dport), address6, address6); - printf(", idiag_if=if_nametoindex(\"lo\")"); + printf(", idiag_if=" IFINDEX_LO_STR); PRINT_FIELD_COOKIE(", ", req.id, idiag_cookie); printf("}}")); } @@ -452,7 +444,7 @@ test_inet_diag_req(const int fd) .id = { .idiag_sport = 0xdead, .idiag_dport = 0xadcd, - .idiag_if = IFINDEX_LO, + .idiag_if = ifindex_lo(), .idiag_cookie = { 0xdeadbeef, 0xbadc0ded } }, .idiag_states = 1 << TCP_LAST_ACK, @@ -476,7 +468,7 @@ test_inet_diag_req(const int fd) ntohs(req.id.idiag_sport), ntohs(req.id.idiag_dport), address, address); - printf(", idiag_if=if_nametoindex(\"lo\")"); + printf(", idiag_if=" IFINDEX_LO_STR); PRINT_FIELD_COOKIE(", ", req.id, idiag_cookie); printf("}, idiag_states=1<id.idiag_src) || @@ -75,7 +69,8 @@ print_inet_diag_msg(const unsigned int msg_len) ", id={idiag_sport=htons(0), idiag_dport=htons(0)" ", idiag_src=inet_addr(\"%s\")" ", idiag_dst=inet_addr(\"%s\")" - ", idiag_if=if_nametoindex(\"lo\"), idiag_cookie=[0, 0]}" + ", idiag_if=" IFINDEX_LO_STR + ", idiag_cookie=[0, 0]}" ", idiag_expires=0, idiag_rqueue=0, idiag_wqueue=0" ", idiag_uid=0, idiag_inode=0}", msg_len, address, address); diff --git a/tests/nlattr_inet_diag_req_compat.c b/tests/nlattr_inet_diag_req_compat.c index 76b79889..b0b21713 100644 --- a/tests/nlattr_inet_diag_req_compat.c +++ b/tests/nlattr_inet_diag_req_compat.c @@ -41,12 +41,6 @@ static const char address[] = "10.11.12.13"; -#ifdef HAVE_IF_INDEXTONAME -# define IFINDEX_LO (if_nametoindex("lo")) -#else -# define IFINDEX_LO 1 -#endif - static void init_inet_diag_req(struct nlmsghdr *const nlh, const unsigned int msg_len) { @@ -61,7 +55,7 @@ init_inet_diag_req(struct nlmsghdr *const nlh, const unsigned int msg_len) .idiag_family = AF_INET, .idiag_ext = 1 << (INET_DIAG_TOS - 1), .idiag_states = 1 << TCP_LAST_ACK, - .id.idiag_if = IFINDEX_LO + .id.idiag_if = ifindex_lo() ); if (!inet_pton(AF_INET, address, req->id.idiag_src) || @@ -79,7 +73,7 @@ print_inet_diag_req(const unsigned int msg_len) ", id={idiag_sport=htons(0), idiag_dport=htons(0)" ", idiag_src=inet_addr(\"%s\")" ", idiag_dst=inet_addr(\"%s\")" - ", idiag_if=if_nametoindex(\"lo\")" + ", idiag_if=" IFINDEX_LO_STR ", idiag_cookie=[0, 0]}" ", idiag_states=1< #include -#ifdef HAVE_IF_INDEXTONAME -# define IFINDEX_LO (if_nametoindex("lo")) -#else -# define IFINDEX_LO 1 -#endif - static const char address[] = "10.11.12.13"; static const unsigned int hdrlen = sizeof(struct inet_diag_req_v2); static void *nlh0; @@ -65,7 +59,7 @@ init_inet_diag_req_v2(struct nlmsghdr *const nlh, const unsigned int msg_len) .idiag_ext = 1 << (INET_DIAG_CONG - 1), .sdiag_protocol = IPPROTO_TCP, .idiag_states = 1 << TCP_CLOSE, - .id.idiag_if = IFINDEX_LO + .id.idiag_if = ifindex_lo() ); if (!inet_pton(AF_INET, address, req->id.idiag_src) || @@ -84,7 +78,8 @@ print_inet_diag_req_v2(const unsigned int msg_len) ", id={idiag_sport=htons(0), idiag_dport=htons(0)" ", idiag_src=inet_addr(\"%s\")" ", idiag_dst=inet_addr(\"%s\")" - ", idiag_if=if_nametoindex(\"lo\"), idiag_cookie=[0, 0]}}", + ", idiag_if=" IFINDEX_LO_STR + ", idiag_cookie=[0, 0]}}", msg_len, address, address); } @@ -268,7 +263,7 @@ test_inet_diag_bc_dev_cond(const int fd) static const struct inet_diag_bc_op op = { .code = INET_DIAG_BC_DEV_COND, }; - const uint32_t ifindex = IFINDEX_LO; + const uint32_t ifindex = ifindex_lo(); char buf[sizeof(op) + sizeof(ifindex)]; memcpy(buf, &op, sizeof(op)); memcpy(buf + sizeof(op), pattern, sizeof(ifindex)); @@ -294,7 +289,7 @@ test_inet_diag_bc_dev_cond(const int fd) INET_DIAG_REQ_BYTECODE, sizeof(buf), buf, sizeof(buf), print_inet_diag_bc_op("INET_DIAG_BC_DEV_COND"); - printf("if_nametoindex(\"lo\")}")); + printf(IFINDEX_LO_STR "}")); } static void diff --git a/tests/nlattr_packet_diag_msg.c b/tests/nlattr_packet_diag_msg.c index 55b4144d..4211e201 100644 --- a/tests/nlattr_packet_diag_msg.c +++ b/tests/nlattr_packet_diag_msg.c @@ -39,12 +39,6 @@ #include #include -#ifdef HAVE_IF_INDEXTONAME -# define IFINDEX_LO (if_nametoindex("lo")) -#else -# define IFINDEX_LO 1 -#endif - static void init_packet_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len) { @@ -75,7 +69,7 @@ print_packet_diag_msg(const unsigned int msg_len) static void print_packet_diag_mclist(const struct packet_diag_mclist *const dml) { - printf("{pdmc_index=if_nametoindex(\"lo\")"); + printf("{pdmc_index=" IFINDEX_LO_STR); PRINT_FIELD_U(", ", *dml, pdmc_count); PRINT_FIELD_U(", ", *dml, pdmc_type); PRINT_FIELD_U(", ", *dml, pdmc_alen); @@ -131,14 +125,14 @@ main(void) const struct packet_diag_mclist dml[] = { { - .pdmc_index = IFINDEX_LO, + .pdmc_index = ifindex_lo(), .pdmc_count = 0xabcdaefc, .pdmc_type = 0xcdaf, .pdmc_alen = 4, .pdmc_addr = "1234" }, { - .pdmc_index = IFINDEX_LO, + .pdmc_index = ifindex_lo(), .pdmc_count = 0xdaefeafc, .pdmc_type = 0xadef, .pdmc_alen = 4, -- 2.40.0