Skip to content

Commit 977341a

Browse files
authored
Merge pull request #163 from baranyaib90/master
Fixes 13
2 parents d03e115 + bd71243 commit 977341a

File tree

12 files changed

+104
-70
lines changed

12 files changed

+104
-70
lines changed

.github/workflows/cmake.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,27 @@ jobs:
88
# well on Windows or Mac. You can convert this to a matrix build if you need
99
# cross-platform coverage.
1010
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
11-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-22.04
1212

1313
strategy:
14+
fail-fast: false
1415
matrix:
15-
compiler: [gcc-10, clang-12]
16+
compiler: [gcc-12, clang-15]
1617

1718
steps:
18-
- uses: actions/checkout@v2
19+
- uses: actions/checkout@main
1920

2021
- name: Update APT
2122
run: sudo apt-get update
2223

2324
- name: Setup Dependencies
24-
run: sudo apt-get install cmake libc-ares-dev libcurl4-openssl-dev libev-dev build-essential clang-tidy-12 ${{ matrix.compiler }} dnsutils python3-pip valgrind
25+
run: sudo apt-get install cmake libc-ares-dev libcurl4-openssl-dev libev-dev build-essential clang-tidy-15 ${{ matrix.compiler }} dnsutils python3-pip valgrind
2526

2627
- name: Setup Robot Framework
2728
run: sudo pip3 install robotframework
2829

2930
- name: Set clang-tidy
30-
run: sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-12 100
31+
run: sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-15 100
3132

3233
- name: Configure CMake
3334
env:
@@ -43,7 +44,7 @@ jobs:
4344
- name: Test
4445
run: make -C ${{github.workspace}}/ test ARGS="--verbose"
4546

46-
- uses: actions/upload-artifact@v2
47+
- uses: actions/upload-artifact@v3
4748
if: ${{ success() || failure() }}
4849
with:
4950
name: robot-logs-${{ matrix.compiler }}

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
project(HttpsDnsProxy C)
21
cmake_minimum_required(VERSION 3.7)
2+
project(HttpsDnsProxy C)
33

44
# FUNCTIONS
55

@@ -26,7 +26,7 @@ if (NOT CMAKE_INSTALL_BINDIR)
2626
endif()
2727

2828
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra --pedantic -Wno-strict-aliasing -Wno-variadic-macros")
29-
set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG")
29+
set(CMAKE_C_FLAGS_DEBUG "-gdwarf-4 -DDEBUG")
3030
set(CMAKE_C_FLAGS_RELEASE "-O2")
3131

