예제 #1
0
파일: layers.go 프로젝트: kelixin/go.pkt
// 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
}