]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
almost have encode/decode working
authorAlan T. DeKok <aland@freeradius.org>
Wed, 28 Jun 2017 20:03:29 +0000 (16:03 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 29 Jun 2017 12:34:36 +0000 (08:34 -0400)
src/modules/proto_vmps/proto_vmps.c
src/modules/proto_vmps/proto_vmps.h
src/modules/proto_vmps/proto_vmps_udp.c

index 66247b5d0dbd600de7b6a9cf94d1c3ecee93c002..ea69b4c6a7b7a274c8972ad1f71e174940cbdb8e 100644 (file)
@@ -114,72 +114,21 @@ static int transport_parse(TALLOC_CTX *ctx, void *out, CONF_ITEM *ci, UNUSED CON
        return dl_instance(ctx, out, transport_cs, parent_inst, name, DL_TYPE_SUBMODULE);
 }
 
-/** Decode the packet, and set the request->process function
+/** Decode the packet.
  *
  */
 static int mod_decode(void const *instance, REQUEST *request, uint8_t *const data, size_t data_len)
 {
        proto_vmps_t const *inst = talloc_get_type_abort(instance, proto_vmps_t);
 
-#if 0
-       rad_assert(data[0] < FR_MAX_PACKET_CODE);
-
-       client = inst->app_io_private->client(inst->app_io, request->async->packet_ctx);
-       rad_assert(client);
-
-       /*
-        *      Hacks for now until we have a lower-level decode routine.
-        */
-       request->packet->code = data[0];
-       request->packet->id = data[1];
-       request->reply->id = data[1];
-       memcpy(request->packet->vector, data + 4, sizeof(request->packet->vector));
-
-       request->packet->data = talloc_memdup(request->packet, data, data_len);
-       request->packet->data_len = data_len;
-
-       if (fr_vmps_packet_decode(request->packet, NULL, client->secret) < 0) {
-               RDEBUG("Failed decoding packet: %s", fr_strerror());
-               return -1;
-       }
-#endif
-
-       /*
-        *      Let the app_io take care of populating additional fields in the request
-        */
        return inst->app_io->decode(inst->app_io_instance, request, data, data_len);
 }
 
-static ssize_t mod_encode(UNUSED void const *instance, UNUSED REQUEST *request, UNUSED uint8_t *buffer, UNUSED size_t buffer_len)
+static ssize_t mod_encode(void const *instance, REQUEST *request, uint8_t *buffer, size_t buffer_len)
 {
-#if 0
-       size_t len;
-
        proto_vmps_t const *inst = talloc_get_type_abort(instance, proto_vmps_t);
-       RADCLIENT *client;
-
-       client = inst->app_io_private->client(inst->app_io, request->async->packet_ctx);
-       rad_assert(client);
-
-       if (fr_vmps_packet_encode(request->reply, request->packet, client->secret) < 0) {
-               RDEBUG("Failed encoding VMPS reply: %s", fr_strerror());
-               return -1;
-       }
-
-       if (fr_vmps_packet_sign(request->reply, request->packet, client->secret) < 0) {
-               RDEBUG("Failed signing VMPS reply: %s", fr_strerror());
-               return -1;
-       }
-
-       len = request->reply->data_len;
-       if (buffer_len < len) len = buffer_len;
-
-       memcpy(buffer, request->reply->data, len);
-
-       return len;
-#endif
 
-       return -1;
+       return inst->app_io->encode(inst->app_io_instance, request, buffer, buffer_len);
 }
 
 static void mod_process_set(void const *instance, REQUEST *request)
index 952a07078c1e7da628cc55b8344ff4a001cdef1a..279be5de9bf7677c7196d48a7fa4d837a5ecd01e 100644 (file)
@@ -51,9 +51,6 @@ typedef int (*proto_vmps_addr_get_t)(fr_socket_addr_t *sockaddr,
  * Should only be used by the proto_vmps module, and submodules.
  */
 typedef struct {
-       proto_vmps_client_get_t client;                         //!< Retrieve the client the packet was
-                                                                       ///< received from.
-
        proto_vmps_addr_get_t           src;                            //!< Retrieve the src address of the packet.
        proto_vmps_addr_get_t           dst;                            //!< Retrieve the dst address of the packet.
 } proto_vmps_app_io_t;
index 2ffd5800525f63b609f8c8ace1986107aa67650c..fca1a912d8de55d1184ed9da0b4d7aa5830acd64 100644 (file)
 #include <freeradius-devel/rad_assert.h>
 #include "proto_vmps.h"
 
-typedef struct {
-       int                             if_index;
-
-       fr_ipaddr_t                     src_ipaddr;
-       fr_ipaddr_t                     dst_ipaddr;
-       uint16_t                        src_port;
-       uint16_t                        dst_port;
-
-       fr_time_t                       timestamp;
-
-       RADCLIENT                       *client;
-} proto_vmps_udp_address_t;
-
 typedef struct {
        proto_vmps_t    const           *parent;                //!< The module that spawned us!
 
        int                             sockfd;
 
-       fr_event_list_t                 *el;                    //!< for cleanup timers on Access-Request
-
        fr_ipaddr_t                     ipaddr;                 //!< Ipaddr to listen on.
 
        bool                            ipaddr_is_set;          //!< ipaddr config item is set.
@@ -66,9 +51,6 @@ typedef struct {
        uint32_t                        recv_buff;              //!< How big the kernel's receive buffer should be.
        bool                            recv_buff_is_set;       //!< Whether we were provided with a receive
                                                                //!< buffer value.
-
-//     fr_tracking_t                   *ft;                    //!< tracking table
-       uint32_t                        cleanup_delay;          //!< cleanup delay for Access-Request packets
 } proto_vmps_udp_t;
 
 static const CONF_PARSER udp_listen_config[] = {
@@ -82,8 +64,6 @@ static const CONF_PARSER udp_listen_config[] = {
        { FR_CONF_OFFSET("port", FR_TYPE_UINT16, proto_vmps_udp_t, port) },
        { FR_CONF_IS_SET_OFFSET("recv_buff", FR_TYPE_UINT32, proto_vmps_udp_t, recv_buff) },
 
-       { FR_CONF_OFFSET("cleanup_delay", FR_TYPE_UINT32, proto_vmps_udp_t, cleanup_delay), .dflt = "5" },
-
        CONF_PARSER_TERMINATOR
 };
 
@@ -93,15 +73,12 @@ static const CONF_PARSER udp_listen_config[] = {
  */
 static int mod_src_address(fr_socket_addr_t *src, UNUSED void const *instance, void const *packet_ctx)
 {
-       fr_tracking_entry_t const               *track = packet_ctx;
-       proto_vmps_udp_address_t const  *address = track->src_dst;
-
-       rad_assert(track->src_dst_size == sizeof(proto_vmps_udp_address_t));
+       fr_ip_srcdst_t const *ip = packet_ctx;
 
        memset(src, 0, sizeof(*src));
 
        src->proto = IPPROTO_UDP;
-       memcpy(&src->ipaddr, &address->src_ipaddr, sizeof(src->ipaddr));
+       memcpy(&src->ipaddr, &ip->src_ipaddr, sizeof(src->ipaddr));
 
        return 0;
 }
@@ -111,55 +88,54 @@ static int mod_src_address(fr_socket_addr_t *src, UNUSED void const *instance, v
  */
 static int mod_dst_address(fr_socket_addr_t *dst, UNUSED void const *instance, void const *packet_ctx)
 {
-       fr_tracking_entry_t const               *track = packet_ctx;
-       proto_vmps_udp_address_t const  *address = track->src_dst;
-
-       rad_assert(track->src_dst_size == sizeof(proto_vmps_udp_address_t));
+       fr_ip_srcdst_t const *ip = packet_ctx;
 
        memset(dst, 0, sizeof(*dst));
 
        dst->proto = IPPROTO_UDP;
-       memcpy(&dst->ipaddr, &address->dst_ipaddr, sizeof(dst->ipaddr));
+       memcpy(&dst->ipaddr, &ip->dst_ipaddr, sizeof(dst->ipaddr));
 
        return 0;
 }
 
-/** Return the client associated with the packet_ctx
+
+/** Decode the packet.
  *
  */
-static RADCLIENT *mod_client(UNUSED void const *instance, void const *packet_ctx)
+static int mod_decode(UNUSED void const *instance, REQUEST *request, uint8_t *const data, size_t data_len)
 {
-       fr_tracking_entry_t const               *track = packet_ctx;
-       proto_vmps_udp_address_t const  *address = track->src_dst;
+//     proto_vmps_udp_t const *inst = talloc_get_type_abort(instance, proto_vmps_udp_t);
+       fr_ip_srcdst_t *ip;
+       uint8_t *packet;
+       size_t packet_len;
 
-       rad_assert(track->src_dst_size == sizeof(proto_vmps_udp_address_t));
+       ip = talloc_memdup(request, request->async->packet_ctx, sizeof(*ip));
+       if (!ip) return -1;
 
-       return address->client;
-}
+       request->async->packet_ctx = ip;
 
-static int mod_decode(UNUSED void const *instance, REQUEST *request, UNUSED uint8_t *const data, UNUSED size_t data_len)
-{
+       packet = data + sizeof(*ip);
+       packet_len = data_len - sizeof(*ip);
 
-       fr_tracking_entry_t const               *track = request->async->packet_ctx;
-       proto_vmps_udp_address_t const  *address = track->src_dst;
+       // decode the packet into attributes.
 
-       rad_assert(track->src_dst_size == sizeof(proto_vmps_udp_address_t));
+       return 0;
+}
 
-       request->client = address->client;
-       request->packet->if_index = address->if_index;
-       request->packet->src_ipaddr = address->src_ipaddr;
-       request->packet->src_port = address->src_port;
-       request->packet->dst_ipaddr = address->dst_ipaddr;
-       request->packet->dst_port = address->dst_port;
+static ssize_t mod_encode(UNUSED void const *instance, REQUEST *request, uint8_t *buffer, size_t buffer_len)
+{
+//     proto_vmps_udp_t const *inst = talloc_get_type_abort(instance, proto_vmps_udp_t);
+       fr_ip_srcdst_t *ip;
+       uint8_t *packet;
+       size_t packet_len;
 
-       request->reply->if_index = address->if_index;
-       request->reply->src_ipaddr = address->dst_ipaddr;
-       request->reply->src_port = address->dst_port;
-       request->reply->dst_ipaddr = address->src_ipaddr;
-       request->reply->dst_port = address->src_port;
+       ip = request->async->packet_ctx;
+       packet = buffer + sizeof(*ip);
+       packet_len = buffer_len - sizeof(*ip);
+
+       memcpy(buffer, ip, sizeof(*ip));
 
-       request->root = &main_config;
-       VERIFY_REQUEST(request);
+       // encode packet in buffer
 
        return 0;
 }
@@ -311,15 +287,6 @@ static int mod_instantiate(void *instance, CONF_SECTION *cs)
                inst->port = ntohl(s->s_port);
        }
 
-       FR_INTEGER_BOUND_CHECK("cleanup_delay", inst->cleanup_delay, <=, 30);
-
-#if 0
-       inst->ft = fr_vmps_tracking_create(inst, sizeof(proto_vmps_udp_address_t), inst->parent->code_allowed);
-       if (!inst->ft) {
-               cf_log_err(cs, "Failed to create tracking table: %s", fr_strerror());
-       }
-#endif
-
        return 0;
 }
 
@@ -361,7 +328,6 @@ static int mod_detach(void *instance)
  */
 extern proto_vmps_app_io_t proto_vmps_app_io_private;
 proto_vmps_app_io_t proto_vmps_app_io_private = {
-       .client                 = mod_client,
        .src                    = mod_src_address,
        .dst                    = mod_dst_address
 };
@@ -380,6 +346,7 @@ fr_app_io_t proto_vmps_udp = {
        .open                   = mod_open,
        .read                   = mod_read,
        .decode                 = mod_decode,
+       .encode                 = mod_encode,
        .write                  = mod_write,
        .fd                     = mod_fd,
 };