diff --git a/libc/include/bits/sockaddr_storage.h b/libc/include/bits/sockaddr_storage.h new file mode 100644 index 000000000..effafab0c --- /dev/null +++ b/libc/include/bits/sockaddr_storage.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#pragma once + +/** + * @file bits/sockaddr_storage.h + * @brief The `sockaddr_storage` struct. + */ + +#include + +#include + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wnullability-completeness" +/** + * [sockaddr_storage](https://man7.org/linux/man-pages/man3/sockaddr.3type.html) + * is a structure large enough to contain any other `sockaddr_*` type, used to + * pass socket addresses without needing to know what kind of socket address + * you're passing. + */ +struct sockaddr_storage { + union { + struct { + sa_family_t ss_family; + char __data[128 - sizeof(sa_family_t)]; + }; + void* __align; + }; +}; +#pragma clang diagnostic pop diff --git a/libc/include/netinet/in.h b/libc/include/netinet/in.h index b235e6e38..163e614d9 100644 --- a/libc/include/netinet/in.h +++ b/libc/include/netinet/in.h @@ -33,20 +33,10 @@ #include #include -// Include linux/socket.h first to trigger the header guard without -// the__kernel_sockaddr_storage define, so its definition uses the -// kernel name. -#include - -// Redefine __kernel_sockaddr_storage to sockaddr_storage so that -// the structs defined in linux/in.h use the sockaddr_storage defined -// in sys/sockets.h. -#define __kernel_sockaddr_storage sockaddr_storage #include -#undef __kernel_sockaddr_storage - #include #include +#include __BEGIN_DECLS diff --git a/libc/include/sys/socket.h b/libc/include/sys/socket.h index 22b88cba4..9402e7007 100644 --- a/libc/include/sys/socket.h +++ b/libc/include/sys/socket.h @@ -39,6 +39,7 @@ #include #include +#include #include __BEGIN_DECLS @@ -72,15 +73,6 @@ struct sockaddr { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wnullability-completeness" -struct sockaddr_storage { - union { - struct { - sa_family_t ss_family; - char __data[128 - sizeof(sa_family_t)]; - }; - void* __align; - }; -}; struct linger { int l_onoff; diff --git a/libc/kernel/tools/cpp.py b/libc/kernel/tools/cpp.py index 6939bdafa..08b786ac5 100755 --- a/libc/kernel/tools/cpp.py +++ b/libc/kernel/tools/cpp.py @@ -1247,8 +1247,8 @@ class BlockList(object): # Add an include for the structure to be removed of the form: # #include struct_token = b.tokens[i + 1] - if not structs[struct_token.id]: - extra_includes.add("" % struct_token.id) + if struct_token.id in structs and structs[struct_token.id]: + extra_includes.add("<%s>" % structs[struct_token.id]) # Search forward for the end of the structure. # Very simple search, look for } and ; tokens. diff --git a/libc/kernel/tools/defaults.py b/libc/kernel/tools/defaults.py index 9b357f128..1d7b4276a 100644 --- a/libc/kernel/tools/defaults.py +++ b/libc/kernel/tools/defaults.py @@ -28,23 +28,23 @@ kernel_known_macros = { } # This is the set of known kernel data structures we want to remove from -# the final headers. If the map value is False, that means that in -# addition to removing the structure, add an #include -# to the file. +# the final headers. If the map value is non-empty, that means that in +# addition to removing the structure, add a #include to the file. kernel_structs_to_remove = { - # Remove the structures since they are still the same as + # Remove these structures since they are still the same as # timeval, itimerval. - "__kernel_old_timeval": True, - "__kernel_old_itimerval": True, + "__kernel_old_timeval": None, + "__kernel_old_itimerval": None, # Replace all of the below structures with #include - "epoll_event": False, - "flock": False, - "flock64": False, - "in_addr": False, - "ip_mreq_source": False, - "ip_msfilter": False, - "tcphdr": False, - "timespec": False, + "__kernel_sockaddr_storage": "bits/sockaddr_storage.h", + "epoll_event": "bits/epoll_event.h", + "flock": "bits/flock.h", + "flock64": "bits/flock64.h", + "in_addr": "bits/in_addr.h", + "ip_mreq_source": "bits/ip_mreq_source.h", + "ip_msfilter": "bits/ip_msfilter.h", + "tcphdr": "bits/tcphdr.h", + "timespec": "bits/timespec.h", } # define to true if you want to remove all defined(CONFIG_FOO) tests @@ -92,6 +92,8 @@ kernel_token_replacements = { "__kernel_old_timeval": "timeval", # Do the same for __kernel_old_itimerval as for timeval. "__kernel_old_itimerval": "itimerval", + # Do the same for __kernel_sockaddr_storage. + "__kernel_sockaddr_storage": "sockaddr_storage", # Replace __packed with __attribute__((__packed__)) to avoid depending # on sys/cdefs.h "__packed": "__attribute__((__packed__))", diff --git a/libc/kernel/uapi/linux/in.h b/libc/kernel/uapi/linux/in.h index e784690cd..44efdd830 100644 --- a/libc/kernel/uapi/linux/in.h +++ b/libc/kernel/uapi/linux/in.h @@ -148,32 +148,32 @@ struct ip_mreqn { #define IP_MSFILTER_SIZE(numsrc) (sizeof(struct ip_msfilter) - sizeof(__u32) + (numsrc) * sizeof(__u32)) struct group_req { __u32 gr_interface; - struct __kernel_sockaddr_storage gr_group; + struct sockaddr_storage gr_group; }; struct group_source_req { __u32 gsr_interface; - struct __kernel_sockaddr_storage gsr_group; - struct __kernel_sockaddr_storage gsr_source; + struct sockaddr_storage gsr_group; + struct sockaddr_storage gsr_source; }; struct group_filter { union { struct { __u32 gf_interface_aux; - struct __kernel_sockaddr_storage gf_group_aux; + struct sockaddr_storage gf_group_aux; __u32 gf_fmode_aux; __u32 gf_numsrc_aux; - struct __kernel_sockaddr_storage gf_slist[1]; + struct sockaddr_storage gf_slist[1]; }; struct { __u32 gf_interface; - struct __kernel_sockaddr_storage gf_group; + struct sockaddr_storage gf_group; __u32 gf_fmode; __u32 gf_numsrc; - struct __kernel_sockaddr_storage gf_slist_flex[]; + struct sockaddr_storage gf_slist_flex[]; }; }; }; -#define GROUP_FILTER_SIZE(numsrc) (sizeof(struct group_filter) - sizeof(struct __kernel_sockaddr_storage) + (numsrc) * sizeof(struct __kernel_sockaddr_storage)) +#define GROUP_FILTER_SIZE(numsrc) (sizeof(struct group_filter) - sizeof(struct sockaddr_storage) + (numsrc) * sizeof(struct sockaddr_storage)) #endif #if __UAPI_DEF_IN_PKTINFO struct in_pktinfo { diff --git a/libc/kernel/uapi/linux/mptcp.h b/libc/kernel/uapi/linux/mptcp.h index f00abae31..d97f29bd8 100644 --- a/libc/kernel/uapi/linux/mptcp.h +++ b/libc/kernel/uapi/linux/mptcp.h @@ -163,13 +163,13 @@ struct mptcp_subflow_addrs { struct sockaddr sa_local; struct sockaddr_in sin_local; struct sockaddr_in6 sin6_local; - struct __kernel_sockaddr_storage ss_local; + struct sockaddr_storage ss_local; }; union { struct sockaddr sa_remote; struct sockaddr_in sin_remote; struct sockaddr_in6 sin6_remote; - struct __kernel_sockaddr_storage ss_remote; + struct sockaddr_storage ss_remote; }; }; struct mptcp_subflow_info { diff --git a/libc/kernel/uapi/linux/rds.h b/libc/kernel/uapi/linux/rds.h index bc4194669..e2a7a38f0 100644 --- a/libc/kernel/uapi/linux/rds.h +++ b/libc/kernel/uapi/linux/rds.h @@ -205,7 +205,7 @@ struct rds_get_mr_args { __u64 flags; }; struct rds_get_mr_for_dest_args { - struct __kernel_sockaddr_storage dest_addr; + struct sockaddr_storage dest_addr; struct rds_iovec vec; __u64 cookie_addr; __u64 flags; diff --git a/libc/kernel/uapi/linux/socket.h b/libc/kernel/uapi/linux/socket.h index 936f44afd..27890a422 100644 --- a/libc/kernel/uapi/linux/socket.h +++ b/libc/kernel/uapi/linux/socket.h @@ -6,17 +6,9 @@ */ #ifndef _UAPI_LINUX_SOCKET_H #define _UAPI_LINUX_SOCKET_H +#include #define _K_SS_MAXSIZE 128 typedef unsigned short __kernel_sa_family_t; -struct __kernel_sockaddr_storage { - union { - struct { - __kernel_sa_family_t ss_family; - char __data[_K_SS_MAXSIZE - sizeof(unsigned short)]; - }; - void * __align; - }; -}; #define SOCK_SNDBUF_LOCK 1 #define SOCK_RCVBUF_LOCK 2 #define SOCK_BUF_LOCK_MASK (SOCK_SNDBUF_LOCK | SOCK_RCVBUF_LOCK) diff --git a/libc/kernel/uapi/linux/tcp.h b/libc/kernel/uapi/linux/tcp.h index 20fbcec9f..65bab5466 100644 --- a/libc/kernel/uapi/linux/tcp.h +++ b/libc/kernel/uapi/linux/tcp.h @@ -202,7 +202,7 @@ enum { #define TCP_MD5SIG_FLAG_PREFIX 0x1 #define TCP_MD5SIG_FLAG_IFINDEX 0x2 struct tcp_md5sig { - struct __kernel_sockaddr_storage tcpm_addr; + struct sockaddr_storage tcpm_addr; __u8 tcpm_flags; __u8 tcpm_prefixlen; __u16 tcpm_keylen; diff --git a/libc/kernel/uapi/rdma/rdma_user_cm.h b/libc/kernel/uapi/rdma/rdma_user_cm.h index dbe79bc21..5228e62f8 100644 --- a/libc/kernel/uapi/rdma/rdma_user_cm.h +++ b/libc/kernel/uapi/rdma/rdma_user_cm.h @@ -76,7 +76,7 @@ struct rdma_ucm_bind { __u32 id; __u16 addr_size; __u16 reserved; - struct __kernel_sockaddr_storage addr; + struct sockaddr_storage addr; }; struct rdma_ucm_resolve_ip { struct sockaddr_in6 src_addr; @@ -90,8 +90,8 @@ struct rdma_ucm_resolve_addr { __u16 src_size; __u16 dst_size; __u32 reserved; - struct __kernel_sockaddr_storage src_addr; - struct __kernel_sockaddr_storage dst_addr; + struct sockaddr_storage src_addr; + struct sockaddr_storage dst_addr; }; struct rdma_ucm_resolve_route { __u32 id; @@ -125,8 +125,8 @@ struct rdma_ucm_query_addr_resp { __u16 pkey; __u16 src_size; __u16 dst_size; - struct __kernel_sockaddr_storage src_addr; - struct __kernel_sockaddr_storage dst_addr; + struct sockaddr_storage src_addr; + struct sockaddr_storage dst_addr; __u32 ibdev_index; __u32 reserved1; }; @@ -213,7 +213,7 @@ struct rdma_ucm_join_mcast { __u32 id; __u16 addr_size; __u16 join_flags; - struct __kernel_sockaddr_storage addr; + struct sockaddr_storage addr; }; struct rdma_ucm_get_event { __aligned_u64 response;