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