Skip to content

Commit

Permalink
Musl
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Sep 27, 2024
1 parent 7783b63 commit 476d9c8
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions FlyingFox/Sources/SocketAddress+Glibc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
// SOFTWARE.
//

#if canImport(Glibc)
#if canImport(Musl)
// swift on linux fails to import comformance for these Glibc types 🤷🏻‍♂️:
import Glibc
import Musl
import FlyingSocks

extension sockaddr_in: SocketAddress {
Expand Down
4 changes: 2 additions & 2 deletions FlyingSocks/Sources/Mutex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ extension Mutex {
}
}

#elseif canImport(Glibc)
#elseif canImport(Musl)

import Glibc
import Musl

extension Mutex {

Expand Down
64 changes: 32 additions & 32 deletions FlyingSocks/Sources/Socket+Glibc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
// SOFTWARE.
//

#if canImport(Glibc)
import Glibc
#if canImport(Musl)
import Musl

public extension Socket {
typealias FileDescriptorType = Int32
Expand All @@ -41,20 +41,20 @@ extension Socket.FileDescriptor {
}

extension Socket {
static let stream = Int32(SOCK_STREAM.rawValue)
static let in_addr_any = Glibc.in_addr(s_addr: Glibc.in_addr_t(0))
static let stream = Int32(SOCK_STREAM)
static let in_addr_any = Musl.in_addr(s_addr: Musl.in_addr_t(0))

static func makeAddressINET(port: UInt16) -> Glibc.sockaddr_in {
Glibc.sockaddr_in(
static func makeAddressINET(port: UInt16) -> Musl.sockaddr_in {
Musl.sockaddr_in(
sin_family: sa_family_t(AF_INET),
sin_port: port.bigEndian,
sin_addr: in_addr_any,
sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)
)
}

static func makeAddressINET6(port: UInt16) -> Glibc.sockaddr_in6 {
Glibc.sockaddr_in6(
static func makeAddressINET6(port: UInt16) -> Musl.sockaddr_in6 {
Musl.sockaddr_in6(
sin6_family: sa_family_t(AF_INET6),
sin6_port: port.bigEndian,
sin6_flowinfo: 0,
Expand All @@ -63,8 +63,8 @@ extension Socket {
)
}

static func makeAddressLoopback(port: UInt16) -> Glibc.sockaddr_in6 {
Glibc.sockaddr_in6(
static func makeAddressLoopback(port: UInt16) -> Musl.sockaddr_in6 {
Musl.sockaddr_in6(
sin6_family: sa_family_t(AF_INET6),
sin6_port: port.bigEndian,
sin6_flowinfo: 0,
Expand All @@ -73,8 +73,8 @@ extension Socket {
)
}

static func makeAddressUnix(path: String) -> Glibc.sockaddr_un {
var addr = Glibc.sockaddr_un()
static func makeAddressUnix(path: String) -> Musl.sockaddr_un {
var addr = Musl.sockaddr_un()
addr.sun_family = sa_family_t(AF_UNIX)
let pathCount = min(path.utf8.count, 104)
let len = UInt8(MemoryLayout<UInt8>.size + MemoryLayout<sa_family_t>.size + pathCount + 1)
Expand All @@ -87,84 +87,84 @@ extension Socket {
}

static func socket(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> Int32 {
Glibc.socket(domain, type, `protocol`)
Musl.socket(domain, type, `protocol`)
}

static func fcntl(_ fd: Int32, _ cmd: Int32) -> Int32 {
Glibc.fcntl(fd, cmd)
Musl.fcntl(fd, cmd)
}

static func fcntl(_ fd: Int32, _ cmd: Int32, _ value: Int32) -> Int32 {
Glibc.fcntl(fd, cmd, value)
Musl.fcntl(fd, cmd, value)
}

static func setsockopt(_ fd: Int32, _ level: Int32, _ name: Int32,
_ value: UnsafeRawPointer!, _ len: socklen_t) -> Int32 {
Glibc.setsockopt(fd, level, name, value, len)
Musl.setsockopt(fd, level, name, value, len)
}

static func getsockopt(_ fd: Int32, _ level: Int32, _ name: Int32,
_ value: UnsafeMutableRawPointer!, _ len: UnsafeMutablePointer<socklen_t>!) -> Int32 {
Glibc.getsockopt(fd, level, name, value, len)
Musl.getsockopt(fd, level, name, value, len)
}

static func getpeername(_ fd: Int32, _ addr: UnsafeMutablePointer<sockaddr>!, _ len: UnsafeMutablePointer<socklen_t>!) -> Int32 {
Glibc.getpeername(fd, addr, len)
Musl.getpeername(fd, addr, len)
}

static func getsockname(_ fd: Int32, _ addr: UnsafeMutablePointer<sockaddr>!, _ len: UnsafeMutablePointer<socklen_t>!) -> Int32 {
Glibc.getsockname(fd, addr, len)
Musl.getsockname(fd, addr, len)
}

static func inet_ntop(_ domain: Int32, _ addr: UnsafeRawPointer!,
_ buffer: UnsafeMutablePointer<CChar>!, _ addrLen: socklen_t) throws {
if Glibc.inet_ntop(domain, addr, buffer, addrLen) == nil {
if Musl.inet_ntop(domain, addr, buffer, addrLen) == nil {
throw SocketError.makeFailed("inet_ntop")
}
}

static func inet_pton(_ domain: Int32, _ buffer: UnsafePointer<CChar>!, _ addr: UnsafeMutableRawPointer!) -> Int32 {
Glibc.inet_pton(domain, buffer, addr)
Musl.inet_pton(domain, buffer, addr)
}

static func bind(_ fd: Int32, _ addr: UnsafePointer<sockaddr>!, _ len: socklen_t) -> Int32 {
Glibc.bind(fd, addr, len)
Musl.bind(fd, addr, len)
}

static func listen(_ fd: Int32, _ backlog: Int32) -> Int32 {
Glibc.listen(fd, backlog)
Musl.listen(fd, backlog)
}

static func accept(_ fd: Int32, _ addr: UnsafeMutablePointer<sockaddr>!, _ len: UnsafeMutablePointer<socklen_t>!) -> Int32 {
Glibc.accept(fd, addr, len)
Musl.accept(fd, addr, len)
}

static func connect(_ fd: Int32, _ addr: UnsafePointer<sockaddr>!, _ len: socklen_t) -> Int32 {
Glibc.connect(fd, addr, len)
Musl.connect(fd, addr, len)
}

static func read(_ fd: Int32, _ buffer: UnsafeMutableRawPointer!, _ nbyte: Int) -> Int {
Glibc.read(fd, buffer, nbyte)
Musl.read(fd, buffer, nbyte)
}

static func write(_ fd: Int32, _ buffer: UnsafeRawPointer!, _ nbyte: Int) -> Int {
Glibc.send(fd, buffer, nbyte, Int32(MSG_NOSIGNAL))
Musl.send(fd, buffer, nbyte, Int32(MSG_NOSIGNAL))
}

static func close(_ fd: Int32) -> Int32 {
Glibc.close(fd)
Musl.close(fd)
}

static func unlink(_ addr: UnsafePointer<CChar>!) -> Int32 {
Glibc.unlink(addr)
Musl.unlink(addr)
}

static func poll(_ fds: UnsafeMutablePointer<pollfd>!, _ nfds: UInt32, _ tmo_p: Int32) -> Int32 {
Glibc.poll(fds, UInt(nfds), tmo_p)
Musl.poll(fds, UInt(nfds), tmo_p)
}

static func pollfd(fd: FileDescriptorType, events: Int16, revents: Int16) -> Glibc.pollfd {
Glibc.pollfd(fd: fd, events: events, revents: revents)
static func pollfd(fd: FileDescriptorType, events: Int16, revents: Int16) -> Musl.pollfd {
Musl.pollfd(fd: fd, events: events, revents: revents)
}
}

Expand Down
4 changes: 2 additions & 2 deletions FlyingSocks/Sources/Socket+Pair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#if canImport(Darwin)
import Darwin
#else
import Glibc
import Musl
#endif

package extension Socket {
Expand All @@ -42,7 +42,7 @@ package extension Socket {
#if canImport(Darwin)
_ = Darwin.socketpair(domain, type, `protocol`, &sockets)
#else
_ = Glibc.socketpair(domain, type, `protocol`, &sockets)
_ = Musl.socketpair(domain, type, `protocol`, &sockets)
#endif
return (FileDescriptor(rawValue: sockets[0]), FileDescriptor(rawValue: sockets[1]))
}
Expand Down
14 changes: 7 additions & 7 deletions FlyingSocks/Sources/SocketPool+ePoll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ extension EventNotification {
private struct EPOLLEvents: OptionSet, Hashable {
var rawValue: UInt32

static let read = EPOLLEvents(rawValue: EPOLLIN.rawValue)
static let write = EPOLLEvents(rawValue: EPOLLOUT.rawValue)
static let hup = EPOLLEvents(rawValue: EPOLLHUP.rawValue)
static let rdhup = EPOLLEvents(rawValue: EPOLLRDHUP.rawValue)
static let err = EPOLLEvents(rawValue: EPOLLERR.rawValue)
static let pri = EPOLLEvents(rawValue: EPOLLPRI.rawValue)
static let edgeTriggered = EPOLLEvents(rawValue: EPOLLET.rawValue)
static let read = EPOLLEvents(rawValue: UInt32(EPOLLIN))
static let write = EPOLLEvents(rawValue: UInt32(EPOLLOUT))
static let hup = EPOLLEvents(rawValue: UInt32(EPOLLHUP))
static let rdhup = EPOLLEvents(rawValue: UInt32(EPOLLRDHUP))
static let err = EPOLLEvents(rawValue: UInt32(EPOLLERR))
static let pri = EPOLLEvents(rawValue: UInt32(EPOLLPRI))
static let edgeTriggered = EPOLLEvents(rawValue: UInt32(EPOLLET))
}

private extension Socket.Events {
Expand Down
4 changes: 2 additions & 2 deletions FlyingSocks/XCTests/Socket+Pair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#if canImport(Darwin)
import Darwin
#else
import Glibc
import Musl
#endif

@testable import FlyingSocks
Expand All @@ -44,7 +44,7 @@ extension Socket {
#if canImport(Darwin)
_ = Darwin.socketpair(domain, type, `protocol`, &sockets)
#else
_ = Glibc.socketpair(domain, type, `protocol`, &sockets)
_ = Musl.socketpair(domain, type, `protocol`, &sockets)
#endif
return (FileDescriptor(rawValue: sockets[0]), FileDescriptor(rawValue: sockets[1]))
}
Expand Down
4 changes: 2 additions & 2 deletions FlyingSocks/XCTests/SocketAddress+Glibc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
// SOFTWARE.
//

#if canImport(Glibc)
#if canImport(Musl)
// swift on linux fails to import comformance for these Glibc types 🤷🏻‍♂️:
import Glibc
import Musl
import FlyingSocks

extension sockaddr_in: SocketAddress { }
Expand Down
4 changes: 2 additions & 2 deletions docker-run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set -eu
docker run -it \
--rm \
--mount src="$(pwd)",target=/flyingfox,type=bind \
swiftlang/swift:nightly-jammy \
/usr/bin/swift test --package-path /flyingfox
swift:latest \
/usr/bin/bash

0 comments on commit 476d9c8

Please sign in to comment.