@@ -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 :
0 commit comments