]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/lib/lldpctl.h
lib: keep space for new values in `lldpctl_key_t`
[thirdparty/lldpd.git] / src / lib / lldpctl.h
CommitLineData
4b292b55
VB
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#ifndef LLDPCTL_H
19#define LLDPCTL_H
20
21/**
b1eceab6 22 * @defgroup liblldpctl liblldpctl: library to interface with lldpd
4b292b55 23 *
b1eceab6
VB
24 * `liblldpctl` allows any program to convenienty query and modify the behaviour
25 * of a running lldpd daemon.
26 *
27 * To use this library, use `pkg-config` to get the appropriate options:
28 * * `pkg-config --libs lldpctl` for `LIBS` or `LDFLAGS`
29 * * `pkg-config --cflags lldpctl` for `CFLAGS`
30 *
31 * @warning This library is tightly coupled with lldpd. The library to use
32 * should be the one shipped with lldpd. Clients of the library are then tied
33 * by the classic API/ABI rules and may be compiled separatly.
34 *
35 * There are two important structures in this library: @c lldpctl_conn_t which
36 * represents a connection and @c lldpctl_atom_t which represents a piece of
37 * information. Those types are opaque. No direct access to them should be done.
38 *
39 * The library is expected to be reentrant and therefore thread-safe. It is
40 * however not expected that a connection to be used in several thread
41 * simultaneously. This also applies to the different pieces of information
42 * gathered through this connection. Several connection to lldpd can be used
43 * simultaneously.
44 *
45 * The first step is to establish a connection. See @ref lldpctl_connection for
46 * more information about this. The next step is to query the lldpd daemon. See
47 * @ref lldpctl_atoms on how to do this.
48 *
49 * `liblldpctl` tries to handle errors in a coherent way. Any function returning
50 * a pointer will return @c NULL on error and the last error can be retrieved
51 * through @ref lldpctl_last_error() function. Most functions returning integers
52 * will return a negative integer representing the error if something goes
53 * wrong. The use of @ref lldpctl_last_error() allows one to check if this is a
54 * real error if there is a doubt. See @ref lldpctl_errors_logs for more about
55 * this.
56 *
57 * @{
4b292b55
VB
58 */
59
b1eceab6 60
4b292b55
VB
61#ifdef __cplusplus
62extern "C" {
63#endif
64
65#include <stdlib.h>
66#include <stdint.h>
67#include <sys/types.h>
68
69/**
b1eceab6 70 * @defgroup lldpctl_connection Managing connection to lldpd
4b292b55 71 *
94d2efe5
VB
72 * Connection with lldpd.
73 *
b1eceab6
VB
74 * This library does not handle IO. They are delegated to a set of functions to
75 * allow a user to specify exactly how IO should be done. A user is expected to
76 * provide two functions: the first one is called when the library requests
77 * incoming data, the other one when it requests outgoing data. Moreover, the
78 * user is also expected to call the appropriate functions when data comes back
79 * (@ref lldpctl_recv()) or needs to be sent (@ref lldpctl_send()).
80 *
81 * Because the most common case is synchronous IO, `liblldpctl` will use classic
82 * synchronous IO with the Unix socket if no IO functions are provided by the
83 * user. For all other cases, the user must provide the appropriate functions.
84 *
85 * A connection should be allocated by using @ref lldpctl_new(). It needs to be
86 * released with @ref lldpctl_release().
87 *
88 * @{
4b292b55 89 */
4b292b55
VB
90
91/**
b1eceab6 92 * Get default transport name.
4b292b55 93 *
b1eceab6 94 * Currently, this is the default location of the Unix socket.
4b292b55 95 */
b1eceab6 96const char* lldpctl_get_default_transport(void);
4b292b55 97
4b292b55
VB
98/**
99 * Structure referencing a connection with lldpd.
100 *
101 * This structure should be handled as opaque. It can be allocated
102 * with @c lldpctl_new() and the associated resources will be freed
103 * with @c lldpctl_release().
104 */
105typedef struct lldpctl_conn_t lldpctl_conn_t;
106
107/**
108 * Callback function invoked to send data to lldpd.
109 *
110 * @param lldpctl Handle to the connection to lldpd.
111 * @param data Bytes to be sent.
112 * @param length Length of provided data.
113 * @param user_data Provided user data.
114 * @return The number of bytes really sent or either @c LLDPCTL_ERR_WOULDBLOCK
115 * if no bytes can be sent without blocking or @c
116 * LLDPCTL_ERR_CALLBACK_FAILURE for other errors.
117 */
b1eceab6 118typedef ssize_t (*lldpctl_send_callback)(lldpctl_conn_t *conn,
4b292b55
VB
119 const uint8_t *data, size_t length, void *user_data);
120
121/**
122 * Callback function invoked to receive data from lldpd.
123 *
124 * @param lldpctl Handle to the connection to lldpd.
125 * @param data Buffer for receiving data
126 * @param length Maximum bytes we can receive
127 * @param user_data Provided user data.
128 * @return The number of bytes really received or either @c
129 * LLDPCTL_ERR_WOULDBLOCK if no bytes can be received without blocking,
130 * @c LLDPCTL_ERR_CALLBACK_FAILURE for other errors or @c
131 * LLDPCTL_ERR_EOF if end of file was reached.
132 */
b1eceab6 133typedef ssize_t (*lldpctl_recv_callback)(lldpctl_conn_t *conn,
4b292b55
VB
134 const uint8_t *data, size_t length, void *user_data);
135
136/**
137 * Function invoked when additional data is available from lldpd.
138 *
139 * This function should be invoked in case of asynchronous IO when new data is
140 * available from lldpd (expected or unexpected).
141 *
b1eceab6 142 * @param conn Handle to the connection to lldpd.
4b292b55
VB
143 * @param data Data received from lldpd.
144 * @param length Length of data received.
4e90a9e0
VB
145 * @return The number of bytes available or a negative integer if an error has
146 * occurred. 0 is not an error. It usually means that a notification has
147 * been processed.
4b292b55 148 */
b1eceab6 149ssize_t lldpctl_recv(lldpctl_conn_t *conn, const uint8_t *data, size_t length);
4b292b55
VB
150
151/**
152 * Function invoked when there is an opportunity to send data to lldpd.
153 *
154 * This function should be invoked in case of asynchronous IO when new data can
155 * be written to lldpd.
156 *
b1eceab6 157 * @param conn Handle to the connection to lldpd.
4b292b55
VB
158 * @return The number of bytes processed or a negative integer if an error has
159 * occured.
160 */
b1eceab6 161ssize_t lldpctl_send(lldpctl_conn_t *conn);
4b292b55
VB
162
163/**
164 * Allocate a new handler for connecting to lldpd.
165 *
4b292b55
VB
166 * @param send Callback to be used when sending new data is requested.
167 * @param recv Callback to be used when receiving new data is requested.
168 * @param user_data Data to pass to callbacks.
169 * @return An handler to be used to connect to lldpd or @c NULL in
170 * case of error. In the later case, the error is probable an
171 * out of memory condition.
172 *
173 * The allocated handler can be released with @c lldpctl_release(). If the
174 * provided parameters are both @c NULL, default synchronous callbacks will be
175 * used.
176 */
177lldpctl_conn_t *lldpctl_new(lldpctl_send_callback send,
178 lldpctl_recv_callback recv, void *user_data);
179
0262adbb
ZM
180/**
181 * Allocate a new handler for connecting to lldpd.
182 *
183 * @param ctlname the Unix-domain socket to connect to lldpd.
184 * @param send Callback to be used when sending new data is requested.
185 * @param recv Callback to be used when receiving new data is requested.
186 * @param user_data Data to pass to callbacks.
187 * @return An handler to be used to connect to lldpd or @c NULL in
188 * case of error. In the later case, the error is probable an
189 * out of memory condition.
190 *
191 * The allocated handler can be released with @c lldpctl_release(). If the
192 * provided parameters are both @c NULL, default synchronous callbacks will be
193 * used.
194 */
195lldpctl_conn_t *lldpctl_new_name(const char *ctlname, lldpctl_send_callback send,
196 lldpctl_recv_callback recv, void *user_data);
197
4b292b55
VB
198/**
199 * Release resources associated with a connection to lldpd.
200 *
b1eceab6 201 * @param conn Previously allocated handler to a connection to lldpd.
4b292b55
VB
202 * @return 0 on success or a negative integer
203 *
204 * @see lldpctl_new()
205 */
b1eceab6
VB
206int lldpctl_release(lldpctl_conn_t *conn);
207/**@}*/
208
209/**
210 * @defgroup lldpctl_errors_logs Errors and logs handling
211 *
94d2efe5
VB
212 * Error codes and logs handling.
213 *
b1eceab6
VB
214 * When a function returns a pointer, it may return @c NULL to indicate an error
215 * condition. In this case, it is possible to use @ref lldpctl_last_error() to
216 * get the related error code which is one of the values in @ref lldpctl_error_t
217 * enumeration. For display purpose @ref lldpctl_strerror() may be used to
218 * translate this error code.
219 *
220 * When a function returns an integer, it may return a negative value. It
221 * usually means this is an error but some functions may return a legetimate
222 * negative value (for example @ref lldpctl_atom_get_int()). When there is a
223 * doubt, @ref lldpctl_last_error() should be checked.
224 *
225 * An error is attached to a connection. If there is no connection, no error
226 * handling is available. Most functions use a connection or an atom as first
227 * argument and therefore are attached to a connection. To get the connection
228 * related to an atom, use @ref lldpctl_atom_get_connection().
229 *
230 * Also have a look at @ref lldpctl_log_callback() function if you want a custom
231 * log handling.
232 *
233 * @{
234 */
235
236/**
237 * Setup log handlers.
238 *
239 * By default, liblldpctl will log to stderr. The following function will
240 * register another callback for this purpose. Messages logged through this
241 * callback may be cryptic. They are targeted for the developer. Message for end
242 * users should rely on return codes.
243 */
244void lldpctl_log_callback(void (*cb)(int severity, const char *msg));
4b292b55 245
4b292b55
VB
246/**
247 * Possible error codes for functions that return negative integers on
248 * this purpose or for @c lldpctl_last_error().
249 */
250typedef enum {
251 /**
252 * No error has happened (yet).
253 */
254 LLDPCTL_NO_ERROR = 0,
255 /**
256 * A IO related operation would block if performed.
257 */
258 LLDPCTL_ERR_WOULDBLOCK = -501,
259 /**
260 * A IO related operation has reached a end of file condition.
261 */
262 LLDPCTL_ERR_EOF = -502,
263 /**
264 * The requested information does not exist. For example, when
265 * requesting an inexistant information from an atom.
266 */
267 LLDPCTL_ERR_NOT_EXIST = -503,
268 /**
269 * Cannot connect to the lldpd daemon. This error only happens with
270 * default synchronous handlers.
271 */
272 LLDPCTL_ERR_CANNOT_CONNECT = -504,
273 /**
274 * Atom is of incorrect type for the requested operation.
275 */
276 LLDPCTL_ERR_INCORRECT_ATOM_TYPE = -505,
277 /**
278 * An error occurred during serialization of message.
279 */
280 LLDPCTL_ERR_SERIALIZATION = -506,
281 /**
282 * The requested operation cannot be performed because we have another
283 * operation already running.
284 */
285 LLDPCTL_ERR_INVALID_STATE = -507,
286 /**
287 * The provided atom cannot be iterated.
288 */
289 LLDPCTL_ERR_CANNOT_ITERATE = -508,
290 /**
291 * The provided value is invalid.
292 */
293 LLDPCTL_ERR_BAD_VALUE = -509,
294 /**
295 * No new element can be created for this element.
296 */
297 LLDPCTL_ERR_CANNOT_CREATE = -510,
298 /**
299 * The library is under unexpected conditions and cannot process
300 * any further data reliably.
301 */
302 LLDPCTL_ERR_FATAL = -900,
303 /**
304 * Out of memory condition. Things may get havoc here but we
305 * should be able to recover.
306 */
307 LLDPCTL_ERR_NOMEM = -901,
308 /**
309 * An error occurred in a user provided callback.
310 */
311 LLDPCTL_ERR_CALLBACK_FAILURE = -902
312} lldpctl_error_t;
313
314/**
315 * Describe a provided error code.
316 *
317 * @param error Error code to be described.
318 * @return Statically allocated string describing the error.
319 */
320const char *lldpctl_strerror(lldpctl_error_t error);
321
322/**
323 * Get the last error associated to a connection to lldpd.
324 *
b1eceab6 325 * @param conn Previously allocated handler to a connection to lldpd.
4b292b55
VB
326 * @return 0 if no error is currently registered. A negative integer
327 * otherwise.
328 *
329 * For functions returning int, this function will return the same
330 * error number. For functions returning something else, you can use
331 * this function to get the appropriate error number.
332 */
b1eceab6 333lldpctl_error_t lldpctl_last_error(lldpctl_conn_t *conn);
4b292b55
VB
334
335/**
336 * Describe the last error associate to a connection.
337 *
338 * @param conn Previously allocated handler to a connection to lldpd.
339 * @return Statically allocated string describing the error
340 */
341#define lldpctl_last_strerror(conn) lldpctl_strerror(lldpctl_last_error(conn))
b1eceab6 342/**@}*/
4b292b55 343
4b292b55 344/**
b1eceab6
VB
345 * @defgroup lldpctl_atoms Extracting information: atoms
346 *
94d2efe5 347 * Information retrieved from lldpd is represented as an atom.
4b292b55
VB
348 *
349 * This is an opaque structure that can be passed along some functions to
350 * transmit chassis, ports, VLAN and other information related to LLDP. Most
351 * information are extracted using @c lldpctl_atom_get(), @c
352 * lldpctl_atom_get_str(), @c lldpctl_atom_get_buffer() or @c
353 * lldpctl_atom_get_int(), unless some IO with lldpd is needed to retrieve the
354 * requested information. In this case, there exists an appropriate function to
355 * convert the "deferred" atom into a normal one (like @c lldpctl_get_port()).
356 *
b1eceab6
VB
357 * For some information, setters are also available: @c lldpctl_atom_set(), @c
358 * lldpctl_atom_set_str(), @c lldpctl_atom_set_buffer() or @c
359 * lldpctl_atom_set_int(). Unlike getters, some of those may require IO to
360 * achieve their goal.
361 *
4b292b55
VB
362 * An atom is reference counted. Unless documented otherwise, a function
363 * returning an atom will return a new reference that should be decremented if
b1eceab6
VB
364 * not used anymore. It is quite important to use the reference counting
365 * functions correctly. Segfaults or memory leaks may occur otherwise.
366 *
367 * @{
368 */
369
370/**
371 * Structure representing an element (chassis, port, VLAN, ...)
4b292b55
VB
372 *
373 * @see lldpctl_atom_inc_ref(), lldpctl_atom_dec_ref().
374 */
375typedef struct lldpctl_atom_t lldpctl_atom_t;
376
6fcf11ca
VB
377/**
378 * Structure representing a map from an integer to a character string.
379 *
380 * @see lldpctl_key_get_map().
381 */
382typedef const struct {
383 int value;
384 char *string;
385} lldpctl_map_t;
386
4b292b55
VB
387/**
388 * Return the reference to connection with lldpd.
389 *
390 * @param atom The atom we want reference from.
391 * @return The reference to the connection to lldpd.
392 *
393 * Each atom contains an internal reference to the corresponding connection to
394 * lldpd. Use this function to get it.
395 */
396lldpctl_conn_t *lldpctl_atom_get_connection(lldpctl_atom_t *atom);
397
398/**
399 * Increment reference count for an atom.
400 *
401 * @param atom Atom we which to increase reference count.
402 */
403void lldpctl_atom_inc_ref(lldpctl_atom_t *atom);
404
405/**
406 * Decrement reference count for an atom.
407 *
408 * @param atom Atom we want to decrease reference count. Can be @c NULL. In this
409 * case, nothing happens.
410 *
4e90a9e0 411 * When the reference count becomes 0, the atom is freed.
4b292b55
VB
412 */
413void lldpctl_atom_dec_ref(lldpctl_atom_t *atom);
414
4e90a9e0 415/**
b1eceab6
VB
416 * Possible events for a change (notification).
417 *
418 * @see lldpctl_watch_callback
4e90a9e0
VB
419 */
420typedef enum {
421 lldpctl_c_deleted, /**< The neighbor has been deleted */
422 lldpctl_c_updated, /**< The neighbor has been updated */
423 lldpctl_c_added, /**< This is a new neighbor */
424} lldpctl_change_t;
425
426/**
427 * Callback function invoked when a change is detected.
428 *
429 * @param conn Connection with lldpd.
430 * @param type Type of change detected.
431 * @param interface Physical interface on which the change has happened.
432 * @param neighbor Changed neighbor.
433 * @param data Data provided when registering the callback.
434 *
435 * The provided interface and neighbor atoms will have their reference count
436 * decremented when the callback ends. If you want to keep a reference to it, be
437 * sure to increment the reference count in the callback.
b1eceab6
VB
438 *
439 * @see lldpctl_watch_callback
4e90a9e0
VB
440 */
441typedef void (*lldpctl_change_callback)(lldpctl_conn_t *conn,
442 lldpctl_change_t type,
443 lldpctl_atom_t *interface,
444 lldpctl_atom_t *neighbor,
445 void *data);
446
447/**
448 * Register a callback to be called on changes.
449 *
450 * @param conn Connection with lldpd.
451 * @param cb Replace the current callback with the provided one.
452 * @param data Data that will be passed to the callback.
453 * @return 0 in case of success or -1 in case of errors.
454 *
455 * This function will register the necessity to push neighbor changes to lldpd
456 * and therefore will issue IO operations. The error code could then be @c
457 * LLDPCTL_ERR_WOULDBLOCK.
458 */
459int lldpctl_watch_callback(lldpctl_conn_t *conn,
460 lldpctl_change_callback cb,
461 void *data);
462
463/**
464 * Wait for the next change.
465 *
466 * @param conn Connection with lldpd.
467 * @return 0 on success or a negative integer in case of error.
468 *
469 * This function will return once a change has been detected. It is only useful
470 * as a main loop when using the builtin blocking IO mechanism.
471 */
472int lldpctl_watch(lldpctl_conn_t *conn);
473
b1eceab6
VB
474/**
475 * @defgroup liblldpctl_atom_get_special Retrieving atoms from lldpd
476 *
477 * Special access functions.
478 *
479 * Most information can be retrieved through @ref lldpctl_atom_get(), @ref
480 * lldpctl_atom_get_int(), @ref lldpctl_atom_get_str() or @ref
481 * lldpctl_atom_get_buffer() but some information can only be retrieved through
482 * special functions because IO operation is needed (and also, for some of them,
483 * because we don't have an atom yet).
484 *
485 * @{
486 */
487
8729d69f
VB
488/**
489 * Retrieve global configuration of lldpd daemon.
490 *
491 * @param conn Connection with lldpd.
492 * @return The global configuration or @c NULL if an error happened.
493 *
494 * This function will make IO with the daemon to get the
495 * configuration. Depending on the IO model, information may not be available
496 * right now and the function should be called again later. If @c NULL is
497 * returned, check the last error. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again
498 * later.
499 */
500lldpctl_atom_t *lldpctl_get_configuration(lldpctl_conn_t *conn);
501
4b292b55
VB
502/**
503 * Retrieve the list of available interfaces.
504 *
b1eceab6 505 * @param conn Previously allocated handler to a connection to lldpd.
4b292b55
VB
506 * @return The list of available ports or @c NULL if an error happened.
507 *
508 * This function will make IO with the daemon to get the list of
509 * ports. Depending on the IO model, information may not be available right now
510 * and the function should be called again later. If @c NULL is returned, check
511 * what the last error is. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again later
512 * (when more data is available).
b1eceab6
VB
513 *
514 * The list of available ports can be iterated with @ref lldpctl_atom_foreach().
4b292b55 515 */
b1eceab6 516lldpctl_atom_t *lldpctl_get_interfaces(lldpctl_conn_t *conn);
4b292b55
VB
517
518/**
519 * Retrieve the information related to a given interface.
520 *
521 * @param port The port we want to retrieve information from. This port is an
b1eceab6 522 * atom retrieved from an interation on @c lldpctl_get_interfaces().
4b292b55
VB
523 * @return Atom related to this port which may be used in subsequent functions.
524 *
525 * This functions may have to do IO to get the information related to the given
526 * port. Depending on the IO mode, information may not be available tight now
527 * and the function should be called again later. If @c NULL is returned, check
528 * what the last error is. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again later
529 * (when more data is available).
530 */
531lldpctl_atom_t *lldpctl_get_port(lldpctl_atom_t *port);
532
b1eceab6
VB
533/**@}*/
534
4b292b55
VB
535/**
536 * Piece of information that can be retrieved from/written to an atom.
537 *
538 * Each piece of information can potentially be retrieved as an atom (A), a
539 * string (S), a buffer (B) or an integer (I). Additionaly, when an information
540 * can be retrieved as an atom, it is usually iterable (L). When an atom can be
541 * retrieved as a string and as an additional type, the string is expected to be
542 * formatted. For example, the MAC address of a local port can be retrieved as a
543 * buffer and a string. As a string, you'll get something like
544 * "00:11:22:33:44:55". Also, all values that can be get as an integer or a
545 * buffer can be get as a string too. There is no special formatting in this
546 * case. "(BS)" means that the string get a special appropriate format.
547 *
548 * The name of a key is an indication on the type of atom that information can
549 * be extracted from. For example, @c lldpctl_k_med_policy_type can be extracted
550 * from an atom you got by iterating on @c lldpctl_k_port_med_policies. On the
551 * other hand, @c lldpctl_k_port_descr and @c lldpctl_k_chassis can be retrieved
552 * from an atom retrieved either by iterating @c lldpctl_k_port_neighbors or
553 * with @c lldpctl_get_port().
554 *
555 * Some values may be written. They are marked with (W). Such a change may or
556 * may not be transmitted immediatly. If they are not transmitted immediatly,
557 * this means that the resulting atom should be written to another atom. For
558 * example, when writting @c lldpctl_k_med_policy_tagged, you need to write the
559 * resulting atom to @c lldpctl_k_port_med_policies. If the change is
560 * transmitted immediatly, you need to check the error status of the connection
561 * to know if it has been transmitted correctly. Notably, if you get @c
562 * LLDPCTL_ERR_WOULDBLOCK, you need to try again later. Usually, changes are
563 * transmitted immediatly. The exception are changes that need to be grouped to
564 * be consistent, like a LLDP MED location. When a change is transmitted
565 * immediatly, it is marked with (O).
566 *
567 * Some values may also be created. They are flagged with (C). This only applies
568 * to elements that can be iterated (L) and written (W). The element created
569 * still needs to be appended to the list by being written to it. The creation
570 * is done with @c lldpctl_atom_create().
571 *
572 * An atom marked with (S) can be retrieved as a string only. It cannot be
573 * written. An atom marked with (IS) can be retrieved as an integer and features
574 * an appropriate representation as a string (usually, the name of a constant)
575 * which is more meaningful than just the integer. An atom marked as (I) can be
576 * retrieved and as a string. In the later case, this is just a string
577 * representation of the integer. An atom marked with (AL) can be retrieved as
578 * an atom only and can be iterated over. This is usually a list of things. An
579 * atom marked (I,W) can be read as an integer or a string and can be written as
580 * an integer. The change would not be commited until the atom is written to the
581 * nearest atom supporting (A,WO) operation (eventually with an indirection, i.e
582 * first write to a (A,W), then to a (A,WO)).
583 */
584typedef enum {
8843f168 585 lldpctl_k_config_tx_interval, /**< `(I,WO)` Transmit interval. When set to -1, it is meant to transmit now. */
486a6133 586 lldpctl_k_config_receiveonly, /**< `(I,WO)` Receive only mode */
b1eceab6
VB
587 lldpctl_k_config_mgmt_pattern, /**< `(S)` Pattern to choose the management address */
588 lldpctl_k_config_iface_pattern, /**< `(S)` Pattern of enabled interfaces */
589 lldpctl_k_config_cid_pattern, /**< `(S)` Interface pattern to choose the chassis ID */
80c974a8
VB
590 lldpctl_k_config_description, /**< `(S)` Chassis description overridden */
591 lldpctl_k_config_platform, /**< `(S)` Platform description overridden (CDP) */
b1eceab6
VB
592 lldpctl_k_config_advertise_version, /**< `(I)` Advertise version */
593 lldpctl_k_config_lldpmed_noinventory, /**< `(I)` Disable LLDP-MED inventory */
e4ff3ed5 594 lldpctl_k_config_paused, /**< `(I)` lldpd is paused */
486a6133
VB
595 lldpctl_k_config_fast_start_enabled, /**< `(I,WO)` Is fast start enabled */
596 lldpctl_k_config_fast_start_interval, /**< `(I,WO)` Start fast transmit interval */
b1eceab6 597
d1dc456f 598 lldpctl_k_interface_name = 1000, /**< `(S)` The interface name. */
b1eceab6 599
d1dc456f 600 lldpctl_k_port_name = 1100, /**< `(S)` The port name. Only works for a local port. */
b1eceab6 601 lldpctl_k_port_index, /**< `(I)` The port index. Only works for a local port. */
4b292b55 602 /**
b1eceab6 603 * `(AL)` The list of known neighbors for this port.
4b292b55
VB
604 *
605 * A neighbor is in fact a remote port.
606 */
d1dc456f 607 lldpctl_k_port_neighbors = 1200,
b1eceab6
VB
608 lldpctl_k_port_protocol, /**< `(IS)` The protocol that was used to retrieve this information. */
609 lldpctl_k_port_age, /**< `(I)` Age of information, seconds from epoch. */
610 lldpctl_k_port_id_subtype, /**< `(IS)` The subtype ID of this port. */
611 lldpctl_k_port_id, /**< `(BS)` The ID of this port. */
612 lldpctl_k_port_descr, /**< `(S)` The description of this port. */
613 lldpctl_k_port_hidden, /**< `(I)` Is this port hidden (or should it be displayed?)? */
614
d1dc456f 615 lldpctl_k_port_dot3_mfs = 1300, /**< `(I)` MFS */
b1eceab6
VB
616 lldpctl_k_port_dot3_aggregid, /**< `(I)` Port aggregation ID */
617 lldpctl_k_port_dot3_autoneg_support, /**< `(I)` Autonegotiation support. */
618 lldpctl_k_port_dot3_autoneg_enabled, /**< `(I)` Autonegotiation enabled. */
619 lldpctl_k_port_dot3_autoneg_advertised, /**< `(I)` Advertised protocols. See `LLDP_DOT3_LINK_AUTONEG_*` */
620 lldpctl_k_port_dot3_mautype, /**< `(IS)` Current MAU type. See `LLDP_DOT3_MAU_*` */
621
d1dc456f 622 lldpctl_k_port_dot3_power = 1400, /**< `(A,WO)` Dot3 power related stuff. */
b1eceab6
VB
623 lldpctl_k_dot3_power_devicetype, /**< `(IS,W)` Device type. See `LLDP_DOT3_POWER_PSE/PD` */
624 lldpctl_k_dot3_power_supported, /**< `(I,W)` Is MDI power supported. */
625 lldpctl_k_dot3_power_enabled, /**< `(I,W)` Is MDI power enabled. */
626 lldpctl_k_dot3_power_paircontrol, /**< `(I,W)` Pair-control enabled? */
627 lldpctl_k_dot3_power_pairs, /**< `(IS,W)` See `LLDP_DOT3_POWERPAIRS_*` */
628 lldpctl_k_dot3_power_class, /**< `(IS,W)` Power class. */
629 lldpctl_k_dot3_power_type, /**< `(I,W)` 802.3AT power type */
630 lldpctl_k_dot3_power_source, /**< `(IS,W)` 802.3AT power source */
631 lldpctl_k_dot3_power_priority, /**< `(IS,W)` 802.3AT power priority */
632 lldpctl_k_dot3_power_allocated, /**< `(I,W)` 802.3AT power allocated */
633 lldpctl_k_dot3_power_requested, /**< `(I,W)` 802.3AT power requested */
634
d1dc456f 635 lldpctl_k_port_vlan_pvid = 1500, /**< `(I)` Primary VLAN ID */
b1eceab6
VB
636 lldpctl_k_port_vlans, /**< `(AL)` List of VLAN */
637 lldpctl_k_vlan_id, /**< `(I)` VLAN ID */
638 lldpctl_k_vlan_name, /**< `(S)` VLAN name */
639
d1dc456f 640 lldpctl_k_port_ppvids = 1600, /**< `(AL)` List of PPVIDs */
b1eceab6
VB
641 lldpctl_k_ppvid_status, /**< `(I)` Status of PPVID (see `LLDP_PPVID_CAP_*`) */
642 lldpctl_k_ppvid_id, /**< `(I)` ID of PPVID */
643
d1dc456f 644 lldpctl_k_port_pis = 1700, /**< `(AL)` List of PIDs */
b1eceab6
VB
645 lldpctl_k_pi_id, /**< `(B)` PID value */
646
d1dc456f 647 lldpctl_k_chassis_index = 1800, /**< `(I)` The chassis index. */
b1eceab6
VB
648 lldpctl_k_chassis_id_subtype, /**< `(IS)` The subtype ID of this chassis. */
649 lldpctl_k_chassis_id, /**< `(BS)` The ID of this chassis. */
650 lldpctl_k_chassis_name, /**< `(S)` The name of this chassis. */
651 lldpctl_k_chassis_descr, /**< `(S)` The description of this chassis. */
652 lldpctl_k_chassis_cap_available, /**< `(I)` Available capabalities (see `LLDP_CAP_*`) */
653 lldpctl_k_chassis_cap_enabled, /**< `(I)` Enabled capabilities (see `LLDP_CAP_*`) */
654 lldpctl_k_chassis_mgmt, /**< `(AL)` List of management addresses */
655
d1dc456f 656 lldpctl_k_chassis_med_type = 1900, /**< `(IS)` Chassis MED type. See `LLDP_MED_CLASS_*` */
b1eceab6
VB
657 lldpctl_k_chassis_med_cap, /**< `(I)` Available MED capabilitied. See `LLDP_MED_CAP_*` */
658 lldpctl_k_chassis_med_inventory_hw, /**< `(S)` LLDP MED inventory "Hardware Revision" */
659 lldpctl_k_chassis_med_inventory_sw, /**< `(S)` LLDP MED inventory "Software Revision" */
660 lldpctl_k_chassis_med_inventory_fw, /**< `(S)` LLDP MED inventory "Firmware Revision" */
661 lldpctl_k_chassis_med_inventory_sn, /**< `(S)` LLDP MED inventory "Serial Number" */
662 lldpctl_k_chassis_med_inventory_manuf, /**< `(S)` LLDP MED inventory "Manufacturer" */
663 lldpctl_k_chassis_med_inventory_model, /**< `(S)` LLDP MED inventory "Model" */
664 lldpctl_k_chassis_med_inventory_asset, /**< `(S)` LLDP MED inventory "Asset ID" */
665
d1dc456f 666 lldpctl_k_port_med_policies = 2000, /**< `(AL,WO)` MED policies attached to a port. */
b1eceab6
VB
667 lldpctl_k_med_policy_type, /**< `(IS,W)` MED policy app type. See `LLDP_MED_APPTYPE_*`. 0 if a policy is not defined. */
668 lldpctl_k_med_policy_unknown, /**< `(I,W)` Is MED policy defined? */
669 lldpctl_k_med_policy_tagged, /**< `(I,W)` MED policy tagging */
670 lldpctl_k_med_policy_vid, /**< `(I,W)` MED policy VID */
671 lldpctl_k_med_policy_priority, /**< `(I,W)` MED policy priority */
672 lldpctl_k_med_policy_dscp, /**< `(I,W)` MED policy DSCP */
673
d1dc456f 674 lldpctl_k_port_med_locations = 2100, /**< `(AL,WO)` MED locations attached to a port. */
b1eceab6
VB
675 lldpctl_k_med_location_format, /**< `(IS,W)` MED location format. See
676 * `LLDP_MED_LOCFORMAT_*`. 0 if this
4b292b55
VB
677 * location is not defined. When written,
678 * the following fields will be zeroed
679 * out. */
b1eceab6
VB
680 lldpctl_k_med_location_geoid, /**< `(IS,W)` MED geoid. See `LLDP_MED_LOCATION_GEOID_*`. Only if format is COORD. */
681 lldpctl_k_med_location_latitude, /**< `(S,W)` MED latitude. Only if format is COORD. */
682 lldpctl_k_med_location_longitude, /**< `(S,W)` MED longitude. Only if format is COORD. */
683 lldpctl_k_med_location_altitude, /**< `(S,W)` MED altitude. Only if format is COORD. */
684 lldpctl_k_med_location_altitude_unit, /**< `(S,W)` MED altitude unit. See `LLDP_MED_LOCATION_ALTITUDE_UNIT_*`.
4b292b55
VB
685 * Only if format is COORD. */
686
d1dc456f 687 lldpctl_k_med_location_country = 2200, /**< `(S,W)` MED country. Only if format is CIVIC. */
b1eceab6 688 lldpctl_k_med_location_elin, /**< `(S,W)` MED ELIN. Only if format is ELIN. */
4b292b55 689
d1dc456f 690 lldpctl_k_med_location_ca_elements = 2300, /**< `(AL,WC)` MED civic address elements. Only if format is CIVIC */
b1eceab6
VB
691 lldpctl_k_med_civicaddress_type, /**< `(IS,W)` MED civic address type. */
692 lldpctl_k_med_civicaddress_value, /**< `(S,W)` MED civic address value. */
4b292b55 693
d1dc456f 694 lldpctl_k_port_med_power = 2400, /**< `(A,WO)` LLDP-MED power related stuff. */
b1eceab6
VB
695 lldpctl_k_med_power_type, /**< `(IS,W)` LLDP MED power device type. See `LLDP_MED_POW_TYPE_*` */
696 lldpctl_k_med_power_source, /**< `(IS,W)` LLDP MED power source. See `LLDP_MED_POW_SOURCE_*` */
697 lldpctl_k_med_power_priority, /**< `(IS,W)` LLDP MED power priority. See `LLDP_MED_POW_PRIO_*` */
698 lldpctl_k_med_power_val, /**< `(I,W)` LLDP MED power value */
4b292b55 699
d1dc456f 700 lldpctl_k_mgmt_ip = 3000, /**< `(S)` IP address */
78356144 701
d1dc456f 702 lldpctl_k_tx_cnt = 4000, /**< `(I)` tx cnt. Only works for a local port. */
78356144 703 lldpctl_k_rx_cnt, /**< `(I)` rx cnt. Only works for a local port. */
704 lldpctl_k_rx_discarded_cnt, /**< `(I)` discarded cnt. Only works for a local port. */
705 lldpctl_k_rx_unrecognized_cnt, /**< `(I)` unrecognized cnt. Only works for a local port. */
706 lldpctl_k_ageout_cnt, /**< `(I)` ageout cnt. Only works for a local port. */
707 lldpctl_k_insert_cnt, /**< `(I)` insert cnt. Only works for a local port. */
708 lldpctl_k_delete_cnt, /**< `(I)` delete cnt. Only works for a local port. */
4b292b55
VB
709} lldpctl_key_t;
710
6fcf11ca
VB
711/**
712 * Get a map related to a key.
713 *
714 * Many keys expect to be written with a discrete number of values. Take for
715 * example @c lldpctl_k_med_civicaddress_type, it can take any integer between 1
716 * and 128. However, each integer can be named. It can be useful for an
717 * application to get a translation between the integer that can be provided and
718 * a more human-readable name. This function allows to retrieve the
719 * corresponding map.
720 *
721 * @param key The piece of information we want a map from.
9cac8fed 722 * @return The map or @c NULL if no map is available.
6fcf11ca
VB
723 *
724 * The returned map has its last element set to 0. It is also expected that the
725 * string value can be used with a set operation. It will be translated to the
726 * integer value.
727 */
728lldpctl_map_t *lldpctl_key_get_map(lldpctl_key_t key);
729
4b292b55
VB
730/**
731 * Retrieve a bit of information as an atom.
732 *
733 * @param atom The atom we want to query.
734 * @param key The information we want from the atom.
735 * @return The atom representing the requested information or @c NULL if the
736 * information is not available.
737 *
738 * Not every value of @c info will be available as an atom. See the
739 * documentation of @c lldpctl_key_t for values accepting to be extracted as an
740 * atom. Usually, this is only iterable values or values representing a complex
741 * object.
742 *
743 * The provided atom is not a _borrowed_ reference. You need to decrement the
744 * reference count when you don't need it anymore.
745 *
746 * As a convenience, this function will return @c NULL if the first parameter is
747 * @c NULL and no error will be raised.
748 */
749lldpctl_atom_t *lldpctl_atom_get(lldpctl_atom_t *atom, lldpctl_key_t key);
750
751/**
752 * Set a bit of information with an atom.
753 *
754 * @param atom The atom we want to write to.
755 * @param key The key information we want to write.
756 * @param value The value of the information we want to write.
757 * @return The updated atom with the appropriate information.
758 *
759 * This function will return @c NULL in case of error. If the last error is @c
760 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
761 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
762 * correct.
763 */
764lldpctl_atom_t *lldpctl_atom_set(lldpctl_atom_t *atom, lldpctl_key_t key,
765 lldpctl_atom_t *value);
766
767/**
768 * Retrieve a bit of information as a null-terminated string.
769 *
770 * @param atom The atom we want to query.
771 * @param key The information we want from the atom.
772 * @return The requested string or @c NULL if the information is not available.
773 *
774 * Not every value of @c info will be available as a string. See the
775 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
776 * string. Usually, only piece of information stored as string are available in
777 * this form but sometimes, you can get a nice formatted string instead of an
778 * integer with this function.
779 *
780 * As a convenience, this function will return @c NULL if the first parameter is
781 * @c NULL and no error will be raised.
782 *
783 * The provided string may live inside the atom providing it. If you need it
784 * longer, duplicate it.
785 */
786const char *lldpctl_atom_get_str(lldpctl_atom_t *atom, lldpctl_key_t key);
787
788/**
789 * Set a bit of information using a null-terminated string.
790 *
791 * @param atom The atom we want to write to.
792 * @param key The key information we want to write.
793 * @param value The value of the information we want to write.
794 * @return The updated atom with the appropriate information.
795 *
796 * This function will return @c NULL in case of error. If the last error is @c
797 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
798 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
799 * correct.
800 */
801lldpctl_atom_t *lldpctl_atom_set_str(lldpctl_atom_t *atom, lldpctl_key_t key,
802 const char *value);
803
804/**
805 * Retrieve a bit of information as a buffer.
806 *
807 * @param atom The atom we want to query.
808 * @param key The information we want from the atom.
b1eceab6 809 * @param[out] length The size of the returned buffer.
4b292b55
VB
810 * @return The requested buffer or @c NULL if the information is not available.
811 *
812 * Not every value of @c info will be available as a buffer. See the
813 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
814 * string. Usually, only piece of information stored as buffer are available in
815 * this form.
816 *
817 * As a convenience, this function will return @c NULL if the first parameter is
818 * @c NULL and no error will be raised. If this function returns @c NULL, the
819 * third parameter is set to 0.
820 *
821 * The provided buffer may live inside the atom providing it. If you need it
822 * longer, duplicate it.
823 */
ae5cae02 824const uint8_t *lldpctl_atom_get_buffer(lldpctl_atom_t *atom, lldpctl_key_t key,
4b292b55
VB
825 size_t *length);
826
827/**
828 * Set a bit of information using a buffer
829 *
830 * @param atom The atom we want to write to.
831 * @param key The key information we want to write.
832 * @param value The value of the information we want to write.
833 * @param length The length of the provided buffer.
834 * @return The updated atom with the appropriate information.
835 *
836 * This function will return @c NULL in case of error. If the last error is @c
837 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
838 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
839 * correct.
840 */
841lldpctl_atom_t *lldpctl_atom_set_buffer(lldpctl_atom_t *atom, lldpctl_key_t key,
ae5cae02 842 const uint8_t *value, size_t length);
4b292b55
VB
843
844/**
845 * Retrieve a bit of information as an integer.
846 *
847 * @param atom The atom we want to query.
848 * @param key The information we want from the atom.
849 * @return The requested integer or -1 if the information is not available
850 *
851 * Not every value of @c info will be available as an integer. See the
852 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
853 * string. Usually, only piece of information stored as an integer are available
854 * in this form.
855 *
856 * Only @c lldpctl_last_error() can tell if the returned value is an error or
857 * not. However, most values extracted from lldpd cannot be negative.
858 */
859long int lldpctl_atom_get_int(lldpctl_atom_t *atom, lldpctl_key_t key);
860
861/**
862 * Set a bit of information using an integer
863 *
864 * @param atom The atom we want to write to.
865 * @param key The key information we want to write.
866 * @param value The value of the information we want to write.
867 * @return The updated atom with the appropriate information.
868 *
869 * This function will return @c NULL in case of error. If the last error is @c
870 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
871 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
872 * correct.
873 */
874lldpctl_atom_t *lldpctl_atom_set_int(lldpctl_atom_t *atom, lldpctl_key_t key,
875 long int value);
876
b1eceab6
VB
877/**
878 * @defgroup liblldpctl_atom_iter Iterating over atoms
879 *
880 * Iterate over atoms (lists).
881 *
882 * @{
883 */
4b292b55
VB
884/**
885 * Iterator over an iterable atom (a list of ports, a list of VLAN, ...). When
886 * an atom is a list, it can be iterated over to extract the appropriate values.
887 *
888 * @see lldpctl_atom_iter(), lldpctl_atom_iter_next(), lldpctl_atom_iter_value()
889 */
890typedef struct lldpctl_atom_iter_t lldpctl_atom_iter_t;
891
892/**
893 * Return an iterator over a given atom.
894 *
895 * If an atom is iterable (if it is a list, like a list of ports, a list of
896 * VLAN, a list of neighbors), it is possible to iterate over it. First use this
897 * function to get an iterator then use @c lldpctl_atom_iter_next() to get the
898 * next item and @c lldpctl_atom_iter_value() to the actuel item.
899 *
900 * @param atom The atom we want to create an iterator from.
901 * @return The iterator or @c NULL if an error happened or if the atom is empty
902 * (check with @c lldpctl_last_error()).
903 *
904 * As a convenience, if the provided atom is @c NULL, this function will return
905 * @c NULL and no error will be raised.
906 */
907lldpctl_atom_iter_t *lldpctl_atom_iter(lldpctl_atom_t *atom);
908
909/**
910 * Return the next element of an iterator.
911 *
912 * @param atom The atom we are currently iterating.
913 * @param iter The iterator we want the next element from.
914 * @return An iterator starting on the next element or @c NULL if we have no
915 * more elements
916 *
917 * @see lldpctl_atom_iter(), lldpctl_atom_iter_value().
918 *
919 * As a convenience, if the provided atom is @c NULL, this function will return
920 * @c NULL and no error will be raised.
921 */
922lldpctl_atom_iter_t *lldpctl_atom_iter_next(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter);
923
924/**
925 * Return the value of an iterator.
926 *
927 * @param atom The atom we are currently iterating.
928 * @param iter The iterator we want the next element from.
929 * @return The atom currently associated with the iterator.
930 *
931 * @see lldpctl_atom_iter(), lldpctl_atom_iter_next().
932 */
933lldpctl_atom_t *lldpctl_atom_iter_value(lldpctl_atom_t *atom, lldpctl_atom_iter_t *iter);
934
935/**
936 * Convenience macro to iter over every value of an iterable object.
937 *
938 * @param atom The atom you want to iterate on.
939 * @param value Atom that will be used to contain each value.
940 *
941 * This macro behaves as a for loop. Moreover, at the end of each iteration,
942 * value is deallocated. Don't use it outside of the loop!
943 */
944#define lldpctl_atom_foreach(atom, value) \
945 for (lldpctl_atom_iter_t *iter = lldpctl_atom_iter(atom); \
946 iter && (value = lldpctl_atom_iter_value(atom, iter)); \
947 iter = lldpctl_atom_iter_next(atom, iter), \
948 lldpctl_atom_dec_ref(value))
949
950/**
951 * Create a new value for an iterable element.
952 *
953 * The value is meant to be appended using @c lldpctl_atom_set(). Currently,
954 * there is no way to delete an element from a list. It is also not advisable to
955 * use getters on a newly created object until it is fully initialized. If its
956 * internal representation is using a buffer, it may not be initialized until
957 * the first set.
958 *
959 * @param atom The atom we want to create a new element for.
960 * @return The new element.
961 */
962lldpctl_atom_t *lldpctl_atom_create(lldpctl_atom_t *atom);
b1eceab6
VB
963/**@}*/
964/**@}*/
4b292b55
VB
965
966#ifdef __cplusplus
967}
968#endif
969
b1eceab6
VB
970/**@}*/
971
4b292b55 972#endif