123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- package layers
- import (
- "encoding/binary"
- "errors"
- "net"
- "time"
- "github.com/google/gopacket"
- )
- type IGMPType uint8
- const (
- IGMPMembershipQuery IGMPType = 0x11
- IGMPMembershipReportV1 IGMPType = 0x12
- IGMPMembershipReportV2 IGMPType = 0x16
- IGMPLeaveGroup IGMPType = 0x17
- IGMPMembershipReportV3 IGMPType = 0x22
- )
- func (i IGMPType) String() string {
- switch i {
- case IGMPMembershipQuery:
- return "IGMP Membership Query"
- case IGMPMembershipReportV1:
- return "IGMPv1 Membership Report"
- case IGMPMembershipReportV2:
- return "IGMPv2 Membership Report"
- case IGMPMembershipReportV3:
- return "IGMPv3 Membership Report"
- case IGMPLeaveGroup:
- return "Leave Group"
- default:
- return ""
- }
- }
- type IGMPv3GroupRecordType uint8
- const (
- IGMPIsIn IGMPv3GroupRecordType = 0x01
- IGMPIsEx IGMPv3GroupRecordType = 0x02
- IGMPToIn IGMPv3GroupRecordType = 0x03
- IGMPToEx IGMPv3GroupRecordType = 0x04
- IGMPAllow IGMPv3GroupRecordType = 0x05
- IGMPBlock IGMPv3GroupRecordType = 0x06
- )
- func (i IGMPv3GroupRecordType) String() string {
- switch i {
- case IGMPIsIn:
- return "MODE_IS_INCLUDE"
- case IGMPIsEx:
- return "MODE_IS_EXCLUDE"
- case IGMPToIn:
- return "CHANGE_TO_INCLUDE_MODE"
- case IGMPToEx:
- return "CHANGE_TO_EXCLUDE_MODE"
- case IGMPAllow:
- return "ALLOW_NEW_SOURCES"
- case IGMPBlock:
- return "BLOCK_OLD_SOURCES"
- default:
- return ""
- }
- }
- type IGMP struct {
- BaseLayer
- Type IGMPType
- MaxResponseTime time.Duration
- Checksum uint16
- GroupAddress net.IP
- SupressRouterProcessing bool
- RobustnessValue uint8
- IntervalTime time.Duration
- SourceAddresses []net.IP
- NumberOfGroupRecords uint16
- NumberOfSources uint16
- GroupRecords []IGMPv3GroupRecord
- Version uint8
- }
- type IGMPv1or2 struct {
- BaseLayer
- Type IGMPType
- MaxResponseTime time.Duration
- Checksum uint16
- GroupAddress net.IP
- Version uint8
- }
- func (i *IGMPv1or2) decodeResponse(data []byte) error {
- if len(data) < 8 {
- return errors.New("IGMP packet too small")
- }
- i.MaxResponseTime = igmpTimeDecode(data[1])
- i.Checksum = binary.BigEndian.Uint16(data[2:4])
- i.GroupAddress = net.IP(data[4:8])
- return nil
- }
- type IGMPv3GroupRecord struct {
- Type IGMPv3GroupRecordType
- AuxDataLen uint8
- NumberOfSources uint16
- MulticastAddress net.IP
- SourceAddresses []net.IP
- AuxData uint32
- }
- func (i *IGMP) decodeIGMPv3MembershipReport(data []byte) error {
- if len(data) < 8 {
- return errors.New("IGMPv3 Membership Report too small #1")
- }
- i.Checksum = binary.BigEndian.Uint16(data[2:4])
- i.NumberOfGroupRecords = binary.BigEndian.Uint16(data[6:8])
- recordOffset := 8
- for j := 0; j < int(i.NumberOfGroupRecords); j++ {
- if len(data) < recordOffset+8 {
- return errors.New("IGMPv3 Membership Report too small #2")
- }
- var gr IGMPv3GroupRecord
- gr.Type = IGMPv3GroupRecordType(data[recordOffset])
- gr.AuxDataLen = data[recordOffset+1]
- gr.NumberOfSources = binary.BigEndian.Uint16(data[recordOffset+2 : recordOffset+4])
- gr.MulticastAddress = net.IP(data[recordOffset+4 : recordOffset+8])
- if len(data) < recordOffset+8+int(gr.NumberOfSources)*4 {
- return errors.New("IGMPv3 Membership Report too small #3")
- }
-
- for i := 0; i < int(gr.NumberOfSources); i++ {
- sourceAddr := net.IP(data[recordOffset+8+i*4 : recordOffset+12+i*4])
- gr.SourceAddresses = append(gr.SourceAddresses, sourceAddr)
- }
- i.GroupRecords = append(i.GroupRecords, gr)
- recordOffset += 8 + 4*int(gr.NumberOfSources)
- }
- return nil
- }
- func (i *IGMP) decodeIGMPv3MembershipQuery(data []byte) error {
- if len(data) < 12 {
- return errors.New("IGMPv3 Membership Query too small #1")
- }
- i.MaxResponseTime = igmpTimeDecode(data[1])
- i.Checksum = binary.BigEndian.Uint16(data[2:4])
- i.SupressRouterProcessing = data[8]&0x8 != 0
- i.GroupAddress = net.IP(data[4:8])
- i.RobustnessValue = data[8] & 0x7
- i.IntervalTime = igmpTimeDecode(data[9])
- i.NumberOfSources = binary.BigEndian.Uint16(data[10:12])
- if len(data) < 12+int(i.NumberOfSources)*4 {
- return errors.New("IGMPv3 Membership Query too small #2")
- }
- for j := 0; j < int(i.NumberOfSources); j++ {
- i.SourceAddresses = append(i.SourceAddresses, net.IP(data[12+j*4:16+j*4]))
- }
- return nil
- }
- func igmpTimeDecode(t uint8) time.Duration {
- if t&0x80 == 0 {
- return time.Millisecond * 100 * time.Duration(t)
- }
- mant := (t & 0x70) >> 4
- exp := t & 0x0F
- return time.Millisecond * 100 * time.Duration((mant|0x10)<<(exp+3))
- }
- func (i *IGMP) LayerType() gopacket.LayerType { return LayerTypeIGMP }
- func (i *IGMPv1or2) LayerType() gopacket.LayerType { return LayerTypeIGMP }
- func (i *IGMPv1or2) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
- if len(data) < 8 {
- return errors.New("IGMP Packet too small")
- }
- i.Type = IGMPType(data[0])
- i.MaxResponseTime = igmpTimeDecode(data[1])
- i.Checksum = binary.BigEndian.Uint16(data[2:4])
- i.GroupAddress = net.IP(data[4:8])
- return nil
- }
- func (i *IGMPv1or2) NextLayerType() gopacket.LayerType {
- return gopacket.LayerTypeZero
- }
- func (i *IGMPv1or2) CanDecode() gopacket.LayerClass {
- return LayerTypeIGMP
- }
- func (i *IGMP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
- if len(data) < 1 {
- return errors.New("IGMP packet is too small")
- }
-
- i.Type = IGMPType(data[0])
- switch i.Type {
- case IGMPMembershipQuery:
- i.decodeIGMPv3MembershipQuery(data)
- case IGMPMembershipReportV3:
- i.decodeIGMPv3MembershipReport(data)
- default:
- return errors.New("unsupported IGMP type")
- }
- return nil
- }
- func (i *IGMP) CanDecode() gopacket.LayerClass {
- return LayerTypeIGMP
- }
- func (i *IGMP) NextLayerType() gopacket.LayerType {
- return gopacket.LayerTypeZero
- }
- func decodeIGMP(data []byte, p gopacket.PacketBuilder) error {
- if len(data) < 1 {
- return errors.New("IGMP packet is too small")
- }
-
- switch IGMPType(data[0]) {
- case IGMPMembershipQuery:
-
- if len(data) >= 12 {
- i := &IGMP{Version: 3}
- return decodingLayerDecoder(i, data, p)
- } else if len(data) == 8 {
- i := &IGMPv1or2{}
- if data[1] == 0x00 {
- i.Version = 1
- } else {
- i.Version = 2
- }
- return decodingLayerDecoder(i, data, p)
- }
- case IGMPMembershipReportV3:
- i := &IGMP{Version: 3}
- return decodingLayerDecoder(i, data, p)
- case IGMPMembershipReportV1:
- i := &IGMPv1or2{Version: 1}
- return decodingLayerDecoder(i, data, p)
- case IGMPLeaveGroup, IGMPMembershipReportV2:
-
- i := &IGMPv1or2{Version: 2}
- return decodingLayerDecoder(i, data, p)
- default:
- }
- return errors.New("Unable to determine IGMP type.")
- }
|