3232
if ((CMAKE_C_COMPILER_ID MATCHES GNU AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 9) OR
@@ -92,7 +92,7 @@ if(NOT CLANG_TIDY_EXE)
9292
message(STATUS "clang-tidy not found.")
9393
else()
9494
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
95-
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-fix" "-checks=*,-clang-analyzer-alpha.*,-misc-unused-parameters,-cert-err34-c,-google-readability-todo,-hicpp-signed-bitwise,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-gnu-folding-constant,-gnu-zero-variadic-macro-arguments,-readability-function-cognitive-complexity,-concurrency-mt-unsafe")
95+
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-fix" "-checks=*,-cert-err34-c,-readability-identifier-length,-altera-unroll-loops,-bugprone-easily-swappable-parameters,-concurrency-mt-unsafe,-*magic-numbers,-hicpp-signed-bitwise,-readability-function-cognitive-complexity,-altera-id-dependent-backward-branch,-google-readability-todo")
9696
endif()
9797

9898
# BUILD

src/dns_poller.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ void dns_poller_init(dns_poller_t *d, struct ev_loop *loop,
135135
int bootstrap_dns_polling_interval,
136136
const char *hostname,
137137
int family, dns_poller_cb cb, void *cb_data) {
138-
int r = 0;
139-
if ((r = ares_library_init(ARES_LIB_INIT_ALL)) != ARES_SUCCESS) {
138+
int r = ares_library_init(ARES_LIB_INIT_ALL);
139+
if (r != ARES_SUCCESS) {
140140
FLOG("ares_library_init error: %s", ares_strerror(r));
141141
}
142142

@@ -148,11 +148,13 @@ void dns_poller_init(dns_poller_t *d, struct ev_loop *loop,
148148
};
149149
int optmask = ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES | ARES_OPT_SOCK_STATE_CB;
150150

151-
if ((r = ares_init_options(&d->ares, &options, optmask)) != ARES_SUCCESS) {
151+
r = ares_init_options(&d->ares, &options, optmask);
152+
if (r != ARES_SUCCESS) {
152153
FLOG("ares_init_options error: %s", ares_strerror(r));
153154
}
154155

155-
if((r = ares_set_servers_ports_csv(d->ares, bootstrap_dns)) != ARES_SUCCESS) {
156+
r = ares_set_servers_ports_csv(d->ares, bootstrap_dns);
157+
if (r != ARES_SUCCESS) {
156158
FLOG("ares_set_servers_ports_csv error: %s", ares_strerror(r));
157159
}
158160

src/dns_server.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
#include "dns_server.h"
1111
#include "logging.h"
1212

13+
14+
enum {
15+
REQUEST_MAX = 1500 // A default MTU. We don't do TCP so any bigger is likely a waste
16+
};
17+
18+
1319
// Creates and bind a listening UDP socket for incoming requests.
1420
static int get_listen_sock(const char *listen_addr, int listen_port,
1521
unsigned int *addrlen) {
@@ -40,7 +46,8 @@ static int get_listen_sock(const char *listen_addr, int listen_port,
4046
FLOG("Error creating socket");
4147
}
4248

43-
if ((res = bind(sock, ai->ai_addr, ai->ai_addrlen)) < 0) {
49+
res = bind(sock, ai->ai_addr, ai->ai_addrlen);
50+
if (res < 0) {
4451
FLOG("Error binding %s:%d: %s (%d)", listen_addr, listen_port,
4552
strerror(errno), res);
4653
}
@@ -51,9 +58,6 @@ static int get_listen_sock(const char *listen_addr, int listen_port,
5158
return sock;
5259
}
5360

54-
// A default MTU. We don't do TCP so any bigger is likely a waste.
55-
#define REQUEST_MAX 1500
56-
5761
static void watcher_cb(struct ev_loop __attribute__((unused)) *loop,
5862
ev_io *w, int __attribute__((unused)) revents) {
5963
dns_server_t *d = (dns_server_t *)w->data;

src/https_client.c

100755100644
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include "options.h"
1414

1515
#define DOH_CONTENT_TYPE "application/dns-message"
16-
#define DOH_MAX_RESPONSE_SIZE 65535
16+
enum {
17+
DOH_MAX_RESPONSE_SIZE = 65535
18+
};
1719

1820
// the following macros require to have ctx pointer to https_fetch_ctx structure
1921
// else: compilation failure will occur
@@ -354,8 +356,8 @@ static int https_fetch_ctx_process_response(https_client_t *client,
354356
}
355357
}
356358

357-
if ((res = curl_easy_getinfo(
358-
ctx->curl, CURLINFO_RESPONSE_CODE, &long_resp)) != CURLE_OK) {
359+
res = curl_easy_getinfo(ctx->curl, CURLINFO_RESPONSE_CODE, &long_resp);
360+
if (res != CURLE_OK) {
359361
ELOG_REQ("CURLINFO_RESPONSE_CODE: %s", curl_easy_strerror(res));
360362
faulty_response = 1;
361363
} else if (long_resp != 200) {
@@ -389,8 +391,8 @@ static int https_fetch_ctx_process_response(https_client_t *client,
389391

390392
if (!faulty_response)
391393
{
392-
if ((res = curl_easy_getinfo(
393-
ctx->curl, CURLINFO_CONTENT_TYPE, &str_resp)) != CURLE_OK) {
394+
res = curl_easy_getinfo(ctx->curl, CURLINFO_CONTENT_TYPE, &str_resp);
395+
if (res != CURLE_OK) {
394396
ELOG_REQ("CURLINFO_CONTENT_TYPE: %s", curl_easy_strerror(res));
395397
} else if (str_resp == NULL ||
396398
strncmp(str_resp, DOH_CONTENT_TYPE, sizeof(DOH_CONTENT_TYPE) - 1) != 0) { // at least, start with it
@@ -400,23 +402,25 @@ static int https_fetch_ctx_process_response(https_client_t *client,
400402
}
401403

402404
if (logging_debug_enabled() || faulty_response || ctx->buflen == 0) {
403-
if ((res = curl_easy_getinfo(
404-
ctx->curl, CURLINFO_REDIRECT_URL, &str_resp)) != CURLE_OK) {
405+
res = curl_easy_getinfo(ctx->curl, CURLINFO_REDIRECT_URL, &str_resp);
406+
if (res != CURLE_OK) {
405407
ELOG_REQ("CURLINFO_REDIRECT_URL: %s", curl_easy_strerror(res));
406408
} else if (str_resp != NULL) {
407409
WLOG_REQ("Request would be redirected to: %s", str_resp);
408410
if (strcmp(str_resp, client->opt->resolver_url) != 0) {
409411
WLOG("Please update Resolver URL to avoid redirection!");
410412
}
411413
}
412-
if ((res = curl_easy_getinfo(
413-
ctx->curl, CURLINFO_SSL_VERIFYRESULT, &long_resp)) != CURLE_OK) {
414+
415+
res = curl_easy_getinfo(ctx->curl, CURLINFO_SSL_VERIFYRESULT, &long_resp);
416+
if (res != CURLE_OK) {
414417
ELOG_REQ("CURLINFO_SSL_VERIFYRESULT: %s", curl_easy_strerror(res));
415418
} else if (long_resp != CURLE_OK) {
416419
WLOG_REQ("CURLINFO_SSL_VERIFYRESULT: %s", curl_easy_strerror(long_resp));
417420
}
418-
if ((res = curl_easy_getinfo(
419-
ctx->curl, CURLINFO_OS_ERRNO, &long_resp)) != CURLE_OK) {
421+
422+
res = curl_easy_getinfo(ctx->curl, CURLINFO_OS_ERRNO, &long_resp);
423+
if (res != CURLE_OK) {
420424
ELOG_REQ("CURLINFO_OS_ERRNO: %s", curl_easy_strerror(res));
421425
} else if (long_resp != 0) {
422426
WLOG_REQ("CURLINFO_OS_ERRNO: %d %s", long_resp, strerror(long_resp));
@@ -428,8 +432,8 @@ static int https_fetch_ctx_process_response(https_client_t *client,
428432
}
429433

430434
if (logging_debug_enabled() || client->stat) {
431-
if ((res = curl_easy_getinfo(
432-
ctx->curl, CURLINFO_NUM_CONNECTS , &long_resp)) != CURLE_OK) {
435+
res = curl_easy_getinfo(ctx->curl, CURLINFO_NUM_CONNECTS , &long_resp);
436+
if (res != CURLE_OK) {
433437
ELOG_REQ("CURLINFO_NUM_CONNECTS: %s", curl_easy_strerror(res));
434438
} else {
435439
DLOG_REQ("CURLINFO_NUM_CONNECTS: %d", long_resp);
@@ -440,20 +444,22 @@ static int https_fetch_ctx_process_response(https_client_t *client,
440444
}
441445

442446
if (logging_debug_enabled()) {
443-
if ((res = curl_easy_getinfo(
444-
ctx->curl, CURLINFO_EFFECTIVE_URL, &str_resp)) != CURLE_OK) {
447+
res = curl_easy_getinfo(ctx->curl, CURLINFO_EFFECTIVE_URL, &str_resp);
448+
if (res != CURLE_OK) {
445449
ELOG_REQ("CURLINFO_EFFECTIVE_URL: %s", curl_easy_strerror(res));
446450
} else {
447451
DLOG_REQ("CURLINFO_EFFECTIVE_URL: %s", str_resp);
448452
}
449-
if ((res = curl_easy_getinfo(
450-
ctx->curl, CURLINFO_HTTP_VERSION, &long_resp)) != CURLE_OK) {
453+
454+
res = curl_easy_getinfo(ctx->curl, CURLINFO_HTTP_VERSION, &long_resp);
455+
if (res != CURLE_OK) {
451456
ELOG_REQ("CURLINFO_HTTP_VERSION: %s", curl_easy_strerror(res));
452457
} else if (long_resp != CURL_HTTP_VERSION_NONE) {
453458
DLOG_REQ("CURLINFO_HTTP_VERSION: %s", http_version_str(long_resp));
454459
}
455-
if ((res = curl_easy_getinfo(
456-
ctx->curl, CURLINFO_PROTOCOL, &long_resp)) != CURLE_OK) {
460+
461+
res = curl_easy_getinfo(ctx->curl, CURLINFO_PROTOCOL, &long_resp);
462+
if (res != CURLE_OK) {
457463
ELOG_REQ("CURLINFO_PROTOCOL: %s", curl_easy_strerror(res));
458464
} else if (long_resp != CURLPROTO_HTTPS) {
459465
DLOG_REQ("CURLINFO_PROTOCOL: %d", long_resp);

src/logging.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <inttypes.h>
12
#include <stdarg.h>
23
#include <stdint.h>
34
#include <stdio.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
@@ -26,7 +27,7 @@ static void logging_timer_cb(struct ev_loop __attribute__((unused)) *loop,
2627
ev_timer __attribute__((unused)) *w,
2728
int __attribute__((unused)) revents) {
2829
if (logf) {
29-
fflush(logf);
30+
(void)fflush(logf);
3031
}
3132
}
3233

@@ -47,20 +48,20 @@ void logging_flush_cleanup(struct ev_loop *loop) {
4748

4849
void logging_init(int fd, int level) {
4950
if (logf) {
50-
fclose(logf);
51+
(void)fclose(logf);
5152
}
5253
logf = fdopen(fd, "a");
5354
loglevel = level;
5455
}
5556

56-
void logging_cleanup() {
57+
void logging_cleanup(void) {
5758
if (logf) {
58-
fclose(logf);
59+
(void)fclose(logf);
5960
}
6061
logf = NULL;
6162
}
6263

63-
int logging_debug_enabled() {
64+
int logging_debug_enabled(void) {
6465
return loglevel <= LOG_DEBUG;
6566
}
6667

@@ -78,18 +79,18 @@ void _log(const char *file, int line, int severity, const char *fmt, ...) {
7879

7980
struct timeval tv;
8081
gettimeofday(&tv, NULL);
81-
fprintf(logf, "%s %8lu.%06lu %s:%d ", SeverityStr[severity],
82+
(void)fprintf(logf, "%s %8"PRIu64".%06"PRIu64" %s:%d ", SeverityStr[severity],
8283
(uint64_t)tv.tv_sec,
8384
(uint64_t)tv.tv_usec, file, line);
8485

8586
va_list args;
8687
va_start(args, fmt);
87-
vfprintf(logf, fmt, args);
88+
(void)vfprintf(logf, fmt, args);
8889
va_end(args);
89-
fprintf(logf, "\n");
90+
(void)fprintf(logf, "\n");
9091

9192
if (severity >= LOG_FLUSH_LEVEL) {
92-
fflush(logf);
93+
(void)fflush(logf);
9394
}
9495
if (severity == LOG_FATAL) {
9596
#ifdef DEBUG

src/logging.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ void logging_flush_init(struct ev_loop *loop);
1616
void logging_flush_cleanup(struct ev_loop *loop);
1717

1818
// Cleans up and flushes open logs.
19-
void logging_cleanup();
19+
void logging_cleanup(void);
2020

2121
// Returns 1 if debug logging is enabled.
22-
int logging_debug_enabled();
22+
int logging_debug_enabled(void);
2323

2424
// Internal. Don't use.
2525
void _log(const char *file, int line, int severity, const char *fmt, ...);

src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ static int hostname_from_uri(const char* uri,
5353
if (!isalpha(*(end - 1))) { return 0; } // last digit non-alpha.
5454

5555
// If using basic authentication in URL, chop off prefix.
56-
char *tmp = NULL;
57-
if ((tmp = strchr(uri, '@'))) {
56+
char *tmp = strchr(uri, '@');
57+
if (tmp) {
5858
tmp++;
5959
if (tmp < end) {
6060
uri = tmp;
@@ -169,7 +169,7 @@ static void dns_poll_cb(const char* hostname, void *data,
169169
memset(buf, 0, sizeof(buf)); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
170170
if (strlen(hostname) > 254) { FLOG("Hostname too long."); }
171171
int ip_start = snprintf(buf, sizeof(buf) - 1, "%s:443:", hostname); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
172-
snprintf(buf + ip_start, sizeof(buf) - 1 - ip_start, "%s", addr_list); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
172+
(void)snprintf(buf + ip_start, sizeof(buf) - 1 - ip_start, "%s", addr_list); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
173173
if (app->resolv && app->resolv->data) {
174174
char * old_addr_list = strstr(app->resolv->data, ":443:");
175175
if (old_addr_list) {

src/options.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@
1515
#define O_CLOEXEC 0
1616
#endif
1717

18-
#define DEFAULT_HTTP_VERSION 2
18+
enum {
19+
DEFAULT_HTTP_VERSION = 2
20+
};
1921

2022

21-
const char * options_sw_version() {
23+
const char * options_sw_version(void) {
2224
#ifdef SW_VERSION
2325
return SW_VERSION;
2426
#else
25-
return "2023.01.01-atLeast"; // update date sometimes, like 1-2 times a year
27+
return "2023.10.10-atLeast"; // update date sometimes, like 1-2 times a year
2628
#endif
2729
}
2830

2931
void options_init(struct Options *opt) {
3032
opt->listen_addr = "127.0.0.1";
3133
opt->listen_port = 5053;
3234
opt->logfile = "-";
33-
opt->logfd = -1;
35+
opt->logfd = STDOUT_FILENO;
3436
opt->loglevel = LOG_ERROR;
3537
opt->daemonize = 0;
3638
opt->dscp = 0;
@@ -124,16 +126,16 @@ int options_parse_args(struct Options *opt, int argc, char **argv) {
124126
}
125127
}
126128
if (opt->user) {
127-
struct passwd *p = NULL;
128-
if (!(p = getpwnam(opt->user)) || !p->pw_uid) {
129+
struct passwd *p = getpwnam(opt->user);
130+
if (!p || !p->pw_uid) {
129131
printf("Username (%s) invalid.\n", opt->user);
130132
return -1;
131133
}
132134
opt->uid = p->pw_uid;
133135
}
134136
if (opt->group) {
135-
struct group *g = NULL;
136-
if (!(g = getgrnam(opt->group)) || !g->gr_gid) {
137+
struct group *g = getgrnam(opt->group);
138+
if (!g || !g->gr_gid) {
137139
printf("Group (%s) invalid.\n", opt->group);
138140
return -1;
139141
}
@@ -152,13 +154,13 @@ int options_parse_args(struct Options *opt, int argc, char **argv) {
152154
"----------------------------\n");
153155
sleep(1);
154156
}
155-
if (opt->logfile == NULL ||
156-
!strcmp(opt->logfile, "-")) {
157-
opt->logfd = STDOUT_FILENO;
158-
} else if ((opt->logfd = open(opt->logfile,
159-
O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC,
160-
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) <= 0) {
161-
printf("Logfile '%s' is not writable.\n", opt->logfile);
157+
if (opt->logfile != NULL && strcmp(opt->logfile, "-") != 0) {
158+
opt->logfd = open(opt->logfile,
159+
O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC,
160+
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
161+
if (opt->logfd <= 0) {
162+
printf("Could not open logfile '%s' for writing.\n", opt->logfile);
163+
}
162164
}
163165
if (opt->resolver_url == NULL ||
164166
strncmp(opt->resolver_url, "https://", 8) != 0) {

0 commit comments

Comments
 (0)