Ensure <sys/socket.h> is included by using "rtc_base/net_helpers.h".

* IWYU export <sys/socket.h> from rtc_base/net_helpers.h.
* Add a presubmit check to ensures that <sys/socket.h> is included through net_helpers.h (expect if there is a IWYU pragma or a no-presubmit-check).
* Clean up existing includes of <sys/socket.h>

Change-Id: I4bc6cce045c046287f8f74f89edfc9321293b274
Bug: b/236227627
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/362082
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Jeremy Leconte <jleconte@google.com>
Cr-Commit-Position: refs/heads/main@{#42996}
This commit is contained in:
Jeremy Leconte 2024-09-10 09:47:49 +02:00 committed by WebRTC LUCI CQ
parent 84273f56d9
commit 83d1f9abd0
20 changed files with 45 additions and 24 deletions

View File

@ -1051,6 +1051,8 @@ def CommonChecks(input_api, output_api):
non_third_party_sources))
results.extend(
CheckObjcApiSymbols(input_api, output_api, non_third_party_sources))
results.extend(
CheckSysSocketInclude(input_api, output_api, non_third_party_sources))
return results
@ -1157,6 +1159,29 @@ def CheckBannedAbslOptional(input_api, output_api, source_file_filter):
return []
def CheckSysSocketInclude(input_api, output_api, source_file_filter):
sys_socket = re.compile(
'^#include <sys/socket.h>((?!IWYU pragma|no-presubmit-check).)*$')
file_filter = lambda f: (f.LocalPath().endswith(
('.cc', '.h')) and source_file_filter(f))
files = []
for f in input_api.AffectedFiles(include_deletes=False,
file_filter=file_filter):
for _, line in f.ChangedContents():
if sys_socket.search(line):
files.append(f.LocalPath())
break
if files:
return [
output_api.PresubmitError(
'PleaseInclude "rtc_base/net_helpers.h" instead of '
'<sys/socket.h>.\nAffected files:', files)
]
return []
def CheckObjcApiSymbols(input_api, output_api, source_file_filter):
rtc_objc_export = re.compile(r'RTC_OBJC_EXPORT(.|\n){26}',
re.MULTILINE | re.DOTALL)

View File

