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

pcap: use C.struct, not _Ctype_struct #589

Merged
merged 1 commit into from
Jan 4, 2019
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
2 changes: 1 addition & 1 deletion pcap/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (p *Handle) ListDataLinks() (datalinks []Datalink, err error) {
return p.pcapListDatalinks()
}

// compileBPFFilter always returns an allocated _Ctype_struct_bpf_program
// compileBPFFilter always returns an allocated C.struct_bpf_program
// It is the callers responsibility to free the memory again, e.g.
//
// C.pcap_freecode(&bpf)
Expand Down
16 changes: 8 additions & 8 deletions pcap/pcap_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (p *Handle) pcapGeterr() error {
}

func (p *Handle) pcapStats() (*Stats, error) {
var cstats _Ctype_struct_pcap_stat
var cstats C.struct_pcap_stat
if C.pcap_stats(p.cptr, &cstats) < 0 {
return nil, p.pcapGeterr()
}
Expand All @@ -231,18 +231,18 @@ func (p *Handle) pcapCompile(expr string, maskp uint32) (pcapBpfProgram, error)

pcapCompileMu.Lock()
defer pcapCompileMu.Unlock()
if C.pcap_compile(p.cptr, (*_Ctype_struct_bpf_program)(&bpf), cexpr, 1, C.bpf_u_int32(maskp)) < 0 {
if C.pcap_compile(p.cptr, (*C.struct_bpf_program)(&bpf), cexpr, 1, C.bpf_u_int32(maskp)) < 0 {
return bpf, p.pcapGeterr()
}
return bpf, nil
}

func (p pcapBpfProgram) free() {
C.pcap_freecode((*_Ctype_struct_bpf_program)(&p))
C.pcap_freecode((*C.struct_bpf_program)(&p))
}

func (p pcapBpfProgram) toBPFInstruction() []BPFInstruction {
bpfInsn := (*[bpfInstructionBufferSize]_Ctype_struct_bpf_insn)(unsafe.Pointer(p.bf_insns))[0:p.bf_len:p.bf_len]
bpfInsn := (*[bpfInstructionBufferSize]C.struct_bpf_insn)(unsafe.Pointer(p.bf_insns))[0:p.bf_len:p.bf_len]
bpfInstruction := make([]BPFInstruction, len(bpfInsn), len(bpfInsn))

for i, v := range bpfInsn {
Expand All @@ -258,7 +258,7 @@ func pcapBpfProgramFromInstructions(bpfInstructions []BPFInstruction) pcapBpfPro
var bpf pcapBpfProgram
bpf.bf_len = C.u_int(len(bpfInstructions))
cbpfInsns := C.calloc(C.size_t(len(bpfInstructions)), C.size_t(unsafe.Sizeof(bpfInstructions[0])))
gbpfInsns := (*[bpfInstructionBufferSize]_Ctype_struct_bpf_insn)(cbpfInsns)
gbpfInsns := (*[bpfInstructionBufferSize]C.struct_bpf_insn)(cbpfInsns)

for i, v := range bpfInstructions {
gbpfInsns[i].code = C.ushort(v.Code)
Expand All @@ -267,7 +267,7 @@ func pcapBpfProgramFromInstructions(bpfInstructions []BPFInstruction) pcapBpfPro
gbpfInsns[i].k = C.uint(v.K)
}

bpf.bf_insns = (*_Ctype_struct_bpf_insn)(cbpfInsns)
bpf.bf_insns = (*C.struct_bpf_insn)(cbpfInsns)
return bpf
}

Expand Down Expand Up @@ -296,11 +296,11 @@ func (b *BPF) pcapOfflineFilter(ci gopacket.CaptureInfo, data []byte) bool {
hdr.caplen = C.bpf_u_int32(len(data)) // Trust actual length over ci.Length.
hdr.len = C.bpf_u_int32(ci.Length)
dataptr := (*C.u_char)(unsafe.Pointer(&data[0]))
return C.pcap_offline_filter((*_Ctype_struct_bpf_program)(&b.bpf), &hdr, dataptr) != 0
return C.pcap_offline_filter((*C.struct_bpf_program)(&b.bpf), &hdr, dataptr) != 0
}

func (p *Handle) pcapSetfilter(bpf pcapBpfProgram) error {
if C.pcap_setfilter(p.cptr, (*_Ctype_struct_bpf_program)(&bpf)) < 0 {
if C.pcap_setfilter(p.cptr, (*C.struct_bpf_program)(&bpf)) < 0 {
return p.pcapGeterr()
}
return nil
Expand Down