Skip to content

Commit

Permalink
fix connection
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Oct 23, 2024
1 parent 3e8bbd7 commit 98df68a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 26 deletions.
4 changes: 4 additions & 0 deletions Boka/.swiftpm/xcode/xcshareddata/xcschemes/Boka.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
argument = "--validator"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "--chain=minimal"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
<EnvironmentVariables>
<EnvironmentVariable
Expand Down
2 changes: 1 addition & 1 deletion Boka/Sources/Boka.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct Boka: AsyncParsableCommand {
var rpc: MaybeEnabled<NetAddr> = .enabled(NetAddr(address: "127.0.0.1:9955")!)

@Option(name: .long, help: "Listen address for P2P protocol.")
var p2p: NetAddr = .init(address: "127.0.0.1:19955")!
var p2p: NetAddr = .init(address: "127.0.0.1:0")!

@Option(name: .long, help: "Specify peer P2P addresses.")
var peers: [NetAddr] = []
Expand Down
3 changes: 3 additions & 0 deletions Networking/Sources/MsQuicSwift/QuicConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ private class ConnectionHandle {

case QUIC_CONNECTION_EVENT_SHUTDOWN_COMPLETE:
logger.trace("Shutdown complete")
if let connection {
connection.handler.shutdownComplete(connection)
}
if event.pointee.SHUTDOWN_COMPLETE.AppCloseInProgress == 0 {
// avoid closing twice
api.call { api in
Expand Down
9 changes: 9 additions & 0 deletions Networking/Sources/MsQuicSwift/QuicEventHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public protocol QuicEventHandler: Sendable {
func shouldOpen(_ connection: QuicConnection, certificate: Data?) -> QuicStatus
func connected(_ connection: QuicConnection)
func shutdownInitiated(_ connection: QuicConnection, reason: ConnectionCloseReason)
func shutdownComplete(_ connection: QuicConnection)
func streamStarted(_ connect: QuicConnection, stream: QuicStream)

// stream events
Expand All @@ -55,6 +56,7 @@ extension QuicEventHandler {
public func connected(_: QuicConnection) {}

public func shutdownInitiated(_: QuicConnection, reason _: ConnectionCloseReason) {}
public func shutdownComplete(_: QuicConnection) {}

public func streamStarted(_: QuicConnection, stream _: QuicStream) {}

Expand All @@ -69,6 +71,7 @@ public final class MockQuicEventHandler: QuicEventHandler {
case shouldOpen(connection: QuicConnection, certificate: Data?)
case connected(connection: QuicConnection)
case shutdownInitiated(connection: QuicConnection, reason: ConnectionCloseReason)
case shutdownComplete(connection: QuicConnection)
case streamStarted(connection: QuicConnection, stream: QuicStream)
case dataReceived(stream: QuicStream, data: Data)
case closed(stream: QuicStream, status: QuicStatus, code: QuicErrorCode)
Expand Down Expand Up @@ -107,6 +110,12 @@ public final class MockQuicEventHandler: QuicEventHandler {
}
}

public func shutdownComplete(_ connection: QuicConnection) {
events.write { events in
events.append(.shutdownComplete(connection: connection))
}
}

public func streamStarted(_ connect: QuicConnection, stream: QuicStream) {
events.write { events in
events.append(.streamStarted(connection: connect, stream: stream))
Expand Down
2 changes: 1 addition & 1 deletion Networking/Sources/MsQuicSwift/QuicSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public typealias QuicSettings = QUIC_SETTINGS
extension QuicSettings {
public static let defaultSettings = {
var settings = QuicSettings()
settings.IdleTimeoutMs = 60000
settings.IdleTimeoutMs = 300_000 // 5 minutes
settings.IsSet.IdleTimeoutMs = 1
settings.ServerResumptionLevel = 2 // QUIC_SERVER_RESUME_AND_ZERORTT
settings.IsSet.ServerResumptionLevel = 1
Expand Down
51 changes: 28 additions & 23 deletions Networking/Sources/Networking/Peer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ public final class Peer<Handler: StreamHandler>: Sendable {
if let curr {
return curr
}
let conn = try Connection(
QuicConnection(
handler: PeerEventHandler(self.impl),
registration: self.impl.clientConfiguration.registration,
configuration: self.impl.clientConfiguration
),
let quicConn = try QuicConnection(
handler: PeerEventHandler(self.impl),
registration: self.impl.clientConfiguration.registration,
configuration: self.impl.clientConfiguration
)
try quicConn.connect(to: address)
let conn = Connection(
quicConn,
impl: self.impl,
mode: mode,
remoteAddress: address,
Expand Down Expand Up @@ -266,25 +268,27 @@ private struct PeerEventHandler<Handler: StreamHandler>: QuicEventHandler {

// TODO: implement a peer and test this
func shouldOpen(_: QuicConnection, certificate: Data?) -> QuicStatus {
// TODO: enable certificate validation logic once parsing logic is fixed
guard let certificate else {
return .code(.requiredCert)
}
do {
let (publicKey, alternativeName) = try parseCertificate(data: certificate)
logger.debug(
"Certificate parsed",
metadata: ["publicKey": "\(publicKey.toHexString())", "alternativeName": "\(alternativeName)"]
)
if alternativeName != generateSubjectAlternativeName(pubkey: publicKey) {
return .code(.badCert)
}
if impl.mode == PeerMode.validator {
// TODO: verify if it is current or next validator
}
} catch {
logger.error("Failed to parse certificate", metadata: ["error": "\(error)"])
return .code(.badCert)
}
logger.trace("Received certificate", metadata: ["cert": "\(certificate.toHexString())"])
// do {
// let (publicKey, alternativeName) = try parseCertificate(data: certificate)
// logger.debug(
// "Certificate parsed",
// metadata: ["publicKey": "\(publicKey.toHexString())", "alternativeName": "\(alternativeName)"]
// )
// if alternativeName != generateSubjectAlternativeName(pubkey: publicKey) {
// return .code(.badCert)
// }
// if impl.mode == PeerMode.validator {
// // TODO: verify if it is current or next validator
// }
// } catch {
// logger.error("Failed to parse certificate", metadata: ["error": "\(error)"])
// return .code(.badCert)
// }
return .code(.success)
}

Expand Down Expand Up @@ -313,7 +317,8 @@ private struct PeerEventHandler<Handler: StreamHandler>: QuicEventHandler {
}
}

func shutdownInitiated(_ connection: QuicConnection, reason _: ConnectionCloseReason) {
func shutdownComplete(_ connection: QuicConnection) {
logger.trace("connection shutdown complete", metadata: ["connectionId": "\(connection.id)"])
impl.connections.write { connections in
if let conn = connections.byId[connection.id] {
connections.byId.removeValue(forKey: connection.id)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98df68a

Please sign in to comment.