Skip to content

Commit

Permalink
fix bug for listening unwanted packet
Browse files Browse the repository at this point in the history
  • Loading branch information
Lqlsoftware committed Apr 26, 2018
1 parent 9fc2cb9 commit e06a4a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
6 changes: 1 addition & 5 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ func getAdapter() *pcap.Interface {
}
}
// 输出IPv4地址
for _,v := range adapters[idx].Addresses {
if len(v.IP) == 4 {
log.Print("IPv4: ",v.IP)
}
}
log.Print("IPv4: ", getIPV4(&adapters[idx]))
return &adapters[idx]
}
2 changes: 1 addition & 1 deletion listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func listen(adapter *pcap.Interface, port layers.TCPPort) {
check(err)

// 设置过滤器
err = handle.SetBPFFilter("tcp and dst port " + strconv.Itoa(int(port)))
err = handle.SetBPFFilter("tcp and dst port " + strconv.Itoa(int(port)) + " and ip dst " + getIPV4(adapter).String())
check(err)

// 建立数据源
Expand Down
16 changes: 15 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package gopcap

import "os"
import (
"net"
"os"

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

func check(err error) {
if err != nil {
Expand All @@ -20,4 +25,13 @@ func checkDirIsExist(filename string) bool {
return false
}
return true
}

func getIPV4(adapter *pcap.Interface) net.IP {
for _,v := range adapter.Addresses {
if len(v.IP) == 4 {
return v.IP
}
}
return net.IPv4(0,0,0,0)
}

0 comments on commit e06a4a3

Please sign in to comment.