@@ -197,6 +197,68 @@ static int _dns_server_add_srv(struct dns_server_post_context *context)
197197 return 0 ;
198198}
199199
200+ static int _dns_add_rrs_ip_hint (struct dns_server_post_context * context , struct dns_rr_nested * param , dns_type_t qtype )
201+ {
202+ typedef int (* addfunc )(struct dns_rr_nested * svcparam , unsigned char * addr [], int addr_num );
203+ struct dns_request * request = context -> request ;
204+ struct dns_ip_address * addr_map = NULL ;
205+ unsigned long bucket = 0 ;
206+ struct hlist_node * tmp = NULL ;
207+ int ret = 0 ;
208+ int all_ips = 0 ;
209+ int addr_num = 0 ;
210+ addfunc add_func = NULL ;
211+
212+ unsigned char * addr [8 ];
213+ int addr_buffer_size = sizeof (addr ) / sizeof (addr [0 ]);
214+
215+ if (qtype == DNS_T_A ) {
216+ add_func = dns_HTTPS_add_ipv4hint ;
217+ } else if (qtype == DNS_T_AAAA ) {
218+ add_func = dns_HTTPS_add_ipv6hint ;
219+ } else {
220+ return 0 ; // Unsupported type
221+ }
222+
223+ if (request -> passthrough == 2 ) {
224+ all_ips = 1 ;
225+ }
226+
227+ if (request -> has_ip == 0 ) {
228+ return 0 ;
229+ }
230+
231+ if (all_ips == 0 ) {
232+ if (request -> ip_addr_type == (int )qtype ) {
233+ addr [0 ] = request -> ip_addr ;
234+ ret = add_func (param , addr , 1 );
235+
236+ return ret ;
237+ }
238+ return 0 ;
239+ }
240+
241+ ret = 0 ;
242+ pthread_mutex_lock (& request -> ip_map_lock );
243+ hash_for_each_safe (request -> ip_map , bucket , tmp , addr_map , node )
244+ {
245+ if (addr_map -> addr_type == qtype ) {
246+ addr [addr_num ] = addr_map -> ip_addr ;
247+ addr_num ++ ;
248+ if (addr_num >= addr_buffer_size ) {
249+ break ;
250+ }
251+ }
252+ }
253+ pthread_mutex_unlock (& request -> ip_map_lock );
254+
255+ if (addr_num > 0 ) {
256+ ret = add_func (param , addr , addr_num );
257+ }
258+
259+ return ret ;
260+ }
261+
200262static int _dns_add_rrs_HTTPS (struct dns_server_post_context * context )
201263{
202264 struct dns_request * request = context -> request ;
@@ -228,12 +290,9 @@ static int _dns_add_rrs_HTTPS(struct dns_server_post_context *context)
228290 }
229291 }
230292
231- if (request -> has_ip ) {
232- unsigned char * addr [1 ];
233- addr [0 ] = request -> ip_addr ;
234- if (request -> ip_addr_type == DNS_T_A ) {
235- ret = dns_HTTPS_add_ipv4hint (& param , addr , 1 );
236- }
293+ ret = _dns_add_rrs_ip_hint (context , & param , DNS_T_A );
294+ if (ret != 0 ) {
295+ return ret ;
237296 }
238297
239298 if (https_svcb -> ech_len > 0 ) {
@@ -243,12 +302,9 @@ static int _dns_add_rrs_HTTPS(struct dns_server_post_context *context)
243302 }
244303 }
245304
246- if (request -> has_ip ) {
247- unsigned char * addr [1 ];
248- addr [0 ] = request -> ip_addr ;
249- if (request -> ip_addr_type == DNS_T_AAAA ) {
250- ret = dns_HTTPS_add_ipv6hint (& param , addr , 1 );
251- }
305+ ret = _dns_add_rrs_ip_hint (context , & param , DNS_T_AAAA );
306+ if (ret != 0 ) {
307+ return ret ;
252308 }
253309
254310 dns_add_HTTPS_end (& param );
0 commit comments