]>
Commit | Line | Data |
---|---|---|
60f067b4 JS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright (C) 2014 Axis Communications AB. All rights reserved. | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
60f067b4 JS |
20 | #include <assert.h> |
21 | #include <errno.h> | |
22 | #include <stdio.h> | |
db2df898 | 23 | #include <stdlib.h> |
60f067b4 | 24 | #include <sys/socket.h> |
db2df898 | 25 | #include <sys/types.h> |
60f067b4 JS |
26 | #include <unistd.h> |
27 | ||
60f067b4 | 28 | #include "sd-ipv4ll.h" |
db2df898 | 29 | |
6300502b | 30 | #include "arp-util.h" |
db2df898 MP |
31 | #include "fd-util.h" |
32 | #include "socket-util.h" | |
33 | #include "util.h" | |
60f067b4 JS |
34 | |
35 | static bool verbose = false; | |
36 | static bool extended = false; | |
37 | static int test_fd[2]; | |
38 | ||
39 | static int basic_request_handler_bind = 0; | |
40 | static int basic_request_handler_stop = 0; | |
5a920b42 MP |
41 | static void* basic_request_handler_userdata = (void*) 0xCABCAB; |
42 | ||
60f067b4 | 43 | static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) { |
d9dfd233 | 44 | assert_se(userdata == basic_request_handler_userdata); |
60f067b4 JS |
45 | |
46 | switch(event) { | |
6300502b | 47 | case SD_IPV4LL_EVENT_STOP: |
60f067b4 JS |
48 | basic_request_handler_stop = 1; |
49 | break; | |
6300502b | 50 | case SD_IPV4LL_EVENT_BIND: |
60f067b4 JS |
51 | basic_request_handler_bind = 1; |
52 | break; | |
53 | default: | |
54 | assert_se(0); | |
55 | break; | |
56 | } | |
57 | } | |
58 | ||
6300502b MP |
59 | static int arp_network_send_raw_socket(int fd, int ifindex, |
60 | const struct ether_arp *arp) { | |
60f067b4 | 61 | assert_se(arp); |
6300502b | 62 | assert_se(ifindex > 0); |
60f067b4 JS |
63 | assert_se(fd >= 0); |
64 | ||
65 | if (send(fd, arp, sizeof(struct ether_arp), 0) < 0) | |
66 | return -errno; | |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
6300502b MP |
71 | int arp_send_probe(int fd, int ifindex, |
72 | be32_t pa, const struct ether_addr *ha) { | |
73 | struct ether_arp ea = {}; | |
60f067b4 | 74 | |
6300502b MP |
75 | assert(fd >= 0); |
76 | assert(ifindex > 0); | |
77 | assert(pa != 0); | |
78 | assert(ha); | |
60f067b4 | 79 | |
6300502b | 80 | return arp_network_send_raw_socket(fd, ifindex, &ea); |
60f067b4 JS |
81 | } |
82 | ||
6300502b MP |
83 | int arp_send_announcement(int fd, int ifindex, |
84 | be32_t pa, const struct ether_addr *ha) { | |
85 | struct ether_arp ea = {}; | |
60f067b4 | 86 | |
6300502b MP |
87 | assert(fd >= 0); |
88 | assert(ifindex > 0); | |
89 | assert(pa != 0); | |
90 | assert(ha); | |
60f067b4 | 91 | |
6300502b | 92 | return arp_network_send_raw_socket(fd, ifindex, &ea); |
60f067b4 JS |
93 | } |
94 | ||
6300502b MP |
95 | int arp_network_bind_raw_socket(int index, be32_t address, const struct ether_addr *eth_mac) { |
96 | if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, test_fd) < 0) | |
97 | return -errno; | |
60f067b4 | 98 | |
6300502b | 99 | return test_fd[0]; |
60f067b4 JS |
100 | } |
101 | ||
102 | static void test_public_api_setters(sd_event *e) { | |
db2df898 | 103 | struct in_addr address = {}; |
5a920b42 | 104 | uint64_t seed = 0; |
60f067b4 JS |
105 | sd_ipv4ll *ll; |
106 | struct ether_addr mac_addr = { | |
107 | .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; | |
108 | ||
109 | if (verbose) | |
110 | printf("* %s\n", __FUNCTION__); | |
111 | ||
112 | assert_se(sd_ipv4ll_new(&ll) == 0); | |
113 | assert_se(ll); | |
114 | ||
115 | assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL); | |
116 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0); | |
117 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY); | |
118 | ||
119 | assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL); | |
120 | assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0); | |
121 | ||
db2df898 MP |
122 | assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL); |
123 | address.s_addr |= htobe32(169U << 24 | 254U << 16); | |
124 | assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL); | |
125 | address.s_addr |= htobe32(0x00FF); | |
126 | assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL); | |
127 | address.s_addr |= htobe32(0xF000); | |
128 | assert_se(sd_ipv4ll_set_address(ll, &address) == 0); | |
129 | address.s_addr |= htobe32(0x0F00); | |
130 | assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL); | |
131 | ||
6300502b | 132 | assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL); |
60f067b4 JS |
133 | assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0); |
134 | ||
135 | assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL); | |
136 | assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL); | |
137 | assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0); | |
138 | ||
5a920b42 MP |
139 | assert_se(sd_ipv4ll_set_ifindex(NULL, -1) == -EINVAL); |
140 | assert_se(sd_ipv4ll_set_ifindex(ll, -1) == -EINVAL); | |
141 | assert_se(sd_ipv4ll_set_ifindex(ll, -99) == -EINVAL); | |
142 | assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0); | |
143 | assert_se(sd_ipv4ll_set_ifindex(ll, 99) == 0); | |
60f067b4 JS |
144 | |
145 | assert_se(sd_ipv4ll_ref(ll) == ll); | |
6300502b | 146 | assert_se(sd_ipv4ll_unref(ll) == NULL); |
60f067b4 JS |
147 | |
148 | /* Cleanup */ | |
149 | assert_se(sd_ipv4ll_unref(ll) == NULL); | |
150 | } | |
151 | ||
152 | static void test_basic_request(sd_event *e) { | |
153 | ||
154 | sd_ipv4ll *ll; | |
155 | struct ether_arp arp; | |
156 | struct ether_addr mac_addr = { | |
157 | .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; | |
158 | ||
159 | if (verbose) | |
160 | printf("* %s\n", __FUNCTION__); | |
161 | ||
162 | assert_se(sd_ipv4ll_new(&ll) == 0); | |
163 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
164 | ||
165 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0); | |
166 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
167 | ||
168 | assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0); | |
169 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
170 | ||
171 | assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler, | |
d9dfd233 | 172 | basic_request_handler_userdata) == 0); |
60f067b4 JS |
173 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); |
174 | ||
5a920b42 | 175 | assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0); |
60f067b4 JS |
176 | assert_se(sd_ipv4ll_start(ll) == 0); |
177 | ||
178 | sd_event_run(e, (uint64_t) -1); | |
179 | assert_se(sd_ipv4ll_start(ll) == -EBUSY); | |
180 | ||
6300502b MP |
181 | assert_se(sd_ipv4ll_is_running(ll)); |
182 | ||
60f067b4 JS |
183 | /* PROBE */ |
184 | sd_event_run(e, (uint64_t) -1); | |
5a920b42 | 185 | assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
60f067b4 JS |
186 | |
187 | if (extended) { | |
188 | /* PROBE */ | |
189 | sd_event_run(e, (uint64_t) -1); | |
5a920b42 | 190 | assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
60f067b4 JS |
191 | |
192 | /* PROBE */ | |
193 | sd_event_run(e, (uint64_t) -1); | |
5a920b42 | 194 | assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
60f067b4 JS |
195 | |
196 | sd_event_run(e, (uint64_t) -1); | |
197 | assert_se(basic_request_handler_bind == 1); | |
198 | } | |
199 | ||
200 | sd_ipv4ll_stop(ll); | |
201 | assert_se(basic_request_handler_stop == 1); | |
202 | ||
203 | /* Cleanup */ | |
204 | assert_se(sd_ipv4ll_unref(ll) == NULL); | |
205 | safe_close(test_fd[1]); | |
206 | } | |
207 | ||
208 | int main(int argc, char *argv[]) { | |
4c89c718 | 209 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
60f067b4 | 210 | |
6300502b MP |
211 | log_set_max_level(LOG_DEBUG); |
212 | log_parse_environment(); | |
213 | log_open(); | |
214 | ||
60f067b4 JS |
215 | assert_se(sd_event_new(&e) >= 0); |
216 | ||
217 | test_public_api_setters(e); | |
60f067b4 JS |
218 | test_basic_request(e); |
219 | ||
220 | return 0; | |
221 | } |