Răsfoiți Sursa

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.
Michael Jones 1 an în urmă
părinte
comite
d1db5e590d

+ 6 - 0
.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,
 '

+ 1 - 1
.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

+ 1 - 1
.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

+ 2 - 1
src/apps/common/apputils.h

@@ -31,10 +31,11 @@
 #ifndef __APP_LIB__
 #define __APP_LIB__
 
-#include <event2/event.h>
+#include <event2/util.h> // 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"

+ 12 - 10
src/apps/common/ns_turn_openssl.h

@@ -31,15 +31,17 @@
 #ifndef __NST_OPENSSL_LIB__
 #define __NST_OPENSSL_LIB__
 
-#include <openssl/bio.h>
-#include <openssl/bn.h>
-#include <openssl/crypto.h>
-#include <openssl/dh.h>
-#include <openssl/err.h>
-#include <openssl/hmac.h>
-#include <openssl/md5.h>
-#include <openssl/opensslv.h>
-#include <openssl/rand.h>
-#include <openssl/ssl.h>
+#include <openssl/bio.h>      // IWYU pragma: export
+#include <openssl/bn.h>       // IWYU pragma: export
+#include <openssl/crypto.h>   // IWYU pragma: export
+#include <openssl/dh.h>       // IWYU pragma: export
+#include <openssl/err.h>      // IWYU pragma: export
+#include <openssl/evp.h>      // IWYU pragma: export
+#include <openssl/hmac.h>     // IWYU pragma: export
+#include <openssl/md5.h>      // IWYU pragma: export
+#include <openssl/opensslv.h> // IWYU pragma: export
+#include <openssl/rand.h>     // IWYU pragma: export
+#include <openssl/sha.h>      // IWYU pragma: export
+#include <openssl/ssl.h>      // IWYU pragma: export
 
 #endif //__NST_OPENSSL_LIB__

+ 3 - 1
src/apps/common/ns_turn_utils.c

@@ -51,12 +51,14 @@
 
 #if !defined(WINDOWS) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(__CYGWIN64__)
 #include <sys/syscall.h>
-#include <unistd.h>
 #ifdef SYS_gettid
 #define gettid() ((pid_t)syscall(SYS_gettid))
 #endif
 #endif
 
+#include <ctype.h>  // for tolower
+#include <string.h> // for memcmp, strstr, strcmp, strdup, strlen
+
 ////////// LOG TIME OPTIMIZATION ///////////
 
 static volatile int _log_file_line_set = 0;

+ 1 - 0
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

+ 2 - 0
src/apps/common/stun_buffer.c

@@ -30,6 +30,8 @@
 
 #include "stun_buffer.h"
 
+#include <string.h> // for memset
+
 ////////////////////// BUFFERS ///////////////////////////
 
 int stun_init_buffer(stun_buffer *buf) {

+ 3 - 0
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" {

+ 8 - 4
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 <unistd.h>
 #endif
@@ -39,10 +47,6 @@
 #include <string.h>
 #include <time.h>
 
-#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

+ 2 - 0
src/apps/peer/udpserver.h

@@ -37,6 +37,8 @@
 
 #include <event2/event.h>
 
+#include <stddef.h> // for size_t
+
 #ifdef __cplusplus
 extern "C" {
 #endif

+ 7 - 6
src/apps/relay/ns_ioalib_impl.h

@@ -35,15 +35,10 @@
 #ifndef __IOA_LIBIMPL__
 #define __IOA_LIBIMPL__
 
-#include <event2/buffer.h>
-#include <event2/bufferevent.h>
-#include <event2/event.h>
-
-#include <event2/thread.h>
+#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 <event2/buffer.h>
+#include <event2/bufferevent.h>
+#include <event2/event.h>
+
+#include <event2/thread.h>
+
 #include <pthread.h>
 
 #ifdef __cplusplus

+ 13 - 3
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 <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -66,12 +65,17 @@
 #include "mainrelay.h"
 #include "userdb.h"
 
-#include "ns_turn_utils.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 "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;

+ 4 - 5
src/apps/uclient/startuclient.c

@@ -28,13 +28,12 @@
  * SUCH DAMAGE.
  */
 
-#if defined(__linux__)
-#include <unistd.h>
-#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"

+ 2 - 0
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" {

+ 6 - 0
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 <stdio.h>  // for snprintf, fprintf, stderr
+#include <stdlib.h> // for atoi, malloc, realloc, free
+#include <string.h> // for memcpy, strncpy, memset, NULL, memcmp, strstr
+
 #if defined(__unix__) || defined(unix) || defined(__APPLE__)
 #include <netdb.h>
 #endif

+ 3 - 0
src/client/ns_turn_msg.c

@@ -38,7 +38,10 @@
 
 ///////////
 
+#include <ctype.h> // for tolower
+#include <stdio.h> // for fprintf, printf, stderr, snprintf
 #include <stdlib.h>
+#include <string.h> // for memcpy, strlen, memset, strncpy, strcmp
 
 ///////////
 

+ 1 - 0
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"
 

+ 3 - 0
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 <string.h> // for memcpy
 
 //////////////////////////////////////////////////////////////////////////////
 

+ 1 - 0
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

+ 3 - 1
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

+ 8 - 7
src/ns_turn_defs.h

@@ -50,20 +50,21 @@
 #include <process.h>
 #include <ws2tcpip.h>
 #else
-#include <arpa/inet.h>
-#include <net/if.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
+#include <arpa/inet.h>   // IWYU pragma: export
+#include <net/if.h>      // IWYU pragma: export
+#include <netinet/in.h>  // IWYU pragma: export
+#include <netinet/tcp.h> // IWYU pragma: export
 #include <strings.h>
-#include <sys/socket.h>
-#include <unistd.h>
+#include <sys/socket.h> // IWYU pragma: export
+#include <unistd.h>     // IWYU pragma: export
 #endif
 
 #include <ctype.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <stdarg.h>
-#include <stdint.h>
+#include <stddef.h> // IWYU pragma: export
+#include <stdint.h> // IWYU pragma: export
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

+ 5 - 0
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 <stdlib.h> // for NULL, size_t, free, realloc, calloc
+#include <string.h> // for memset, memcpy
 
 /////////////// Permission forward declarations /////////////////
 

+ 2 - 3
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;

+ 1 - 0
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

+ 3 - 0
src/server/ns_turn_maps.c

@@ -34,6 +34,9 @@
 
 #include "ns_turn_khash.h"
 
+#include <stdlib.h> // for size_t, free, malloc, NULL, realloc
+#include <string.h> // for memset, strcmp, memcpy, strlen
+
 KHASH_MAP_INIT_INT64(3, ur_map_value_type)
 
 #define MAGIC_HASH ((uint64_t)(0x90ABCDEFL))

+ 3 - 3
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 <stdbool.h>
@@ -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);

+ 3 - 1
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 <stdlib.h> // for free, calloc
 
 ////////////////////////////////////////////
 

+ 3 - 1
src/server/ns_turn_maps_rtcp.h

@@ -34,6 +34,8 @@
 #include "ns_turn_ioalib.h"
 #include "ns_turn_maps.h"
 
+#include <stddef.h> // 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;
 
 ////////////////////////////////////////////////

+ 7 - 0
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 <stdio.h>  // for snprintf
+#include <stdlib.h> // for free, malloc, calloc, realloc
+#include <string.h> // for memcpy, strlen, strcmp
+
 ///////////////////////////////////////////
 
 #define FUNCSTART                                                                                                      \

+ 6 - 1
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" {