]>
Commit | Line | Data |
---|---|---|
60f067b4 JS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | /*** | |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright (C) 2014 Axis Communications AB. All rights reserved. | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <stdlib.h> | |
22 | #include <assert.h> | |
23 | #include <errno.h> | |
24 | #include <stdio.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/socket.h> | |
27 | #include <unistd.h> | |
28 | ||
29 | #include "util.h" | |
30 | #include "socket-util.h" | |
e3bff60a | 31 | #include "event-util.h" |
60f067b4 JS |
32 | |
33 | #include "sd-ipv4ll.h" | |
6300502b | 34 | #include "arp-util.h" |
60f067b4 JS |
35 | |
36 | static bool verbose = false; | |
37 | static bool extended = false; | |
38 | static int test_fd[2]; | |
39 | ||
40 | static int basic_request_handler_bind = 0; | |
41 | static int basic_request_handler_stop = 0; | |
d9dfd233 | 42 | static void* basic_request_handler_userdata = (void*)0xCABCAB; |
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) { | |
6300502b | 103 | unsigned seed = 0; |
60f067b4 JS |
104 | sd_ipv4ll *ll; |
105 | struct ether_addr mac_addr = { | |
106 | .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; | |
107 | ||
108 | if (verbose) | |
109 | printf("* %s\n", __FUNCTION__); | |
110 | ||
111 | assert_se(sd_ipv4ll_new(&ll) == 0); | |
112 | assert_se(ll); | |
113 | ||
114 | assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL); | |
115 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0); | |
116 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY); | |
117 | ||
118 | assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL); | |
119 | assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0); | |
120 | ||
6300502b | 121 | assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL); |
60f067b4 JS |
122 | assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0); |
123 | ||
124 | assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL); | |
125 | assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL); | |
126 | assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0); | |
127 | ||
128 | assert_se(sd_ipv4ll_set_index(NULL, -1) == -EINVAL); | |
129 | assert_se(sd_ipv4ll_set_index(ll, -1) == -EINVAL); | |
130 | assert_se(sd_ipv4ll_set_index(ll, -99) == -EINVAL); | |
131 | assert_se(sd_ipv4ll_set_index(ll, 1) == 0); | |
132 | assert_se(sd_ipv4ll_set_index(ll, 99) == 0); | |
133 | ||
134 | assert_se(sd_ipv4ll_ref(ll) == ll); | |
6300502b | 135 | assert_se(sd_ipv4ll_unref(ll) == NULL); |
60f067b4 JS |
136 | |
137 | /* Cleanup */ | |
138 | assert_se(sd_ipv4ll_unref(ll) == NULL); | |
139 | } | |
140 | ||
141 | static void test_basic_request(sd_event *e) { | |
142 | ||
143 | sd_ipv4ll *ll; | |
144 | struct ether_arp arp; | |
145 | struct ether_addr mac_addr = { | |
146 | .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; | |
147 | ||
148 | if (verbose) | |
149 | printf("* %s\n", __FUNCTION__); | |
150 | ||
151 | assert_se(sd_ipv4ll_new(&ll) == 0); | |
152 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
153 | ||
154 | assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0); | |
155 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
156 | ||
157 | assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0); | |
158 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); | |
159 | ||
160 | assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler, | |
d9dfd233 | 161 | basic_request_handler_userdata) == 0); |
60f067b4 JS |
162 | assert_se(sd_ipv4ll_start(ll) == -EINVAL); |
163 | ||
164 | assert_se(sd_ipv4ll_set_index(ll, 1) == 0); | |
165 | assert_se(sd_ipv4ll_start(ll) == 0); | |
166 | ||
167 | sd_event_run(e, (uint64_t) -1); | |
168 | assert_se(sd_ipv4ll_start(ll) == -EBUSY); | |
169 | ||
6300502b MP |
170 | assert_se(sd_ipv4ll_is_running(ll)); |
171 | ||
60f067b4 JS |
172 | /* PROBE */ |
173 | sd_event_run(e, (uint64_t) -1); | |
174 | assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp)); | |
60f067b4 JS |
175 | |
176 | if (extended) { | |
177 | /* PROBE */ | |
178 | sd_event_run(e, (uint64_t) -1); | |
179 | assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp)); | |
60f067b4 JS |
180 | |
181 | /* PROBE */ | |
182 | sd_event_run(e, (uint64_t) -1); | |
183 | assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp)); | |
60f067b4 JS |
184 | |
185 | sd_event_run(e, (uint64_t) -1); | |
186 | assert_se(basic_request_handler_bind == 1); | |
187 | } | |
188 | ||
189 | sd_ipv4ll_stop(ll); | |
190 | assert_se(basic_request_handler_stop == 1); | |
191 | ||
192 | /* Cleanup */ | |
193 | assert_se(sd_ipv4ll_unref(ll) == NULL); | |
194 | safe_close(test_fd[1]); | |
195 | } | |
196 | ||
197 | int main(int argc, char *argv[]) { | |
e3bff60a | 198 | _cleanup_event_unref_ sd_event *e = NULL; |
60f067b4 | 199 | |
6300502b MP |
200 | log_set_max_level(LOG_DEBUG); |
201 | log_parse_environment(); | |
202 | log_open(); | |
203 | ||
60f067b4 JS |
204 | assert_se(sd_event_new(&e) >= 0); |
205 | ||
206 | test_public_api_setters(e); | |
60f067b4 JS |
207 | test_basic_request(e); |
208 | ||
209 | return 0; | |
210 | } |