]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/lldp-port.c
sd-lldp: drop keeping of statistics
[thirdparty/systemd.git] / src / libsystemd-network / lldp-port.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2014 Tom Gundersen
5 Copyright (C) 2014 Susant Sahani
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 "alloc-util.h"
22 #include "async.h"
23 #include "lldp-internal.h"
24 #include "lldp-network.h"
25 #include "lldp-port.h"
26
27 int lldp_port_start(lldp_port *p) {
28 int r;
29
30 assert_return(p, -EINVAL);
31
32 r = lldp_network_bind_raw_socket(p->ifindex);
33 if (r < 0)
34 return r;
35
36 p->rawfd = r;
37
38 r = sd_event_add_io(p->event, &p->lldp_port_rx,
39 p->rawfd, EPOLLIN, lldp_receive_packet, p);
40 if (r < 0) {
41 log_debug_errno(r, "Failed to allocate event source: %m");
42 goto fail;
43 }
44
45 r = sd_event_source_set_priority(p->lldp_port_rx, p->event_priority);
46 if (r < 0) {
47 log_debug_errno(r, "Failed to set event priority: %m");
48 goto fail;
49 }
50
51 r = sd_event_source_set_description(p->lldp_port_rx, "lldp-port-rx");
52 if (r < 0) {
53 log_debug_errno(r, "Failed to set event name: %m");
54 goto fail;
55 }
56
57 return 0;
58
59 fail:
60 lldp_port_stop(p);
61
62 return r;
63 }
64
65 int lldp_port_stop(lldp_port *p) {
66
67 assert_return(p, -EINVAL);
68
69 p->rawfd = asynchronous_close(p->rawfd);
70 p->lldp_port_rx = sd_event_source_unref(p->lldp_port_rx);
71
72 return 0;
73 }
74
75 void lldp_port_free(lldp_port *p) {
76 if (!p)
77 return;
78
79 lldp_port_stop(p);
80
81 free(p->ifname);
82 free(p);
83 }
84
85 int lldp_port_new(int ifindex,
86 const char *ifname,
87 const struct ether_addr *addr,
88 void *userdata,
89 lldp_port **ret) {
90 _cleanup_free_ lldp_port *p = NULL;
91
92 assert_return(ifindex, -EINVAL);
93 assert_return(ifname, -EINVAL);
94 assert_return(addr, -EINVAL);
95
96 p = new0(lldp_port, 1);
97 if (!p)
98 return -ENOMEM;
99
100 p->rawfd = -1;
101 p->ifindex = ifindex;
102
103 p->ifname = strdup(ifname);
104 if (!p->ifname)
105 return -ENOMEM;
106
107 memcpy(&p->mac, addr, ETH_ALEN);
108
109 p->userdata = userdata;
110
111 *ret = p;
112
113 p = NULL;
114
115 return 0;
116 }