2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-04-27 21:58:26 +02:00
|
|
|
/***
|
2018-06-12 17:15:23 +02:00
|
|
|
Copyright © 2014 Axis Communications AB. All rights reserved.
|
2014-04-27 21:58:26 +02:00
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2019-07-24 11:22:43 +02:00
|
|
|
#include <netinet/if_ether.h>
|
2014-04-27 21:58:26 +02:00
|
|
|
#include <stdio.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2014-04-27 21:58:26 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "sd-ipv4ll.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-08-18 15:37:43 +02:00
|
|
|
#include "arp-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "socket-util.h"
|
2018-09-13 14:31:13 +02:00
|
|
|
#include "tests.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "util.h"
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
static bool verbose = false;
|
|
|
|
static bool extended = false;
|
|
|
|
static int test_fd[2];
|
|
|
|
|
|
|
|
static int basic_request_handler_bind = 0;
|
|
|
|
static int basic_request_handler_stop = 0;
|
2016-05-22 14:26:36 +02:00
|
|
|
static void* basic_request_handler_userdata = (void*) 0xCABCAB;
|
|
|
|
|
2014-04-27 21:58:26 +02:00
|
|
|
static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
|
2015-08-26 22:47:53 +02:00
|
|
|
assert_se(userdata == basic_request_handler_userdata);
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
switch(event) {
|
2015-09-22 15:08:28 +02:00
|
|
|
case SD_IPV4LL_EVENT_STOP:
|
2014-04-27 21:58:26 +02:00
|
|
|
basic_request_handler_stop = 1;
|
|
|
|
break;
|
2015-09-22 15:08:28 +02:00
|
|
|
case SD_IPV4LL_EVENT_BIND:
|
2014-04-27 21:58:26 +02:00
|
|
|
basic_request_handler_bind = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert_se(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 15:37:43 +02:00
|
|
|
static int arp_network_send_raw_socket(int fd, int ifindex,
|
|
|
|
const struct ether_arp *arp) {
|
2014-04-27 21:58:26 +02:00
|
|
|
assert_se(arp);
|
2015-08-18 15:37:43 +02:00
|
|
|
assert_se(ifindex > 0);
|
2014-04-27 21:58:26 +02:00
|
|
|
assert_se(fd >= 0);
|
|
|
|
|
|
|
|
if (send(fd, arp, sizeof(struct ether_arp), 0) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-18 15:37:43 +02:00
|
|
|
int arp_send_probe(int fd, int ifindex,
|
|
|
|
be32_t pa, const struct ether_addr *ha) {
|
|
|
|
struct ether_arp ea = {};
|
2014-04-27 21:58:26 +02:00
|
|
|
|
2017-05-13 17:47:36 +02:00
|
|
|
assert_se(fd >= 0);
|
|
|
|
assert_se(ifindex > 0);
|
|
|
|
assert_se(pa != 0);
|
|
|
|
assert_se(ha);
|
2014-04-27 21:58:26 +02:00
|
|
|
|
2015-08-18 15:37:43 +02:00
|
|
|
return arp_network_send_raw_socket(fd, ifindex, &ea);
|
2014-04-27 21:58:26 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 15:37:43 +02:00
|
|
|
int arp_send_announcement(int fd, int ifindex,
|
|
|
|
be32_t pa, const struct ether_addr *ha) {
|
|
|
|
struct ether_arp ea = {};
|
2014-04-27 21:58:26 +02:00
|
|
|
|
2017-05-13 17:47:36 +02:00
|
|
|
assert_se(fd >= 0);
|
|
|
|
assert_se(ifindex > 0);
|
|
|
|
assert_se(pa != 0);
|
|
|
|
assert_se(ha);
|
2014-04-27 21:58:26 +02:00
|
|
|
|
2015-08-18 15:37:43 +02:00
|
|
|
return arp_network_send_raw_socket(fd, ifindex, &ea);
|
2014-04-27 21:58:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-02 08:49:19 +02:00
|
|
|
int arp_network_bind_raw_socket(int ifindex, be32_t address, const struct ether_addr *eth_mac) {
|
2018-11-11 18:03:22 +01:00
|
|
|
if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0)
|
2015-08-18 15:37:43 +02:00
|
|
|
return -errno;
|
2014-04-27 21:58:26 +02:00
|
|
|
|
2015-08-18 15:37:43 +02:00
|
|
|
return test_fd[0];
|
2014-04-27 21:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_public_api_setters(sd_event *e) {
|
2015-10-01 21:51:49 +02:00
|
|
|
struct in_addr address = {};
|
2016-05-23 18:33:17 +02:00
|
|
|
uint64_t seed = 0;
|
2014-04-27 21:58:26 +02:00
|
|
|
sd_ipv4ll *ll;
|
|
|
|
struct ether_addr mac_addr = {
|
|
|
|
.ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf("* %s\n", __FUNCTION__);
|
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_new(&ll) == 0);
|
|
|
|
assert_se(ll);
|
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL);
|
|
|
|
assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
|
|
|
|
assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY);
|
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL);
|
|
|
|
assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0);
|
|
|
|
|
2015-10-01 21:51:49 +02:00
|
|
|
assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
|
|
|
|
address.s_addr |= htobe32(169U << 24 | 254U << 16);
|
|
|
|
assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
|
|
|
|
address.s_addr |= htobe32(0x00FF);
|
|
|
|
assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
|
|
|
|
address.s_addr |= htobe32(0xF000);
|
|
|
|
assert_se(sd_ipv4ll_set_address(ll, &address) == 0);
|
|
|
|
address.s_addr |= htobe32(0x0F00);
|
|
|
|
assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
|
|
|
|
|
2015-09-21 16:49:59 +02:00
|
|
|
assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL);
|
|
|
|
assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL);
|
|
|
|
assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL);
|
|
|
|
assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
|
|
|
|
|
2016-05-23 16:13:18 +02:00
|
|
|
assert_se(sd_ipv4ll_set_ifindex(NULL, -1) == -EINVAL);
|
|
|
|
assert_se(sd_ipv4ll_set_ifindex(ll, -1) == -EINVAL);
|
|
|
|
assert_se(sd_ipv4ll_set_ifindex(ll, -99) == -EINVAL);
|
|
|
|
assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
|
|
|
|
assert_se(sd_ipv4ll_set_ifindex(ll, 99) == 0);
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_ref(ll) == ll);
|
2015-08-20 11:40:10 +02:00
|
|
|
assert_se(sd_ipv4ll_unref(ll) == NULL);
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
assert_se(sd_ipv4ll_unref(ll) == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_basic_request(sd_event *e) {
|
|
|
|
|
|
|
|
sd_ipv4ll *ll;
|
|
|
|
struct ether_arp arp;
|
|
|
|
struct ether_addr mac_addr = {
|
|
|
|
.ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf("* %s\n", __FUNCTION__);
|
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_new(&ll) == 0);
|
|
|
|
assert_se(sd_ipv4ll_start(ll) == -EINVAL);
|
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
|
|
|
|
assert_se(sd_ipv4ll_start(ll) == -EINVAL);
|
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
|
|
|
|
assert_se(sd_ipv4ll_start(ll) == -EINVAL);
|
|
|
|
|
|
|
|
assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler,
|
2015-08-26 22:47:53 +02:00
|
|
|
basic_request_handler_userdata) == 0);
|
2014-04-27 21:58:26 +02:00
|
|
|
assert_se(sd_ipv4ll_start(ll) == -EINVAL);
|
|
|
|
|
2016-05-23 16:13:18 +02:00
|
|
|
assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
|
2020-05-02 15:56:33 +02:00
|
|
|
assert_se(sd_ipv4ll_start(ll) == 1);
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
sd_event_run(e, (uint64_t) -1);
|
2020-05-02 15:56:33 +02:00
|
|
|
assert_se(sd_ipv4ll_start(ll) == 0);
|
2014-04-27 21:58:26 +02:00
|
|
|
|
2015-08-21 12:50:31 +02:00
|
|
|
assert_se(sd_ipv4ll_is_running(ll));
|
|
|
|
|
2014-04-27 21:58:26 +02:00
|
|
|
/* PROBE */
|
|
|
|
sd_event_run(e, (uint64_t) -1);
|
2016-05-22 14:26:36 +02:00
|
|
|
assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
if (extended) {
|
|
|
|
/* PROBE */
|
|
|
|
sd_event_run(e, (uint64_t) -1);
|
2016-05-22 14:26:36 +02:00
|
|
|
assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
/* PROBE */
|
|
|
|
sd_event_run(e, (uint64_t) -1);
|
2016-05-22 14:26:36 +02:00
|
|
|
assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
|
2014-04-27 21:58:26 +02:00
|
|
|
|
|
|
|
sd_event_run(e, (uint64_t) -1);
|
|
|
|
assert_se(basic_request_handler_bind == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sd_ipv4ll_stop(ll);
|
|
|
|
assert_se(basic_request_handler_stop == 1);
|
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
assert_se(sd_ipv4ll_unref(ll) == NULL);
|
|
|
|
safe_close(test_fd[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
|
2014-04-27 21:58:26 +02:00
|
|
|
|
2018-09-13 14:31:13 +02:00
|
|
|
test_setup_logging(LOG_DEBUG);
|
2015-08-21 12:50:31 +02:00
|
|
|
|
2014-04-27 21:58:26 +02:00
|
|
|
assert_se(sd_event_new(&e) >= 0);
|
|
|
|
|
|
|
|
test_public_api_setters(e);
|
|
|
|
test_basic_request(e);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|