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

Update PCAP example to be async #1422

Merged
merged 5 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ extension Target {
.nioCore,
.nioPosix,
.nioExtras,
.logging,
.argumentParser,
],
path: "Sources/Examples/PacketCapture",
Expand Down
17 changes: 17 additions & 0 deletions Sources/Examples/PacketCapture/Empty.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2022, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// This file exists to workaround https://github.com/apple/swift/issues/55127.
94 changes: 94 additions & 0 deletions Sources/Examples/PacketCapture/PacketCapture.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2020, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if compiler(>=5.6)
import ArgumentParser
import EchoModel
import GRPC
import NIOCore
import NIOExtras
import NIOPosix

@main
@available(macOS 10.15, *)
struct PCAP: AsyncParsableCommand {
@Option(help: "The port to connect to")
var port = 1234

func run() async throws {
// Create an `EventLoopGroup`.
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}

// The filename for the .pcap file to write to.
let path = "packet-capture-example.pcap"
let fileSink = try NIOWritePCAPHandler.SynchronizedFileSink.fileSinkWritingToFile(
path: path
) { error in
print("Failed to write with error '\(error)' for path '\(path)'")
}

// Ensure that we close the file sink when we're done with it.
defer {
try! fileSink.syncClose()
}

let channel = try GRPCChannelPool.with(
target: .host("localhost", port: self.port),
transportSecurity: .plaintext,
eventLoopGroup: group
) {
$0.debugChannelInitializer = { channel in
// Create the PCAP handler and add it to the start of the channel pipeline. If this example
// used TLS we would likely want to place the handler in a different position in the
// pipeline so that the captured packets in the trace would not be encrypted.
let writePCAPHandler = NIOWritePCAPHandler(mode: .client, fileSink: fileSink.write(buffer:))
return channel.eventLoop.makeCompletedFuture(Result {
try channel.pipeline.syncOperations.addHandler(writePCAPHandler, position: .first)
})
}
}

// Create a client.
let echo = Echo_EchoAsyncClient(channel: channel)

let messages = ["foo", "bar", "baz", "thud", "grunt", "gorp"].map { text in
Echo_EchoRequest.with { $0.text = text }
}

do {
for try await response in echo.update(messages) {
print("Received response '\(response.text)'")
}
print("RPC completed successfully")
} catch {
print("RPC failed with error '\(error)'")
}

print("Try opening '\(path)' in Wireshark or with 'tcpdump -r \(path)'")

try await echo.channel.close().get()
}
}
#else
@main
enum PCAP {
static func main() {
print("This example requires Swift >= 5.6")
}
}
#endif // compiler(>=5.6)
12 changes: 1 addition & 11 deletions Sources/Examples/PacketCapture/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@ In a separate shell run:
$ swift run PacketCapture
```

Some logs should be emitted similar to below, including the path of the
*.pcap* file:

```sh
2020-07-24T10:48:50+0100 info gRPC PCAP Demo : Creating fileSink for path './channel-ObjectIdentifier(0x00007f8a25604c40).pcap'
2020-07-24T10:48:50+0100 info gRPC PCAP Demo : ✅ Successfully created fileSink for path './channel-ObjectIdentifier(0x00007f8a25604c40).pcap'
...
2020-07-24T10:48:50+0100 info gRPC PCAP Demo : ✅ RPC completed successfully
...
2020-07-24T10:48:50+0100 info gRPC PCAP Demo : Done!
```
The pcap file will be written to 'packet-capture-example.pcap'.

The *.pcap* file can be opened with either: [Wireshark][wireshark] or `tcpdump
-r <PCAP_FILE>`.
Expand Down
146 changes: 0 additions & 146 deletions Sources/Examples/PacketCapture/main.swift

This file was deleted.