1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #ifndef foosdlldprxhfoo
3 #define foosdlldprxhfoo
4 
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 
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <net/ethernet.h>
23 #include <sys/types.h>
24 
25 #include "sd-event.h"
26 #include "sd-lldp.h"
27 
28 #include "_sd-common.h"
29 
30 _SD_BEGIN_DECLARATIONS;
31 
32 typedef struct sd_lldp_rx sd_lldp_rx;
33 typedef struct sd_lldp_neighbor sd_lldp_neighbor;
34 
35 typedef enum sd_lldp_rx_event_t {
36         SD_LLDP_RX_EVENT_ADDED,
37         SD_LLDP_RX_EVENT_REMOVED,
38         SD_LLDP_RX_EVENT_UPDATED,
39         SD_LLDP_RX_EVENT_REFRESHED,
40         _SD_LLDP_RX_EVENT_MAX,
41         _SD_LLDP_RX_EVENT_INVALID = -EINVAL,
42         _SD_ENUM_FORCE_S64(LLDP_RX_EVENT),
43 } sd_lldp_rx_event_t;
44 
45 typedef void (*sd_lldp_rx_callback_t)(sd_lldp_rx *lldp_rx, sd_lldp_rx_event_t event, sd_lldp_neighbor *n, void *userdata);
46 
47 int sd_lldp_rx_new(sd_lldp_rx **ret);
48 sd_lldp_rx *sd_lldp_rx_ref(sd_lldp_rx *lldp_rx);
49 sd_lldp_rx *sd_lldp_rx_unref(sd_lldp_rx *lldp_rx);
50 
51 int sd_lldp_rx_start(sd_lldp_rx *lldp_rx);
52 int sd_lldp_rx_stop(sd_lldp_rx *lldp_rx);
53 int sd_lldp_rx_is_running(sd_lldp_rx *lldp_rx);
54 
55 int sd_lldp_rx_attach_event(sd_lldp_rx *lldp_rx, sd_event *event, int64_t priority);
56 int sd_lldp_rx_detach_event(sd_lldp_rx *lldp_rx);
57 sd_event *sd_lldp_rx_get_event(sd_lldp_rx *lldp_rx);
58 
59 int sd_lldp_rx_set_callback(sd_lldp_rx *lldp_rx, sd_lldp_rx_callback_t cb, void *userdata);
60 int sd_lldp_rx_set_ifindex(sd_lldp_rx *lldp_rx, int ifindex);
61 int sd_lldp_rx_set_ifname(sd_lldp_rx *lldp_rx, const char *ifname);
62 int sd_lldp_rx_get_ifname(sd_lldp_rx *lldp_rx, const char **ret);
63 
64 /* Controls how much and what to store in the neighbors database */
65 int sd_lldp_rx_set_neighbors_max(sd_lldp_rx *lldp_rx, uint64_t n);
66 int sd_lldp_rx_match_capabilities(sd_lldp_rx *lldp_rx, uint16_t mask);
67 int sd_lldp_rx_set_filter_address(sd_lldp_rx *lldp_rx, const struct ether_addr *address);
68 
69 int sd_lldp_rx_get_neighbors(sd_lldp_rx *lldp_rx, sd_lldp_neighbor ***neighbors);
70 
71 int sd_lldp_neighbor_from_raw(sd_lldp_neighbor **ret, const void *raw, size_t raw_size);
72 sd_lldp_neighbor *sd_lldp_neighbor_ref(sd_lldp_neighbor *n);
73 sd_lldp_neighbor *sd_lldp_neighbor_unref(sd_lldp_neighbor *n);
74 
75 /* Access to LLDP frame metadata */
76 int sd_lldp_neighbor_get_source_address(sd_lldp_neighbor *n, struct ether_addr* address);
77 int sd_lldp_neighbor_get_destination_address(sd_lldp_neighbor *n, struct ether_addr* address);
78 int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor *n, clockid_t clock, uint64_t *ret);
79 int sd_lldp_neighbor_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size);
80 
81 /* High-level, direct, parsed out field access. These fields exist at most once, hence may be queried directly. */
82 int sd_lldp_neighbor_get_chassis_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size);
83 int sd_lldp_neighbor_get_chassis_id_as_string(sd_lldp_neighbor *n, const char **ret);
84 int sd_lldp_neighbor_get_port_id(sd_lldp_neighbor *n, uint8_t *type, const void **ret, size_t *size);
85 int sd_lldp_neighbor_get_port_id_as_string(sd_lldp_neighbor *n, const char **ret);
86 int sd_lldp_neighbor_get_ttl(sd_lldp_neighbor *n, uint16_t *ret_sec);
87 int sd_lldp_neighbor_get_system_name(sd_lldp_neighbor *n, const char **ret);
88 int sd_lldp_neighbor_get_system_description(sd_lldp_neighbor *n, const char **ret);
89 int sd_lldp_neighbor_get_port_description(sd_lldp_neighbor *n, const char **ret);
90 int sd_lldp_neighbor_get_mud_url(sd_lldp_neighbor *n, const char **ret);
91 int sd_lldp_neighbor_get_system_capabilities(sd_lldp_neighbor *n, uint16_t *ret);
92 int sd_lldp_neighbor_get_enabled_capabilities(sd_lldp_neighbor *n, uint16_t *ret);
93 
94 /* Low-level, iterative TLV access. This is for everything else, it iteratively goes through all available TLVs
95  * (including the ones covered with the calls above), and allows multiple TLVs for the same fields. */
96 int sd_lldp_neighbor_tlv_rewind(sd_lldp_neighbor *n);
97 int sd_lldp_neighbor_tlv_next(sd_lldp_neighbor *n);
98 int sd_lldp_neighbor_tlv_get_type(sd_lldp_neighbor *n, uint8_t *type);
99 int sd_lldp_neighbor_tlv_is_type(sd_lldp_neighbor *n, uint8_t type);
100 int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t *subtype);
101 int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t subtype);
102 int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size);
103 
104 _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_rx, sd_lldp_rx_unref);
105 _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_neighbor, sd_lldp_neighbor_unref);
106 
107 _SD_END_DECLARATIONS;
108 
109 #endif
110