Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ let package = Package(
"NIOConcurrencyHelpers",
"_NIOBase64",
"CNIOOpenBSD",
"CNIOFreeBSD",
"CNIODarwin",
"CNIOLinux",
"CNIOWindows",
Expand Down Expand Up @@ -167,6 +168,10 @@ let package = Package(
name: "CNIOOpenBSD",
dependencies: []
),
.target(
name: "CNIOFreeBSD",
dependencies: []
),
.target(
name: "CNIOLinux",
dependencies: [],
Expand Down
57 changes: 57 additions & 0 deletions Sources/CNIOFreeBSD/include/CNIOFreeBSD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2026 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#ifndef C_NIO_FREEBSD_H
#define C_NIO_FREEBSD_H

#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <pthread.h>
#include <pthread_np.h>

typedef struct {
struct msghdr msg_hdr;
unsigned int msg_len;
} CNIOFreeBSD_mmsghdr;

typedef struct {
struct in6_addr ipi6_addr;
unsigned int ipi6_ifindex;
} CNIOFreeBSD_in6_pktinfo;

int CNIOFreeBSD_sendmmsg(int sockfd, CNIOFreeBSD_mmsghdr *msgvec, unsigned int vlen, int flags);
int CNIOFreeBSD_recvmmsg(int sockfd, CNIOFreeBSD_mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout);

int CNIOFreeBSD_pthread_setname_np(pthread_t thread, const char *name);
int CNIOFreeBSD_pthread_getname_np(pthread_t thread, char *name, size_t len);

struct cmsghdr *CNIOFreeBSD_CMSG_FIRSTHDR(const struct msghdr *);
struct cmsghdr *CNIOFreeBSD_CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
const void *CNIOFreeBSD_CMSG_DATA(const struct cmsghdr *);
void *CNIOFreeBSD_CMSG_DATA_MUTABLE(struct cmsghdr *);
size_t CNIOFreeBSD_CMSG_LEN(size_t);
size_t CNIOFreeBSD_CMSG_SPACE(size_t);

extern const int CNIOFreeBSD_IP_PKTINFO;
extern const int CNIOFreeBSD_IPV6_RECVPKTINFO;
extern const int CNIOFreeBSD_IPV6_PKTINFO;

const char *CNIOFreeBSD_inet_ntop(int af, const void *src, char *dst, socklen_t size);
int CNIOFreeBSD_inet_pton(int af, const char *src, void *dst);

#endif
#endif
93 changes: 93 additions & 0 deletions Sources/CNIOFreeBSD/shim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2026 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

void CNIOFreeBSD_i_do_nothing_just_working_around_a_darwin_toolchain_bug(void) {}

#if defined(__FreeBSD__)

#include <CNIOFreeBSD.h>
#include <assert.h>

int CNIOFreeBSD_sendmmsg(int sockfd, CNIOFreeBSD_mmsghdr *msgvec, unsigned int vlen, int flags) {
return sendmmsg(sockfd, (struct mmsghdr *)msgvec, vlen, flags);
}

int CNIOFreeBSD_recvmmsg(int sockfd, CNIOFreeBSD_mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout) {
return recvmmsg(sockfd, (struct mmsghdr *)msgvec, vlen, flags, timeout);
}

int CNIOFreeBSD_pthread_setname_np(pthread_t thread, const char *name) {
pthread_set_name_np(thread, name);
return 0;
}

int CNIOFreeBSD_pthread_getname_np(pthread_t thread, char *name, size_t len) {
pthread_get_name_np(thread, name, len);
return 0;
}

struct cmsghdr *CNIOFreeBSD_CMSG_FIRSTHDR(const struct msghdr *mhdr) {
assert(mhdr != NULL);
return CMSG_FIRSTHDR(mhdr);
}

struct cmsghdr *CNIOFreeBSD_CMSG_NXTHDR(struct msghdr *mhdr, struct cmsghdr *cmsg) {
assert(mhdr != NULL);
assert(cmsg != NULL);
return CMSG_NXTHDR(mhdr, cmsg);
}

const void *CNIOFreeBSD_CMSG_DATA(const struct cmsghdr *cmsg) {
assert(cmsg != NULL);
return CMSG_DATA(cmsg);
}

void *CNIOFreeBSD_CMSG_DATA_MUTABLE(struct cmsghdr *cmsg) {
assert(cmsg != NULL);
return CMSG_DATA(cmsg);
}

size_t CNIOFreeBSD_CMSG_LEN(size_t payloadSizeBytes) {
return CMSG_LEN(payloadSizeBytes);
}

size_t CNIOFreeBSD_CMSG_SPACE(size_t payloadSizeBytes) {
return CMSG_SPACE(payloadSizeBytes);
}

#ifdef IP_PKTINFO
const int CNIOFreeBSD_IP_PKTINFO = IP_PKTINFO;
#else
const int CNIOFreeBSD_IP_PKTINFO = -1;
#endif
#ifdef IPV6_RECVPKTINFO
const int CNIOFreeBSD_IPV6_RECVPKTINFO = IPV6_RECVPKTINFO;
#else
const int CNIOFreeBSD_IPV6_RECVPKTINFO = -1;
#endif
#ifdef IPV6_PKTINFO
const int CNIOFreeBSD_IPV6_PKTINFO = IPV6_PKTINFO;
#else
const int CNIOFreeBSD_IPV6_PKTINFO = -1;
#endif

const char *CNIOFreeBSD_inet_ntop(int af, const void *src, char *dst, socklen_t size) {
return inet_ntop(af, src, dst, size);
}

int CNIOFreeBSD_inet_pton(int af, const char *src, void *dst) {
return inet_pton(af, src, dst);
}

#endif
10 changes: 10 additions & 0 deletions Sources/NIOCore/BSDSocketAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ private let sysInet_ntop:
inet_ntop
private let sysInet_pton: @convention(c) (CInt, UnsafePointer<CChar>?, UnsafeMutableRawPointer?) -> CInt = inet_pton
#endif
#elseif os(FreeBSD)
@preconcurrency import Glibc
import CNIOFreeBSD

private let sysInet_ntop:
@convention(c) (CInt, UnsafeRawPointer?, UnsafeMutablePointer<CChar>?, socklen_t) -> UnsafePointer<CChar>? =
CNIOFreeBSD_inet_ntop
private let sysInet_pton: @convention(c) (CInt, UnsafePointer<CChar>?, UnsafeMutableRawPointer?) -> CInt =
CNIOFreeBSD_inet_pton

#elseif os(OpenBSD)
@preconcurrency import Glibc
import CNIOOpenBSD
Expand Down
9 changes: 6 additions & 3 deletions Sources/NIOCore/Interfaces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if os(Linux) || os(FreeBSD) || os(Android)
#if os(Linux) || os(Android)
#if canImport(Glibc)
@preconcurrency import Glibc
#elseif canImport(Musl)
Expand All @@ -20,6 +20,9 @@
@preconcurrency import Bionic
#endif
import CNIOLinux
#elseif os(FreeBSD)
@preconcurrency import Glibc
import CNIOFreeBSD
#elseif os(OpenBSD)
import CNIOOpenBSD
#elseif canImport(Darwin)
Expand Down Expand Up @@ -53,15 +56,15 @@ extension ifaddrs {
fileprivate var dstaddr: UnsafeMutablePointer<sockaddr>? {
#if os(Linux) || os(Android)
return self.ifa_ifu.ifu_dstaddr
#elseif canImport(Darwin) || os(OpenBSD)
#elseif canImport(Darwin) || os(FreeBSD) || os(OpenBSD)
return self.ifa_dstaddr
#endif
}

fileprivate var broadaddr: UnsafeMutablePointer<sockaddr>? {
#if os(Linux) || os(Android)
return self.ifa_ifu.ifu_broadaddr
#elseif canImport(Darwin) || os(OpenBSD)
#elseif canImport(Darwin) || os(FreeBSD) || os(OpenBSD)
return self.ifa_dstaddr
#endif
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/NIOCore/SocketAddresses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private typealias in_port_t = WinSDK.u_short
private typealias sa_family_t = WinSDK.ADDRESS_FAMILY
#elseif canImport(Darwin)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
#elseif os(Linux) || os(Android)
#if canImport(Glibc)
@preconcurrency import Glibc
#elseif canImport(Musl)
Expand All @@ -53,6 +53,9 @@ import Darwin
@preconcurrency import Android
#endif
import CNIOLinux
#elseif os(FreeBSD)
@preconcurrency import Glibc
import CNIOFreeBSD
#elseif os(OpenBSD)
@preconcurrency import Glibc
import CNIOOpenBSD
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOCore/SocketOptionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
import Darwin
#elseif os(Linux) || os(Android)
#elseif os(Linux) || os(FreeBSD) || os(Android)
#if canImport(Glibc)
@preconcurrency import Glibc
#elseif canImport(Musl)
Expand Down
8 changes: 8 additions & 0 deletions Sources/NIOPosix/BSDSocketAPIPosix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ private let CMSG_DATA = CNIOOpenBSD_CMSG_DATA
private let CMSG_DATA_MUTABLE = CNIOOpenBSD_CMSG_DATA_MUTABLE
private let CMSG_SPACE = CNIOOpenBSD_CMSG_SPACE
private let CMSG_LEN = CNIOOpenBSD_CMSG_LEN
#elseif os(FreeBSD)
import CNIOFreeBSD
private let CMSG_FIRSTHDR = CNIOFreeBSD_CMSG_FIRSTHDR
private let CMSG_NXTHDR = CNIOFreeBSD_CMSG_NXTHDR
private let CMSG_DATA = CNIOFreeBSD_CMSG_DATA
private let CMSG_DATA_MUTABLE = CNIOFreeBSD_CMSG_DATA_MUTABLE
private let CMSG_SPACE = CNIOFreeBSD_CMSG_SPACE
private let CMSG_LEN = CNIOFreeBSD_CMSG_LEN
#else
import CNIOLinux
private let CMSG_FIRSTHDR = CNIOLinux_CMSG_FIRSTHDR
Expand Down
1 change: 1 addition & 0 deletions Sources/NIOPosix/BaseSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#if !os(WASI)

import CNIOFreeBSD
import CNIOLinux
import CNIOOpenBSD
import NIOConcurrencyHelpers
Expand Down
6 changes: 4 additions & 2 deletions Sources/NIOPosix/ControlMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import NIOCore

#if canImport(Darwin)
import CNIODarwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
#elseif os(FreeBSD)
import CNIOFreeBSD
#elseif os(Linux) || os(Android)
import CNIOLinux
#elseif os(OpenBSD)
import CNIOOpenBSD
Expand Down Expand Up @@ -243,7 +245,7 @@ struct ControlMessageParser {
self.ecnValue = .init(receivedValue: readValue)
}
} else if controlMessage.type == Posix.IP_PKTINFO {
#if !os(OpenBSD)
#if !os(OpenBSD) && !os(FreeBSD)
if let data = controlMessage.data {
let info = data.load(as: in_pktinfo.self)
var addr = sockaddr_in()
Expand Down
1 change: 1 addition & 0 deletions Sources/NIOPosix/DatagramVectorReadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#if !os(WASI)

import CNIODarwin
import CNIOFreeBSD
import CNIOLinux
import CNIOOpenBSD
import NIOCore
Expand Down
4 changes: 2 additions & 2 deletions Sources/NIOPosix/NonBlockingFileIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ public struct NonBlockingFileIO: Sendable {
let ptr = pointer.baseAddress!.assumingMemoryBound(to: CChar.self)
return String(cString: ptr)
}
#if os(OpenBSD)
#if os(OpenBSD) || os(FreeBSD)
let ino = entry.pointee.d_fileno
#else
let ino = entry.pointee.d_ino
Expand Down Expand Up @@ -1286,7 +1286,7 @@ extension NonBlockingFileIO {
let ptr = pointer.baseAddress!.assumingMemoryBound(to: CChar.self)
return String(cString: ptr)
}
#if os(OpenBSD)
#if os(OpenBSD) || os(FreeBSD)
let ino = entry.pointee.d_fileno
#else
let ino = entry.pointee.d_ino
Expand Down
1 change: 1 addition & 0 deletions Sources/NIOPosix/PendingDatagramWritesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import Atomics
import CNIODarwin
import CNIOFreeBSD
import CNIOLinux
import CNIOOpenBSD
import CNIOWindows
Expand Down
1 change: 1 addition & 0 deletions Sources/NIOPosix/PendingWritesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#if !os(WASI)

import Atomics
import CNIOFreeBSD
import CNIOLinux
import CNIOOpenBSD
import NIOCore
Expand Down
4 changes: 2 additions & 2 deletions Sources/NIOPosix/SelectorGeneric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct SelectorEventSet: OptionSet, Equatable, Sendable {
}

internal let isEarlyEOFDeliveryWorkingOnThisOS: Bool = {
#if canImport(Darwin)
#if canImport(Darwin) || os(FreeBSD)
return false // rdar://53656794 , once fixed we need to do an OS version check here.
#else
return true
Expand Down Expand Up @@ -187,7 +187,7 @@ internal class Selector<R: Registration> {
var selectorFD: CInt = -1 // -1 == we're closed

// Here we add the stored properties that are used by the specific backends
#if canImport(Darwin) || os(OpenBSD)
#if canImport(Darwin) || os(OpenBSD) || os(FreeBSD)
@usableFromInline
typealias EventType = kevent
#elseif os(Linux) || os(Android)
Expand Down
Loading