Skip to content

Commit

Permalink
Basic reading in a few packets and checking contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Pix committed Jul 1, 2023
1 parent 534add4 commit 06f215d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,47 @@ basic_find_devs :: proc(t: ^_t.T) {

pcap.freealldevs(interfaces)
}

@(test)
basic_capture :: proc(t: ^_t.T) {
errbuf: [pcap.PCAP_ERRBUF_SIZE]byte
interfaces: ^pcap.pcap_if_t
header: ^pcap.pcap_pkthdr
packet: [^]byte


err := pcap.findalldevs(&interfaces, &errbuf[0])
if err == pcap.PCAP_ERROR {
fmt.println(
"Error while finding all devs: ",
strings.string_from_ptr(&errbuf[0], len(errbuf)),
)
}

handler := pcap.open_live(interfaces.name, 512, true, 1000, &errbuf[0])

PCAP_OKAY :: 1
for i := 0; i < 5; i += 1 {
err := pcap.next_ex(handler, &header, &packet)
if err == PCAP_OKAY {
fmt.println("TS: ", header.ts)
fmt.println("Cap Len: ", header.caplen)
fmt.println("Len: ", header.len)
plen: u32 = 64
if header.caplen < plen do plen = header.caplen
for j: u32 = 0; j < plen; j += 1 {
if (j % 16 == 0 && j != 0) {
fmt.printf("%2X \n", packet[j])
} else if (j % 8 == 0 && j != 0) {
fmt.printf("%2X ", packet[j])
} else {
fmt.printf("%2X ", packet[j])
}
}
fmt.println()
}
}

pcap.freealldevs(interfaces)
pcap.close(handler)
}

0 comments on commit 06f215d

Please sign in to comment.