How to use c function with argument type `[*c] u8`

I am attempting to use a c function with the following signature (provided by @cImport) (this is pretty effing cool btw):

pub extern fn pcap_open(source: [*c]const u8, snaplen: c_int, flags: c_int, read_timeout: c_int, auth: [*c]struct_pcap_rmtauth, errbuf: [*c]u8) ?*pcap_t;
const npcap = @cImport({
    @cInclude("pcap.h");
});

var pcap_errbuf: [npcap.PCAP_ERRBUF_SIZE]u8 = [_]u8{0} ** npcap.PCAP_ERRBUF_SIZE;

pub const WindowsRawSocket = struct {
    socket: *npcap.struct_pcap,

    pub fn init(ifname: [:0]const u8) !WindowsRawSocket {
        const socket = npcap.pcap_open(ifname, 65536, npcap.PCAP_OPENFLAG_PROMISCUOUS |
            npcap.PCAP_OPENFLAG_MAX_RESPONSIVENESS |
            npcap.PCAP_OPENFLAG_NOCAPTURE_LOCAL, -1, null, *pcap_errbuf) orelse {
            std.log.err("Failed to open interface {s}, npcap error: {s}", .{ ifname, pcap_errbuf });
            return error.FailedToOpenInterface;
        };

        return WindowsRawSocket{
            .socket = socket,
        };
    }
};

But I get the following compile error:

src/nic.zig:390:61: error: expected type 'type', found '[256]u8'
            npcap.PCAP_OPENFLAG_NOCAPTURE_LOCAL, -1, null, *pcap_errbuf) orelse {

The docs for this library show usage that looks like this:

char errbuf[PCAP_ERRBUF_SIZE];
if ( (fp= pcap_open(source, // name of the device
          65536, // portion of the packet to capture
                 // 65536 guarantees that the whole packet
                 // will be captured on all the link layers
           PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
           1000, // read timeout
           NULL, // authentication on the remote machine
           errbuf // error buffer
           ) ) == NULL)
{
  fprintf(stderr,"\nUnable to open the file %s.\n", source);
  return -1;
}

  1. What is a [*c] u8
  2. How do I make and use one?

The error is because this is invalid syntax:

*pcap_errbuf

You want to take the address of pcap_errbuf instead:

&pcap_errbuf

As for what [*c] is, see the C Pointers section of the langref

1 Like