| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <net/ethernet.h> |
| 4 |
|
| 5 |
#include "sd-event.h" |
| 6 |
#include "sd-ipv4acd.h" |
| 7 |
#include "sd-netlink.h" |
| 8 |
|
| 9 |
#include "in-addr-util.h" |
| 10 |
#include "log.h" |
| 11 |
#include "tests.h" |
| 12 |
|
| 13 |
static void acd_handler(sd_ipv4acd *acd, int event, void *userdata) { |
| 14 |
assert_se(acd); |
| 15 |
|
| 16 |
switch (event) { |
| 17 |
case SD_IPV4ACD_EVENT_BIND: |
| 18 |
log_info("bound"); |
| 19 |
break; |
| 20 |
case SD_IPV4ACD_EVENT_CONFLICT: |
| 21 |
log_info("conflict"); |
| 22 |
break; |
| 23 |
case SD_IPV4ACD_EVENT_STOP: |
| 24 |
log_error("the client was stopped"); |
| 25 |
break; |
| 26 |
default: |
| 27 |
assert_not_reached(); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
static int client_run(int ifindex, const struct in_addr *pa, const struct ether_addr *ha, sd_event *e) { |
| 32 |
sd_ipv4acd *acd; |
| 33 |
|
| 34 |
assert_se(sd_ipv4acd_new(&acd) >= 0); |
| 35 |
assert_se(sd_ipv4acd_attach_event(acd, e, 0) >= 0); |
| 36 |
|
| 37 |
assert_se(sd_ipv4acd_set_ifindex(acd, ifindex) >= 0); |
| 38 |
assert_se(sd_ipv4acd_set_mac(acd, ha) >= 0); |
| 39 |
assert_se(sd_ipv4acd_set_address(acd, pa) >= 0); |
| 40 |
assert_se(sd_ipv4acd_set_callback(acd, acd_handler, NULL) >= 0); |
| 41 |
|
| 42 |
log_info("starting IPv4ACD client"); |
| 43 |
|
| 44 |
assert_se(sd_ipv4acd_start(acd, true) >= 0); |
| 45 |
|
| 46 |
assert_se(sd_event_loop(e) >= 0); |
| 47 |
|
| 48 |
assert_se(!sd_ipv4acd_unref(acd)); |
| 49 |
|
| 50 |
return EXIT_SUCCESS; |
| 51 |
} |
| 52 |
|
| 53 |
static int test_acd(const char *ifname, const char *address) { |
| 54 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 55 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 56 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL; |
| 57 |
union in_addr_union pa; |
| 58 |
struct ether_addr ha; |
| 59 |
int ifindex; |
| 60 |
|
| 61 |
assert_se(in_addr_from_string(AF_INET, address, &pa) >= 0); |
| 62 |
|
| 63 |
assert_se(sd_event_new(&e) >= 0); |
| 64 |
|
| 65 |
assert_se(sd_netlink_open(&rtnl) >= 0); |
| 66 |
assert_se(sd_netlink_attach_event(rtnl, e, 0) >= 0); |
| 67 |
|
| 68 |
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, 0) >= 0); |
| 69 |
assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, ifname) >= 0); |
| 70 |
assert_se(sd_netlink_call(rtnl, m, 0, &reply) >= 0); |
| 71 |
|
| 72 |
assert_se(sd_rtnl_message_link_get_ifindex(reply, &ifindex) >= 0); |
| 73 |
assert_se(sd_netlink_message_read_ether_addr(reply, IFLA_ADDRESS, &ha) >= 0); |
| 74 |
|
| 75 |
client_run(ifindex, &pa.in, &ha, e); |
| 76 |
|
| 77 |
return EXIT_SUCCESS; |
| 78 |
} |
| 79 |
|
| 80 |
int main(int argc, char *argv[]) { |
| 81 |
test_setup_logging(LOG_DEBUG); |
| 82 |
|
| 83 |
if (argc == 3) |
| 84 |
return test_acd(argv[1], argv[2]); |
| 85 |
else { |
| 86 |
log_error("This program takes two arguments.\n" |
| 87 |
"\t %s <ifname> <IPv4 address>", program_invocation_short_name); |
| 88 |
return EXIT_FAILURE; |
| 89 |
} |
| 90 |
} |