func signalCatcher(pcapreader *pcap.Pcap) { ch := make(chan os.Signal) signal.Notify(ch, syscall.SIGINT) <-ch fmt.Println("CTRL-C; exiting") pcapreader.Close() }
func readPackets(pcapreader *pcap.Pcap, quit_chan chan bool) { count := 0 timebegin := time.Now() for pkt := pcapreader.Next(); pkt != nil; pkt = pcapreader.Next() { go launchParser(pkt) count += 1 if count%1000000 == 0 { fmt.Println("num pkts=", count, "in", time.Now().Sub(timebegin)) timebegin = time.Now() } } fmt.Print("Nothing more to read\n") quit_chan <- true }
func main() { var h *pcap.Pcap var err string var dumper *pcap.PcapDumper if *offlinefn == "" { h, err = pcap.Openlive(*device, 1500, true, 0) } else { h, err = pcap.Openoffline(*offlinefn) } if *writefile != "" { dumper = pcap.NewPcapDumper(h, *writefile) } if h == nil { fmt.Printf("Warning: no devices found : %s\n", err) os.Exit(-1) } h.Setfilter(expr) for pkt := h.Next(); pkt != nil; pkt = h.Next() { packet := pcap.DecodeEthernetPkt(pkt) if dumper == nil { pcap.PrintDecodedPkt(packet) } else { dumper.Dump(packet.PcapPktHdr) } } }
func controler(pcapreader *pcap.Pcap, quit_chan chan bool) { timebegin := time.Now() MAIN: for { select { case <-quit_chan: fmt.Print("\nEND\n") fmt.Printf("ETH routines: %d\n", len(data.ETHMAP.StatsChans)) for _, chans := range data.ETHMAP.StatsChans { chans.Control <- "<kill>" } fmt.Printf("IP routines: %d\n", len(data.IPv4MAP.StatsChans)) for _, chans := range data.IPv4MAP.StatsChans { chans.Control <- "<kill>" } fmt.Printf("TCP routines: %d\n", len(data.TcpMAP.StatsChans)) for _, chans := range data.TcpMAP.StatsChans { chans.Control <- "<kill>" } break MAIN case <-clock.Clock.DumpChan: dumpbegin := time.Now() pcapreader.Paused = true dump.WriteEthernet(CONFIG, data.ETHMAP) dump.WriteIpv4(CONFIG, data.IPv4MAP) dump.WriteTcp(CONFIG, data.TcpMAP) pcapreader.Paused = false if CONFIG["debug"] == "true" { fmt.Println("<< END DUMPS ", time.Now().Sub(dumpbegin), clock.Clock.Get(), "\n") } } } fmt.Println("controler ends, total time:", time.Now().Sub(timebegin)) }
func main() { var device *string = flag.String("d", "", "device") var file *string = flag.String("r", "", "file") var expr *string = flag.String("e", "", "filter expression") flag.Parse() var h *pcap.Pcap var err string ifs, err := pcap.Findalldevs() if len(ifs) == 0 { fmt.Printf("Warning: no devices found : %s\n", err) } else { for i := 0; i < len(ifs); i++ { fmt.Printf("dev %d: %s (%s)\n", i+1, ifs[i].Name, ifs[i].Description) } } if *device != "" { h, err = pcap.Openlive(*device, 65535, true, 0) if h == nil { fmt.Printf("Openlive(%s) failed: %s\n", *device, err) return } } else if *file != "" { h, err = pcap.Openoffline(*file) if h == nil { fmt.Printf("Openoffline(%s) failed: %s\n", *file, err) return } } else { fmt.Printf("usage: pcaptest [-d <device> | -r <file>]\n") return } fmt.Printf("pcap version: %s\n", pcap.Version()) if *expr != "" { fmt.Printf("Setting filter: %s\n", *expr) err := h.Setfilter(*expr) if err != "" { fmt.Printf("Warning: setting filter failed: %s\n", err) } } for pkt := h.Next(); pkt != nil; pkt = h.Next() { fmt.Printf("time: %d.%06d (%s) caplen: %d len: %d\nData:", int64(pkt.Time.Sec), int64(pkt.Time.Usec), time.SecondsToLocalTime(int64(pkt.Time.Sec)).String(), int64(pkt.Caplen), int64(pkt.Len)) for i := uint32(0); i < pkt.Caplen; i++ { if i%32 == 0 { fmt.Printf("\n") } if 32 <= pkt.Data[i] && pkt.Data[i] <= 126 { fmt.Printf("%c", pkt.Data[i]) } else { fmt.Printf(".") } } fmt.Printf("\n\n") } }