@ -48,6 +48,7 @@ rtc_library("network_emulation") {
"../../../rtc_base:copy_on_write_buffer",
"../../../rtc_base:ip_address",
"../../../rtc_base:net_helper",
"../../../rtc_base:net_helpers",
"../../../rtc_base:socket_address",
"../../numerics",
"../../task_queue",

View File

@ -9,10 +9,6 @@
*/
#include "api/test/network_emulation/network_emulation_interfaces.h"
#if defined(WEBRTC_POSIX)
#include <sys/socket.h>
#endif
#include <cstdint>
#include "api/units/data_rate.h"
@ -20,6 +16,7 @@
#include "rtc_base/checks.h"
#include "rtc_base/copy_on_write_buffer.h"
#include "rtc_base/net_helper.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/socket_address.h"
namespace webrtc {

View File

@ -117,6 +117,7 @@ rtc_source_set("stun_types") {
"../../rtc_base:digest",
"../../rtc_base:ip_address",
"../../rtc_base:logging",
"../../rtc_base:net_helpers",
"../../rtc_base:socket_address",
"../../system_wrappers:metrics",
"//third_party/abseil-cpp/absl/strings:string_view",

View File

@ -2,6 +2,7 @@ specific_include_rules = {
"stun\.h": [
"+rtc_base/byte_buffer.h",
"+rtc_base/ip_address.h",
"+rtc_base/net_helpers.h",
"+rtc_base/socket_address.h",
],
}

View File

@ -12,7 +12,6 @@
#if defined(WEBRTC_POSIX)
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <string.h>
@ -36,6 +35,7 @@
#include "rtc_base/ip_address.h"
#include "rtc_base/logging.h"
#include "rtc_base/message_digest.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/socket_address.h"
#include "system_wrappers/include/metrics.h"

View File

@ -14,9 +14,6 @@
// This file contains classes for dealing with the STUN protocol, as specified
// in RFC 5389, and its descendants.
#if defined(WEBRTC_POSIX)
#include <sys/socket.h>
#endif
#include <stddef.h>
#include <stdint.h>
@ -30,6 +27,7 @@
#include "rtc_base/byte_buffer.h"
#include "rtc_base/checks.h"
#include "rtc_base/ip_address.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/socket_address.h"
namespace cricket {

View File

@ -792,6 +792,7 @@ if (is_linux || is_chromeos || is_win) {
]
deps = [
"../rtc_base:checks",
"../rtc_base:net_helpers",
"../rtc_base:stringutils",
"../system_wrappers:field_trial",
"../test:field_trial",

View File

@ -11,7 +11,7 @@
#include "examples/androidvoip/jni/android_voip_client.h"
#include <errno.h>
#include <sys/socket.h>
#include <sys/socket.h> // no-presubmit-check
#include <algorithm>
#include <map>

View File

@ -11,14 +11,16 @@
#ifndef EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_
#define EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_
#include <string>
#include "rtc_base/net_helpers.h"
#ifdef WIN32
#include <winsock2.h>
typedef int socklen_t;
typedef SOCKET NativeSocket;
#else
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/socket.h>
#define closesocket close
typedef int NativeSocket;
@ -31,7 +33,6 @@ typedef int NativeSocket;
#endif
#endif
#include <string>
class SocketBase {
public:

View File

@ -1158,6 +1158,7 @@ rtc_source_set("peer_connection") {
"../rtc_base:logging",
"../rtc_base:macromagic",
"../rtc_base:net_helper",
"../rtc_base:net_helpers",
"../rtc_base:network",
"../rtc_base:network_constants",
"../rtc_base:socket_address",

View File

@ -109,6 +109,7 @@
#include "rtc_base/ip_address.h"
#include "rtc_base/logging.h"
#include "rtc_base/net_helper.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/network.h"
#include "rtc_base/network_constants.h"
#include "rtc_base/rtc_certificate.h"

View File

@ -1064,6 +1064,7 @@ rtc_library("socket") {
":buffer",
":checks",
":macromagic",
":net_helpers",
":socket_address",
"../api/units:timestamp",
"./network:ecn_marking",

View File

@ -19,7 +19,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/socket.h> // no-presubmit-check
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>

View File

@ -12,7 +12,7 @@
#define RTC_BASE_IFADDRS_ANDROID_H_
#include <stdio.h>
#include <sys/socket.h>
#include <sys/socket.h> // no-presubmit-check
// Implementation of getifaddrs for Android.
// Fills out a list of ifaddr structs (see below) which contain information

View File

@ -10,7 +10,6 @@
#if defined(WEBRTC_POSIX)
#include <netinet/in.h>
#include <sys/socket.h>
#include "absl/strings/string_view.h"
#ifdef OPENBSD

View File

@ -15,12 +15,10 @@
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "absl/strings/string_view.h"
#endif
#if defined(WEBRTC_WIN)
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <string.h>
@ -32,6 +30,7 @@
#include "rtc_base/win32.h"
#endif
#include "absl/strings/string_view.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/system/rtc_export.h"
namespace rtc {

View File

@ -12,7 +12,7 @@
#define RTC_BASE_NET_HELPERS_H_
#if defined(WEBRTC_POSIX)
#include <sys/socket.h>
#include <sys/socket.h> // IWYU pragma: export
#elif WEBRTC_WIN
#include <winsock2.h> // NOLINT

View File

@ -20,17 +20,13 @@
#if defined(WEBRTC_POSIX)
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#define SOCKET_EACCES EACCES
#endif
#if defined(WEBRTC_WIN)
#include "rtc_base/win32.h"
#endif
#include "api/units/timestamp.h"
#include "rtc_base/buffer.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/network/ecn_marking.h"
#include "rtc_base/socket_address.h"
#include "rtc_base/system/rtc_export.h"

View File

@ -15,7 +15,6 @@
#if defined(WEBRTC_POSIX)
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#if defined(OPENBSD)
#include <netinet/in_systm.h>