1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
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.
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.
22 * @defgroup liblldpctl liblldpctl: library to interface with lldpd
24 * `liblldpctl` allows any program to convenienty query and modify the behaviour
25 * of a running lldpd daemon.
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`
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.
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.
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
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.
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
67 #include <sys/types.h>
70 * @defgroup lldpctl_connection Managing connection to lldpd
72 * Connection with lldpd.
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()).
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.
85 * A connection should be allocated by using @ref lldpctl_new(). It needs to be
86 * released with @ref lldpctl_release().
92 * Get default transport name.
94 * Currently, this is the default location of the Unix socket.
96 const char* lldpctl_get_default_transport(void);
99 * Structure referencing a connection with lldpd.
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().
105 typedef struct lldpctl_conn_t lldpctl_conn_t
;
108 * Callback function invoked to send data to lldpd.
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.
118 typedef ssize_t (*lldpctl_send_callback
)(lldpctl_conn_t
*conn
,
119 const uint8_t *data
, size_t length
, void *user_data
);
122 * Callback function invoked to receive data from lldpd.
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.
133 typedef ssize_t (*lldpctl_recv_callback
)(lldpctl_conn_t
*conn
,
134 const uint8_t *data
, size_t length
, void *user_data
);
137 * Function invoked when additional data is available from lldpd.
139 * This function should be invoked in case of asynchronous IO when new data is
140 * available from lldpd (expected or unexpected).
142 * @param conn Handle to the connection to lldpd.
143 * @param data Data received from lldpd.
144 * @param length Length of data received.
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
149 ssize_t
lldpctl_recv(lldpctl_conn_t
*conn
, const uint8_t *data
, size_t length
);
152 * Function invoked when there is an opportunity to send data to lldpd.
154 * This function should be invoked in case of asynchronous IO when new data can
155 * be written to lldpd.
157 * @param conn Handle to the connection to lldpd.
158 * @return The number of bytes processed or a negative integer if an error has
161 ssize_t
lldpctl_send(lldpctl_conn_t
*conn
);
164 * Function invoked to see if there's more data to be processed in the buffer.
166 * This function should be invoked to check for notifications in the data that
167 * has already been read. Its used typically for asynchronous connections.
169 * @param conn Handle to the connection to lldpd.
170 * @return 0 to indicate maybe more data is available for processing
171 * !0 to indicate no data or insufficient data for processing
173 int lldpctl_process_conn_buffer(lldpctl_conn_t
*conn
);
177 * Allocate a new handler for connecting to lldpd.
179 * @param send Callback to be used when sending new data is requested.
180 * @param recv Callback to be used when receiving new data is requested.
181 * @param user_data Data to pass to callbacks.
182 * @return An handler to be used to connect to lldpd or @c NULL in
183 * case of error. In the later case, the error is probable an
184 * out of memory condition.
186 * The allocated handler can be released with @c lldpctl_release(). If the
187 * provided parameters are both @c NULL, default synchronous callbacks will be
190 lldpctl_conn_t
*lldpctl_new(lldpctl_send_callback send
,
191 lldpctl_recv_callback recv
, void *user_data
);
194 * Allocate a new handler for connecting to lldpd.
196 * @param ctlname the Unix-domain socket to connect to lldpd.
197 * @param send Callback to be used when sending new data is requested.
198 * @param recv Callback to be used when receiving new data is requested.
199 * @param user_data Data to pass to callbacks.
200 * @return An handler to be used to connect to lldpd or @c NULL in
201 * case of error. In the later case, the error is probable an
202 * out of memory condition.
204 * The allocated handler can be released with @c lldpctl_release(). If the
205 * provided parameters are both @c NULL, default synchronous callbacks will be
208 lldpctl_conn_t
*lldpctl_new_name(const char *ctlname
, lldpctl_send_callback send
,
209 lldpctl_recv_callback recv
, void *user_data
);
212 * Release resources associated with a connection to lldpd.
214 * @param conn Previously allocated handler to a connection to lldpd.
215 * @return 0 on success or a negative integer
219 int lldpctl_release(lldpctl_conn_t
*conn
);
223 * @defgroup lldpctl_errors_logs Errors and logs handling
225 * Error codes and logs handling.
227 * When a function returns a pointer, it may return @c NULL to indicate an error
228 * condition. In this case, it is possible to use @ref lldpctl_last_error() to
229 * get the related error code which is one of the values in @ref lldpctl_error_t
230 * enumeration. For display purpose @ref lldpctl_strerror() may be used to
231 * translate this error code.
233 * When a function returns an integer, it may return a negative value. It
234 * usually means this is an error but some functions may return a legetimate
235 * negative value (for example @ref lldpctl_atom_get_int()). When there is a
236 * doubt, @ref lldpctl_last_error() should be checked.
238 * An error is attached to a connection. If there is no connection, no error
239 * handling is available. Most functions use a connection or an atom as first
240 * argument and therefore are attached to a connection. To get the connection
241 * related to an atom, use @ref lldpctl_atom_get_connection().
243 * Also have a look at @ref lldpctl_log_callback() function if you want a custom
250 * Setup log handlers.
252 * By default, liblldpctl will log to stderr. The following function will
253 * register another callback for this purpose. Messages logged through this
254 * callback may be cryptic. They are targeted for the developer. Message for end
255 * users should rely on return codes.
257 void lldpctl_log_callback(void (*cb
)(int severity
, const char *msg
));
260 * Possible error codes for functions that return negative integers on
261 * this purpose or for @c lldpctl_last_error().
265 * No error has happened (yet).
267 LLDPCTL_NO_ERROR
= 0,
269 * A IO related operation would block if performed.
271 LLDPCTL_ERR_WOULDBLOCK
= -501,
273 * A IO related operation has reached a end of file condition.
275 LLDPCTL_ERR_EOF
= -502,
277 * The requested information does not exist. For example, when
278 * requesting an inexistant information from an atom.
280 LLDPCTL_ERR_NOT_EXIST
= -503,
282 * Cannot connect to the lldpd daemon. This error only happens with
283 * default synchronous handlers.
285 LLDPCTL_ERR_CANNOT_CONNECT
= -504,
287 * Atom is of incorrect type for the requested operation.
289 LLDPCTL_ERR_INCORRECT_ATOM_TYPE
= -505,
291 * An error occurred during serialization of message.
293 LLDPCTL_ERR_SERIALIZATION
= -506,
295 * The requested operation cannot be performed because we have another
296 * operation already running.
298 LLDPCTL_ERR_INVALID_STATE
= -507,
300 * The provided atom cannot be iterated.
302 LLDPCTL_ERR_CANNOT_ITERATE
= -508,
304 * The provided value is invalid.
306 LLDPCTL_ERR_BAD_VALUE
= -509,
308 * No new element can be created for this element.
310 LLDPCTL_ERR_CANNOT_CREATE
= -510,
312 * The library is under unexpected conditions and cannot process
313 * any further data reliably.
315 LLDPCTL_ERR_FATAL
= -900,
317 * Out of memory condition. Things may get havoc here but we
318 * should be able to recover.
320 LLDPCTL_ERR_NOMEM
= -901,
322 * An error occurred in a user provided callback.
324 LLDPCTL_ERR_CALLBACK_FAILURE
= -902
328 * Describe a provided error code.
330 * @param error Error code to be described.
331 * @return Statically allocated string describing the error.
333 const char *lldpctl_strerror(lldpctl_error_t error
);
336 * Get the last error associated to a connection to lldpd.
338 * @param conn Previously allocated handler to a connection to lldpd.
339 * @return 0 if no error is currently registered. A negative integer
342 * For functions returning int, this function will return the same
343 * error number. For functions returning something else, you can use
344 * this function to get the appropriate error number.
346 lldpctl_error_t
lldpctl_last_error(lldpctl_conn_t
*conn
);
349 * Describe the last error associate to a connection.
351 * @param conn Previously allocated handler to a connection to lldpd.
352 * @return Statically allocated string describing the error
354 #define lldpctl_last_strerror(conn) lldpctl_strerror(lldpctl_last_error(conn))
358 * @defgroup lldpctl_atoms Extracting information: atoms
360 * Information retrieved from lldpd is represented as an atom.
362 * This is an opaque structure that can be passed along some functions to
363 * transmit chassis, ports, VLAN and other information related to LLDP. Most
364 * information are extracted using @c lldpctl_atom_get(), @c
365 * lldpctl_atom_get_str(), @c lldpctl_atom_get_buffer() or @c
366 * lldpctl_atom_get_int(), unless some IO with lldpd is needed to retrieve the
367 * requested information. In this case, there exists an appropriate function to
368 * convert the "deferred" atom into a normal one (like @c lldpctl_get_port()).
370 * For some information, setters are also available: @c lldpctl_atom_set(), @c
371 * lldpctl_atom_set_str(), @c lldpctl_atom_set_buffer() or @c
372 * lldpctl_atom_set_int(). Unlike getters, some of those may require IO to
373 * achieve their goal.
375 * An atom is reference counted. Unless documented otherwise, a function
376 * returning an atom will return a new reference that should be decremented if
377 * not used anymore. It is quite important to use the reference counting
378 * functions correctly. Segfaults or memory leaks may occur otherwise.
384 * Structure representing an element (chassis, port, VLAN, ...)
386 * @see lldpctl_atom_inc_ref(), lldpctl_atom_dec_ref().
388 typedef struct lldpctl_atom_t lldpctl_atom_t
;
391 * Structure representing a map from an integer to a character string.
393 * @see lldpctl_key_get_map().
395 typedef const struct {
401 * Return the reference to connection with lldpd.
403 * @param atom The atom we want reference from.
404 * @return The reference to the connection to lldpd.
406 * Each atom contains an internal reference to the corresponding connection to
407 * lldpd. Use this function to get it.
409 lldpctl_conn_t
*lldpctl_atom_get_connection(lldpctl_atom_t
*atom
);
412 * Increment reference count for an atom.
414 * @param atom Atom we which to increase reference count.
416 void lldpctl_atom_inc_ref(lldpctl_atom_t
*atom
);
419 * Decrement reference count for an atom.
421 * @param atom Atom we want to decrease reference count. Can be @c NULL. In this
422 * case, nothing happens.
424 * When the reference count becomes 0, the atom is freed.
426 void lldpctl_atom_dec_ref(lldpctl_atom_t
*atom
);
429 * Possible events for a change (notification).
431 * @see lldpctl_watch_callback
434 lldpctl_c_deleted
, /**< The neighbor has been deleted */
435 lldpctl_c_updated
, /**< The neighbor has been updated */
436 lldpctl_c_added
, /**< This is a new neighbor */
440 * Callback function invoked when a change is detected.
442 * @param conn Connection with lldpd.
443 * @param type Type of change detected.
444 * @param interface Physical interface on which the change has happened.
445 * @param neighbor Changed neighbor.
446 * @param data Data provided when registering the callback.
448 * The provided interface and neighbor atoms will have their reference count
449 * decremented when the callback ends. If you want to keep a reference to it, be
450 * sure to increment the reference count in the callback.
452 * @see lldpctl_watch_callback
454 typedef void (*lldpctl_change_callback
)(lldpctl_conn_t
*conn
,
455 lldpctl_change_t type
,
456 lldpctl_atom_t
*interface
,
457 lldpctl_atom_t
*neighbor
,
461 * Register a callback to be called on changes.
463 * @param conn Connection with lldpd.
464 * @param cb Replace the current callback with the provided one.
465 * @param data Data that will be passed to the callback.
466 * @return 0 in case of success or -1 in case of errors.
468 * This function will register the necessity to push neighbor changes to lldpd
469 * and therefore will issue IO operations. The error code could then be @c
470 * LLDPCTL_ERR_WOULDBLOCK.
472 int lldpctl_watch_callback(lldpctl_conn_t
*conn
,
473 lldpctl_change_callback cb
,
477 * Wait for the next change.
479 * @param conn Connection with lldpd.
480 * @return 0 on success or a negative integer in case of error.
482 * This function will return once a change has been detected. It is only useful
483 * as a main loop when using the builtin blocking IO mechanism.
485 int lldpctl_watch(lldpctl_conn_t
*conn
);
488 * @defgroup liblldpctl_atom_get_special Retrieving atoms from lldpd
490 * Special access functions.
492 * Most information can be retrieved through @ref lldpctl_atom_get(), @ref
493 * lldpctl_atom_get_int(), @ref lldpctl_atom_get_str() or @ref
494 * lldpctl_atom_get_buffer() but some information can only be retrieved through
495 * special functions because IO operation is needed (and also, for some of them,
496 * because we don't have an atom yet).
502 * Retrieve global configuration of lldpd daemon.
504 * @param conn Connection with lldpd.
505 * @return The global configuration or @c NULL if an error happened.
507 * This function will make IO with the daemon to get the
508 * configuration. Depending on the IO model, information may not be available
509 * right now and the function should be called again later. If @c NULL is
510 * returned, check the last error. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again
513 lldpctl_atom_t
*lldpctl_get_configuration(lldpctl_conn_t
*conn
);
516 * Retrieve the list of available interfaces.
518 * @param conn Previously allocated handler to a connection to lldpd.
519 * @return The list of available ports or @c NULL if an error happened.
521 * This function will make IO with the daemon to get the list of
522 * ports. Depending on the IO model, information may not be available right now
523 * and the function should be called again later. If @c NULL is returned, check
524 * what the last error is. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again later
525 * (when more data is available).
527 * The list of available ports can be iterated with @ref lldpctl_atom_foreach().
529 lldpctl_atom_t
*lldpctl_get_interfaces(lldpctl_conn_t
*conn
);
532 * Retrieve the information related to a given interface.
534 * @param port The port we want to retrieve information from. This port is an
535 * atom retrieved from an interation on @c lldpctl_get_interfaces().
536 * @return Atom related to this port which may be used in subsequent functions.
538 * This functions may have to do IO to get the information related to the given
539 * port. Depending on the IO mode, information may not be available tight now
540 * and the function should be called again later. If @c NULL is returned, check
541 * what the last error is. If it is @c LLDPCTL_ERR_WOULDBLOCK, try again later
542 * (when more data is available).
544 lldpctl_atom_t
*lldpctl_get_port(lldpctl_atom_t
*port
);
549 * Piece of information that can be retrieved from/written to an atom.
551 * Each piece of information can potentially be retrieved as an atom (A), a
552 * string (S), a buffer (B) or an integer (I). Additionaly, when an information
553 * can be retrieved as an atom, it is usually iterable (L). When an atom can be
554 * retrieved as a string and as an additional type, the string is expected to be
555 * formatted. For example, the MAC address of a local port can be retrieved as a
556 * buffer and a string. As a string, you'll get something like
557 * "00:11:22:33:44:55". Also, all values that can be get as an integer or a
558 * buffer can be get as a string too. There is no special formatting in this
559 * case. "(BS)" means that the string get a special appropriate format.
561 * The name of a key is an indication on the type of atom that information can
562 * be extracted from. For example, @c lldpctl_k_med_policy_type can be extracted
563 * from an atom you got by iterating on @c lldpctl_k_port_med_policies. On the
564 * other hand, @c lldpctl_k_port_descr and @c lldpctl_k_chassis can be retrieved
565 * from an atom retrieved either by iterating @c lldpctl_k_port_neighbors or
566 * with @c lldpctl_get_port().
568 * Some values may be written. They are marked with (W). Such a change may or
569 * may not be transmitted immediatly. If they are not transmitted immediatly,
570 * this means that the resulting atom should be written to another atom. For
571 * example, when writting @c lldpctl_k_med_policy_tagged, you need to write the
572 * resulting atom to @c lldpctl_k_port_med_policies. If the change is
573 * transmitted immediatly, you need to check the error status of the connection
574 * to know if it has been transmitted correctly. Notably, if you get @c
575 * LLDPCTL_ERR_WOULDBLOCK, you need to try again later. Usually, changes are
576 * transmitted immediatly. The exception are changes that need to be grouped to
577 * be consistent, like a LLDP MED location. When a change is transmitted
578 * immediatly, it is marked with (O). @c lldpctl_atom_set_str() may accept a @c
579 * NULL value. This case is marked with (N) and usually reset the item to the
580 * default value or no value.
582 * Some values may also be created. They are flagged with (C). This only applies
583 * to elements that can be iterated (L) and written (W). The element created
584 * still needs to be appended to the list by being written to it. The creation
585 * is done with @c lldpctl_atom_create().
587 * An atom marked with (S) can be retrieved as a string only. It cannot be
588 * written. An atom marked with (IS) can be retrieved as an integer and features
589 * an appropriate representation as a string (usually, the name of a constant)
590 * which is more meaningful than just the integer. An atom marked as (I) can be
591 * retrieved and as a string. In the later case, this is just a string
592 * representation of the integer. An atom marked with (AL) can be retrieved as
593 * an atom only and can be iterated over. This is usually a list of things. An
594 * atom marked (I,W) can be read as an integer or a string and can be written as
595 * an integer. The change would not be commited until the atom is written to the
596 * nearest atom supporting (A,WO) operation (eventually with an indirection, i.e
597 * first write to a (A,W), then to a (A,WO)).
600 lldpctl_k_config_tx_interval
, /**< `(I,WO)` Transmit interval. When set to -1, it is meant to transmit now. */
601 lldpctl_k_config_receiveonly
, /**< `(I)` Receive only mode */
602 lldpctl_k_config_mgmt_pattern
, /**< `(S,WON)` Pattern to choose the management address */
603 lldpctl_k_config_iface_pattern
, /**< `(S,WON)` Pattern of enabled interfaces */
604 lldpctl_k_config_cid_pattern
, /**< `(S)` Interface pattern to choose the chassis ID */
605 lldpctl_k_config_description
, /**< `(S,WON)` Chassis description overridden */
606 lldpctl_k_config_platform
, /**< `(S,WON)` Platform description overridden (CDP) */
607 lldpctl_k_config_hostname
, /**< `(S,WON)` System name overridden */
608 lldpctl_k_config_advertise_version
, /**< `(I)` Advertise version */
609 lldpctl_k_config_lldpmed_noinventory
, /**< `(I)` Disable LLDP-MED inventory */
610 lldpctl_k_config_paused
, /**< `(I)` lldpd is paused */
611 lldpctl_k_config_fast_start_enabled
, /**< `(I,WO)` Is fast start enabled */
612 lldpctl_k_config_fast_start_interval
, /**< `(I,WO)` Start fast transmit interval */
613 lldpctl_k_config_ifdescr_update
, /**< `(I,WO)` Enable or disable setting interface description */
614 lldpctl_k_config_iface_promisc
, /**< `(I,WO)` Enable or disable promiscuous mode on interfaces */
615 lldpctl_k_config_chassis_cap_advertise
, /**< `(I,WO)` Enable or disable chassis capabilities advertisement */
616 lldpctl_k_config_chassis_mgmt_advertise
, /**< `(I,WO)` Enable or disable management addresses advertisement */
618 lldpctl_k_interface_name
= 1000, /**< `(S)` The interface name. */
620 lldpctl_k_port_name
= 1100, /**< `(S)` The port name. Only works for a local port. */
621 lldpctl_k_port_index
, /**< `(I)` The port index. Only works for a local port. */
623 * `(AL)` The list of known neighbors for this port.
625 * A neighbor is in fact a remote port.
627 lldpctl_k_port_neighbors
= 1200,
628 lldpctl_k_port_protocol
, /**< `(IS)` The protocol that was used to retrieve this information. */
629 lldpctl_k_port_age
, /**< `(I)` Age of information, seconds from epoch. */
630 lldpctl_k_port_id_subtype
, /**< `(IS)` The subtype ID of this port. */
631 lldpctl_k_port_id
, /**< `(BS,WO)` The ID of this port. */
632 lldpctl_k_port_descr
, /**< `(S,WO)` The description of this port. */
633 lldpctl_k_port_hidden
, /**< `(I)` Is this port hidden (or should it be displayed?)? */
635 lldpctl_k_port_dot3_mfs
= 1300, /**< `(I)` MFS */
636 lldpctl_k_port_dot3_aggregid
, /**< `(I)` Port aggregation ID */
637 lldpctl_k_port_dot3_autoneg_support
, /**< `(I)` Autonegotiation support. */
638 lldpctl_k_port_dot3_autoneg_enabled
, /**< `(I)` Autonegotiation enabled. */
639 lldpctl_k_port_dot3_autoneg_advertised
, /**< `(I)` Advertised protocols. See `LLDP_DOT3_LINK_AUTONEG_*` */
640 lldpctl_k_port_dot3_mautype
, /**< `(IS)` Current MAU type. See `LLDP_DOT3_MAU_*` */
642 lldpctl_k_port_dot3_power
= 1400, /**< `(A,WO)` Dot3 power related stuff. */
643 lldpctl_k_dot3_power_devicetype
, /**< `(IS,W)` Device type. See `LLDP_DOT3_POWER_PSE/PD` */
644 lldpctl_k_dot3_power_supported
, /**< `(I,W)` Is MDI power supported. */
645 lldpctl_k_dot3_power_enabled
, /**< `(I,W)` Is MDI power enabled. */
646 lldpctl_k_dot3_power_paircontrol
, /**< `(I,W)` Pair-control enabled? */
647 lldpctl_k_dot3_power_pairs
, /**< `(IS,W)` See `LLDP_DOT3_POWERPAIRS_*` */
648 lldpctl_k_dot3_power_class
, /**< `(IS,W)` Power class. */
649 lldpctl_k_dot3_power_type
, /**< `(I,W)` 802.3AT power type */
650 lldpctl_k_dot3_power_source
, /**< `(IS,W)` 802.3AT power source */
651 lldpctl_k_dot3_power_priority
, /**< `(IS,W)` 802.3AT power priority */
652 lldpctl_k_dot3_power_allocated
, /**< `(I,W)` 802.3AT power allocated */
653 lldpctl_k_dot3_power_requested
, /**< `(I,W)` 802.3AT power requested */
655 lldpctl_k_port_vlan_pvid
= 1500, /**< `(I)` Primary VLAN ID */
656 lldpctl_k_port_vlans
, /**< `(AL)` List of VLAN */
657 lldpctl_k_vlan_id
, /**< `(I)` VLAN ID */
658 lldpctl_k_vlan_name
, /**< `(S)` VLAN name */
660 lldpctl_k_port_ppvids
= 1600, /**< `(AL)` List of PPVIDs */
661 lldpctl_k_ppvid_status
, /**< `(I)` Status of PPVID (see `LLDP_PPVID_CAP_*`) */
662 lldpctl_k_ppvid_id
, /**< `(I)` ID of PPVID */
664 lldpctl_k_port_pis
= 1700, /**< `(AL)` List of PIDs */
665 lldpctl_k_pi_id
, /**< `(B)` PID value */
667 lldpctl_k_chassis_index
= 1800, /**< `(I)` The chassis index. */
668 lldpctl_k_chassis_id_subtype
, /**< `(IS)` The subtype ID of this chassis. */
669 lldpctl_k_chassis_id
, /**< `(BS)` The ID of this chassis. */
670 lldpctl_k_chassis_name
, /**< `(S)` The name of this chassis. */
671 lldpctl_k_chassis_descr
, /**< `(S)` The description of this chassis. */
672 lldpctl_k_chassis_cap_available
, /**< `(I)` Available capabalities (see `LLDP_CAP_*`) */
673 lldpctl_k_chassis_cap_enabled
, /**< `(I)` Enabled capabilities (see `LLDP_CAP_*`) */
674 lldpctl_k_chassis_mgmt
, /**< `(AL)` List of management addresses */
676 lldpctl_k_chassis_med_type
= 1900, /**< `(IS)` Chassis MED type. See `LLDP_MED_CLASS_*` */
677 lldpctl_k_chassis_med_cap
, /**< `(I)` Available MED capabilitied. See `LLDP_MED_CAP_*` */
678 lldpctl_k_chassis_med_inventory_hw
, /**< `(S)` LLDP MED inventory "Hardware Revision" */
679 lldpctl_k_chassis_med_inventory_sw
, /**< `(S)` LLDP MED inventory "Software Revision" */
680 lldpctl_k_chassis_med_inventory_fw
, /**< `(S)` LLDP MED inventory "Firmware Revision" */
681 lldpctl_k_chassis_med_inventory_sn
, /**< `(S)` LLDP MED inventory "Serial Number" */
682 lldpctl_k_chassis_med_inventory_manuf
, /**< `(S)` LLDP MED inventory "Manufacturer" */
683 lldpctl_k_chassis_med_inventory_model
, /**< `(S)` LLDP MED inventory "Model" */
684 lldpctl_k_chassis_med_inventory_asset
, /**< `(S)` LLDP MED inventory "Asset ID" */
686 lldpctl_k_port_med_policies
= 2000, /**< `(AL,WO)` MED policies attached to a port. */
687 lldpctl_k_med_policy_type
, /**< `(IS,W)` MED policy app type. See `LLDP_MED_APPTYPE_*`. 0 if a policy is not defined. */
688 lldpctl_k_med_policy_unknown
, /**< `(I,W)` Is MED policy defined? */
689 lldpctl_k_med_policy_tagged
, /**< `(I,W)` MED policy tagging */
690 lldpctl_k_med_policy_vid
, /**< `(I,W)` MED policy VID */
691 lldpctl_k_med_policy_priority
, /**< `(I,W)` MED policy priority */
692 lldpctl_k_med_policy_dscp
, /**< `(I,W)` MED policy DSCP */
694 lldpctl_k_port_med_locations
= 2100, /**< `(AL,WO)` MED locations attached to a port. */
695 lldpctl_k_med_location_format
, /**< `(IS,W)` MED location format. See
696 * `LLDP_MED_LOCFORMAT_*`. 0 if this
697 * location is not defined. When written,
698 * the following fields will be zeroed
700 lldpctl_k_med_location_geoid
, /**< `(IS,W)` MED geoid. See `LLDP_MED_LOCATION_GEOID_*`. Only if format is COORD. */
701 lldpctl_k_med_location_latitude
, /**< `(S,W)` MED latitude. Only if format is COORD. */
702 lldpctl_k_med_location_longitude
, /**< `(S,W)` MED longitude. Only if format is COORD. */
703 lldpctl_k_med_location_altitude
, /**< `(S,W)` MED altitude. Only if format is COORD. */
704 lldpctl_k_med_location_altitude_unit
, /**< `(S,W)` MED altitude unit. See `LLDP_MED_LOCATION_ALTITUDE_UNIT_*`.
705 * Only if format is COORD. */
707 lldpctl_k_med_location_country
= 2200, /**< `(S,W)` MED country. Only if format is CIVIC. */
708 lldpctl_k_med_location_elin
, /**< `(S,W)` MED ELIN. Only if format is ELIN. */
710 lldpctl_k_med_location_ca_elements
= 2300, /**< `(AL,WC)` MED civic address elements. Only if format is CIVIC */
711 lldpctl_k_med_civicaddress_type
, /**< `(IS,W)` MED civic address type. */
712 lldpctl_k_med_civicaddress_value
, /**< `(S,W)` MED civic address value. */
714 lldpctl_k_port_med_power
= 2400, /**< `(A,WO)` LLDP-MED power related stuff. */
715 lldpctl_k_med_power_type
, /**< `(IS,W)` LLDP MED power device type. See `LLDP_MED_POW_TYPE_*` */
716 lldpctl_k_med_power_source
, /**< `(IS,W)` LLDP MED power source. See `LLDP_MED_POW_SOURCE_*` */
717 lldpctl_k_med_power_priority
, /**< `(IS,W)` LLDP MED power priority. See `LLDP_MED_POW_PRIO_*` */
718 lldpctl_k_med_power_val
, /**< `(I,W)` LLDP MED power value */
720 lldpctl_k_mgmt_ip
= 3000, /**< `(S)` IP address */
722 lldpctl_k_tx_cnt
= 4000, /**< `(I)` tx cnt. Only works for a local port. */
723 lldpctl_k_rx_cnt
, /**< `(I)` rx cnt. Only works for a local port. */
724 lldpctl_k_rx_discarded_cnt
, /**< `(I)` discarded cnt. Only works for a local port. */
725 lldpctl_k_rx_unrecognized_cnt
, /**< `(I)` unrecognized cnt. Only works for a local port. */
726 lldpctl_k_ageout_cnt
, /**< `(I)` ageout cnt. Only works for a local port. */
727 lldpctl_k_insert_cnt
, /**< `(I)` insert cnt. Only works for a local port. */
728 lldpctl_k_delete_cnt
, /**< `(I)` delete cnt. Only works for a local port. */
729 lldpctl_k_config_tx_hold
, /**< `(I,WO)` Transmit hold interval. */
730 lldpctl_k_config_bond_slave_src_mac_type
, /**< `(I,WO)` bond slave src mac type. */
731 lldpctl_k_config_lldp_portid_type
, /**< `(I,WO)` LLDP PortID TLV Subtype */
735 * Get a map related to a key.
737 * Many keys expect to be written with a discrete number of values. Take for
738 * example @c lldpctl_k_med_civicaddress_type, it can take any integer between 1
739 * and 128. However, each integer can be named. It can be useful for an
740 * application to get a translation between the integer that can be provided and
741 * a more human-readable name. This function allows to retrieve the
744 * @param key The piece of information we want a map from.
745 * @return The map or @c NULL if no map is available.
747 * The returned map has its last element set to 0. It is also expected that the
748 * string value can be used with a set operation. It will be translated to the
751 lldpctl_map_t
*lldpctl_key_get_map(lldpctl_key_t key
);
754 * Retrieve a bit of information as an atom.
756 * @param atom The atom we want to query.
757 * @param key The information we want from the atom.
758 * @return The atom representing the requested information or @c NULL if the
759 * information is not available.
761 * Not every value of @c info will be available as an atom. See the
762 * documentation of @c lldpctl_key_t for values accepting to be extracted as an
763 * atom. Usually, this is only iterable values or values representing a complex
766 * The provided atom is not a _borrowed_ reference. You need to decrement the
767 * reference count when you don't need it anymore.
769 * As a convenience, this function will return @c NULL if the first parameter is
770 * @c NULL and no error will be raised.
772 lldpctl_atom_t
*lldpctl_atom_get(lldpctl_atom_t
*atom
, lldpctl_key_t key
);
775 * Set a bit of information with an atom.
777 * @param atom The atom we want to write to.
778 * @param key The key information we want to write.
779 * @param value The value of the information we want to write.
780 * @return The updated atom with the appropriate information.
782 * This function will return @c NULL in case of error. If the last error is @c
783 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
784 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
787 lldpctl_atom_t
*lldpctl_atom_set(lldpctl_atom_t
*atom
, lldpctl_key_t key
,
788 lldpctl_atom_t
*value
);
791 * Retrieve a bit of information as a null-terminated string.
793 * @param atom The atom we want to query.
794 * @param key The information we want from the atom.
795 * @return The requested string or @c NULL if the information is not available.
797 * Not every value of @c info will be available as a string. See the
798 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
799 * string. Usually, only piece of information stored as string are available in
800 * this form but sometimes, you can get a nice formatted string instead of an
801 * integer with this function.
803 * As a convenience, this function will return @c NULL if the first parameter is
804 * @c NULL and no error will be raised.
806 * The provided string may live inside the atom providing it. If you need it
807 * longer, duplicate it.
809 const char *lldpctl_atom_get_str(lldpctl_atom_t
*atom
, lldpctl_key_t key
);
812 * Set a bit of information using a null-terminated string.
814 * @param atom The atom we want to write to.
815 * @param key The key information we want to write.
816 * @param value The value of the information we want to write.
817 * @return The updated atom with the appropriate information.
819 * This function will return @c NULL in case of error. If the last error is @c
820 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
821 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
824 lldpctl_atom_t
*lldpctl_atom_set_str(lldpctl_atom_t
*atom
, lldpctl_key_t key
,
828 * Retrieve a bit of information as a buffer.
830 * @param atom The atom we want to query.
831 * @param key The information we want from the atom.
832 * @param[out] length The size of the returned buffer.
833 * @return The requested buffer or @c NULL if the information is not available.
835 * Not every value of @c info will be available as a buffer. See the
836 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
837 * string. Usually, only piece of information stored as buffer are available in
840 * As a convenience, this function will return @c NULL if the first parameter is
841 * @c NULL and no error will be raised. If this function returns @c NULL, the
842 * third parameter is set to 0.
844 * The provided buffer may live inside the atom providing it. If you need it
845 * longer, duplicate it.
847 const uint8_t *lldpctl_atom_get_buffer(lldpctl_atom_t
*atom
, lldpctl_key_t key
,
851 * Set a bit of information using a buffer
853 * @param atom The atom we want to write to.
854 * @param key The key information we want to write.
855 * @param value The value of the information we want to write.
856 * @param length The length of the provided buffer.
857 * @return The updated atom with the appropriate information.
859 * This function will return @c NULL in case of error. If the last error is @c
860 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
861 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
864 lldpctl_atom_t
*lldpctl_atom_set_buffer(lldpctl_atom_t
*atom
, lldpctl_key_t key
,
865 const uint8_t *value
, size_t length
);
868 * Retrieve a bit of information as an integer.
870 * @param atom The atom we want to query.
871 * @param key The information we want from the atom.
872 * @return The requested integer or -1 if the information is not available
874 * Not every value of @c info will be available as an integer. See the
875 * documentation of @c lldpctl_key_t for values accepting to be extracted as a
876 * string. Usually, only piece of information stored as an integer are available
879 * Only @c lldpctl_last_error() can tell if the returned value is an error or
880 * not. However, most values extracted from lldpd cannot be negative.
882 long int lldpctl_atom_get_int(lldpctl_atom_t
*atom
, lldpctl_key_t key
);
885 * Set a bit of information using an integer
887 * @param atom The atom we want to write to.
888 * @param key The key information we want to write.
889 * @param value The value of the information we want to write.
890 * @return The updated atom with the appropriate information.
892 * This function will return @c NULL in case of error. If the last error is @c
893 * LLDPCTL_ERR_WOULDBLOCK, the write should be retried later with the exact same
894 * parameters. LLDPCTL_ERR_BAD_VALUE is raised when the provided atom is not
897 lldpctl_atom_t
*lldpctl_atom_set_int(lldpctl_atom_t
*atom
, lldpctl_key_t key
,
901 * @defgroup liblldpctl_atom_iter Iterating over atoms
903 * Iterate over atoms (lists).
908 * Iterator over an iterable atom (a list of ports, a list of VLAN, ...). When
909 * an atom is a list, it can be iterated over to extract the appropriate values.
911 * @see lldpctl_atom_iter(), lldpctl_atom_iter_next(), lldpctl_atom_iter_value()
913 typedef struct lldpctl_atom_iter_t lldpctl_atom_iter_t
;
916 * Return an iterator over a given atom.
918 * If an atom is iterable (if it is a list, like a list of ports, a list of
919 * VLAN, a list of neighbors), it is possible to iterate over it. First use this
920 * function to get an iterator then use @c lldpctl_atom_iter_next() to get the
921 * next item and @c lldpctl_atom_iter_value() to the actuel item.
923 * @param atom The atom we want to create an iterator from.
924 * @return The iterator or @c NULL if an error happened or if the atom is empty
925 * (check with @c lldpctl_last_error()).
927 * As a convenience, if the provided atom is @c NULL, this function will return
928 * @c NULL and no error will be raised.
930 lldpctl_atom_iter_t
*lldpctl_atom_iter(lldpctl_atom_t
*atom
);
933 * Return the next element of an iterator.
935 * @param atom The atom we are currently iterating.
936 * @param iter The iterator we want the next element from.
937 * @return An iterator starting on the next element or @c NULL if we have no
940 * @see lldpctl_atom_iter(), lldpctl_atom_iter_value().
942 * As a convenience, if the provided atom is @c NULL, this function will return
943 * @c NULL and no error will be raised.
945 lldpctl_atom_iter_t
*lldpctl_atom_iter_next(lldpctl_atom_t
*atom
, lldpctl_atom_iter_t
*iter
);
948 * Return the value of an iterator.
950 * @param atom The atom we are currently iterating.
951 * @param iter The iterator we want the next element from.
952 * @return The atom currently associated with the iterator.
954 * @see lldpctl_atom_iter(), lldpctl_atom_iter_next().
956 lldpctl_atom_t
*lldpctl_atom_iter_value(lldpctl_atom_t
*atom
, lldpctl_atom_iter_t
*iter
);
959 * Convenience macro to iter over every value of an iterable object.
961 * @param atom The atom you want to iterate on.
962 * @param value Atom that will be used to contain each value.
964 * This macro behaves as a for loop. Moreover, at the end of each iteration,
965 * value is deallocated. Don't use it outside of the loop!
967 #define lldpctl_atom_foreach(atom, value) \
968 for (lldpctl_atom_iter_t *iter = lldpctl_atom_iter(atom); \
969 iter && (value = lldpctl_atom_iter_value(atom, iter)); \
970 iter = lldpctl_atom_iter_next(atom, iter), \
971 lldpctl_atom_dec_ref(value))
974 * Create a new value for an iterable element.
976 * The value is meant to be appended using @c lldpctl_atom_set(). Currently,
977 * there is no way to delete an element from a list. It is also not advisable to
978 * use getters on a newly created object until it is fully initialized. If its
979 * internal representation is using a buffer, it may not be initialized until
982 * @param atom The atom we want to create a new element for.
983 * @return The new element.
985 lldpctl_atom_t
*lldpctl_atom_create(lldpctl_atom_t
*atom
);