Skip to content

Commit a377c43

Browse files
committed
Fix ipv6 format
1 parent 723bf92 commit a377c43

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

Sources/GRPCInterceptors/Tracing/SpanAttributes+RPCAttributes.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ private enum PeerAddress {
162162
// - ipv4:<host>:<port> for ipv4 addresses
163163
// - ipv6:[<host>]:<port> for ipv6 addresses
164164
// - unix:<uds-pathname> for UNIX domain sockets
165-
let addressComponents = address.split(separator: ":", omittingEmptySubsequences: false)
165+
166+
// First get the first component so that we know what type of address we're dealing with
167+
let addressComponents = address.split(separator: ":", maxSplits: 1)
166168

167169
guard addressComponents.count > 1 else {
168170
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
@@ -173,32 +175,28 @@ private enum PeerAddress {
173175
// Check what type the transport is...
174176
switch addressComponents[0] {
175177
case "ipv4":
176-
guard addressComponents.count == 3, let port = Int(addressComponents[2]) else {
178+
let ipv4AddressComponents = addressComponents[1].split(separator: ":")
179+
guard ipv4AddressComponents.count == 2, let port = Int(ipv4AddressComponents[1]) else {
177180
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
178181
self = .other(address)
179182
return
180183
}
181-
self = .ipv4(address: String(addressComponents[1]), port: port)
184+
self = .ipv4(address: String(ipv4AddressComponents[0]), port: port)
182185

183186
case "ipv6":
184-
guard addressComponents.count > 2, let port = Int(addressComponents.last!) else {
187+
// At this point, we are looking at an address with format: [<address>]:<port>
188+
// We drop the first character ('[') and split by ']:' to keep two components: the address
189+
// and the port.
190+
let ipv6AddressComponents = addressComponents[1].dropFirst().split(separator: "]:")
191+
guard ipv6AddressComponents.count == 2, let port = Int(ipv6AddressComponents[1]) else {
185192
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
186193
self = .other(address)
187194
return
188195
}
189-
self = .ipv6(
190-
address: String(
191-
addressComponents[1 ..< addressComponents.count - 1].joined(separator: ":")
192-
),
193-
port: port
194-
)
196+
self = .ipv6(address: String(ipv6AddressComponents[0]), port: port)
195197

196198
case "unix":
197-
guard addressComponents.count == 2 else {
198-
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
199-
self = .other(address)
200-
return
201-
}
199+
// Whatever comes after "unix:" is the <pathname>
202200
self = .unixDomainSocket(path: String(addressComponents[1]))
203201

204202
default:

Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ final class TracingInterceptorTests: XCTestCase {
131131
}),
132132
context: ClientContext(
133133
descriptor: methodDescriptor,
134-
remotePeer: "ipv6:2001::130F:::09C0:876A:130B:1234",
135-
localPeer: "ipv6:ff06:0:0:0:0:0:0:c3:5678"
134+
remotePeer: "ipv6:[2001::130F:::09C0:876A:130B]:1234",
135+
localPeer: "ipv6:[ff06:0:0:0:0:0:0:c3]:5678"
136136
)
137137
) { stream, _ in
138138
// Assert the metadata contains the injected context key-value.
@@ -595,8 +595,8 @@ final class TracingInterceptorTests: XCTestCase {
595595
request: .init(single: request),
596596
context: ServerContext(
597597
descriptor: methodDescriptor,
598-
remotePeer: "ipv6:2001::130F:::09C0:876A:130B:1234",
599-
localPeer: "ipv6:ff06:0:0:0:0:0:0:c3:5678",
598+
remotePeer: "ipv6:[2001::130F:::09C0:876A:130B]:1234",
599+
localPeer: "ipv6:[ff06:0:0:0:0:0:0:c3]:5678",
600600
cancellation: .init()
601601
)
602602
) { _, _ in

Tests/GRPCInterceptorsTests/TracingTestsUtilities.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ struct TestSpanEvent: Equatable, CustomDebugStringConvertible {
210210
var name: String
211211
var attributes: SpanAttributes
212212

213+
// This conformance is so any test errors are nicer to look at and understand
213214
var debugDescription: String {
214215
var attributesDescription = ""
215216
self.attributes.forEach { key, value in

0 commit comments

Comments
 (0)