From d1db5e590d502c0e2b085b929d07a578a877bee0 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Sat, 1 Jun 2024 20:13:08 -0500 Subject: [PATCH] Include what you use (#1512) Use the include-what-you-use program to (partially) clean up header includes, so that only includes which are needed, and no includes that are not needed (or at least closer to that ideal) are done. For a c-language project, the build-time improvements from this change is minimal. This would have a much bigger impact on a C++ project than a C-project for build times. So for coturn, this change is mostly intended to just provide consistency and make it easier to locate weird issues like strange dependencies, and unnecessary connections between code. --- .clang-tidy | 6 +++++ .../actions/ubuntu-build-deps/action.yml | 2 +- .github/workflows/clang-tidy.yml | 2 +- src/apps/common/apputils.h | 3 ++- src/apps/common/ns_turn_openssl.h | 22 ++++++++++--------- src/apps/common/ns_turn_utils.c | 4 +++- src/apps/common/ns_turn_utils.h | 1 + src/apps/common/stun_buffer.c | 2 ++ src/apps/common/stun_buffer.h | 3 +++ src/apps/oauth/oauth.c | 12 ++++++---- src/apps/peer/udpserver.h | 2 ++ src/apps/relay/ns_ioalib_impl.h | 13 ++++++----- src/apps/relay/turn_admin_server.c | 18 +++++++++++---- src/apps/uclient/startuclient.c | 9 ++++---- src/apps/uclient/startuclient.h | 2 ++ src/client/ns_turn_ioaddr.c | 6 +++++ src/client/ns_turn_msg.c | 3 +++ src/client/ns_turn_msg.h | 1 + src/client/ns_turn_msg_addr.c | 3 +++ src/client/ns_turn_msg_addr.h | 1 + src/client/ns_turn_msg_defs.h | 4 +++- src/ns_turn_defs.h | 15 +++++++------ src/server/ns_turn_allocation.c | 5 +++++ src/server/ns_turn_allocation.h | 5 ++--- src/server/ns_turn_ioalib.h | 1 + src/server/ns_turn_maps.c | 3 +++ src/server/ns_turn_maps.h | 6 ++--- src/server/ns_turn_maps_rtcp.c | 4 +++- src/server/ns_turn_maps_rtcp.h | 4 +++- src/server/ns_turn_server.c | 7 ++++++ src/server/ns_turn_server.h | 7 +++++- 31 files changed, 126 insertions(+), 50 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 0b6f8e1..ae49d65 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -3,6 +3,7 @@ Checks: 'clang-diagnostic-*, ,clang-analyzer-*, ,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, ,-clang-analyzer-security.insecureAPI.strcpy, + ,-clang-analyzer-cplusplus.InnerPointer, ,bugprone-*, ,-bugprone-easily-swappable-parameters, ,performance-*, @@ -12,5 +13,10 @@ Checks: 'clang-diagnostic-*, ,-readability-else-after-return, ,-readability-magic-numbers, ,-readability-function-cognitive-complexity, + ,-readability-uppercase-literal-suffix, ,modernize-*, + ,-modernize-use-trailing-return-type, + ,-modernize-use-auto, + ,-cplusplus.InnerPointer, + ,-clang-diagnostic-ignored-optimization-argument, ' diff --git a/.github/workflows/actions/ubuntu-build-deps/action.yml b/.github/workflows/actions/ubuntu-build-deps/action.yml index 9ca7603..a86bbc9 100644 --- a/.github/workflows/actions/ubuntu-build-deps/action.yml +++ b/.github/workflows/actions/ubuntu-build-deps/action.yml @@ -36,7 +36,7 @@ runs: ninja-build \ iwyu \ pkgconf \ - wget + wget if [ "$(lsb_release -s -r)x" == "16.04x" ]; then apt install -y clang-tools; fi - name: install coturn dependencies diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 87dc713..1130374 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -44,4 +44,4 @@ jobs: wget https://raw.githubusercontent.com/include-what-you-use/include-what-you-use/clang_14/iwyu_tool.py chmod +x iwyu_tool.py # iwyu_tool.py returns non-zero if any executions returned nonzero. Which... happens to be useless unless the project is already IWYU clean. - ./iwyu_tool.py -j $(nproc) -p build -- -Xiwyu --mapping_file=${GITHUB_WORKSPACE}/iwyu-ubuntu.imp || exit 0 + ./iwyu_tool.py -j $(nproc) -p build -- -Xiwyu --mapping_file=${GITHUB_WORKSPACE}/iwyu-ubuntu.imp | grep -v "has correct" | uniq || exit 0 diff --git a/src/apps/common/apputils.h b/src/apps/common/apputils.h index a2dbeff..8218cd8 100644 --- a/src/apps/common/apputils.h +++ b/src/apps/common/apputils.h @@ -31,10 +31,11 @@ #ifndef __APP_LIB__ #define __APP_LIB__ -#include +#include // for evutil_socket_t #include "ns_turn_openssl.h" +#include "ns_turn_defs.h" #include "ns_turn_ioaddr.h" #include "ns_turn_ioalib.h" #include "ns_turn_msg_defs.h" diff --git a/src/apps/common/ns_turn_openssl.h b/src/apps/common/ns_turn_openssl.h index 3289c93..e9ae9ea 100644 --- a/src/apps/common/ns_turn_openssl.h +++ b/src/apps/common/ns_turn_openssl.h @@ -31,15 +31,17 @@ #ifndef __NST_OPENSSL_LIB__ #define __NST_OPENSSL_LIB__ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export #endif //__NST_OPENSSL_LIB__ diff --git a/src/apps/common/ns_turn_utils.c b/src/apps/common/ns_turn_utils.c index 3f3c4a5..ad08308 100644 --- a/src/apps/common/ns_turn_utils.c +++ b/src/apps/common/ns_turn_utils.c @@ -51,12 +51,14 @@ #if !defined(WINDOWS) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(__CYGWIN64__) #include -#include #ifdef SYS_gettid #define gettid() ((pid_t)syscall(SYS_gettid)) #endif #endif +#include // for tolower +#include // for memcmp, strstr, strcmp, strdup, strlen + ////////// LOG TIME OPTIMIZATION /////////// static volatile int _log_file_line_set = 0; diff --git a/src/apps/common/ns_turn_utils.h b/src/apps/common/ns_turn_utils.h index de6d43c..654b41b 100644 --- a/src/apps/common/ns_turn_utils.h +++ b/src/apps/common/ns_turn_utils.h @@ -41,6 +41,7 @@ void err(int eval, const char *format, ...); #endif #endif +#include "ns_turn_defs.h" // for turn_time_t #include "ns_turn_ioaddr.h" #ifdef __cplusplus diff --git a/src/apps/common/stun_buffer.c b/src/apps/common/stun_buffer.c index 9e5e24a..d6bc080 100644 --- a/src/apps/common/stun_buffer.c +++ b/src/apps/common/stun_buffer.c @@ -30,6 +30,8 @@ #include "stun_buffer.h" +#include // for memset + ////////////////////// BUFFERS /////////////////////////// int stun_init_buffer(stun_buffer *buf) { diff --git a/src/apps/common/stun_buffer.h b/src/apps/common/stun_buffer.h index ee7f21a..d28ea9b 100644 --- a/src/apps/common/stun_buffer.h +++ b/src/apps/common/stun_buffer.h @@ -31,7 +31,10 @@ #ifndef __TURN_STUN_BUF__ #define __TURN_STUN_BUF__ +#include "ns_turn_defs.h" // for uint16_t, uint8_t, uint32_t, size_t +#include "ns_turn_ioaddr.h" // for ioa_addr #include "ns_turn_msg.h" +#include "ns_turn_msg_defs.h" // for STUN_CHANNEL_HEADER_LENGTH #ifdef __cplusplus extern "C" { diff --git a/src/apps/oauth/oauth.c b/src/apps/oauth/oauth.c index e44b963..f4ec694 100644 --- a/src/apps/oauth/oauth.c +++ b/src/apps/oauth/oauth.c @@ -28,6 +28,14 @@ * SUCH DAMAGE. */ +#include "ns_turn_defs.h" // for STRCPY, turn_time_t, uint8_t, uint32_t +#include "ns_turn_msg.h" // for convert_oauth_key_data, decode_oauth_t... +#include "ns_turn_msg_defs.h" // for oauth_token, oauth_encrypted_block +#include "ns_turn_utils.h" + +#include "apputils.h" +#include "stun_buffer.h" + #if defined(__unix__) #include #endif @@ -39,10 +47,6 @@ #include #include -#include "apputils.h" -#include "ns_turn_utils.h" -#include "stun_buffer.h" - //////////////////////////////////////////////////// #define OAUTH_TOKEN_SIZE 1000 // TODO: find insted of 1000 the real max of encoded token length diff --git a/src/apps/peer/udpserver.h b/src/apps/peer/udpserver.h index 3ff1d86..9107c81 100644 --- a/src/apps/peer/udpserver.h +++ b/src/apps/peer/udpserver.h @@ -37,6 +37,8 @@ #include +#include // for size_t + #ifdef __cplusplus extern "C" { #endif diff --git a/src/apps/relay/ns_ioalib_impl.h b/src/apps/relay/ns_ioalib_impl.h index 39b984f..bb7cccd 100644 --- a/src/apps/relay/ns_ioalib_impl.h +++ b/src/apps/relay/ns_ioalib_impl.h @@ -35,15 +35,10 @@ #ifndef __IOA_LIBIMPL__ #define __IOA_LIBIMPL__ -#include -#include -#include - -#include +#include "ns_turn_ioalib.h" // IWYU pragma: export #include "ns_turn_openssl.h" -#include "ns_turn_ioalib.h" #include "ns_turn_maps.h" #include "ns_turn_maps_rtcp.h" #include "ns_turn_server.h" @@ -55,6 +50,12 @@ #include "ns_sm.h" +#include +#include +#include + +#include + #include #ifdef __cplusplus diff --git a/src/apps/relay/turn_admin_server.c b/src/apps/relay/turn_admin_server.c index 0411fcb..1e55b81 100644 --- a/src/apps/relay/turn_admin_server.c +++ b/src/apps/relay/turn_admin_server.c @@ -27,7 +27,6 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - #include #include #include @@ -66,12 +65,17 @@ #include "mainrelay.h" #include "userdb.h" +#include "ns_ioalib_impl.h" // for ioa_socket, ip_range_t, ioa_network_... +#include "ns_sm.h" // for new_super_memory_region, super_memory_t +#include "ns_turn_defs.h" // for uint8_t, STRCPY, UNUSED_ARG, getcwd +#include "ns_turn_maps.h" +#include "ns_turn_msg.h" // for check_password, band_limit_t, conver... +#include "ns_turn_server.h" +#include "ns_turn_session.h" // for turn_session_info, addr_data, _realm... #include "ns_turn_utils.h" -#include "ns_turn_maps.h" -#include "ns_turn_server.h" - #include "apputils.h" +#include "stun_buffer.h" // for stun_buffer #include "turn_admin_server.h" @@ -83,6 +87,12 @@ /////////////////////////////// +struct bufferevent; +struct evconnlistener; +struct str_buffer; + +/////////////////////////////// + struct admin_server adminserver; int use_cli = 1; diff --git a/src/apps/uclient/startuclient.c b/src/apps/uclient/startuclient.c index 69240db..e9df185 100644 --- a/src/apps/uclient/startuclient.c +++ b/src/apps/uclient/startuclient.c @@ -28,13 +28,12 @@ * SUCH DAMAGE. */ -#if defined(__linux__) -#include -#endif - -#include "apputils.h" +#include "ns_turn_defs.h" +#include "ns_turn_ioalib.h" // for ioa_engine_handle #include "ns_turn_msg.h" #include "ns_turn_utils.h" + +#include "apputils.h" #include "session.h" #include "startuclient.h" #include "uclient.h" diff --git a/src/apps/uclient/startuclient.h b/src/apps/uclient/startuclient.h index 92f546d..72f8d67 100644 --- a/src/apps/uclient/startuclient.h +++ b/src/apps/uclient/startuclient.h @@ -31,7 +31,9 @@ #ifndef __STARTCLIENT_TURN__ #define __STARTCLIENT_TURN__ +#include "ns_turn_ioaddr.h" // for ioa_addr #include "session.h" +#include "stun_buffer.h" // for stun_buffer #ifdef __cplusplus extern "C" { diff --git a/src/client/ns_turn_ioaddr.c b/src/client/ns_turn_ioaddr.c index c520053..37a0ab7 100644 --- a/src/client/ns_turn_ioaddr.c +++ b/src/client/ns_turn_ioaddr.c @@ -30,6 +30,12 @@ #include "ns_turn_ioaddr.h" +#include "ns_turn_defs.h" // for nswap16, nswap32, STRCPY + +#include // for snprintf, fprintf, stderr +#include // for atoi, malloc, realloc, free +#include // for memcpy, strncpy, memset, NULL, memcmp, strstr + #if defined(__unix__) || defined(unix) || defined(__APPLE__) #include #endif diff --git a/src/client/ns_turn_msg.c b/src/client/ns_turn_msg.c index 769dba1..4a5531d 100644 --- a/src/client/ns_turn_msg.c +++ b/src/client/ns_turn_msg.c @@ -38,7 +38,10 @@ /////////// +#include // for tolower +#include // for fprintf, printf, stderr, snprintf #include +#include // for memcpy, strlen, memset, strncpy, strcmp /////////// diff --git a/src/client/ns_turn_msg.h b/src/client/ns_turn_msg.h index 219543b..cee053d 100644 --- a/src/client/ns_turn_msg.h +++ b/src/client/ns_turn_msg.h @@ -31,6 +31,7 @@ #ifndef __LIB_TURN_MSG__ #define __LIB_TURN_MSG__ +#include "ns_turn_defs.h" // for turn_time_t #include "ns_turn_ioaddr.h" #include "ns_turn_msg_defs.h" diff --git a/src/client/ns_turn_msg_addr.c b/src/client/ns_turn_msg_addr.c index 63a189b..019aa5f 100644 --- a/src/client/ns_turn_msg_addr.c +++ b/src/client/ns_turn_msg_addr.c @@ -29,6 +29,9 @@ */ #include "ns_turn_msg_addr.h" +#include "ns_turn_defs.h" // for nswap16, nswap32 + +#include // for memcpy ////////////////////////////////////////////////////////////////////////////// diff --git a/src/client/ns_turn_msg_addr.h b/src/client/ns_turn_msg_addr.h index c984fc5..72a465d 100644 --- a/src/client/ns_turn_msg_addr.h +++ b/src/client/ns_turn_msg_addr.h @@ -31,6 +31,7 @@ #ifndef __LIB_TURN_MSG_ADDR__ #define __LIB_TURN_MSG_ADDR__ +#include "ns_turn_defs.h" // for ioa_addr, uint8_t, uint32_t #include "ns_turn_ioaddr.h" #ifdef __cplusplus diff --git a/src/client/ns_turn_msg_defs.h b/src/client/ns_turn_msg_defs.h index 214deb9..c133fe5 100644 --- a/src/client/ns_turn_msg_defs.h +++ b/src/client/ns_turn_msg_defs.h @@ -31,7 +31,9 @@ #ifndef __LIB_TURN_MSG_DEFS__ #define __LIB_TURN_MSG_DEFS__ -#include "ns_turn_msg_defs_experimental.h" +#include "ns_turn_defs.h" // for turn_time_t + +#include "ns_turn_msg_defs_experimental.h" // IWYU pragma: export /////////////////////////////////////////// // http://www.iana.org/assignments/stun-parameters/stun-parameters.xhtml diff --git a/src/ns_turn_defs.h b/src/ns_turn_defs.h index 78722db..9fe0a3f 100644 --- a/src/ns_turn_defs.h +++ b/src/ns_turn_defs.h @@ -50,20 +50,21 @@ #include #include #else -#include -#include -#include -#include +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export +#include // IWYU pragma: export #include -#include -#include +#include // IWYU pragma: export +#include // IWYU pragma: export #endif #include #include #include #include -#include +#include // IWYU pragma: export +#include // IWYU pragma: export #include #include #include diff --git a/src/server/ns_turn_allocation.c b/src/server/ns_turn_allocation.c index 1ce8912..1abe346 100644 --- a/src/server/ns_turn_allocation.c +++ b/src/server/ns_turn_allocation.c @@ -30,6 +30,11 @@ #include "ns_turn_allocation.h" #include "ns_turn_ioalib.h" +#include "ns_turn_msg_defs.h" // for STUN_VALID_CHANNEL +#include "ns_turn_utils.h" // for TURN_LOG_FUNC, TURN_LOG_LEVEL_ERROR + +#include // for NULL, size_t, free, realloc, calloc +#include // for memset, memcpy /////////////// Permission forward declarations ///////////////// diff --git a/src/server/ns_turn_allocation.h b/src/server/ns_turn_allocation.h index eeb6979..33f850f 100644 --- a/src/server/ns_turn_allocation.h +++ b/src/server/ns_turn_allocation.h @@ -31,10 +31,11 @@ #ifndef __TURN_TURN_A_LIB__ #define __TURN_TURN_A_LIB__ +#include "ns_turn_defs.h" // for turn_time_t, TURN_CHANNEL_HANDLER_KERNEL +#include "ns_turn_ioaddr.h" // for ioa_addr #include "ns_turn_ioalib.h" #include "ns_turn_maps.h" #include "ns_turn_msg.h" -#include "ns_turn_utils.h" #ifdef __cplusplus extern "C" { @@ -105,8 +106,6 @@ typedef struct _tcp_connection_list { #define TURN_PERMISSION_HASHTABLE_SIZE (0x8) #define TURN_PERMISSION_ARRAY_SIZE (0x3) -struct _allocation; - typedef struct _ch_info { uint16_t chnum; int allocated; diff --git a/src/server/ns_turn_ioalib.h b/src/server/ns_turn_ioalib.h index b208330..867c0be 100644 --- a/src/server/ns_turn_ioalib.h +++ b/src/server/ns_turn_ioalib.h @@ -35,6 +35,7 @@ #ifndef __IOA_LIB__ #define __IOA_LIB__ +#include "ns_turn_defs.h" // for turn_time_t #include "ns_turn_ioaddr.h" #ifdef __cplusplus diff --git a/src/server/ns_turn_maps.c b/src/server/ns_turn_maps.c index 57afe51..a3f9a0a 100644 --- a/src/server/ns_turn_maps.c +++ b/src/server/ns_turn_maps.c @@ -34,6 +34,9 @@ #include "ns_turn_khash.h" +#include // for size_t, free, malloc, NULL, realloc +#include // for memset, strcmp, memcpy, strlen + KHASH_MAP_INIT_INT64(3, ur_map_value_type) #define MAGIC_HASH ((uint64_t)(0x90ABCDEFL)) diff --git a/src/server/ns_turn_maps.h b/src/server/ns_turn_maps.h index 88cfe37..f9c1e3f 100644 --- a/src/server/ns_turn_maps.h +++ b/src/server/ns_turn_maps.h @@ -31,6 +31,7 @@ #ifndef __TURN_MAPS__ #define __TURN_MAPS__ +#include "ns_turn_defs.h" // for size_t, uint64_t, uintptr_t #include "ns_turn_ioaddr.h" #include @@ -41,7 +42,7 @@ extern "C" { //////////////// UR MAP ////////////////// -struct _ur_map; +struct _ur_map; // IWYU pragma: keep typedef struct _ur_map ur_map; //////////////// Common Definitions ////// @@ -206,7 +207,6 @@ struct _ur_addr_map { uint64_t magic; }; -struct _ur_addr_map; typedef struct _ur_addr_map ur_addr_map; typedef void (*ur_addr_map_func)(ur_addr_map_value_type); @@ -245,7 +245,7 @@ size_t ur_addr_map_size(const ur_addr_map *map); typedef char *ur_string_map_key_type; typedef void *ur_string_map_value_type; -struct _ur_string_map; +struct _ur_string_map; // IWYU pragma: keep typedef struct _ur_string_map ur_string_map; typedef void (*ur_string_map_func)(ur_string_map_value_type); diff --git a/src/server/ns_turn_maps_rtcp.c b/src/server/ns_turn_maps_rtcp.c index bb477d7..f91e09e 100644 --- a/src/server/ns_turn_maps_rtcp.c +++ b/src/server/ns_turn_maps_rtcp.c @@ -30,7 +30,9 @@ #include "ns_turn_maps_rtcp.h" -#include "ns_turn_ioaddr.h" +#include "ns_turn_defs.h" // for NULL, UNUSED_ARG, size_t, turn_time, + +#include // for free, calloc //////////////////////////////////////////// diff --git a/src/server/ns_turn_maps_rtcp.h b/src/server/ns_turn_maps_rtcp.h index 1593125..40acd93 100644 --- a/src/server/ns_turn_maps_rtcp.h +++ b/src/server/ns_turn_maps_rtcp.h @@ -34,6 +34,8 @@ #include "ns_turn_ioalib.h" #include "ns_turn_maps.h" +#include // for size_t + #ifdef __cplusplus extern "C" { #endif @@ -42,7 +44,7 @@ extern "C" { typedef ur_map_key_type rtcp_token_type; -struct _rtcp_map; +struct _rtcp_map; // IWYU pragma: keep typedef struct _rtcp_map rtcp_map; //////////////////////////////////////////////// diff --git a/src/server/ns_turn_server.c b/src/server/ns_turn_server.c index 14d9482..d7b6b0c 100644 --- a/src/server/ns_turn_server.c +++ b/src/server/ns_turn_server.c @@ -33,8 +33,15 @@ #include "../apps/relay/ns_ioalib_impl.h" #include "ns_turn_allocation.h" #include "ns_turn_ioalib.h" +#include "ns_turn_msg_defs.h" // for STUN_ATTRIBUTE_NONCE #include "ns_turn_utils.h" +#include "apputils.h" // for turn_random, base64_decode + +#include // for snprintf +#include // for free, malloc, calloc, realloc +#include // for memcpy, strlen, strcmp + /////////////////////////////////////////// #define FUNCSTART \ diff --git a/src/server/ns_turn_server.h b/src/server/ns_turn_server.h index 526ab15..10d8081 100644 --- a/src/server/ns_turn_server.h +++ b/src/server/ns_turn_server.h @@ -31,8 +31,13 @@ #ifndef __TURN_SERVER__ #define __TURN_SERVER__ +#include "ns_turn_allocation.h" // for tcp_connection_id +#include "ns_turn_defs.h" // for vintp, uint8_t, size_t, uint64_t +#include "ns_turn_ioaddr.h" // for ioa_addr +#include "ns_turn_ioalib.h" // for ioa_net_data, ioa_engine_handle, ioa... +#include "ns_turn_maps.h" // for ur_map +#include "ns_turn_msg.h" // for turn_credential_type, band_limit_t #include "ns_turn_session.h" -#include "ns_turn_utils.h" #ifdef __cplusplus extern "C" {