]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Retrieve the real client in mod_encode and mod_decode
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 21 Jun 2017 20:47:50 +0000 (16:47 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 21 Jun 2017 20:49:57 +0000 (16:49 -0400)
src/lib/io/io.h
src/lib/io/listen.h
src/lib/io/network.c
src/lib/io/worker.c
src/modules/proto_radius/proto_radius.c
src/modules/proto_radius/proto_radius.h
src/modules/proto_radius/proto_radius_udp.c
src/tests/util/radius1_test.c
src/tests/util/radius_schedule_test.c
src/tests/util/worker_test.c

index feaf19df5b93055845750cf3bcc84fb11ebfb9b4..dd0865a1bfb438f4aa9760e882348a268dab3324 100644 (file)
@@ -35,6 +35,8 @@ RCSIDH(transport_h, "$Id$")
 extern "C" {
 #endif
 
+typedef struct fr_listen fr_listen_t;
+
 /**
  *  Tell an async process function if it should run or exit.
  */
@@ -92,16 +94,14 @@ typedef int (*fr_io_get_fd_t)(void const *instance);
  *  know anything about how the data will be used (e.g. authorize,
  *  authenticate, etc. for Access-Request)
  *
- *
  * @param[in] data             the raw packet data
  * @param[in] data_len         the length of the raw data
- * @param[in,out] request      where the decoded VPs should be placed.
- * @param[in] instance         the context for this function.
+ * @param[in] request          where the decoded VPs should be placed.
  * @return
  *     - <0 on error
  *     - 0 on success
  */
-typedef int (*fr_io_decode_t)(void const *instance, REQUEST *request, uint8_t *const data, size_t data_len);
+typedef int (*fr_io_decode_t)(REQUEST *request, uint8_t *const data, size_t data_len);
 
 /** Encode data from a REQUEST into a raw packet.
  *
@@ -113,16 +113,14 @@ typedef int (*fr_io_decode_t)(void const *instance, REQUEST *request, uint8_t *c
  *  know anything about how the data will be used (e.g. reject delay
  *  on Access-Reject)
  *
-
- * @param[in,out]              request where the VPs to be encoded are located
- * @param[in] buffer           the buffer where the raw packet will be written
+ * @param[in] request          request where the VPs to be encoded are located
+ * @param[out] buffer          the buffer where the raw packet will be written
  * @param[in] buffer_len       the length of the buffer
- * @param[in] instance         the context for this function.
  * @return
  *     - <0 on error
  *     - >=0 length of the encoded data in the buffer, will be <=buffer_len
  */
-typedef ssize_t (*fr_io_encode_t)(void const *instance, REQUEST *request, uint8_t *buffer, size_t buffer_len);
+typedef ssize_t (*fr_io_encode_t)(REQUEST *request, uint8_t *buffer, size_t buffer_len);
 
 /** NAK a packet.
  *
@@ -210,15 +208,16 @@ typedef ssize_t (*fr_io_data_read_t)(void const *instance, void **packet_ctx, ui
  *  need to call me again at a later point".
  *
  * @param[in] instance         the context for this function
- * @param[in] request_time     when the original request was received
  * @param[in] packet_ctx       Request specific data.
- * @param[in] buffer   the buffer where the raw packet will be written from
+ * @param[in] request_time     when the original request was received
+ * @param[in] buffer           the buffer where the raw packet will be written from
  * @param[in] buffer_len       the length of the buffer
  * @return
  *     - <0 on error
  *     - >=0 length of the data read or written.
  */
-typedef ssize_t (*fr_io_data_write_t)(void const *instance, fr_time_t request_time, void *packet_ctx, uint8_t *buffer, size_t buffer_len);
+typedef ssize_t (*fr_io_data_write_t)(void const *instance, void *packet_ctx, fr_time_t request_time,
+                                     uint8_t *buffer, size_t buffer_len);
 
 /**  Handle a close or error on the socket.
  *
index 10d9470c86be9d4ea27efc82e731acd5e7d191ed..24cd0750b2cfc8342af035fb24aa6af9bd1641c4 100644 (file)
@@ -50,7 +50,7 @@ struct fr_async_t {
 
        uint32_t                priority;
        void                    *packet_ctx;
-       fr_listen_t const       *io;            //!< How we received this request,
+       fr_listen_t const       *listen;        //!< How we received this request,
                                                //!< and how we'll send the reply.
 };
 #endif
index 78ed51b29336cacee4e62d11368c7a64f1d5c45a..d9b075a055f25b9931c3b1fb6aa7e0d2f13242e1 100644 (file)
@@ -748,8 +748,8 @@ static void fr_network_post_event(UNUSED fr_event_list_t *el, UNUSED struct time
                 *      the reply is a NAK, don't write it to the
                 *      network.
                 */
-               rcode = listen->app_io->write(listen->app_io_instance, cd->reply.request_time,
-                                             cd->packet_ctx, cd->m.data, cd->m.data_size);
+               rcode = listen->app_io->write(listen->app_io_instance, cd->packet_ctx,
+                                             cd->reply.request_time, cd->m.data, cd->m.data_size);
                if (rcode < 0) {
                        fr_network_socket_t my_socket, *s;
 
index 14fb369a68bafa200dcaad7b6316654fb9253339..2306659abce1400f30264b4053bc600687fbc595 100644 (file)
@@ -424,8 +424,7 @@ static void fr_worker_send_reply(fr_worker_t *worker, REQUEST *request, size_t s
        if (size) {
                ssize_t encoded;
 
-               encoded = request->async->io->encode(request->async->io->app_io_instance,
-                                                    request, reply->m.data, reply->m.rb_size);
+               encoded = request->async->listen->encode(request, reply->m.data, reply->m.rb_size);
                if (encoded < 0) {
                        fr_log(worker->log, L_DBG, "\t%sfails encode", worker->name);
                        encoded = 0;
@@ -453,7 +452,7 @@ static void fr_worker_send_reply(fr_worker_t *worker, REQUEST *request, size_t s
        reply->reply.processing_time = request->async->tracking.running;
        reply->reply.request_time = request->async->recv_time;
 
-       reply->listen = request->async->io;
+       reply->listen = request->async->listen;
        reply->packet_ctx = request->async->packet_ctx;
 
        fr_log(worker->log, L_DBG, "(%"PRIu64") finished, sending reply", request->number);
@@ -699,16 +698,16 @@ static REQUEST *fr_worker_get_request(fr_worker_t *worker, fr_time_t now)
        request->async->el = worker->el;
        request->number = worker->number++;
 
-       request->async->io = cd->listen;
+       request->async->listen = cd->listen;
        request->async->packet_ctx = cd->packet_ctx;
-       listen = request->async->io;
+       listen = request->async->listen;
 
        /*
         *      Now that the "request" structure has been initialized, go decode the packet.
         *
         *      Note that this also sets the "async process" function.
         */
-       rcode = listen->decode(listen->app_io_instance, request, cd->m.data, cd->m.data_size);
+       rcode = listen->decode(request, cd->m.data, cd->m.data_size);
        if (rcode < 0) {
                fr_log(worker->log, L_DBG, "\t%sFAILED decode of request %"PRIu64, worker->name, request->number);
                talloc_free(ctx);
@@ -850,7 +849,7 @@ static void fr_worker_run_request(fr_worker_t *worker, REQUEST *request)
                return;
 
        case FR_IO_REPLY:
-               size = request->async->io->app_io->default_message_size;
+               size = request->async->listen->app_io->default_message_size;
                break;
        }
 
@@ -1179,7 +1178,7 @@ static void fr_worker_post_event(UNUSED fr_event_list_t *el, UNUSED struct timev
        if (!request) return;
 
        rad_assert(request->async->process != NULL);
-       rad_assert(request->async->io != NULL);
+       rad_assert(request->async->listen != NULL);
 
        /*
         *      Run the request, and either track it as
index 31ca343f56c3e7c1a6f04a808aaf3142f1c71034..ded87d85516fc1f7f55a227731eee566ecfae6ad 100644 (file)
@@ -145,16 +145,16 @@ static int transport_parse(TALLOC_CTX *ctx, void *out, CONF_ITEM *ci, UNUSED CON
 /** Decode the packet, and set the request->process function
  *
  */
-static int mod_decode(UNUSED void const *instance, REQUEST *request,
-                     uint8_t *const data, size_t data_len)
+static int mod_decode(REQUEST *request, uint8_t *const data, size_t data_len)
 {
-//     proto_radius_t *ctx = instance;
-       char *secret;
-
-       if (fr_radius_verify(data, NULL, (uint8_t const *) "testing123", 10) < 0) return -1;
+       proto_radius_t const *inst = talloc_get_type_abort(request->async->listen->app_instance, proto_radius_t);
+       RADCLIENT *client;
 
        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.
         */
@@ -166,32 +166,30 @@ static int mod_decode(UNUSED void const *instance, REQUEST *request,
        request->packet->data = talloc_memdup(request->packet, data, data_len);
        request->packet->data_len = data_len;
 
-
-       secret = talloc_strdup(request, "testing123");
-
-       if (fr_radius_packet_decode(request->packet, NULL, secret) < 0) {
+       if (fr_radius_packet_decode(request->packet, NULL, client->secret) < 0) {
                RDEBUG("Failed decoding packet: %s", fr_strerror());
                return -1;
        }
 
-//     request->async_process = ctx->process[data[0]];
-
        return 0;
 }
 
-static ssize_t mod_encode(UNUSED void const *instance, REQUEST *request,
-                         uint8_t *buffer, size_t buffer_len)
+static ssize_t mod_encode(REQUEST *request, uint8_t *buffer, size_t buffer_len)
 {
-//     proto_radius_ctx_t *inst = instance;
        size_t len;
-       char *secret = talloc_strdup(request, "testing123");
 
-       if (fr_radius_packet_encode(request->reply, request->packet, secret) < 0) {
+       proto_radius_t const *inst = talloc_get_type_abort(request->async->listen->app_instance, proto_radius_t);
+       RADCLIENT *client;
+
+       client = inst->app_io_private->client(inst->app_io, request->async->packet_ctx);
+       rad_assert(client);
+
+       if (fr_radius_packet_encode(request->reply, request->packet, client->secret) < 0) {
                RDEBUG("Failed encoding RADIUS reply: %s", fr_strerror());
                return -1;
        }
 
-       if (fr_radius_packet_sign(request->reply, request->packet, secret) < 0) {
+       if (fr_radius_packet_sign(request->reply, request->packet, client->secret) < 0) {
                RDEBUG("Failed signing RADIUS reply: %s", fr_strerror());
                return -1;
        }
index 6c7e83c67ac0bf2ba86502150e606300ba781d0f..247403be81ea99c4f63faefd673ba2b6884c1eef 100644 (file)
@@ -49,8 +49,7 @@ typedef int (*proto_radius_addr_get_t)(fr_socket_addr_t *sockaddr,
  */
 typedef struct {
        proto_radius_client_get_t       client;                         //!< Retrieve the client the packet was
-                                                                       ///< received from if any.
-                                                                       ///< NULL field indicates unsupported.
+                                                                       ///< received from.
 
        proto_radius_addr_get_t         src;                            //!< Retrieve the src address of the packet.
        proto_radius_addr_get_t         dst;                            //!< Retrieve the dst address of the packet.
index aa240141b4350a8d88dc181039854825e9ab1d39..f85b05e0e71ef892da9be7a1673f8d9bb9df6427 100644 (file)
@@ -192,8 +192,6 @@ static ssize_t mod_read(void const *instance, void **packet_ctx, uint8_t *buffer
                return 0;
        }
 
-
-
        tracking_status = fr_radius_tracking_entry_insert(&track, inst->ft, buffer, address.timestamp, &address);
        switch (tracking_status) {
        case FR_TRACKING_ERROR:
@@ -233,7 +231,8 @@ static ssize_t mod_read(void const *instance, void **packet_ctx, uint8_t *buffer
        return packet_len;
 }
 
-static ssize_t mod_write(void const *instance, fr_time_t request_time, void *packet_ctx, uint8_t *buffer, size_t buffer_len)
+static ssize_t mod_write(void const *instance, void *packet_ctx,
+                        fr_time_t request_time, uint8_t *buffer, size_t buffer_len)
 {
        proto_radius_udp_t const        *inst = talloc_get_type_abort(instance, proto_radius_udp_t);
        fr_tracking_entry_t             *track = packet_ctx;
index 7963109584e6eb7369ce41b4bc761df901ab1470..b601c7dfe5c8d64986d5e2fe8c16a9eb4b1889df 100644 (file)
@@ -96,9 +96,10 @@ static fr_io_final_t test_process(REQUEST *request, fr_io_action_t action)
 }
 
 
-static int test_decode(void const *instance, REQUEST *request, uint8_t *const data, size_t data_len)
+static int test_decode(REQUEST *request, uint8_t *const data, size_t data_len)
 {
-       fr_radius_packet_ctx_t const *pc = talloc_get_type_abort(instance, fr_radius_packet_ctx_t);
+       fr_radius_packet_ctx_t const *pc = talloc_get_type_abort(request->async->listen->app_instance,
+                                                                fr_radius_packet_ctx_t);
 
        request->number = pc->id;
        request->async->process = test_process;
@@ -110,10 +111,11 @@ static int test_decode(void const *instance, REQUEST *request, uint8_t *const da
        return 0;
 }
 
-static ssize_t test_encode(void const *instance, REQUEST *request, uint8_t *buffer, size_t buffer_len)
+static ssize_t test_encode(REQUEST *request, uint8_t *buffer, size_t buffer_len)
 {
        FR_MD5_CTX context;
-       fr_radius_packet_ctx_t const *pc = talloc_get_type_abort(instance, fr_radius_packet_ctx_t);
+       fr_radius_packet_ctx_t const *pc = talloc_get_type_abort(request->async->listen->app_instance,
+                                                                fr_radius_packet_ctx_t);
 
        MPRINT1("\t\tENCODE >>> request %"PRIu64" - data %p %p room %zd\n",
                request->number, pc, buffer, buffer_len);
index 4cbdb12bce83cbba33c8867eb6d483847cea3283..be1029fabd6772cf618951129ccc12581458ee73 100644 (file)
@@ -70,9 +70,9 @@ static fr_io_final_t test_process(REQUEST *request, fr_io_action_t action)
        return FR_IO_REPLY;
 }
 
-static int test_decode(void const *instance, REQUEST *request, uint8_t *const data, size_t data_len)
+static int test_decode(REQUEST *request, uint8_t *const data, size_t data_len)
 {
-       fr_listen_test_t const *pc = instance;
+       fr_listen_test_t const *pc = request->async->listen->app_instance;
 
        request->async->process = test_process;
 
@@ -83,10 +83,10 @@ static int test_decode(void const *instance, REQUEST *request, uint8_t *const da
        return 0;
 }
 
-static ssize_t test_encode(void const *instance, REQUEST *request, uint8_t *buffer, size_t buffer_len)
+static ssize_t test_encode(REQUEST *request, uint8_t *buffer, size_t buffer_len)
 {
        FR_MD5_CTX context;
-       fr_listen_test_t const *pc = instance;
+       fr_listen_test_t const *pc = request->async->listen->app_instance;
 
        MPRINT1("\t\tENCODE >>> request %"PRIu64"- data %p %p room %zd\n", request->number, pc, buffer, buffer_len);
 
@@ -150,7 +150,8 @@ static ssize_t test_read(void const *ctx, UNUSED void **packet_ctx, uint8_t *buf
 }
 
 
-static ssize_t test_write(void const *ctx, UNUSED fr_time_t request_time, UNUSED void *packet_ctx, uint8_t *buffer, size_t buffer_len)
+static ssize_t test_write(void const *ctx, UNUSED void *packet_ctx,  UNUSED fr_time_t request_time,
+                         uint8_t *buffer, size_t buffer_len)
 {
        ssize_t                 data_size;
        fr_listen_test_t        *io_ctx = talloc_get_type_abort(ctx, fr_listen_test_t);
index 8c41cce991041adc69b3cf1469c9b111bbb30126..acc3f67e829858192da1ee94607c0d22158445e9 100644 (file)
@@ -105,7 +105,7 @@ static fr_io_final_t test_process(REQUEST *request, fr_io_action_t action)
        return FR_IO_REPLY;
 }
 
-static int test_decode(void const *packet_ctx, REQUEST *request, uint8_t *const data, size_t data_len)
+static int test_decode(REQUEST *request, uint8_t *const data, size_t data_len)
 {
        uint32_t number;
 
@@ -117,13 +117,15 @@ static int test_decode(void const *packet_ctx, REQUEST *request, uint8_t *const
 
        request->async->process = test_process;
 
-       MPRINT1("\t\tDECODE <<< request %"PRIu64" - %p data %p size %zd\n", request->number, packet_ctx, data, data_len);
+       MPRINT1("\t\tDECODE <<< request %"PRIu64" - %p data %p size %zd\n", request->number,
+               request->async->packet_ctx, data, data_len);
        return 0;
 }
 
-static ssize_t test_encode(void const *instance, REQUEST *request, uint8_t *const data, size_t data_len)
+static ssize_t test_encode(REQUEST *request, uint8_t *const data, size_t data_len)
 {
-       MPRINT1("\t\tENCODE >>> request %"PRIu64" - data %p %p size %zd\n", request->number, instance, data, data_len);
+       MPRINT1("\t\tENCODE >>> request %"PRIu64" - data %p %p size %zd\n", request->number,
+               request->async->listen->app_instance, data, data_len);
 
        return data_len;
 }