Skip to content

Instantly share code, notes, and snippets.

@unakatsuo
Created April 6, 2018 13:06
Show Gist options
  • Save unakatsuo/035468917f99f0ec4b1b52f65f24a95c to your computer and use it in GitHub Desktop.
Save unakatsuo/035468917f99f0ec4b1b52f65f24a95c to your computer and use it in GitHub Desktop.
nflog with Go
package main
import (
"log"
"os"
"unsafe"
)
/*
#cgo pkg-config: libnetfilter_log
#include <stdlib.h>
#include <libnetfilter_log/libnetfilter_log.h>
#include "binding.h"
*/
import "C"
//export NflogCb
func NflogCb(gh *C.struct_nflog_g_handle, nfmsg *C.struct_nfgenmsg, nfa *C.struct_nflog_data, data unsafe.Pointer) C.int {
log.Print("NflogCb")
return 0
}
func main() {
h := C.nflog_open()
if uintptr(unsafe.Pointer(h)) == uintptr(0) {
log.Fatal("Failed nflog_open")
}
qh := C.nflog_bind_group(h, 10)
if uintptr(unsafe.Pointer(qh)) == uintptr(0) {
log.Fatal("Failed nflog_bind_group")
}
//C.nflog_callback_register(qh, (*C.nflog_callback)(uintptr(unsafe.Pointer(&NflogCb))), unsafe.Pointer(uintptr(0)))
C.SetCallback(qh)
fd := C.nflog_fd(h)
f := os.NewFile(uintptr(fd), "nflog fd")
buf := make([]byte, 1024)
for {
rlen, err := f.Read(buf)
if err != nil {
log.Print(err)
break
}
log.Print("read nflog: len=", rlen)
if err := C.nflog_handle_packet(h, (*C.char)(unsafe.Pointer(&buf[0])), C.int(rlen)); err != 0 {
log.Print("Failed nflog_handle_packet: ", err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment