// Receive packets in a loop until quit func (nflog *NfLog) Loop() { buflen := C.size_t(RecvBufferSize) pbuf := C.malloc(buflen) if pbuf == nil { log.Fatal("No memory for malloc") } defer C.free(pbuf) for { nr, err := C.recv(nflog.fd, pbuf, buflen, 0) select { case <-nflog.quit: return default: } if nr < 0 || err != nil { log.Printf("Recv failed: %s", err) nflog.errors++ } else { // Handle messages in packet reusing memory ps := <-nflog.a.returnAddPackets nflog.packets.index = 0 C.nflog_handle_packet(nflog.h, (*C.char)(pbuf), (C.int)(nr)) nflog.a.processAddPackets <- nflog.processPackets(ps[:0]) } } }
// Receive packets in a loop until quit func (nflog *NfLog) Loop() { buf := make([]byte, RecvBufferSize) pbuf := unsafe.Pointer(&buf[0]) buflen := C.size_t(len(buf)) for !nflog.quit { nr := C.recv(nflog.fd, pbuf, buflen, 0) if nr < 0 { log.Printf("Recvfrom failed: %s", strerror()) nflog.errors++ } else { // Handle messages in packet reusing memory ps := <-nflog.a.returnAddPackets nflog.addPackets = ps[:0] C.nflog_handle_packet(nflog.h, (*C.char)(pbuf), (C.int)(nr)) nflog.a.processAddPackets <- nflog.addPackets nflog.addPackets = nil } } }