Skip to content

Commit

Permalink
Add EtherIP and IP4/6 encapsulation in IP packets.
Browse files Browse the repository at this point in the history
  • Loading branch information
gconnell committed Dec 7, 2012
1 parent e7a97bf commit 063adcb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
17 changes: 13 additions & 4 deletions enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ func (e EthernetType) Decode(data []byte) (out DecodeResult, err error) {
type IPProtocol uint8

const (
IPProtocolICMP IPProtocol = 1
IPProtocolTCP IPProtocol = 6
IPProtocolUDP IPProtocol = 17
IPProtocolSCTP IPProtocol = 132
IPProtocolICMP IPProtocol = 1
IPProtocolTCP IPProtocol = 6
IPProtocolUDP IPProtocol = 17
IPProtocolSCTP IPProtocol = 132
IPProtocolIPv6 IPProtocol = 41
IPProtocolIPIP IPProtocol = 94
IPProtocolEtherIP IPProtocol = 97
)

func (ip IPProtocol) Decode(data []byte) (out DecodeResult, err error) {
Expand All @@ -58,6 +61,12 @@ func (ip IPProtocol) Decode(data []byte) (out DecodeResult, err error) {
return decodeICMP(data)
case IPProtocolSCTP:
return decodeSCTP(data)
case IPProtocolIPv6:
return decodeIPv6(data)
case IPProtocolIPIP:
return decodeIPv4(data)
case IPProtocolEtherIP:
return decodeEtherIP(data)
}
err = fmt.Errorf("Unsupported IP protocol %v", ip)
return
Expand Down
26 changes: 26 additions & 0 deletions layer_etherip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2012 Google, Inc. All rights reserved.

package gopacket

import (
"encoding/binary"
"errors"
)

// EtherIP is the struct for storing RFC 3378 EtherIP packet headers.
type EtherIP struct {
Version uint8
Reserved uint16
}

// LayerType returns LayerTypeEtherIP.
func (e *EtherIP) LayerType() LayerType { return LayerTypeEtherIP }

func decodeEtherIP(data []byte) (out DecodeResult, _ error) {
out.DecodedLayer = &EtherIP{
Version: data[0] >> 4,
Reserved: binary.BigEndian.Uint16(data[:2]) & 0x0fff,
}
out.NextDecoder = decodeEthernet
out.RemainingBytes = data[2:]
}
15 changes: 11 additions & 4 deletions layer_sctp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ var decodeWithSCTPChunkTypePrefix decoderFunc = func(data []byte) (DecodeResult,

// SCTPChunk contains the common fields in all SCTP chunks.
type SCTPChunk struct {
Type SCTPChunkType
Flags uint8
Length uint16
Type SCTPChunkType
Flags uint8
Length uint16
// ActualLength is the total length of an SCTP chunk, including padding.
// SCTP chunks start and end on 4-byte boundaries. So if a chunk has a length
// of 18, it means that it has data up to and including byte 18, then padding
// up to the next 4-byte boundary, 20. In this case, Length would be 18, and
// ActualLength would be 20.
ActualLength int
}

Expand Down Expand Up @@ -90,7 +95,7 @@ func decodeSCTPParameter(data []byte) SCTPParameter {
// SCTPUnknownChunkType is the layer type returned when we don't recognize the
// chunk type. Since there's a length in a known location, we can skip over
// it even if we don't know what it is, and continue parsing the rest of the
// chunks.
// chunks. This chunk is stored as an ErrorLayer in the packet.
type SCTPUnknownChunkType struct {
SCTPChunk
bytes []byte
Expand All @@ -113,6 +118,7 @@ func (s *SCTPUnknownChunkType) LayerType() LayerType { return LayerTypeSCTPUnkno
// and Flags.
func (s *SCTPUnknownChunkType) Payload() []byte { return s.bytes }

// Error implements ErrorLayer.
func (s *SCTPUnknownChunkType) Error() error {
return fmt.Errorf("No decode method available for SCTP chunk type %s", s.Type)
}
Expand Down Expand Up @@ -156,6 +162,7 @@ func decodeSCTPData(data []byte) (out DecodeResult, _ error) {
return
}

// SCTPInitParameter is a parameter for an SCTP Init or InitAck packet.
type SCTPInitParameter SCTPParameter

// SCTPInit is used as the return value for both SCTPInit and SCTPInitAck
Expand Down
3 changes: 3 additions & 0 deletions layertype.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
LayerTypePayload LayerType = iota
LayerTypeDecodeFailure
LayerTypeEthernet
LayerTypeEtherIP
LayerTypePPP
LayerTypeIPv4
LayerTypeIPv6
Expand Down Expand Up @@ -60,6 +61,8 @@ func (l LayerType) String() string {
return "DecodeFailure"
case LayerTypeEthernet:
return "Ethernet"
case LayerTypeEtherIP:
return "EtherIP"
case LayerTypePPP:
return "PPP"
case LayerTypeIPv4:
Expand Down

0 comments on commit 063adcb

Please sign in to comment.