Skip to content

Commit 9f6e9ad

Browse files
committed
code: refactor code of dns_client.c
1 parent 920bc8d commit 9f6e9ad

37 files changed

+7495
-6303
lines changed

src/dns_client.c

Lines changed: 0 additions & 6303 deletions
This file was deleted.

src/dns_client/client_http3.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*************************************************************************
2+
*
3+
* Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
4+
*
5+
* smartdns is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* smartdns is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#define _GNU_SOURCE
20+
21+
#include "client_http3.h"
22+
#include "client_quic.h"
23+
24+
#include "smartdns/http_parse.h"
25+
26+
int _dns_client_send_http3(struct dns_query_struct *query, struct dns_server_info *server_info, void *packet,
27+
unsigned short len)
28+
{
29+
#ifdef OSSL_QUIC1_VERSION
30+
int http_len = 0;
31+
int ret = 0;
32+
unsigned char inpacket_data[DNS_IN_PACKSIZE];
33+
struct client_dns_server_flag_https *https_flag = NULL;
34+
struct http_head *http_head = NULL;
35+
36+
if (len > sizeof(inpacket_data) - 128) {
37+
tlog(TLOG_ERROR, "packet size is invalid.");
38+
goto errout;
39+
}
40+
41+
https_flag = &server_info->flags.https;
42+
http_head = http_head_init(4096, HTTP_VERSION_3_0);
43+
if (http_head == NULL) {
44+
tlog(TLOG_ERROR, "init http head failed.");
45+
goto errout;
46+
}
47+
48+
http_head_set_method(http_head, HTTP_METHOD_POST);
49+
http_head_set_url(http_head, https_flag->path);
50+
http_head_set_head_type(http_head, HTTP_HEAD_REQUEST);
51+
http_head_add_fields(http_head, ":authority", https_flag->httphost);
52+
http_head_add_fields(http_head, "user-agent", "smartdns");
53+
http_head_add_fields(http_head, "content-type", "application/dns-message");
54+
http_head_add_fields(http_head, "accept-encoding", "identity");
55+
http_head_set_data(http_head, packet, len);
56+
57+
http_len = http_head_serialize(http_head, inpacket_data, DNS_IN_PACKSIZE);
58+
if (http_len <= 0) {
59+
tlog(TLOG_ERROR, "serialize http head failed.");
60+
goto errout;
61+
}
62+
63+
ret = _dns_client_send_quic_data(query, server_info, inpacket_data, http_len);
64+
http_head_destroy(http_head);
65+
return ret;
66+
errout:
67+
if (http_head) {
68+
http_head_destroy(http_head);
69+
}
70+
71+
return -1;
72+
#else
73+
tlog(TLOG_ERROR, "http3 is not supported.");
74+
#endif
75+
return 0;
76+
}

src/dns_client/client_http3.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*************************************************************************
2+
*
3+
* Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
4+
*
5+
* smartdns is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* smartdns is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef _DNS_CLIENT_HTTP3_H_
20+
#define _DNS_CLIENT_HTTP3_H_
21+
22+
#include "dns_client.h"
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif /*__cplusplus */
27+
28+
int _dns_client_send_http3(struct dns_query_struct *query, struct dns_server_info *server_info, void *packet,
29+
unsigned short len);
30+
#ifdef __cplusplus
31+
}
32+
#endif /*__cplusplus */
33+
#endif

src/dns_client/client_https.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*************************************************************************
2+
*
3+
* Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
4+
*
5+
* smartdns is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* smartdns is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#define _GNU_SOURCE
19+
20+
#include "client_https.h"
21+
#include "client_socket.h"
22+
#include "client_tls.h"
23+
#include "server_info.h"
24+
25+
#include "smartdns/http_parse.h"
26+
27+
int _dns_client_send_https(struct dns_server_info *server_info, void *packet, unsigned short len)
28+
{
29+
int send_len = 0;
30+
int http_len = 0;
31+
unsigned char inpacket_data[DNS_IN_PACKSIZE];
32+
unsigned char *inpacket = inpacket_data;
33+
struct client_dns_server_flag_https *https_flag = NULL;
34+
35+
if (len > sizeof(inpacket_data) - 2) {
36+
tlog(TLOG_ERROR, "packet size is invalid.");
37+
return -1;
38+
}
39+
40+
https_flag = &server_info->flags.https;
41+
42+
http_len = snprintf((char *)inpacket, DNS_IN_PACKSIZE,
43+
"POST %s HTTP/1.1\r\n"
44+
"Host: %s\r\n"
45+
"User-Agent: smartdns\r\n"
46+
"Content-Type: application/dns-message\r\n"
47+
"Content-Length: %d\r\n"
48+
"\r\n",
49+
https_flag->path, https_flag->httphost, len);
50+
if (http_len < 0 || http_len >= DNS_IN_PACKSIZE) {
51+
tlog(TLOG_ERROR, "http header size is invalid.");
52+
return -1;
53+
}
54+
55+
memcpy(inpacket + http_len, packet, len);
56+
http_len += len;
57+
58+
if (server_info->status != DNS_SERVER_STATUS_CONNECTED) {
59+
return _dns_client_send_data_to_buffer(server_info, inpacket, http_len);
60+
}
61+
62+
if (server_info->ssl == NULL) {
63+
errno = EINVAL;
64+
return -1;
65+
}
66+
67+
send_len = _dns_client_socket_ssl_send(server_info, inpacket, http_len);
68+
if (send_len <= 0) {
69+
if (errno == EAGAIN || errno == EPIPE || server_info->ssl == NULL) {
70+
/* save data to buffer, and retry when EPOLLOUT is available */
71+
return _dns_client_send_data_to_buffer(server_info, inpacket, http_len);
72+
} else if (server_info->ssl && errno != ENOMEM) {
73+
_dns_client_shutdown_socket(server_info);
74+
}
75+
return -1;
76+
} else if (send_len < http_len) {
77+
/* save remain data to buffer, and retry when EPOLLOUT is available */
78+
return _dns_client_send_data_to_buffer(server_info, inpacket + send_len, http_len - send_len);
79+
}
80+
81+
return 0;
82+
}

src/dns_client/client_https.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*************************************************************************
2+
*
3+
* Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
4+
*
5+
* smartdns is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* smartdns is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef _DNS_CLIENT_HTTPS_H_
20+
#define _DNS_CLIENT_HTTPS_H_
21+
22+
#include "dns_client.h"
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif /*__cplusplus */
27+
28+
int _dns_client_send_https(struct dns_server_info *server_info, void *packet, unsigned short len);
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif /*__cplusplus */
33+
#endif

0 commit comments

Comments
 (0)