]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add wrapper structure and update mod_read()
authorAlan T. DeKok <aland@freeradius.org>
Sat, 11 Mar 2023 22:51:31 +0000 (17:51 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 14 Mar 2023 21:10:37 +0000 (17:10 -0400)
so that we can signal the decoder and process code what kind of
packet we're getting:

* recv real packet, but no reply

* send real packet, but no request

* timeout state change

src/listen/bfd/proto_bfd.c
src/listen/bfd/proto_bfd_udp.c
src/listen/bfd/session.c
src/listen/bfd/session.h

index 4e70cffeb2529c2041329c22b66b560d1d07472f..530e0087e2851c18817434378e205b67b831afe9 100644 (file)
@@ -180,8 +180,10 @@ static int mod_decode(void const *instance, request_t *request, uint8_t *const d
        proto_bfd_t const       *inst = talloc_get_type_abort_const(instance, proto_bfd_t);
        fr_io_track_t const     *track = talloc_get_type_abort_const(request->async->packet_ctx, fr_io_track_t);
        fr_io_address_t const   *address = track->address;
-       fr_client_t const               *client;
+       fr_client_t const       *client;
        fr_pair_t               *vp, *reply, *my, *your;
+       bfd_wrapper_t const     *wrapper = (bfd_wrapper_t const *) data;
+       bfd_packet_t const      *packet = (bfd_packet_t const *) wrapper->packet;
 
        /*
         *      Set the request dictionary so that we can do
@@ -195,8 +197,8 @@ static int mod_decode(void const *instance, request_t *request, uint8_t *const d
        /*
         *      Hacks for now until we have a lower-level decode routine.
         */
-       request->packet->code = data[1] >> 6;
-       request->packet->id = fr_nbo_to_uint32(data + 4);
+       request->packet->code = packet->state;
+       request->packet->id = fr_nbo_to_uint32((uint8_t const *) &packet->my_disc);
        request->reply->id = request->packet->id;
 
        request->packet->data = talloc_memdup(request->packet, data, data_len);
@@ -208,7 +210,7 @@ static int mod_decode(void const *instance, request_t *request, uint8_t *const d
         *      transport, via a call to fr_radius_ok().
         */
        if (fr_bfd_decode(request->request_ctx, &request->request_pairs,
-                         request->packet->data, request->packet->data_len,
+                         (uint8_t const *) packet, packet->length,
                          client->secret, talloc_array_length(client->secret) - 1) < 0) {
                RPEDEBUG("Failed decoding packet");
                return -1;
index ee76f90c13536e3d807df6f3162967701699136c..010d13e45cf4087c4861eb6a76b789da1a348b2b 100644 (file)
@@ -117,9 +117,10 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
        ssize_t                         data_size;
        size_t                          packet_len;
 
-       bfd_packet_t                    *packet;
+       bfd_packet_t                    *packet;
        char const                      *err = NULL;
        bfd_state_change_t              state_change;
+       bfd_wrapper_t                   *wrapper = (bfd_wrapper_t *) buffer;
 
        *leftover = 0;          /* always for UDP */
 
@@ -135,7 +136,7 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
         */
        flags = UDP_FLAGS_CONNECTED * (thread->connection != NULL);
 
-       data_size = udp_recv(thread->sockfd, flags, &address->socket, buffer, buffer_len, recv_time_p);
+       data_size = udp_recv(thread->sockfd, flags, &address->socket, wrapper->packet, buffer_len - (wrapper->packet - buffer), recv_time_p);
        if (data_size < 0) {
                PDEBUG2("proto_bfd_udp got read error");
                return data_size;
@@ -159,15 +160,14 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
 
        packet_len = data_size;
 
-       if (!fr_bfd_packet_ok(&err, buffer, packet_len)) {
+       if (!fr_bfd_packet_ok(&err, wrapper->packet, packet_len)) {
                DEBUG2("proto_bfd_udp - Received invalid packet on %s - %s", thread->name, err);
                thread->stats.total_malformed_requests++;
                return 0;
        }
 
        thread->stats.total_requests++;
-
-       packet = (bfd_packet_t *) buffer;
+       packet = (bfd_packet_t *) wrapper->packet;
 
        /*
         *      Print out what we received.
@@ -182,11 +182,15 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
         *      we either send the packet through to unlang, or not.
         */
        state_change = bfd_session_process((bfd_session_t *) client, packet);
+
        if ((state_change == BFD_STATE_CHANGE_INVALID) || (state_change == BFD_STATE_CHANGE_ADMIN_DOWN)) return 0;
 
        if ((state_change == BFD_STATE_CHANGE_NONE) && inst->only_state_changes) return 0;
 
-       return packet_len;
+       wrapper->type = BFD_WRAPPER_RECV_PACKET;
+       wrapper->state_change = state_change;
+
+       return sizeof(wrapper) + packet_len;
 }
 
 static ssize_t mod_write(fr_listen_t *li, void *packet_ctx, UNUSED fr_time_t request_time,
@@ -496,6 +500,7 @@ static void mod_event_list_set(fr_listen_t *li, fr_event_list_t *el, void *nr)
                if (peer->inst != inst) continue;
 
                peer->el = el;
+               peer->listen = li;
                peer->nr = (fr_network_t *) nr;
                peer->sockfd = thread->sockfd;
 
index e95eec13cc3023179db59ecba0bbb400dae455ea..d1ea0423c9fb4378ed929b67e799da859c5f1eda 100644 (file)
@@ -55,20 +55,20 @@ static void bfd_set_timeout(bfd_session_t *session, fr_time_t when);
  *     we will need to define some other kind of fake packet to send to the process
  *     module.
  */
-static void bfd_trigger(bfd_session_t *session, UNUSED bfd_state_change_t change)
+static void bfd_trigger(bfd_session_t *session, bfd_state_change_t change)
 {
-//     fr_radius_packet_t      packet;
-//     request_t               *request = request_local_alloc_external(session, NULL);
-       char                    buffer[256];
+       bfd_wrapper_t wrapper;
 
-       snprintf(buffer, sizeof(buffer), "server.bfd.%s",
-                fr_bfd_packet_names[session->session_state]);
+       wrapper.type = BFD_WRAPPER_STATE_CHANGE;
+       wrapper.state_change = change;
+       wrapper.session = session;
 
-       DEBUG("BFD %s trigger %s", session->client.shortname, buffer);
-
-//     bfd_request(session, request, &packet);
+       /*
+        *      @todo - proto_bfd.c mod_decode() has to be updated to check for non-existent packet,
+        *      and create a fake "timeout" packet with appropriate data.
+        */
 
-//     trigger_exec(unlang_interpret_get_thread_default(), NULL, buffer, false, NULL);
+//     (void) fr_network_listen_inject(session->nr, session->listen, (uint8_t const *) &wrapper, sizeof(wrapper), fr_time());
 }
 
 /*
index 87021ef278a9a7fcd002fd23ed87aa8118bea55f..7bfab9b02ad556d34a8055ddb15c4a7a707f71b7 100644 (file)
@@ -135,14 +135,10 @@ typedef enum {
 } bfd_state_change_t;
 
 typedef struct {
-       uint32_t        type;
-       union {
-               bfd_packet_t                    packet;
-               struct {
-                       bfd_state_change_t      state_change;
-                       bfd_session_t   *peer;
-               };
-       };
+       uint32_t                type;
+       bfd_state_change_t      state_change;
+       bfd_session_t           *session;
+       uint8_t                 packet[];
 } bfd_wrapper_t;
 
 int    bfd_session_init(bfd_session_t *session);