Skip to content

Commit

Permalink
First TCP packet catched
Browse files Browse the repository at this point in the history
  • Loading branch information
Micheloss committed Jan 22, 2016
1 parent 5918b9e commit ae58b17
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package main

import (
"fmt"
"log"
"net"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)

// type Header_IP4 struct {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit ae58b17

Please sign in to comment.