Skip to content

Commit 0c599bb

Browse files
committed
server: fix issue of missing IP address in http record when not checking speed.
1 parent 922e7b7 commit 0c599bb

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

src/dns_server/context.c

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
200262
static 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);

src/dns_server/dns_server.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@ struct dns_request {
329329
atomic_t plugin_complete_called;
330330

331331
/* send original raw packet to server/client like proxy */
332+
333+
/*
334+
0: not passthrough, reply to client
335+
1: passthrough, reply to client, no modify packet
336+
2: passthrough, reply to client, check and filter ip addresses.
337+
*/
332338
int passthrough;
333339

334340
int request_wait;

0 commit comments

Comments
 (0)