]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lib: stricly-prevent use of a connection used to watch events
authorVincent Bernat <vincent@bernat.ch>
Fri, 24 Apr 2020 17:09:42 +0000 (19:09 +0200)
committerVincent Bernat <vincent@bernat.ch>
Fri, 24 Apr 2020 17:15:13 +0000 (19:15 +0200)
The protocol cannot handle received events mixed with regular
messages. Once the connection is dedicated to a watch, prevent its
regular use.

src/lib/atom.c
src/lib/atom.h
src/lib/connection.c
src/lib/lldpctl.h

index f81d3bbc207c02c8aaa9b49297b1bbc4a7a19272..54e94ae63c1c214dbc8e0f2a4c75665e7ac18682 100644 (file)
@@ -314,6 +314,10 @@ _lldpctl_do_something(lldpctl_conn_t *conn,
 {
        ssize_t rc;
 
+       if (conn->state == CONN_STATE_WATCHING)
+               /* The connection cannot be used anymore. */
+               return SET_ERROR(conn, LLDPCTL_ERR_INVALID_STATE);
+
        if (conn->state == CONN_STATE_IDLE) {
                /* We need to build the message to send, then send
                 * it. */
@@ -371,6 +375,7 @@ lldpctl_watch_callback(lldpctl_conn_t *conn,
        if (rc == 0) {
                conn->watch_cb = cb;
                conn->watch_data = data;
+               conn->state = CONN_STATE_WATCHING;
        }
        return rc;
 }
@@ -382,7 +387,7 @@ lldpctl_watch(lldpctl_conn_t *conn)
 
        RESET_ERROR(conn);
 
-       if (conn->state != CONN_STATE_IDLE)
+       if (conn->state != CONN_STATE_WATCHING)
                return SET_ERROR(conn, LLDPCTL_ERR_INVALID_STATE);
 
        conn->watch_triggered = 0;
index a818f7ac967541349f1ad78bfdcad85b40be5c98..d6ae218fafbdeb93796f85d0d394984df97de641 100644 (file)
@@ -54,6 +54,7 @@ struct lldpctl_conn_t {
 #define CONN_STATE_GET_CHASSIS_RECV    14
 #define CONN_STATE_GET_DEFAULT_PORT_SEND 15
 #define CONN_STATE_GET_DEFAULT_PORT_RECV 16
+#define CONN_STATE_WATCHING            17
        int state;              /* Current state */
        /* Data attached to the state. It is used to check that we are using the
         * same data as a previous call until the state machine goes to
index aa88dadf2a6edaa80ae596456c5d5da2e627f2f7..191d7c6a02f04aaf6c5c5d681e638ee68cf99aa0 100644 (file)
@@ -262,7 +262,8 @@ lldpctl_recv(lldpctl_conn_t *conn, const uint8_t *data, size_t length)
        return conn->input_buffer_len;
 }
 
-int lldpctl_process_conn_buffer(lldpctl_conn_t *conn)
+int
+lldpctl_process_conn_buffer(lldpctl_conn_t *conn)
 {
        int rc;
 
index c9f4d474f17965595f700ed8289c9d5eb98e5d95..2369bf20fed449c7544e7e37ebbbbbc28f9358a7 100644 (file)
@@ -471,7 +471,8 @@ typedef enum {
  * reference to it, be sure to increment the reference count in the callback.
  *
  * @warning The provided connection should not be used at all. Do not use @c
- * lldpctl_atom_set_*() functions on @c interface or @c neighbor either.
+ * lldpctl_atom_set_*() functions on @c interface or @c neighbor either. If you
+ * do, you will get a @c LLDPCTL_ERR_INVALID_STATE error.
  *
  * @see lldpctl_watch_callback
  */
@@ -494,7 +495,8 @@ typedef void (*lldpctl_change_callback)(lldpctl_conn_t *conn,
  * LLDPCTL_ERR_WOULDBLOCK.
  *
  * @warning Once a callback is registered, the connection shouldn't be used for
- * anything else than receiving notifications.
+ * anything else than receiving notifications. If you do, you will get a @c
+ * LLDPCTL_ERR_INVALID_STATE error.
  */
 int lldpctl_watch_callback(lldpctl_conn_t *conn,
     lldpctl_change_callback cb,