diff --git a/raw.go b/raw.go index cef66bc..2ef063c 100644 --- a/raw.go +++ b/raw.go @@ -2,7 +2,12 @@ package main import ( "fmt" + "log" "net" + "time" + + "github.com/google/gopacket" + "github.com/google/gopacket/pcap" ) // type Header_IP4 struct { @@ -21,8 +26,35 @@ import ( // Options []byte // options, extension headers // } -func proto(n int, done chan bool) { - // fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW) +var ( + device string = "lo0" + snapshot_len int32 = 1024 + promiscuous bool = false + err error + timeout time.Duration = 0 + handle *pcap.Handle +) + +func tcp(n int, done chan bool) { + + //protocol := "tcp" + + //var packetConn PacketConn + + handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout) + if err != nil { + log.Fatal(err) + } + defer handle.Close() + + // Use the handle as a packet source to process all packets + packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) + for packet := range packetSource.Packets() { + // Process packet here + fmt.Println(packet) + } + + // fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP) // f := os.NewFile(uintptr(fd), fmt.Sprintf("fd %d", fd)) // for { @@ -32,9 +64,14 @@ func proto(n int, done chan bool) { // fmt.Println(err) // } // fmt.Printf("% X\n", buf[:numRead]) - // done <- true // } + done <- true + +} + +func icmp(n int, done chan bool) { + protocol := "icmp" netaddr, _ := net.ResolveIPAddr("ip4", "127.0.0.1") conn, _ := net.ListenIP("ip4:"+protocol, netaddr) @@ -46,12 +83,16 @@ func proto(n int, done chan bool) { fmt.Printf("% X\n", buf[:numRead]) } + done <- true } func main() { - done := make(chan bool) - go proto(4, done) - <-done + //done_icmp := make(chan bool) + done_tcp := make(chan bool) + //go icmp(4, done_icmp) + go tcp(4, done_tcp) + //<-done_icmp + <-done_tcp }