Browse Source

handle IPv6 packets, too

Tobias Begalke 7 years ago
parent
commit
fcc5f5737d
1 changed files with 19 additions and 14 deletions
  1. 19 14
      main.go

+ 19 - 14
main.go

@@ -173,22 +173,27 @@ func liveCapture() {
 func processPacket(packet gopacket.Packet) {
 	count++
 
-	ipLayer := packet.Layer(layers.LayerTypeIPv4)
-	if ipLayer == nil {
-		log.Println("No IPv4 Layer!")
-		return
-	}
-
-	ip, _ := ipLayer.(*layers.IPv4)
-
-	if ip.Protocol != layers.IPProtocolTCP {
-		log.Println("No TCP Protocol!")
-		return
+	hasIPv4 := false
+	var ipSrc, ipDst string
+
+	// IPv4
+	if ipLayer := packet.Layer(layers.LayerTypeIPv4); ipLayer != nil {
+		ip := ipLayer.(*layers.IPv4)
+		ipSrc = ip.SrcIP.String()
+		ipDst = ip.DstIP.String()
+		hasIPv4 = true
+	}
+
+	// IPv6
+	if !hasIPv4 {
+		if ipLayer := packet.Layer(layers.LayerTypeIPv6); ipLayer != nil {
+			ip := ipLayer.(*layers.IPv6)
+			ipSrc = ip.SrcIP.String()
+			ipDst = ip.DstIP.String()
+		}
 	}
 
-	ipSrc := ip.SrcIP.String()
-	ipDst := ip.DstIP.String()
-
+	// TCP
 	tcpLayer := packet.Layer(layers.LayerTypeTCP)
 	if tcpLayer == nil {
 		return