Skip to content

Commit d6ab76c

Browse files
author
yngrtc
committedJun 2, 2024
swift-tools-version: 5.8
1 parent acfd406 commit d6ab76c

File tree

9 files changed

+84
-116
lines changed

9 files changed

+84
-116
lines changed
 

‎.github/workflows/swift.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
os: [ubuntu-latest, macos-latest]
18-
swift: ["5.9"]
18+
swift: ["5.8"]
1919
runs-on: ${{ matrix.os }}
2020
steps:
2121
- uses: actions/checkout@v4

‎Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 5.8
22
//===----------------------------------------------------------------------===//
33
//
44
// This source file is part of the SwiftRTC open source project
@@ -80,7 +80,7 @@ let package = Package(
8080
.testTarget(name: "SDPTests", dependencies: ["SDP"]),
8181
.testTarget(name: "SharedTests", dependencies: ["Shared"]),
8282
.testTarget(name: "SRTPTests", dependencies: ["SRTP"]),
83-
.testTarget(name: "STUNTests",
83+
.testTarget(name: "STUNTests",
8484
dependencies: [
8585
"STUN",
8686
.product(name: "ExtrasBase64", package: "swift-extras-base64"),

‎Sources/SDP/SessionDescription.swift

+33-45
Original file line numberDiff line numberDiff line change
@@ -1017,15 +1017,12 @@ func unmarshalConnectionInformation(value: String) throws -> ConnectionInformati
10171017
}
10181018

10191019
let address: Address? =
1020-
if fields.count > 2 {
1021-
Address(
1022-
address: fields[2],
1023-
ttl: nil,
1024-
range: nil
1025-
)
1026-
} else {
1027-
nil
1028-
}
1020+
fields.count > 2
1021+
? Address(
1022+
address: fields[2],
1023+
ttl: nil,
1024+
range: nil
1025+
) : nil
10291026

10301027
return ConnectionInformation(
10311028
networkType: fields[0],
@@ -1155,16 +1152,10 @@ func unmarshalSessionAttribute(lexer: inout Lexer) throws -> StateFn? {
11551152
let value = try lexer.readValue()
11561153

11571154
let fields = value.split(separator: ":", maxSplits: 1).map { String($0) }
1158-
let attribute =
1159-
if fields.count == 2 {
1160-
Attribute(
1161-
key: fields[0],
1162-
value: fields[1]
1163-
)
1164-
} else {
1165-
Attribute(
1166-
key: fields[0])
1167-
}
1155+
let attribute = Attribute(
1156+
key: fields[0],
1157+
value: fields.count == 2 ? fields[1] : nil
1158+
)
11681159
lexer.desc.attributes.append(attribute)
11691160

11701161
return StateFn(f: s11)
@@ -1294,16 +1285,10 @@ func unmarshalMediaAttribute(lexer: inout Lexer) throws -> StateFn? {
12941285
let value = try lexer.readValue()
12951286

12961287
let fields = value.split(separator: ":", maxSplits: 1).map { String($0) }
1297-
let attribute =
1298-
if fields.count == 2 {
1299-
Attribute(
1300-
key: fields[0],
1301-
value: fields[1])
1302-
} else {
1303-
Attribute(
1304-
key: fields[0])
1305-
}
1306-
1288+
let attribute = Attribute(
1289+
key: fields[0],
1290+
value: fields.count == 2 ? fields[1] : nil
1291+
)
13071292
if lexer.desc.mediaDescriptions.isEmpty {
13081293
throw SDPError.sdpEmptyTimeDescription
13091294
}
@@ -1317,23 +1302,26 @@ func unmarshalMediaAttribute(lexer: inout Lexer) throws -> StateFn? {
13171302
func parseTimeUnits(value: String) throws -> Int64 {
13181303
// Some time offsets in the protocol can be provided with a shorthand
13191304
// notation. This code ensures to convert it to NTP timestamp format.
1320-
let (num, factor) =
1321-
if let last = value.last {
1322-
switch last {
1323-
case "d":
1324-
(String(value.dropLast()), 86400) // days
1325-
case "h":
1326-
(String(value.dropLast()), 3600) // hours
1327-
case "m":
1328-
(String(value.dropLast()), 60) // minutes
1329-
case "s":
1330-
(String(value.dropLast()), 1) // seconds (allowed for completeness)
1331-
default:
1332-
(value, 1)
1333-
}
1334-
} else {
1335-
(value, 1)
1305+
var (num, factor) = (value, 1)
1306+
if let last = value.last {
1307+
switch last {
1308+
case "d":
1309+
num = String(value.dropLast())
1310+
factor = 86400 // days
1311+
case "h":
1312+
num = String(value.dropLast())
1313+
factor = 3600 // hours
1314+
case "m":
1315+
num = String(value.dropLast())
1316+
factor = 60 // minutes
1317+
case "s":
1318+
num = String(value.dropLast())
1319+
factor = 1 // seconds (allowed for completeness)
1320+
default:
1321+
num = value
1322+
factor = 1
13361323
}
1324+
}
13371325

13381326
guard let parsedNum = Int64(num) else {
13391327
throw SDPError.sdpInvalidValue(value)

‎Sources/STUN/Address.swift

+10-15
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,22 @@ public struct MappedAddress {
4949
}
5050
let port = UInt16.fromBeBytes(v[2], v[3])
5151

52-
let l =
53-
if family == familyIpV6 {
54-
min(ipV6Len, v[4...].count)
55-
} else {
56-
min(ipV4Len, v[4...].count)
57-
}
52+
let l = family == familyIpV6 ? min(ipV6Len, v[4...].count) : min(ipV4Len, v[4...].count)
5853
self.socketAddress = try SocketAddress(
5954
packedIPAddress: ByteBuffer(bytes: v[4..<4 + l]), port: Int(port))
6055
}
6156

6257
/// adds MAPPED-ADDRESS value to m as t attribute.
6358
public func addToAs(_ m: inout Message, _ t: AttrType) throws {
64-
let family =
65-
switch self.socketAddress {
66-
case SocketAddress.v4(_):
67-
familyIpV4
68-
case SocketAddress.v6(_):
69-
familyIpV6
70-
default:
71-
throw STUNError.errInvalidFamilyIpValue(0)
72-
}
59+
var family = familyIpV4
60+
switch self.socketAddress {
61+
case SocketAddress.v4(_):
62+
family = familyIpV4
63+
case SocketAddress.v6(_):
64+
family = familyIpV6
65+
default:
66+
throw STUNError.errInvalidFamilyIpValue(0)
67+
}
7368

7469
guard let port = self.socketAddress.port else {
7570
throw STUNError.errInvalidFamilyIpValue(0)

‎Sources/STUN/ErrorCodeAttribute.swift

+7-8
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,13 @@ extension ErrorCodeAttribute: Getter {
7474
let numberByte = UInt16(v[errorCodeNumberByte])
7575
let code = classByte * errorCodeModulo + numberByte
7676
self.code = ErrorCode(code)
77-
self.reason =
78-
if let reason = b.getString(
79-
at: errorCodeReasonStart, length: v.count - errorCodeReasonStart)
80-
{
81-
reason
82-
} else {
83-
throw STUNError.errInvalidString
84-
}
77+
if let reason = b.getString(
78+
at: errorCodeReasonStart, length: v.count - errorCodeReasonStart)
79+
{
80+
self.reason = reason
81+
} else {
82+
throw STUNError.errInvalidString
83+
}
8584
}
8685
}
8786

‎Sources/STUN/TextAttribute.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ extension TextAttribute: Setter {
7373
/// adds attribute with type t to m, checking maximum length. If max_len
7474
/// is less than 0, no check is performed.
7575
public func addTo(_ m: inout Message) throws {
76-
let maxLen =
77-
switch self.attr {
78-
case attrUsername:
79-
maxUsernameB
80-
case attrRealm:
81-
maxRealmB
82-
case attrSoftware:
83-
maxSoftwareB
84-
case attrNonce:
85-
maxNonceB
86-
default:
87-
throw STUNError.errUnsupportedAttrType(self.attr)
88-
}
76+
var maxLen = maxUsernameB
77+
switch self.attr {
78+
case attrUsername:
79+
maxLen = maxUsernameB
80+
case attrRealm:
81+
maxLen = maxRealmB
82+
case attrSoftware:
83+
maxLen = maxSoftwareB
84+
case attrNonce:
85+
maxLen = maxNonceB
86+
default:
87+
throw STUNError.errUnsupportedAttrType(self.attr)
88+
}
8989
let text = ByteBuffer(string: self.text)
9090
let textView = ByteBufferView(text)
9191
try checkOverflow(self.attr, textView.count, maxLen)

‎Sources/STUN/Uri.swift

+5-13
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,18 @@ public struct Uri: Equatable {
5959
.trimmingPrefix("[")
6060
.trimmingSuffix("]")
6161

62-
let port: UInt16? =
63-
if let port = url.port {
64-
UInt16(port)
65-
} else {
66-
nil
67-
}
62+
var port: UInt16? = nil
63+
if let p = url.port {
64+
port = UInt16(p)
65+
}
6866

6967
return Uri(scheme: url.scheme, host: String(host), port: port)
7068
}
7169
}
7270

7371
extension Uri: CustomStringConvertible {
7472
public var description: String {
75-
let host =
76-
if self.host.contains(":") {
77-
"[\(self.host)]"
78-
} else {
79-
self.host
80-
}
81-
73+
let host = self.host.contains(":") ? "[\(self.host)]" : self.host
8274
if let port = self.port {
8375
return "\(self.scheme):\(host):\(port)"
8476
} else {

‎Sources/STUN/XorMappedAddress.swift

+12-15
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@ public struct XorMappedAddress {
5353

5454
/// add_to_as adds XOR-MAPPED-ADDRESS value to m as t attribute.
5555
public func addToAs(_ m: inout Message, _ t: AttrType) throws {
56-
let (family, ipLen) =
57-
switch self.socketAddress {
58-
case SocketAddress.v4(_):
59-
(familyIpV4, ipV4Len)
60-
case SocketAddress.v6(_):
61-
(familyIpV6, ipV6Len)
62-
default:
63-
throw STUNError.errInvalidFamilyIpValue(0)
64-
}
56+
var (family, ipLen) = (familyIpV4, ipV4Len)
57+
switch self.socketAddress {
58+
case SocketAddress.v4(_):
59+
family = familyIpV4
60+
ipLen = ipV4Len
61+
case SocketAddress.v6(_):
62+
family = familyIpV6
63+
ipLen = ipV6Len
64+
default:
65+
throw STUNError.errInvalidFamilyIpValue(0)
66+
}
6567

6668
guard let port = self.socketAddress.port else {
6769
throw STUNError.errInvalidFamilyIpValue(0)
@@ -97,12 +99,7 @@ public struct XorMappedAddress {
9799
throw STUNError.errInvalidFamilyIpValue(family)
98100
}
99101

100-
let ipLen =
101-
if family == familyIpV4 {
102-
ipV4Len
103-
} else {
104-
ipV6Len
105-
}
102+
let ipLen = family == familyIpV4 ? ipV4Len : ipV6Len
106103
try checkOverflow(t, v[4...].count, ipLen)
107104

108105
let port = UInt16.fromBeBytes(v[2], v[3]) ^ UInt16(magicCookie >> 16)

‎Sources/XExampleStunDecode/main.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ import STUN
2121
// just to get an ArraySlice<String> from [String]
2222
let arguments = CommandLine.arguments.dropFirst()
2323

24-
guard let encodedData = arguments.first else {
25-
print("data is missing")
26-
exit(1)
27-
}
24+
let encodedData = arguments.first!
2825

2926
let decodedData = try Base64.decode(string: encodedData)
3027

0 commit comments

Comments
 (0)
Failed to load comments.