]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/lib/connection.c
591d9e9460c5db6cd070e4f5781b68edb8691083
[thirdparty/lldpd.git] / src / lib / connection.c
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23
24 #include "lldpctl.h"
25 #include "atom.h"
26 #include "../compat/compat.h"
27 #include "../ctl.h"
28 #include "../log.h"
29
30 const char*
31 lldpctl_get_default_transport(void)
32 {
33 return LLDPD_CTL_SOCKET;
34 }
35
36 /* Connect to the remote end */
37 static int
38 sync_connect(lldpctl_conn_t *lldpctl)
39 {
40 return ctl_connect(lldpctl->ctlname);
41 }
42
43 /* Synchronously send data to remote end. */
44 static ssize_t
45 sync_send(lldpctl_conn_t *lldpctl,
46 const uint8_t *data, size_t length, void *user_data)
47 {
48 struct lldpctl_conn_sync_t *conn = user_data;
49 ssize_t nb;
50
51 if (conn->fd == -1 &&
52 ((conn->fd = sync_connect(lldpctl)) == -1)) {
53 return LLDPCTL_ERR_CANNOT_CONNECT;
54 }
55
56 while ((nb = write(conn->fd, data, length)) == -1) {
57 if (errno == EAGAIN || errno == EINTR) continue;
58 return LLDPCTL_ERR_CALLBACK_FAILURE;
59 }
60 return nb;
61 }
62
63 /* Statically receive data from remote end. */
64 static ssize_t
65 sync_recv(lldpctl_conn_t *lldpctl,
66 const uint8_t *data, size_t length, void *user_data)
67 {
68 struct lldpctl_conn_sync_t *conn = user_data;
69 ssize_t nb;
70 size_t remain, offset = 0;
71
72 if (conn->fd == -1 &&
73 ((conn->fd = sync_connect(lldpctl)) == -1)) {
74 lldpctl->error = LLDPCTL_ERR_CANNOT_CONNECT;
75 return LLDPCTL_ERR_CANNOT_CONNECT;
76 }
77
78 remain = length;
79 do {
80 if ((nb = read(conn->fd, (unsigned char*)data + offset, remain)) == -1) {
81 if (errno == EAGAIN || errno == EINTR)
82 continue;
83 return LLDPCTL_ERR_CALLBACK_FAILURE;
84 }
85 remain -= nb;
86 offset += nb;
87 } while (remain > 0 && nb != 0);
88 return offset;
89 }
90
91 lldpctl_conn_t*
92 lldpctl_new(lldpctl_send_callback send, lldpctl_recv_callback recv, void *user_data)
93 {
94 return lldpctl_new_name(lldpctl_get_default_transport(), send, recv, user_data);
95 }
96
97 lldpctl_conn_t*
98 lldpctl_new_name(const char *ctlname, lldpctl_send_callback send, lldpctl_recv_callback recv, void *user_data)
99 {
100 lldpctl_conn_t *conn = NULL;
101 struct lldpctl_conn_sync_t *data = NULL;
102
103 /* Both callbacks are mandatory or should be NULL. */
104 if (send && !recv) return NULL;
105 if (recv && !send) return NULL;
106
107 if ((conn = calloc(1, sizeof(lldpctl_conn_t))) == NULL)
108 return NULL;
109
110 conn->ctlname = strdup(ctlname);
111 if (conn->ctlname == NULL) {
112 free(conn);
113 return NULL;
114 }
115 if (!send && !recv) {
116 if ((data = malloc(sizeof(struct lldpctl_conn_sync_t))) == NULL) {
117 free(conn);
118 return NULL;
119 }
120 data->fd = -1;
121 conn->send = sync_send;
122 conn->recv = sync_recv;
123 conn->user_data = data;
124 } else {
125 conn->send = send;
126 conn->recv = recv;
127 conn->user_data = user_data;
128 }
129
130 return conn;
131 }
132
133 int
134 lldpctl_release(lldpctl_conn_t *conn)
135 {
136 if (conn == NULL) return 0;
137 free(conn->ctlname);
138 if (conn->send == sync_send) {
139 struct lldpctl_conn_sync_t *data = conn->user_data;
140 if (data->fd != -1) close(data->fd);
141 free(conn->user_data);
142 }
143 free(conn->input_buffer);
144 free(conn->output_buffer);
145 free(conn);
146 return 0;
147 }
148
149 /**
150 * Request some bytes if they are not already here.
151 *
152 * @param conn The connection to lldpd.
153 * @param length The number of requested bytes.
154 * @return A negative integer if we can't have the bytes or the number of bytes we got.
155 */
156 ssize_t
157 _lldpctl_needs(lldpctl_conn_t *conn, size_t length)
158 {
159 uint8_t *buffer = NULL;
160 ssize_t rc;
161
162 if ((buffer = malloc(length)) == NULL)
163 return SET_ERROR(conn, LLDPCTL_ERR_NOMEM);
164 rc = conn->recv(conn, buffer, length, conn->user_data);
165 if (rc < 0) {
166 free(buffer);
167 return SET_ERROR(conn, rc);
168 }
169 if (rc == 0) {
170 free(buffer);
171 return SET_ERROR(conn, LLDPCTL_ERR_EOF);
172 }
173 rc = lldpctl_recv(conn, buffer, rc);
174 free(buffer);
175 if (rc < 0)
176 return SET_ERROR(conn, rc);
177 RESET_ERROR(conn);
178 return rc;
179 }
180
181 static int
182 check_for_notification(lldpctl_conn_t *conn)
183 {
184 struct lldpd_neighbor_change *change;
185 void *p;
186 int rc;
187 lldpctl_change_t type;
188 lldpctl_atom_t *interface = NULL, *neighbor = NULL;
189 rc = ctl_msg_recv_unserialized(&conn->input_buffer,
190 &conn->input_buffer_len,
191 NOTIFICATION,
192 &p,
193 &MARSHAL_INFO(lldpd_neighbor_change));
194 if (rc != 0) return rc;
195 change = p;
196
197 /* We have a notification, call the callback */
198 if (conn->watch_cb) {
199 switch (change->state) {
200 case NEIGHBOR_CHANGE_DELETED: type = lldpctl_c_deleted; break;
201 case NEIGHBOR_CHANGE_ADDED: type = lldpctl_c_added; break;
202 case NEIGHBOR_CHANGE_UPDATED: type = lldpctl_c_updated; break;
203 default:
204 log_warnx("control", "unknown notification type (%d)",
205 change->state);
206 goto end;
207 }
208 interface = _lldpctl_new_atom(conn, atom_interface,
209 change->ifname);
210 if (interface == NULL) goto end;
211 neighbor = _lldpctl_new_atom(conn, atom_port, 0,
212 NULL, change->neighbor, NULL);
213 if (neighbor == NULL) goto end;
214 conn->watch_cb(conn, type, interface, neighbor, conn->watch_data);
215 conn->watch_triggered = 1;
216 goto end;
217 }
218
219 end:
220 if (interface) lldpctl_atom_dec_ref(interface);
221 if (neighbor) lldpctl_atom_dec_ref(neighbor);
222 else {
223 lldpd_chassis_cleanup(change->neighbor->p_chassis, 1);
224 lldpd_port_cleanup(change->neighbor, 1);
225 free(change->neighbor);
226 }
227 free(change->ifname);
228 free(change);
229
230 /* Indicate if more data remains in the buffer for processing */
231 return (rc);
232 }
233
234 ssize_t
235 lldpctl_recv(lldpctl_conn_t *conn, const uint8_t *data, size_t length)
236 {
237
238 RESET_ERROR(conn);
239
240 if (length == 0) return 0;
241
242 /* Received data should be appended to the input buffer. */
243 if (conn->input_buffer == NULL) {
244 conn->input_buffer_len = 0;
245 if ((conn->input_buffer = malloc(length)) == NULL)
246 return SET_ERROR(conn, LLDPCTL_ERR_NOMEM);
247 } else {
248 uint8_t *new = realloc(conn->input_buffer, conn->input_buffer_len + length);
249 if (new == NULL)
250 return SET_ERROR(conn, LLDPCTL_ERR_NOMEM);
251 conn->input_buffer = new;
252 }
253 memcpy(conn->input_buffer + conn->input_buffer_len, data, length);
254 conn->input_buffer_len += length;
255
256 /* Is it a notification? */
257 check_for_notification(conn);
258
259 RESET_ERROR(conn);
260
261 return conn->input_buffer_len;
262 }
263
264 int lldpctl_process_conn_buffer(lldpctl_conn_t *conn)
265 {
266 int rc;
267
268 rc = check_for_notification(conn);
269
270 RESET_ERROR(conn);
271
272 return rc;
273 }
274
275 ssize_t
276 lldpctl_send(lldpctl_conn_t *conn)
277 {
278 /* Send waiting data. */
279 ssize_t rc;
280
281 RESET_ERROR(conn);
282
283 if (!conn->output_buffer) return 0;
284 rc = conn->send(conn,
285 conn->output_buffer, conn->output_buffer_len,
286 conn->user_data);
287 if (rc < 0) return SET_ERROR(conn, rc);
288
289 /* "Shrink" the output buffer. */
290 if (rc == conn->output_buffer_len) {
291 free(conn->output_buffer);
292 conn->output_buffer = NULL;
293 conn->output_buffer_len = 0;
294 RESET_ERROR(conn);
295 return rc;
296 }
297 conn->output_buffer_len -= rc;
298 memmove(conn->output_buffer, conn->output_buffer + rc, conn->output_buffer_len);
299 /* We don't shrink the buffer. It will be either freed or shrinked later */
300 RESET_ERROR(conn);
301 return rc;
302 }