Esempio n. 1
0
func (packet *Packet) WriteTo(writer io.Writer) (n int64, err error) {
	m, err := writer.Write(FRAME_START_BYTE)
	n = int64(m)
	if err != nil {
		return n, err
	}

	hashedWriter := x25.MakeHashedWriter(writer)

	// Write and hash header
	m, err = hashedWriter.Write(packet.Header.Bytes())
	n += int64(m)
	if err != nil {
		return n, err
	}

	// Write and hash message
	err = binary.Write(&hashedWriter, binary.LittleEndian, packet.Message)
	if err != nil {
		return n, err
	}
	n += int64(packet.Message.TypeSize())

	// Add CRCExtra to the hash
	hashedWriter.Hash.WriteByte(packet.Message.TypeCRCExtra())

	hashBytes := make([]byte, 2)
	binary.LittleEndian.PutUint16(hashBytes, hashedWriter.Hash.Sum)

	// Write hash
	m, err = writer.Write(hashBytes)
	n += int64(m)

	return n, err
}
Esempio n. 2
0
func (packet *Packet) WireBytes() []byte {
	buf := bytes.NewBuffer(make([]byte, 0, packet.WireSize()))
	buf.WriteByte(FRAME_START)

	writer := x25.MakeHashedWriter(buf)
	writer.Write(packet.Header.Bytes())
	binary.Write(&writer, binary.LittleEndian, packet.Message)
	writer.Hash.WriteByte(packet.Message.TypeCRCExtra())

	least, most := dry.EndianSafeSplitUint16(writer.Hash.Sum)
	buf.WriteByte(least)
	buf.WriteByte(most)

	return buf.Bytes()
}