net/packet: add IPv6 source and destination IPs to Parsed.

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2020-11-10 00:04:27 -08:00
committed by Dave Anderson
parent d192bd0f86
commit 89894c6930
8 changed files with 66 additions and 40 deletions

24
net/packet/ip6.go Normal file
View File

@@ -0,0 +1,24 @@
package packet
import (
"fmt"
"inet.af/netaddr"
)
type IP6 [16]byte
func IP6FromNetaddr(ip netaddr.IP) IP6 {
if !ip.Is6() {
panic(fmt.Sprintf("IP6FromNetaddr called with non-v6 addr %q", ip))
}
return IP6(ip.As16())
}
func (ip IP6) Netaddr() netaddr.IP {
return netaddr.IPFrom16(ip)
}
func (ip IP6) String() string {
return ip.Netaddr().String()
}

View File

@@ -45,8 +45,10 @@ type Parsed struct {
IPVersion uint8 // 4, 6, or 0
IPProto IP4Proto // IP subprotocol (UDP, TCP, etc); the NextHeader field for IPv6
SrcIP IP4 // IP source address (not used for IPv6)
DstIP IP4 // IP destination address (not used for IPv6)
SrcIP4 IP4 // IPv4 source address (not used for IPv6)
DstIP4 IP4 // IPv4 destination address (not used for IPv6)
SrcIP6 IP6 // IPv6 source address (not used for IPv4)
DstIP6 IP6 // IPv6 destination address (not used for IPv4)
SrcPort uint16 // TCP/UDP source port
DstPort uint16 // TCP/UDP destination port
TCPFlags uint8 // TCP flags (SYN, ACK, etc)
@@ -66,9 +68,9 @@ func (p *Parsed) String() string {
sb := strbuilder.Get()
sb.WriteString(p.IPProto.String())
sb.WriteByte('{')
writeIPPort(sb, p.SrcIP, p.SrcPort)
writeIPPort(sb, p.SrcIP4, p.SrcPort)
sb.WriteString(" > ")
writeIPPort(sb, p.DstIP, p.DstPort)
writeIPPort(sb, p.DstIP4, p.DstPort)
sb.WriteByte('}')
return sb.String()
}
@@ -140,8 +142,8 @@ func (q *Parsed) Decode(b []byte) {
}
// If it's valid IPv4, then the IP addresses are valid
q.SrcIP = IP4(get32(b[12:16]))
q.DstIP = IP4(get32(b[16:20]))
q.SrcIP4 = IP4(get32(b[12:16]))
q.DstIP4 = IP4(get32(b[16:20]))
q.subofs = int((b[0] & 0x0F) << 2)
sub := b[q.subofs:]
@@ -229,8 +231,8 @@ func (q *Parsed) IPHeader() IP4Header {
return IP4Header{
IPID: ipid,
IPProto: q.IPProto,
SrcIP: q.SrcIP,
DstIP: q.DstIP,
SrcIP: q.SrcIP4,
DstIP: q.DstIP4,
}
}

View File

@@ -49,8 +49,8 @@ var icmpRequestDecode = Parsed{
IPVersion: 4,
IPProto: ICMP,
SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcIP4: NewIP4(net.ParseIP("1.2.3.4")),
DstIP4: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 0,
DstPort: 0,
}
@@ -75,8 +75,8 @@ var icmpReplyDecode = Parsed{
IPVersion: 4,
IPProto: ICMP,
SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcIP4: NewIP4(net.ParseIP("1.2.3.4")),
DstIP4: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 0,
DstPort: 0,
}
@@ -131,8 +131,8 @@ var tcpPacketDecode = Parsed{
IPVersion: 4,
IPProto: TCP,
SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcIP4: NewIP4(net.ParseIP("1.2.3.4")),
DstIP4: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 123,
DstPort: 567,
TCPFlags: TCPSynAck,
@@ -159,8 +159,8 @@ var udpRequestDecode = Parsed{
IPVersion: 4,
IPProto: UDP,
SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcIP4: NewIP4(net.ParseIP("1.2.3.4")),
DstIP4: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 123,
DstPort: 567,
}
@@ -185,8 +185,8 @@ var udpReplyDecode = Parsed{
length: len(udpReplyBuffer),
IPProto: UDP,
SrcIP: NewIP4(net.ParseIP("1.2.3.4")),
DstIP: NewIP4(net.ParseIP("5.6.7.8")),
SrcIP4: NewIP4(net.ParseIP("1.2.3.4")),
DstIP4: NewIP4(net.ParseIP("5.6.7.8")),
SrcPort: 567,
DstPort: 123,
}