Пример #1
0
// Pack packets into their binary form. This will stack the packets before
// encoding them (see the Compose() method) and also calculate the checksums.
func Pack(pkts ...packet.Packet) ([]byte, error) {
	var buf packet.Buffer

	base_pkt, err := Compose(pkts...)
	if err != nil {
		return nil, err
	}

	tot_len := int(base_pkt.GetLength())

	buf.Init(make([]byte, tot_len))

	for i := len(pkts) - 1; i >= 0; i-- {
		cur_pkt := pkts[i]
		cur_len := int(cur_pkt.GetLength())

		buf.SetOffset(tot_len - cur_len)
		buf.NewLayer()

		err := cur_pkt.Pack(&buf)
		if err != nil {
			return nil, err
		}
	}

	buf.SetOffset(0)

	return buf.Bytes(), nil
}
Пример #2
0
func (p *Packet) Unpack(buf *packet.Buffer) error {
	buf.ReadN(&p.DSAP)
	buf.ReadN(&p.SSAP)

	if buf.Bytes()[:1][0]&0x1 == 0 ||
		buf.Bytes()[:1][0]&0x3 == 0x1 {
		buf.ReadN(&p.Control)
	} else {
		var ctrl uint8
		buf.ReadN(&ctrl)
		p.Control = uint16(ctrl)
	}

	return nil
}