Exemple #1
0
func (p *Packet) SetPayload(pl packet.Packet) error {
	p.pkt_payload = pl
	p.Protocol = TypeToProtocol(pl.GetType())
	p.Length = p.GetLength()

	pl.InitChecksum(p.pseudo_checksum())

	return nil
}
Exemple #2
0
func (p *Packet) SetPayload(pl packet.Packet) error {
	p.pkt_payload = pl
	p.Type = TypeToEtherType(pl.GetType())

	if p.Type < 0x0600 {
		p.Length = p.GetLength()
	}

	return nil
}
Exemple #3
0
func (p *Packet) Answers(other packet.Packet) bool {
	if other == nil || other.GetType() != packet.ICMPv6 {
		return false
	}

	if other.(*Packet).Type == EchoRequest && p.Type == EchoReply {
		return true
	}

	return false
}
Exemple #4
0
// Return the first layer of the given type in the packet. If no suitable layer
// is found, return nil.
func FindLayer(p packet.Packet, layer packet.Type) packet.Packet {
	switch {
	case p == nil:
		return nil

	case p.GetType() == layer:
		return p

	default:
		return FindLayer(p.Payload(), layer)
	}
}
Exemple #5
0
func (p *Packet) Answers(other packet.Packet) bool {
	if other == nil || other.GetType() != packet.TCP {
		return false
	}

	if p.SrcPort != other.(*Packet).DstPort ||
		p.DstPort != other.(*Packet).SrcPort {
		return false
	}

	return true
}
Exemple #6
0
func (p *Packet) Answers(other packet.Packet) bool {
	if other == nil || other.GetType() != packet.ARP {
		return false
	}

	if p.Operation == Reply && other.(*Packet).Operation == Request &&
		p.ProtoSrcAddr.Equal(other.(*Packet).ProtoDstAddr) {
		return true
	}

	return false
}
Exemple #7
0
func (p *Packet) Answers(other packet.Packet) bool {
	if other == nil || other.GetType() != packet.ICMPv4 {
		return false
	}

	if (other.(*Packet).Type == EchoRequest && p.Type == EchoReply) ||
		(other.(*Packet).Type == Timestamp && p.Type == TimestampReply) ||
		(other.(*Packet).Type == InfoRequest && p.Type == InfoReply) ||
		(other.(*Packet).Type == AddrMaskRequest && p.Type == AddrMaskReply) {
		return (other.(*Packet).Seq == p.Seq) &&
			(other.(*Packet).Id == p.Id)
	}

	return false
}
Exemple #8
0
func (p *Packet) Answers(other packet.Packet) bool {
	if other == nil || other.GetType() != packet.Eth {
		return false
	}

	if p.Type != other.(*Packet).Type {
		return false
	}

	if p.Payload() != nil {
		return p.Payload().Answers(other.Payload())
	}

	return true
}
Exemple #9
0
func (p *Packet) Answers(other packet.Packet) bool {
	if other == nil {
		return false
	}

	if other.GetType() == packet.VLAN &&
		p.VLAN != other.(*Packet).VLAN {
		return false
	}

	if p.Payload() != nil {
		return p.Payload().Answers(other.Payload())
	}

	return true
}
Exemple #10
0
func (p *Packet) Answers(other packet.Packet) bool {
	if other == nil || other.GetType() != packet.IPv6 {
		return false
	}

	/* TODO: check link-local broadcast addresses */
	if !p.DstAddr.Equal(other.(*Packet).SrcAddr) {
		return false
	}

	/* TODO: check ICMPv6 errors */

	if p.Payload() != nil {
		return p.Payload().Answers(other.Payload())
	}

	return true
}
Exemple #11
0
func (p *Packet) Answers(other packet.Packet) bool {
	if other == nil || other.GetType() != packet.IPv4 {
		return false
	}

	if p.Payload() != nil &&
		p.Payload().GetType() == packet.ICMPv4 &&
		p.Payload().Payload() != nil {
		return p.Payload().Payload().Equals(other)
	}

	if !p.SrcAddr.Equal(other.(*Packet).DstAddr) ||
		p.Protocol != other.(*Packet).Protocol {
		return false
	}

	if p.Payload() != nil {
		return p.Payload().Answers(other.Payload())
	}

	return true
}
Exemple #12
0
func (p *Packet) SetPayload(pl packet.Packet) error {
	p.pkt_payload = pl
	p.Type = eth.TypeToEtherType(pl.GetType())

	return nil
}
Exemple #13
0
// Recursively unpack the given byte slice into a packet. The link_type argument
// must specify the type of the first layer in the input data, successive layers
// will be detected automatically.
//
// Note that unpacking is done without copying the input slice, which means that
// if the slice is modifed, it may affect the packets that where unpacked from
// it. If you can't guarantee that the data slice won't change, you'll need to
// copy it and pass the copy to UnpackAll().
func UnpackAll(buf []byte, link_type packet.Type) (packet.Packet, error) {
	var b packet.Buffer
	b.Init(buf)

	first_pkt := packet.Packet(nil)
	prev_pkt := packet.Packet(nil)

	for link_type != packet.None {
		var p packet.Packet

		if b.Len() <= 0 {
			break
		}

		switch link_type {
		case packet.ARP:
			p = &arp.Packet{}
		case packet.Eth:
			p = &eth.Packet{}
		case packet.ICMPv4:
			p = &icmpv4.Packet{}
		case packet.ICMPv6:
			p = &icmpv6.Packet{}
		case packet.IPv4:
			p = &ipv4.Packet{}
		case packet.IPv6:
			p = &ipv6.Packet{}
		case packet.LLC:
			p = &llc.Packet{}
		case packet.RadioTap:
			p = &radiotap.Packet{}
		case packet.SLL:
			p = &sll.Packet{}
		case packet.SNAP:
			p = &snap.Packet{}
		case packet.TCP:
			p = &tcp.Packet{}
		case packet.UDP:
			p = &udp.Packet{}
		case packet.VLAN:
			p = &vlan.Packet{}
		default:
			p = &raw.Packet{}
		}

		if p == nil {
			break
		}

		b.NewLayer()

		err := p.Unpack(&b)
		if err != nil {
			return nil, err
		}

		if prev_pkt != nil {
			prev_pkt.SetPayload(p)
		} else {
			first_pkt = p
		}

		prev_pkt = p
		link_type = p.GuessPayloadType()
	}

	return first_pkt, nil
}