github.com/gopacket/gopacket@v1.1.0/layers/endpoints.go (about) 1 // Copyright 2012 Google, Inc. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the LICENSE file in the root of the source 5 // tree. 6 7 package layers 8 9 import ( 10 "encoding/binary" 11 "net" 12 "strconv" 13 14 "github.com/gopacket/gopacket" 15 ) 16 17 var ( 18 // We use two different endpoint types for IPv4 vs IPv6 addresses, so that 19 // ordering with endpointA.LessThan(endpointB) sanely groups all IPv4 20 // addresses and all IPv6 addresses, such that IPv6 > IPv4 for all addresses. 21 EndpointIPv4 = gopacket.RegisterEndpointType(1, gopacket.EndpointTypeMetadata{Name: "IPv4", Formatter: func(b []byte) string { 22 return net.IP(b).String() 23 }}) 24 EndpointIPv6 = gopacket.RegisterEndpointType(2, gopacket.EndpointTypeMetadata{Name: "IPv6", Formatter: func(b []byte) string { 25 return net.IP(b).String() 26 }}) 27 28 EndpointMAC = gopacket.RegisterEndpointType(3, gopacket.EndpointTypeMetadata{Name: "MAC", Formatter: func(b []byte) string { 29 return net.HardwareAddr(b).String() 30 }}) 31 EndpointTCPPort = gopacket.RegisterEndpointType(4, gopacket.EndpointTypeMetadata{Name: "TCP", Formatter: func(b []byte) string { 32 return strconv.Itoa(int(binary.BigEndian.Uint16(b))) 33 }}) 34 EndpointUDPPort = gopacket.RegisterEndpointType(5, gopacket.EndpointTypeMetadata{Name: "UDP", Formatter: func(b []byte) string { 35 return strconv.Itoa(int(binary.BigEndian.Uint16(b))) 36 }}) 37 EndpointSCTPPort = gopacket.RegisterEndpointType(6, gopacket.EndpointTypeMetadata{Name: "SCTP", Formatter: func(b []byte) string { 38 return strconv.Itoa(int(binary.BigEndian.Uint16(b))) 39 }}) 40 EndpointRUDPPort = gopacket.RegisterEndpointType(7, gopacket.EndpointTypeMetadata{Name: "RUDP", Formatter: func(b []byte) string { 41 return strconv.Itoa(int(b[0])) 42 }}) 43 EndpointUDPLitePort = gopacket.RegisterEndpointType(8, gopacket.EndpointTypeMetadata{Name: "UDPLite", Formatter: func(b []byte) string { 44 return strconv.Itoa(int(binary.BigEndian.Uint16(b))) 45 }}) 46 EndpointPPP = gopacket.RegisterEndpointType(9, gopacket.EndpointTypeMetadata{Name: "PPP", Formatter: func([]byte) string { 47 return "point" 48 }}) 49 ) 50 51 // NewIPEndpoint creates a new IP (v4 or v6) endpoint from a net.IP address. 52 // It returns gopacket.InvalidEndpoint if the IP address is invalid. 53 func NewIPEndpoint(a net.IP) gopacket.Endpoint { 54 ipv4 := a.To4() 55 if ipv4 != nil { 56 return gopacket.NewEndpoint(EndpointIPv4, []byte(ipv4)) 57 } 58 59 ipv6 := a.To16() 60 if ipv6 != nil { 61 return gopacket.NewEndpoint(EndpointIPv6, []byte(ipv6)) 62 } 63 64 return gopacket.InvalidEndpoint 65 } 66 67 // NewMACEndpoint returns a new MAC address endpoint. 68 func NewMACEndpoint(a net.HardwareAddr) gopacket.Endpoint { 69 return gopacket.NewEndpoint(EndpointMAC, []byte(a)) 70 } 71 func newPortEndpoint(t gopacket.EndpointType, p uint16) gopacket.Endpoint { 72 return gopacket.NewEndpoint(t, []byte{byte(p >> 8), byte(p)}) 73 } 74 75 // NewTCPPortEndpoint returns an endpoint based on a TCP port. 76 func NewTCPPortEndpoint(p TCPPort) gopacket.Endpoint { 77 return newPortEndpoint(EndpointTCPPort, uint16(p)) 78 } 79 80 // NewUDPPortEndpoint returns an endpoint based on a UDP port. 81 func NewUDPPortEndpoint(p UDPPort) gopacket.Endpoint { 82 return newPortEndpoint(EndpointUDPPort, uint16(p)) 83 } 84 85 // NewSCTPPortEndpoint returns an endpoint based on a SCTP port. 86 func NewSCTPPortEndpoint(p SCTPPort) gopacket.Endpoint { 87 return newPortEndpoint(EndpointSCTPPort, uint16(p)) 88 } 89 90 // NewRUDPPortEndpoint returns an endpoint based on a RUDP port. 91 func NewRUDPPortEndpoint(p RUDPPort) gopacket.Endpoint { 92 return gopacket.NewEndpoint(EndpointRUDPPort, []byte{byte(p)}) 93 } 94 95 // NewUDPLitePortEndpoint returns an endpoint based on a UDPLite port. 96 func NewUDPLitePortEndpoint(p UDPLitePort) gopacket.Endpoint { 97 return newPortEndpoint(EndpointUDPLitePort, uint16(p)) 98 }