Skip to content

Commit 54beac7

Browse files
committed
Fixed Linux support
1 parent b99ae01 commit 54beac7

9 files changed

Lines changed: 54 additions & 43 deletions

File tree

Sources/Socket/System/CInternetAddress.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import SystemPackage
22

3-
@usableFromInline
43
internal protocol CInternetAddress {
54

65
static var stringLength: Int { get }
@@ -12,7 +11,6 @@ internal protocol CInternetAddress {
1211

1312
internal extension CInternetAddress {
1413

15-
@usableFromInline
1614
init?(_ string: String) {
1715
self.init()
1816
/**
@@ -32,7 +30,6 @@ internal extension CInternetAddress {
3230

3331
internal extension String {
3432

35-
@usableFromInline
3633
init<T: CInternetAddress>(_ cInternetAddress: T) throws(Errno) {
3734
let cString = UnsafeMutablePointer<CChar>.allocate(capacity: T.stringLength)
3835
defer { cString.deallocate() }
@@ -54,18 +51,14 @@ internal extension String {
5451

5552
extension CInterop.IPv4Address: CInternetAddress {
5653

57-
@usableFromInline
5854
static var stringLength: Int { return numericCast(_INET_ADDRSTRLEN) }
5955

60-
@usableFromInline
6156
static var family: SocketAddressFamily { .ipv4 }
6257
}
6358

6459
extension CInterop.IPv6Address: CInternetAddress {
6560

66-
@usableFromInline
6761
static var stringLength: Int { return numericCast(_INET6_ADDRSTRLEN) }
6862

69-
@usableFromInline
7063
static var family: SocketAddressFamily { .ipv6 }
7164
}

Sources/Socket/System/CInterop.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import SystemPackage
33
#if canImport(Darwin)
44
import Darwin
55
#elseif os(Windows)
6-
import CSystem
6+
import CSocket
77
import ucrt
88
#elseif canImport(Glibc)
9-
@_implementationOnly import CSystem
9+
import CSocket
1010
import Glibc
1111
#elseif canImport(Musl)
12-
@_implementationOnly import CSystem
12+
import CSocket
1313
import Musl
1414
#elseif canImport(WASILibc)
1515
import WASILibc
1616
#elseif canImport(Bionic)
17-
@_implementationOnly import CSystem
17+
import CSocket
1818
import Bionic
1919
#else
2020
#error("Unsupported Platform")
@@ -79,7 +79,7 @@ public extension CInterop {
7979
typealias LinkLayerAddress = sockaddr_dl
8080
#elseif os(Linux)
8181
/// The C `sockaddr_ll` type
82-
//typealias LinkLayerAddress = sockaddr_ll
82+
typealias LinkLayerAddress = sockaddr_ll
8383
#endif
8484

8585
/// The C `if_nameindex` type

Sources/Socket/System/Constants.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ import SystemPackage
33
#if canImport(Darwin)
44
import Darwin
55
#elseif os(Windows)
6-
import CSystem
6+
import CSocket
77
import ucrt
88
#elseif canImport(Glibc)
9-
@_implementationOnly import CSystem
109
import Glibc
1110
#elseif canImport(Musl)
12-
@_implementationOnly import CSystem
11+
import CSocket
1312
import Musl
1413
#elseif canImport(WASILibc)
14+
import CSocket
1515
import WASILibc
16-
#elseif canImport(Bionic)
17-
@_implementationOnly import CSystem
18-
import Bionic
16+
#elseif canImport(Android)
17+
import Android
1918
#else
2019
#error("Unsupported Platform")
2120
#endif
@@ -369,6 +368,32 @@ internal var _SOL_BLUETOOTH: CInt { SOL_BLUETOOTH }
369368
internal var _SOL_ALG: CInt { SOL_ALG }
370369
#endif
371370

371+
@_alwaysEmitIntoClient
372+
internal var _SOCK_STREAM: CInterop.SocketType { SOCK_STREAM }
373+
374+
@_alwaysEmitIntoClient
375+
internal var _SOCK_DGRAM: CInterop.SocketType { SOCK_DGRAM }
376+
377+
@_alwaysEmitIntoClient
378+
internal var _SOCK_RAW: CInterop.SocketType { SOCK_RAW }
379+
380+
@_alwaysEmitIntoClient
381+
internal var _SOCK_RDM: CInterop.SocketType { SOCK_RDM }
382+
383+
@_alwaysEmitIntoClient
384+
internal var _SOCK_SEQPACKET: CInterop.SocketType { SOCK_SEQPACKET }
385+
386+
#if os(Linux)
387+
@_alwaysEmitIntoClient
388+
internal var _SOCK_DCCP: CInterop.SocketType { SOCK_DCCP }
389+
390+
@_alwaysEmitIntoClient
391+
internal var _SOCK_NONBLOCK: CInterop.SocketType { SOCK_NONBLOCK }
392+
393+
@_alwaysEmitIntoClient
394+
internal var _SOCK_CLOEXEC: CInterop.SocketType { SOCK_CLOEXEC }
395+
#endif
396+
372397
@_alwaysEmitIntoClient
373398
internal var _MSG_DONTROUTE: CInt { numericCast(MSG_DONTROUTE) } /* send without using routing tables */
374399

Sources/Socket/System/InternetProtocol.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,13 @@ public extension IPv4Address {
103103

104104
extension IPv4Address: RawRepresentable {
105105

106-
@_alwaysEmitIntoClient
107106
public init?(rawValue: String) {
108107
guard let bytes = CInterop.IPv4Address(rawValue) else {
109108
return nil
110109
}
111110
self.init(bytes)
112111
}
113112

114-
@_alwaysEmitIntoClient
115113
public var rawValue: String {
116114
return try! String(bytes)
117115
}
@@ -175,15 +173,13 @@ public extension IPv6Address {
175173

176174
extension IPv6Address: RawRepresentable {
177175

178-
@_alwaysEmitIntoClient
179176
public init?(rawValue: String) {
180177
guard let bytes = CInterop.IPv6Address(rawValue) else {
181178
return nil
182179
}
183180
self.init(bytes)
184181
}
185182

186-
@_alwaysEmitIntoClient
187183
public var rawValue: String {
188184
return try! String(bytes)
189185
}

Sources/Socket/System/NetworkInterface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
import SystemPackage
9-
@_implementationOnly import CSocket
9+
import CSocket
1010

1111
/// UNIX Network Interface
1212
public struct NetworkInterface <Address: SocketAddress>: Identifiable {

Sources/Socket/System/SocketAddress/LinkLayerSocketAddress.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#if canImport(Darwin) || os(Linux)
99
import Foundation
1010
import SystemPackage
11-
@_implementationOnly import CSocket
11+
import CSocket
1212

1313
/// Unix Socket Address
1414
public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable {
@@ -18,15 +18,13 @@ public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable {
1818
#if canImport(Darwin)
1919
/// Index type
2020
public typealias Index = UInt16
21-
22-
internal typealias CSocketAddressType = CInterop.LinkLayerAddress
2321
#elseif os(Linux)
2422
/// Index type
2523
public typealias Index = Int32
26-
27-
internal typealias CSocketAddressType = sockaddr_ll
2824
#endif
2925

26+
internal typealias CSocketAddressType = CInterop.LinkLayerAddress
27+
3028
/// Index
3129
public let index: Index
3230

@@ -86,7 +84,7 @@ public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable {
8684
}
8785
}
8886

89-
extension LinkLayerSocketAddress.CSocketAddressType: CSocketAddress {
87+
extension CInterop.LinkLayerAddress: CSocketAddress {
9088

9189
@_alwaysEmitIntoClient
9290
static var family: SocketAddressFamily { LinkLayerProtocol.family }

Sources/Socket/System/SocketFlags.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#if os(Linux)
2-
@_implementationOnly import CSocket
2+
import CSocket
33

44
/// Flags when opening sockets.
55
@frozen
@@ -22,10 +22,10 @@ public extension SocketFlags {
2222

2323
/// Set the `O_NONBLOCK` file status flag on the open file description referred to by the new file
2424
/// descriptor. Using this flag saves extra calls to `fcntl()` to achieve the same result.
25-
static var nonBlocking: SocketFlags { SocketFlags(SOCK_NONBLOCK) }
25+
static var nonBlocking: SocketFlags { SocketFlags(_SOCK_NONBLOCK) }
2626

2727
/// Set the close-on-exec (`FD_CLOEXEC`) flag on the new file descriptor.
28-
static var closeOnExec: SocketFlags { SocketFlags(SOCK_CLOEXEC) }
28+
static var closeOnExec: SocketFlags { SocketFlags(_SOCK_CLOEXEC) }
2929
}
3030

3131
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)

Sources/Socket/System/SocketType.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import Darwin
33
#endif
44
import SystemPackage
5-
@_implementationOnly import CSocket
65

76
/// POSIX Socket Type
87
@frozen
@@ -31,21 +30,21 @@ public extension SocketType {
3130
///
3231
/// Provides sequenced, reliable, two-way, connection-based byte streams.
3332
/// An out-of-band data transmission mechanism may be supported.
34-
static var stream: SocketType { SocketType(SOCK_STREAM) }
33+
static var stream: SocketType { SocketType(_SOCK_STREAM) }
3534

3635
/// Supports datagrams (connectionless, unreliable messages of a fixed maximum length).
37-
static var datagram: SocketType { SocketType(SOCK_DGRAM) }
36+
static var datagram: SocketType { SocketType(_SOCK_DGRAM) }
3837

3938
/// Provides raw network protocol access.
40-
static var raw: SocketType { SocketType(SOCK_RAW) }
39+
static var raw: SocketType { SocketType(_SOCK_RAW) }
4140

4241
/// Provides a reliable datagram layer that does not guarantee ordering.
43-
static var reliableDatagramMessage: SocketType { SocketType(SOCK_RDM) }
42+
static var reliableDatagramMessage: SocketType { SocketType(_SOCK_RDM) }
4443

4544
/// Provides a sequenced, reliable, two-way connection-based data transmission
4645
/// path for datagrams of fixed maximum length; a consumer is required to read
4746
/// an entire packet with each input system call.
48-
static var sequencedPacket: SocketType { SocketType(SOCK_SEQPACKET) }
47+
static var sequencedPacket: SocketType { SocketType(_SOCK_SEQPACKET) }
4948
}
5049

5150
#if os(Linux)
@@ -54,6 +53,6 @@ public extension SocketType {
5453
/// Datagram Congestion Control Protocol
5554
///
5655
/// Linux specific way of getting packets at the dev level.
57-
static var datagramCongestionControlProtocol: SocketType { SocketType(SOCK_DCCP) }
56+
static var datagramCongestionControlProtocol: SocketType { SocketType(_SOCK_DCCP) }
5857
}
5958
#endif

Sources/Socket/System/Syscalls.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import SystemPackage
33
#if canImport(Darwin)
44
import Darwin
55
#elseif os(Windows)
6-
import CSystem
6+
import CSocket
77
import ucrt
88
#elseif canImport(Glibc)
9-
@_implementationOnly import CSystem
9+
import CSocket
1010
import Glibc
1111
#elseif canImport(Musl)
12-
@_implementationOnly import CSystem
12+
import CSocket
1313
import Musl
1414
#elseif canImport(WASILibc)
1515
import WASILibc
1616
#elseif canImport(Bionic)
17-
@_implementationOnly import CSystem
17+
import CSocket
1818
import Bionic
1919
#else
2020
#error("Unsupported Platform")

0 commit comments

Comments
 (0)