]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
d9bf4f8c UTL |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright (C) 2014 Axis Communications AB. All rights reserved. | |
d9bf4f8c UTL |
6 | ***/ |
7 | ||
d9bf4f8c UTL |
8 | #include <errno.h> |
9 | #include <stdio.h> | |
07630cea | 10 | #include <stdlib.h> |
d9bf4f8c | 11 | #include <sys/socket.h> |
07630cea | 12 | #include <sys/types.h> |
d9bf4f8c UTL |
13 | #include <unistd.h> |
14 | ||
d9bf4f8c | 15 | #include "sd-ipv4ll.h" |
07630cea | 16 | |
996d1697 | 17 | #include "arp-util.h" |
3ffd4af2 | 18 | #include "fd-util.h" |
07630cea LP |
19 | #include "socket-util.h" |
20 | #include "util.h" | |
d9bf4f8c UTL |
21 | |
22 | static bool verbose = false; | |
23 | static bool extended = false; | |
24 | static int test_fd[2]; | |
25 | ||
26 | static int basic_request_handler_bind = 0; | |
27 | static int basic_request_handler_stop = 0; | |
e095f51d LP |
28 | static void* basic_request_handler_userdata = (void*) 0xCABCAB; |
29 | ||
d9bf4f8c | 30 | static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) { |
89ca10c6 | 31 | assert_se(userdata == basic_request_handler_userdata); |
d9bf4f8c UTL |
32 | |
33 | switch(event) { | |
be19c5b5 | 34 | case SD_IPV4LL_EVENT_STOP: |
d9bf4f8c UTL |
35 | basic_request_handler_stop = 1; |
36 | break; | |
be19c5b5 | 37 | case SD_IPV4LL_EVENT_BIND: |
d9bf4f8c UTL |
38 | basic_request_handler_bind = 1; |
39 | break; | |
40 | default: | |
41 | assert_se(0); | |
42 | break; | |
43 | } | |
44 | } | |
45 | ||
996d1697 TG |
46 | static int arp_network_send_raw_socket(int fd, int ifindex, |
47 | const struct ether_arp *arp) { | |
d9bf4f8c | 48 | assert_se(arp); |
996d1697 | 49 | assert_se(ifindex > 0); |
d9bf4f8c UTL |
50 | assert_se(fd >= 0); |
51 | ||
52 | if (send(fd, arp, sizeof(struct ether_arp), 0) < 0) | |
53 | return -errno; | |
54 | ||
55 | return 0; | |
56 | } | |
57 | ||
996d1697 TG |
58 | int arp_send_probe(int fd, int ifindex, |
59 | be32_t pa, const struct ether_addr *ha) { | |
60 | struct ether_arp ea = {}; | |
d9bf4f8c | 61 | |
3bf47e73 ZJS |
62 | assert_se(fd >= 0); |
63 | assert_se(ifindex > 0); | |
64 | assert_se(pa != 0); | |
65 | assert_se(ha); | |
d9bf4f8c | 66 | |
996d1697 | 67 | return arp_network_send_raw_socket(fd, ifindex, &ea); |
d9bf4f8c UTL |
68 | } |
69 | ||
996d1697 TG |
70 | int arp_send_announcement(int fd, int ifindex, |
71 | be32_t pa, const struct ether_addr *ha) { | |
72 | struct ether_arp ea = {}; | |
d9bf4f8c | 73 | |
3bf47e73 ZJS |
74 | assert_se(fd >= 0); |
75 | assert_se(ifindex > 0); | |
76 | assert_se(pa != 0); | |
77 | assert_se(ha); | |
d9bf4f8c | 78 | |
996d1697 | 79 | return arp_network_send_raw_socket(fd, ifindex, &ea); |
d9bf4f8c UTL |
80 | } |
81 | ||
996d1697 TG |
82 | int arp_network_bind_raw_socket(int index, be32_t address, const struct ether_addr *eth_mac) { |
83 | if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, test_fd) < 0) | |
84 | return -errno; | |
d9bf4f8c | 85 | |
996d1697 | 86 | return test_fd[0]; |
d9bf4f8c UTL |
87 | } |
88 | ||
89 | static void test_public_api_setters(sd_event *e) { | |
129dc1b4 | 90 | struct in_addr address = {}; |
38958cd6 | 91 | uint64_t seed = 0; |
d9bf4f8c UTL |
92 | sd_ipv4ll *ll; |
93 | struct ether_addr mac_addr = { | |
94 | .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; | |
95 | ||
96 | if (verbose) | |
97 | printf("* %s\n", __FUNCTION__); | |
98 | ||
99 | assert_se(sd_ipv4ll_new(&ll) == 0); | |
100 | assert_se(ll); | |
101 | ||
102 | assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL); | |
103 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0); | |
104 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY); | |
105 | ||
106 | assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL); | |
107 | assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0); | |
108 | ||
129dc1b4 TG |
109 | assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL); |
110 | address.s_addr |= htobe32(169U << 24 | 254U << 16); | |
111 | assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL); | |
112 | address.s_addr |= htobe32(0x00FF); | |
113 | assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL); | |
114 | address.s_addr |= htobe32(0xF000); | |
115 | assert_se(sd_ipv4ll_set_address(ll, &address) == 0); | |
116 | address.s_addr |= htobe32(0x0F00); | |
117 | assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL); | |
118 | ||
5625be76 RC |
119 | assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL); |
120 | assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0); | |
d9bf4f8c UTL |
121 | |
122 | assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL); | |
123 | assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL); | |
124 | assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0); | |
125 | ||
2f8e7633 LP |
126 | assert_se(sd_ipv4ll_set_ifindex(NULL, -1) == -EINVAL); |
127 | assert_se(sd_ipv4ll_set_ifindex(ll, -1) == -EINVAL); | |
128 | assert_se(sd_ipv4ll_set_ifindex(ll, -99) == -EINVAL); | |
129 | assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0); | |
130 | assert_se(sd_ipv4ll_set_ifindex(ll, 99) == 0); | |
d9bf4f8c UTL |
131 | |
132 | assert_se(sd_ipv4ll_ref(ll) == ll); | |
b45e4eb6 | 133 | assert_se(sd_ipv4ll_unref(ll) == NULL); |
d9bf4f8c UTL |
134 | |
135 | /* Cleanup */ | |
136 | assert_se(sd_ipv4ll_unref(ll) == NULL); | |
137 | } | |
138 | ||
139 | static void test_basic_request(sd_event *e) { | |
140 | ||
141 | sd_ipv4ll *ll; | |
142 | struct ether_arp arp; | |
143 | struct ether_addr mac_addr = { | |
144 | .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; | |
145 | ||
146 | if (verbose) | |
147 | printf("* %s\n", __FUNCTION__); | |
148 | ||
149 | assert_se(sd_ipv4ll_new(&ll) == 0); | |
150 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
151 | ||
152 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0); | |
153 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
154 | ||
155 | assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0); | |
156 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
157 | ||
158 | assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler, | |
89ca10c6 | 159 | basic_request_handler_userdata) == 0); |
d9bf4f8c UTL |
160 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); |
161 | ||
2f8e7633 | 162 | assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0); |
d9bf4f8c UTL |
163 | assert_se(sd_ipv4ll_start(ll) == 0); |
164 | ||
165 | sd_event_run(e, (uint64_t) -1); | |
166 | assert_se(sd_ipv4ll_start(ll) == -EBUSY); | |
167 | ||
e3dca008 TG |
168 | assert_se(sd_ipv4ll_is_running(ll)); |
169 | ||
d9bf4f8c UTL |
170 | /* PROBE */ |
171 | sd_event_run(e, (uint64_t) -1); | |
e095f51d | 172 | assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
d9bf4f8c UTL |
173 | |
174 | if (extended) { | |
175 | /* PROBE */ | |
176 | sd_event_run(e, (uint64_t) -1); | |
e095f51d | 177 | assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
d9bf4f8c UTL |
178 | |
179 | /* PROBE */ | |
180 | sd_event_run(e, (uint64_t) -1); | |
e095f51d | 181 | assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
d9bf4f8c UTL |
182 | |
183 | sd_event_run(e, (uint64_t) -1); | |
184 | assert_se(basic_request_handler_bind == 1); | |
185 | } | |
186 | ||
187 | sd_ipv4ll_stop(ll); | |
188 | assert_se(basic_request_handler_stop == 1); | |
189 | ||
190 | /* Cleanup */ | |
191 | assert_se(sd_ipv4ll_unref(ll) == NULL); | |
192 | safe_close(test_fd[1]); | |
193 | } | |
194 | ||
195 | int main(int argc, char *argv[]) { | |
4afd3348 | 196 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
d9bf4f8c | 197 | |
e3dca008 TG |
198 | log_set_max_level(LOG_DEBUG); |
199 | log_parse_environment(); | |
200 | log_open(); | |
201 | ||
d9bf4f8c UTL |
202 | assert_se(sd_event_new(&e) >= 0); |
203 | ||
204 | test_public_api_setters(e); | |
d9bf4f8c UTL |
205 | test_basic_request(e); |
206 | ||
207 | return 0; | |
208 | } |