https://github.com/vikingfacer/cappy/tree/main
Main article: https://sillycodeproject.com/2025/11/11/cappy/
I’m not satisfied with how Cappy prints packets. Cappy is expected to print packets the same way if given an live capture or an offline capture. This make testing packet printing easier and more reliable. I do not want to set up a complicated testing environment. I would like to just feed Cappy a capture file. (Rube Goldberg Machine Environment < Capture File).
During testing of Cappy I found that the offline capture file input did not work, as expected. In fact it didn’t work at all. In this post we will fix this. Cappy will use the same functionality given a offline capture or live capture.
Problem 1
Pcap’s Create, creates a file, it does not open a file. We need to add a the correct function in our zig libpcap FFI (foreign function interface) (zig libpcap wrapper). To add whatever libpcap uses to open up the file. After the function is added we just need to replace create with it.
if (res.args.input) |file| {
if (pcap.create(file, &errorBuffer)) |fileOpen| {
capture = fileOpen;
} else {
try stdout.print("Unable to open file: {s}\n", .{file});
try stdout.print("{s}\n", .{errorBuffer});
}
} else {
if(something) |var| { do this w/ var }
The code above may look foreign almost like C or C++ but alien. This because of a feature of Zig. If statements in Zig have capture functionality. Meaning the return of the expression in the if statement is captured. Capture is denoted by the || in if () || {}. Variable capture in a if statement is like using assignment as the predicate an if statement.
This is useful with functions that return: Errors or Values, and Optional types. With Error or value predicates on Errors the else branch is taken. The same is true for nothing returns on optional predicates.
Try
Beyond that the other surprising feature is stdout.print can fail. All Errors/Failures need to be handled, so a try is needed to not handle failures here. Try passes on the failure to next scope, which will need to handle or passed on with another try.
This method for error handling is different than exceptions in C++. In C++ the function throwing the exception isn’t required to denote that it throws exceptions. Also, the calling the throwing function isn’t required to handle or even acknowledge that it is passing on a Failure/Error. Exceptions can be thrown 215 function calls deep from main and handled 1 function call from main.
open_offline
//pcap_open_offline
pub fn open_offline(file: []const u8, errorBuffer: []u8)?pcapture {
if(PCAP_C.pcap_open_offline(@alignCast(file),errorBuffer.ptr))|p| {
return pcapture{
.cap = p,
};
} else {
return null;
}
}
The code above is the newly added Zig wrapper around the pcap lib. In Zig C interoperability easy and a first class feature. C import functions are directly callable in Zig, but Zig and C do clash. One clash is Zig transforms C pointers into dense and verbose Zig types. There are many other clashes. The big differences between C and Zig is C does not have Zig types like optionals, Error types.
Some Syntax Need Explained
(each header is a link to the documentation)
?sometype
Zig has optionals as a built in type. In the code above the function open_offline returns an optional pcap struct. PCAP_C.pcap_open_offline is the “C” function returned by Zig’s C import. It returns an ?* struct pcap, which is a optional pointer to a struct pcap. In the actual C library pcap_open_offline returns a null on failure and valid pointer on success. Zig does not allow Zig pointers to be null, so Zig returns a optional Zig pointer. IMO this is a good choice the language made.
The C import returns an optional. Therefore, the wrapper function can use the `if () || {} else {}` to build the Zig library’s pcap struct. In the success scope we return an anonymous structs. where the captured value is stuff into the anon struct.
if (res.args.input) |file| {
if (pcap.open_offline(file, &errorBuffer)) |fileOpen| {
capture = fileOpen;
} else {
std.debug.print("Unable to open file: {s}\n", .{file});
std.debug.print("{s}\n", .{errorBuffer});
}
} else {
The only changes made from the code we started with is create -> open_offline and stdout -> std.debug.print. The rational for create -> open_offline has already been stated. stdout -> std.debug.print because stdout is a buffered output and debug.print is stderr which is not buffered. When reporting an error reliable is always more important than speed. Buffered printing is faster but can fail. Unbuffered printing does not fail, but is slower. The reporting of errors was updated to align with this rational.
It Worked

In the next post we will work on making the packet capturing more testable. ATM we have offline files working, but still more work needs done.
There is still plenty left to fix.
Links
https://ziglang.org/documentation/master
Leave a Reply