@@ -49,14 +49,13 @@ typedef struct {
4949 rlm_radius_t const * inst ; //!< our instance
5050 fr_event_list_t * el ; //!< Event list.
5151 trunk_t * trunk ; //!< trunk handler
52- fr_bio_fd_config_t * fd_config ; //!< for threads or sockets
52+ fr_bio_fd_config_t fd_config ; //!< for threads or sockets
5353 fr_bio_fd_info_t const * fd_info ; //!< status of the FD.
5454 fr_radius_ctx_t radius_ctx ;
5555} bio_handle_ctx_t ;
5656
5757typedef struct {
58- bio_handle_ctx_t ctx ; //!< for copying to bio_handle_t
59-
58+ bio_handle_ctx_t ctx ; //!< common struct for home servers and BIO handles
6059
6160 struct {
6261 fr_bio_t * fd ; //!< writing
@@ -71,7 +70,7 @@ typedef struct bio_request_s bio_request_t;
7170 *
7271 */
7372typedef struct {
74- bio_handle_ctx_t ctx ; //! from thread or home server
73+ bio_handle_ctx_t ctx ; //!< common struct for home servers and BIO handles
7574
7675 int fd ; //!< File descriptor.
7776
@@ -140,8 +139,6 @@ struct bio_request_s {
140139typedef struct {
141140 bio_handle_ctx_t ctx ; //!< for copying to bio_handle_t
142141
143- fr_bio_fd_config_t fd_config ; //!< fil descriptor configuration
144-
145142 fr_rb_expire_node_t expire ;
146143} home_server_t ;
147144
@@ -747,7 +744,14 @@ static connection_state_t conn_init(void **h_out, connection_t *conn, void *uctx
747744
748745 MEM (h -> tt = radius_track_alloc (h ));
749746
750- h -> bio .fd = fr_bio_fd_alloc (h , h -> ctx .fd_config , 0 );
747+ /*
748+ * Limit the source port to the given range.
749+ */
750+ if (h -> ctx .inst -> src_port_start ) {
751+ DEBUG ("WARNING - src_port_start and src_port_end not currently supported. A random source port will be chosen" );
752+ }
753+
754+ h -> bio .fd = fr_bio_fd_alloc (h , & h -> ctx .fd_config , 0 );
751755 if (!h -> bio .fd ) {
752756 PERROR ("%s - failed opening socket" , h -> ctx .module_name );
753757 fail :
@@ -774,7 +778,7 @@ static connection_state_t conn_init(void **h_out, connection_t *conn, void *uctx
774778 * way we don't need a memory BIO for UDP sockets, but we can still add a verification layer for
775779 * UDP sockets?
776780 */
777- if (h -> ctx .fd_config -> socket_type == SOCK_STREAM ) {
781+ if (h -> ctx .fd_config . socket_type == SOCK_STREAM ) {
778782 h -> bio .mem = fr_bio_mem_alloc (h , 8192 , 0 , h -> bio .fd );
779783 if (!h -> bio .mem ) {
780784 PERROR ("%s - Failed allocating memory buffer - " , h -> ctx .module_name );
@@ -1411,7 +1415,7 @@ static void mod_dup(request_t *request, bio_request_t *u)
14111415
14121416 h = talloc_get_type_abort (u -> treq -> tconn -> conn -> h , bio_handle_t );
14131417
1414- if (h -> ctx .fd_config -> socket_type != SOCK_DGRAM ) {
1418+ if (h -> ctx .fd_config . socket_type != SOCK_DGRAM ) {
14151419 RDEBUG ("Using stream sockets - suppressing retransmission" );
14161420 return ;
14171421 }
@@ -1735,7 +1739,7 @@ static void mod_write(request_t *request, trunk_request_t *treq, bio_handle_t *h
17351739 /*
17361740 * We don't retransmit over TCP.
17371741 */
1738- if (h -> ctx .fd_config -> socket_type != SOCK_DGRAM ) return ;
1742+ if (h -> ctx .fd_config . socket_type != SOCK_DGRAM ) return ;
17391743
17401744 /*
17411745 * If we only send one datagram packet, then don't bother saving it.
@@ -2427,7 +2431,6 @@ static const trunk_io_funcs_t io_funcs = {
24272431 .request_cancel = request_cancel ,
24282432};
24292433
2430-
24312434/** Instantiate thread data for the submodule.
24322435 *
24332436 */
@@ -2438,7 +2441,7 @@ static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
24382441
24392442 thread -> ctx .el = mctx -> el ;
24402443 thread -> ctx .inst = inst ;
2441- thread -> ctx .fd_config = & inst -> fd_config ;
2444+ thread -> ctx .fd_config = inst -> fd_config ;
24422445 thread -> ctx .radius_ctx = inst -> common_ctx ;
24432446
24442447 if ((inst -> mode != RLM_RADIUS_MODE_UNCONNECTED_REPLICATE ) &&
@@ -2449,10 +2452,20 @@ static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
24492452 return 0 ;
24502453 }
24512454
2455+ /*
2456+ * If we have a port range, allocate the source IP based
2457+ * on the range start, plus the thread ID. This means
2458+ * that we can avoid "hunt and peck" attempts to open up
2459+ * the source port.
2460+ */
2461+ if (thread -> ctx .inst -> src_port_start ) {
2462+ thread -> ctx .fd_config .src_port = thread -> ctx .inst -> src_port_start + fr_schedule_worker_id ();
2463+ }
2464+
24522465 /*
24532466 * Allocate the unconnected replication socket.
24542467 */
2455- thread -> bio .fd = fr_bio_fd_alloc (thread , & thread -> ctx .inst -> fd_config , 0 );
2468+ thread -> bio .fd = fr_bio_fd_alloc (thread , & thread -> ctx .fd_config , 0 );
24562469 if (!thread -> bio .fd ) {
24572470 PERROR ("%s - failed opening socket" , inst -> name );
24582471 return CONNECTION_STATE_FAILED ;
@@ -2597,10 +2610,10 @@ static int8_t home_server_cmp(void const *one, void const *two)
25972610 home_server_t const * b = two ;
25982611 int8_t rcode ;
25992612
2600- rcode = fr_ipaddr_cmp (& a -> fd_config .dst_ipaddr , & b -> fd_config .dst_ipaddr );
2613+ rcode = fr_ipaddr_cmp (& a -> ctx . fd_config .dst_ipaddr , & b -> ctx . fd_config .dst_ipaddr );
26012614 if (rcode != 0 ) return rcode ;
26022615
2603- return CMP (a -> fd_config .dst_port , b -> fd_config .dst_port );
2616+ return CMP (a -> ctx . fd_config .dst_port , b -> ctx . fd_config .dst_port );
26042617}
26052618
26062619static xlat_action_t xlat_sendto_resume (TALLOC_CTX * ctx , fr_dcursor_t * out ,
@@ -2665,9 +2678,11 @@ static xlat_action_t xlat_radius_client(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcurso
26652678 }
26662679
26672680 home = fr_rb_find (& thread -> bio .expires .tree , & (home_server_t ) {
2668- .fd_config = (fr_bio_fd_config_t ) {
2669- .dst_ipaddr = ipaddr -> vb_ip ,
2670- .dst_port = port -> vb_uint16 ,
2681+ .ctx = {
2682+ .fd_config = (fr_bio_fd_config_t ) {
2683+ .dst_ipaddr = ipaddr -> vb_ip ,
2684+ .dst_port = port -> vb_uint16 ,
2685+ },
26712686 },
26722687 });
26732688 if (!home ) {
@@ -2681,12 +2696,14 @@ static xlat_action_t xlat_radius_client(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcurso
26812696 },
26822697 };
26832698
2684- home -> fd_config = inst -> fd_config ;
2685- home -> ctx .fd_config = & home -> fd_config ;
2686-
2687- home -> fd_config .type = FR_BIO_FD_CONNECTED ;
2688- home -> fd_config .dst_ipaddr = ipaddr -> vb_ip ;
2689- home -> fd_config .dst_port = port -> vb_uint32 ;
2699+ /*
2700+ * Copy the home server configuration from the root configuration. Then update it with
2701+ * the needs of the home server.
2702+ */
2703+ home -> ctx .fd_config = inst -> fd_config ;
2704+ home -> ctx .fd_config .type = FR_BIO_FD_CONNECTED ;
2705+ home -> ctx .fd_config .dst_ipaddr = ipaddr -> vb_ip ;
2706+ home -> ctx .fd_config .dst_port = port -> vb_uint32 ;
26902707
26912708 home -> ctx .radius_ctx = (fr_radius_ctx_t ) {
26922709 .secret = talloc_strdup (home , secret -> vb_strvalue ),
0 commit comments