Merge branch 'syssocket-bits' into 'master'

move sys_socket bits from C to cbindgen

See merge request redox-os/relibc!1185
This commit is contained in:
Jeremy Soller
2026-04-13 06:47:59 -06:00
7 changed files with 42 additions and 35 deletions
-26
View File
@@ -1,26 +0,0 @@
#ifndef _BITS_SYS_SOCKET_H
#define _BITS_SYS_SOCKET_H
struct sockaddr_storage {
sa_family_t ss_family;
char __ss_padding[128-sizeof(long)-sizeof(sa_family_t)];
unsigned long __ss_align;
};
// These definitions were taken from musl, license MIT {
#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1))
#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg))
#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen)
#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1))
#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \
__CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \
? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg))
#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0)
#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))
#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr)))
#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
// } from musl, license MIT
#endif // _BITS_SYS_SOCKET_H
+2 -1
View File
@@ -1,4 +1,5 @@
sys_includes = ["features.h", "netinet/in.h", "sys/socket.h"]
# netinet/in.h brings in sys/socket.h
sys_includes = ["features.h", "netinet/in.h"]
include_guard = "_RELIBC_IFADDRS_H"
language = "C"
style = "Tag"
+2 -1
View File
@@ -1,4 +1,5 @@
sys_includes = ["sys/socket.h", "netinet/in.h", "features.h"]
# netinet/in.h brings in sys/socket.h
sys_includes = ["netinet/in.h", "features.h"]
include_guard = "_RELIBC_NETDB_H"
trailer = """
#define h_errno (*__h_errno_location())
+30 -3
View File
@@ -1,10 +1,37 @@
sys_includes = ["stddef.h", "stdint.h", "sys/types.h"]
include_guard = "_SYS_SOCKET_H"
include_guard = "_RELIBC_SYS_SOCKET_H"
after_includes = """
#include <bits/iovec.h> // for iovec
#include <bits/iovec.h> // for iovec from sys/uio.h
#include <bits/socklen-t.h> // for socklen_t
"""
trailer = "#include <bits/sys/socket.h>"
trailer = """
#ifndef _RELIBC_BITS_SYS_SOCKET_H
#define _RELIBC_BITS_SYS_SOCKET_H
struct sockaddr_storage {
sa_family_t ss_family;
char __ss_padding[128-sizeof(long)-sizeof(sa_family_t)];
unsigned long __ss_align;
};
// These definitions were taken from musl, license MIT {
#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1))
#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg))
#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen)
#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1))
#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \
__CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \
? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg))
#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0)
#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))
#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr)))
#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
// } from musl, license MIT
#endif // _RELIBC_BITS_SYS_SOCKET_H
"""
language = "C"
style = "Tag"
no_includes = true
+8 -2
View File
@@ -76,8 +76,10 @@ pub struct sockaddr {
}
// Max size of [`sockaddr_storage`]
/// cbindgen:ignore
const _SS_MAXSIZE: usize = 128;
// Align to pointer width
/// cbindgen:ignore
const _SS_PADDING: usize = _SS_MAXSIZE - mem::size_of::<sa_family_t>() - mem::size_of::<usize>();
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
@@ -91,22 +93,26 @@ const _SS_PADDING: usize = _SS_MAXSIZE - mem::size_of::<sa_family_t>() - mem::si
/// from protocol structs in C
#[repr(C)]
//#[derive(CheckVsLibcCrate)] FIXME: can't ignore private fields yet
/// cbindgen:ignore
pub struct sockaddr_storage {
pub ss_family: sa_family_t,
__ss_pad2: [u8; _SS_PADDING],
__ss_align: usize,
}
// These must match C macros in include/bits/sys/socket.h {
// These must match C macros in sys_socket/cbindgen.toml {
/// cbindgen:ignore
pub unsafe extern "C" fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t {
((unsafe { (*cmsg).cmsg_len as size_t } + mem::size_of::<c_long>() - 1)
& !(mem::size_of::<c_long>() - 1)) as ssize_t
}
/// cbindgen:ignore
pub unsafe extern "C" fn __CMSG_NEXT(cmsg: *const cmsghdr) -> *mut c_uchar {
unsafe { (cmsg as *mut c_uchar).offset(__CMSG_LEN(cmsg)) }
}
/// cbindgen:ignore
pub unsafe extern "C" fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar {
unsafe { ((*mhdr).msg_control.cast::<c_uchar>()).add((*mhdr).msg_controllen) }
}
@@ -165,7 +171,7 @@ pub unsafe extern "C" fn CMSG_SPACE(len: c_uint) -> c_uint {
pub unsafe extern "C" fn CMSG_LEN(length: c_uint) -> c_uint {
(unsafe { CMSG_ALIGN(mem::size_of::<cmsghdr>()) } + length as usize) as c_uint
}
// } These must match C macros in include/bits/sys/socket.h
// } These must match C macros in sys_socket/cbindgen.toml
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/accept.html>.
#[unsafe(no_mangle)]
-1
View File
@@ -5,7 +5,6 @@
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "test_helpers.h"
-1
View File
@@ -3,7 +3,6 @@
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>