]>
Commit | Line | Data |
---|---|---|
ad1ad5c8 SS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright (C) 2014 Tom Gundersen | |
7 | Copyright (C) 2014 Susant Sahani | |
8 | ||
9 | systemd is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU Lesser General Public License as published by | |
11 | the Free Software Foundation; either version 2.1 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | systemd is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | Lesser General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU Lesser General Public License | |
20 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
21 | ***/ | |
22 | ||
b5efdb8a | 23 | #include "alloc-util.h" |
ad1ad5c8 SS |
24 | #include "async.h" |
25 | #include "lldp-port.h" | |
26 | #include "lldp-network.h" | |
0037c2dc | 27 | #include "lldp-internal.h" |
ad1ad5c8 SS |
28 | |
29 | int lldp_port_start(lldp_port *p) { | |
30 | int r; | |
31 | ||
32 | assert_return(p, -EINVAL); | |
33 | ||
34 | r = lldp_network_bind_raw_socket(p->ifindex); | |
35 | if (r < 0) | |
36 | return r; | |
37 | ||
38 | p->rawfd = r; | |
39 | ||
40 | r = sd_event_add_io(p->event, &p->lldp_port_rx, | |
41 | p->rawfd, EPOLLIN, lldp_receive_packet, p); | |
42 | if (r < 0) { | |
e53fc357 LP |
43 | log_debug_errno(r, "Failed to allocate event source: %m"); |
44 | goto fail; | |
ad1ad5c8 SS |
45 | } |
46 | ||
47 | r = sd_event_source_set_priority(p->lldp_port_rx, p->event_priority); | |
48 | if (r < 0) { | |
e53fc357 | 49 | log_debug_errno(r, "Failed to set event priority: %m"); |
ad1ad5c8 SS |
50 | goto fail; |
51 | } | |
52 | ||
53 | r = sd_event_source_set_description(p->lldp_port_rx, "lldp-port-rx"); | |
54 | if (r < 0) { | |
e53fc357 | 55 | log_debug_errno(r, "Failed to set event name: %m"); |
ad1ad5c8 SS |
56 | goto fail; |
57 | } | |
58 | ||
59 | return 0; | |
60 | ||
61 | fail: | |
62 | lldp_port_stop(p); | |
63 | ||
64 | return r; | |
65 | } | |
66 | ||
67 | int lldp_port_stop(lldp_port *p) { | |
68 | ||
69 | assert_return(p, -EINVAL); | |
70 | ||
71 | p->rawfd = asynchronous_close(p->rawfd); | |
72 | p->lldp_port_rx = sd_event_source_unref(p->lldp_port_rx); | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
77 | void lldp_port_free(lldp_port *p) { | |
78 | if (!p) | |
79 | return; | |
80 | ||
81 | lldp_port_stop(p); | |
82 | ||
83 | free(p->ifname); | |
84 | free(p); | |
85 | } | |
86 | ||
87 | int lldp_port_new(int ifindex, | |
7a6f1457 | 88 | const char *ifname, |
ad1ad5c8 SS |
89 | const struct ether_addr *addr, |
90 | void *userdata, | |
91 | lldp_port **ret) { | |
92 | _cleanup_free_ lldp_port *p = NULL; | |
93 | ||
94 | assert_return(ifindex, -EINVAL); | |
95 | assert_return(ifname, -EINVAL); | |
96 | assert_return(addr, -EINVAL); | |
97 | ||
98 | p = new0(lldp_port, 1); | |
99 | if (!p) | |
100 | return -ENOMEM; | |
101 | ||
102 | p->rawfd = -1; | |
103 | p->ifindex = ifindex; | |
104 | ||
105 | p->ifname = strdup(ifname); | |
106 | if (!p->ifname) | |
107 | return -ENOMEM; | |
108 | ||
109 | memcpy(&p->mac, addr, ETH_ALEN); | |
110 | ||
111 | p->userdata = userdata; | |
112 | ||
113 | *ret = p; | |
114 | ||
115 | p = NULL; | |
116 | ||
117 | return 0; | |
118 | } |