Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more peer test #191

Merged
merged 8 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions Networking/Sources/Networking/PKCS12.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,31 @@ public func parseCertificate(data: Data, type: CertificateType) throws -> (
var errorMessage: UnsafeMutablePointer<Int8>?
defer { free(altNamePointer) }

let result: Int32 = switch type {
case .x509:
data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
parse_certificate(
bytes.baseAddress!.assumingMemoryBound(to: UInt8.self),
data.count,
&publicKeyPointer,
&publicKeyLen,
&altNamePointer,
&errorMessage
)
}
case .p12:
data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
parse_pkcs12_certificate(
bytes.baseAddress!.assumingMemoryBound(to: UInt8.self),
data.count,
&publicKeyPointer,
&publicKeyLen,
&altNamePointer,
&errorMessage
)
let result: Int32 =
switch type {
case .x509:
data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
parse_certificate(
bytes.baseAddress!.assumingMemoryBound(to: UInt8.self),
data.count,
&publicKeyPointer,
&publicKeyLen,
&altNamePointer,
&errorMessage
)
}
case .p12:
data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
parse_pkcs12_certificate(
bytes.baseAddress!.assumingMemoryBound(to: UInt8.self),
data.count,
&publicKeyPointer,
&publicKeyLen,
&altNamePointer,
&errorMessage
)
}
}
}

guard result == 0 else {
throw CryptoError.parseFailed(String(cString: errorMessage!))
Expand Down
87 changes: 53 additions & 34 deletions Networking/Sources/Networking/Peer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ public final class Peer<Handler: StreamHandler>: Sendable {

let registration = try QuicRegistration()
let serverConfiguration = try QuicConfiguration(
registration: registration, pkcs12: pkcs12, alpns: allAlpns, client: false, settings: options.serverSettings
registration: registration, pkcs12: pkcs12, alpns: allAlpns, client: false,
settings: options.serverSettings
)

let clientAlpn = alpns[options.mode]!
let clientConfiguration = try QuicConfiguration(
registration: registration, pkcs12: pkcs12, alpns: [clientAlpn], client: true, settings: options.clientSettings
registration: registration, pkcs12: pkcs12, alpns: [clientAlpn], client: true,
settings: options.clientSettings
)

impl = PeerImpl(
Expand All @@ -104,26 +106,27 @@ public final class Peer<Handler: StreamHandler>: Sendable {
let conn = impl.connections.read { connections in
connections.byType[mode]?[address]
}
return try conn ?? impl.connections.write { connections in
let curr = connections.byType[mode, default: [:]][address]
if let curr {
return curr
return try conn
?? impl.connections.write { connections in
let curr = connections.byType[mode, default: [:]][address]
if let curr {
return curr
}
let conn = try Connection(
QuicConnection(
handler: PeerEventHandler(self.impl),
registration: self.impl.clientConfiguration.registration,
configuration: self.impl.clientConfiguration
),
impl: self.impl,
mode: mode,
remoteAddress: address,
initiatedByLocal: true
)
connections.byType[mode, default: [:]][address] = conn
connections.byId[conn.id] = conn
return conn
}
let conn = try Connection(
QuicConnection(
handler: PeerEventHandler(self.impl),
registration: self.impl.clientConfiguration.registration,
configuration: self.impl.clientConfiguration
),
impl: self.impl,
mode: mode,
remoteAddress: address,
initiatedByLocal: true
)
connections.byType[mode, default: [:]][address] = conn
connections.byId[conn.id] = conn
return conn
}
}

public func broadcast(kind: Handler.PresistentHandler.StreamKind, message: any MessageProtocol) {
Expand All @@ -142,11 +145,14 @@ public final class Peer<Handler: StreamHandler>: Sendable {
case .success:
break
case let .failure(error):
impl.logger.warning("Failed to send message", metadata: [
"connectionId": "\(connection.id)",
"kind": "\(kind)",
"error": "\(error)",
])
impl.logger.warning(
"Failed to send message",
metadata: [
"connectionId": "\(connection.id)",
"kind": "\(kind)",
"error": "\(error)",
]
)
}
}
}
Expand Down Expand Up @@ -245,11 +251,15 @@ private struct PeerEventHandler<Handler: StreamHandler>: QuicEventHandler {
self.impl = impl
}

func newConnection(_: QuicListener, connection: QuicConnection, info: ConnectionInfo) -> QuicStatus {
func newConnection(_: QuicListener, connection: QuicConnection, info: ConnectionInfo)
MacOMNI marked this conversation as resolved.
Show resolved Hide resolved
-> QuicStatus
{
let addr = info.remoteAddress
let mode = impl.alpnLookup[info.negotiatedAlpn]
guard let mode else {
logger.warning("unknown alpn: \(String(data: info.negotiatedAlpn, encoding: .utf8) ?? info.negotiatedAlpn.toDebugHexString())")
logger.warning(
"unknown alpn: \(String(data: info.negotiatedAlpn, encoding: .utf8) ?? info.negotiatedAlpn.toDebugHexString())"
)
return .code(.alpnNegFailure)
}
logger.debug("new connection: \(addr) mode: \(mode)")
Expand All @@ -266,10 +276,13 @@ private struct PeerEventHandler<Handler: StreamHandler>: QuicEventHandler {
return .code(.requiredCert)
}
do {
let (publicKey, alternativeName) = try parseCertificate(data: certificate,type: .x509)
let (publicKey, alternativeName) = try parseCertificate(data: certificate, type: .x509)
logger.debug(
"Certificate parsed",
metadata: ["publicKey": "\(publicKey.toHexString())", "alternativeName": "\(alternativeName)"]
metadata: [
"publicKey": "\(publicKey.toHexString())",
"alternativeName": "\(alternativeName)",
]
)
if alternativeName != generateSubjectAlternativeName(pubkey: publicKey) {
return .code(.badCert)
Expand All @@ -289,7 +302,9 @@ private struct PeerEventHandler<Handler: StreamHandler>: QuicEventHandler {
connections.byId[connection.id]
}
guard let conn else {
logger.warning("Connected but connection is gone?", metadata: ["connectionId": "\(connection.id)"])
logger.warning(
"Connected but connection is gone?", metadata: ["connectionId": "\(connection.id)"]
)
return
}

Expand Down Expand Up @@ -347,10 +362,14 @@ private struct PeerEventHandler<Handler: StreamHandler>: QuicEventHandler {
if let connection {
connection.streamClosed(stream: stream, abort: !status.isSucceeded)
} else {
logger.warning("Stream closed but connection is gone?", metadata: ["streamId": "\(stream.id)"])
logger.warning(
"Stream closed but connection is gone?", metadata: ["streamId": "\(stream.id)"]
)
}
} else {
logger.warning("Stream closed but stream is gone?", metadata: ["streamId": "\(quicStream.id)"])
logger.warning(
"Stream closed but stream is gone?", metadata: ["streamId": "\(quicStream.id)"]
)
}
}
}
Expand Down Expand Up @@ -385,7 +404,7 @@ public final class MockPeerEventHandler: QuicEventHandler {
return .code(.requiredCert)
}
do {
let (publicKey, alternativeName) = try parseCertificate(data: certificate,type: .x509)
let (publicKey, alternativeName) = try parseCertificate(data: certificate, type: .x509)
if alternativeName != generateSubjectAlternativeName(pubkey: publicKey) {
return .code(.badCert)
}
Expand Down