From: Arran Cudbard-Bell Date: Wed, 21 Jun 2017 20:47:50 +0000 (-0400) Subject: Retrieve the real client in mod_encode and mod_decode X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8cddad29834cd67fa077672fafa6ff537dcad1e;p=thirdparty%2Ffreeradius-server.git Retrieve the real client in mod_encode and mod_decode --- diff --git a/src/lib/io/io.h b/src/lib/io/io.h index feaf19df5b9..dd0865a1bfb 100644 --- a/src/lib/io/io.h +++ b/src/lib/io/io.h @@ -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. * diff --git a/src/lib/io/listen.h b/src/lib/io/listen.h index 10d9470c86b..24cd0750b2c 100644 --- a/src/lib/io/listen.h +++ b/src/lib/io/listen.h @@ -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 diff --git a/src/lib/io/network.c b/src/lib/io/network.c index 78ed51b2933..d9b075a055f 100644 --- a/src/lib/io/network.c +++ b/src/lib/io/network.c @@ -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; diff --git a/src/lib/io/worker.c b/src/lib/io/worker.c index 14fb369a68b..2306659abce 100644 --- a/src/lib/io/worker.c +++ b/src/lib/io/worker.c @@ -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 diff --git a/src/modules/proto_radius/proto_radius.c b/src/modules/proto_radius/proto_radius.c index 31ca343f56c..ded87d85516 100644 --- a/src/modules/proto_radius/proto_radius.c +++ b/src/modules/proto_radius/proto_radius.c @@ -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; } diff --git a/src/modules/proto_radius/proto_radius.h b/src/modules/proto_radius/proto_radius.h index 6c7e83c67ac..247403be81e 100644 --- a/src/modules/proto_radius/proto_radius.h +++ b/src/modules/proto_radius/proto_radius.h @@ -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. diff --git a/src/modules/proto_radius/proto_radius_udp.c b/src/modules/proto_radius/proto_radius_udp.c index aa240141b43..f85b05e0e71 100644 --- a/src/modules/proto_radius/proto_radius_udp.c +++ b/src/modules/proto_radius/proto_radius_udp.c @@ -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; diff --git a/src/tests/util/radius1_test.c b/src/tests/util/radius1_test.c index 7963109584e..b601c7dfe5c 100644 --- a/src/tests/util/radius1_test.c +++ b/src/tests/util/radius1_test.c @@ -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); diff --git a/src/tests/util/radius_schedule_test.c b/src/tests/util/radius_schedule_test.c index 4cbdb12bce8..be1029fabd6 100644 --- a/src/tests/util/radius_schedule_test.c +++ b/src/tests/util/radius_schedule_test.c @@ -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); diff --git a/src/tests/util/worker_test.c b/src/tests/util/worker_test.c index 8c41cce9910..acc3f67e829 100644 --- a/src/tests/util/worker_test.c +++ b/src/tests/util/worker_test.c @@ -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; }