Beispiel #1
0
func (frame *DATA) WriteTo(writer io.Writer) (int64, error) {
	length := len(frame.Data)
	if length > common.MAX_DATA_SIZE {
		return 0, errors.New("Error: Data size too large.")
	}
	if length == 0 && !frame.Flags.FIN() {
		return 0, errors.New("Error: Data is empty.")
	}

	out := make([]byte, 8)

	out[0] = frame.StreamID.B1() // Control bit and Stream ID
	out[1] = frame.StreamID.B2() // Stream ID
	out[2] = frame.StreamID.B3() // Stream ID
	out[3] = frame.StreamID.B4() // Stream ID
	out[4] = byte(frame.Flags)   // Flags
	out[5] = byte(length >> 16)  // Length
	out[6] = byte(length >> 8)   // Length
	out[7] = byte(length)        // Length

	if err := common.WriteExactly(writer, out); err != nil {
		return 0, err
	}

	if err := common.WriteExactly(writer, frame.Data); err != nil {
		return 8, err
	}

	return int64(length + 8), nil
}
Beispiel #2
0
func (frame *SETTINGS) WriteTo(writer io.Writer) (int64, error) {
	c := common.WriteCounter{W: writer}
	settings := encodeSettings(frame.Settings)
	numSettings := uint32(len(frame.Settings))
	length := 4 + len(settings)
	out := make([]byte, 12)

	out[0] = 128                     // Control bit and Version
	out[1] = 3                       // Version
	out[2] = 0                       // Type
	out[3] = 4                       // Type
	out[4] = byte(frame.Flags)       // Flags
	out[5] = byte(length >> 16)      // Length
	out[6] = byte(length >> 8)       // Length
	out[7] = byte(length)            // Length
	out[8] = byte(numSettings >> 24) // Number of Entries
	out[9] = byte(numSettings >> 16) // Number of Entries
	out[10] = byte(numSettings >> 8) // Number of Entries
	out[11] = byte(numSettings)      // Number of Entries

	err := common.WriteExactly(&c, out)
	if err != nil {
		return c.N, err
	}

	err = common.WriteExactly(&c, settings)
	if err != nil {
		return c.N, err
	}

	return c.N, nil
}
Beispiel #3
0
func (frame *SYN_STREAM) WriteTo(writer io.Writer) (int64, error) {
	c := common.WriteCounter{W: writer}
	if frame.rawHeader == nil {
		return c.N, errors.New("Error: Headers not written.")
	}
	if !frame.StreamID.Valid() {
		return c.N, common.StreamIdTooLarge
	}
	if frame.StreamID.Zero() {
		return c.N, common.StreamIdIsZero
	}
	if !frame.AssocStreamID.Valid() {
		return c.N, common.StreamIdTooLarge
	}

	header := frame.rawHeader
	length := 10 + len(header)
	out := make([]byte, 18)

	out[0] = 128                       // Control bit and Version
	out[1] = 2                         // Version
	out[2] = 0                         // Type
	out[3] = 1                         // Type
	out[4] = byte(frame.Flags)         // Flags
	out[5] = byte(length >> 16)        // Length
	out[6] = byte(length >> 8)         // Length
	out[7] = byte(length)              // Length
	out[8] = frame.StreamID.B1()       // Stream ID
	out[9] = frame.StreamID.B2()       // Stream ID
	out[10] = frame.StreamID.B3()      // Stream ID
	out[11] = frame.StreamID.B4()      // Stream ID
	out[12] = frame.AssocStreamID.B1() // Associated Stream ID
	out[13] = frame.AssocStreamID.B2() // Associated Stream ID
	out[14] = frame.AssocStreamID.B3() // Associated Stream ID
	out[15] = frame.AssocStreamID.B4() // Associated Stream ID
	out[16] = frame.Priority.Byte(2)   // Priority and Unused
	out[17] = 0                        // Unused

	err := common.WriteExactly(&c, out)
	if err != nil {
		return c.N, err
	}

	err = common.WriteExactly(&c, header)
	if err != nil {
		return c.N, err
	}

	return c.N, nil
}
Beispiel #4
0
func (frame *CREDENTIAL) WriteTo(writer io.Writer) (int64, error) {
	c := common.WriteCounter{W: writer}
	proofLength := len(frame.Proof)
	certsLength := 0
	for _, cert := range frame.Certificates {
		certsLength += len(cert.Raw)
	}

	length := 6 + proofLength + certsLength
	out := make([]byte, 14)

	out[0] = 128                      // Control bit and Version
	out[1] = 3                        // Version
	out[2] = 0                        // Type
	out[3] = 10                       // Type
	out[4] = 0                        // common.Flags
	out[5] = byte(length >> 16)       // Length
	out[6] = byte(length >> 8)        // Length
	out[7] = byte(length)             // Length
	out[8] = byte(frame.Slot >> 8)    // Slot
	out[9] = byte(frame.Slot)         // Slot
	out[10] = byte(proofLength >> 24) // Proof Length
	out[11] = byte(proofLength >> 16) // Proof Length
	out[12] = byte(proofLength >> 8)  // Proof Length
	out[13] = byte(proofLength)       // Proof Length

	err := common.WriteExactly(&c, out)
	if err != nil {
		return c.N, err
	}

	if len(frame.Proof) > 0 {
		err = common.WriteExactly(&c, frame.Proof)
		if err != nil {
			return c.N, err
		}
	}

	written := int64(14 + len(frame.Proof))
	for _, cert := range frame.Certificates {
		err = common.WriteExactly(&c, cert.Raw)
		if err != nil {
			return c.N, err
		}
		written += int64(len(cert.Raw))
	}

	return c.N, nil
}
Beispiel #5
0
func (frame *RST_STREAM) WriteTo(writer io.Writer) (int64, error) {
	c := common.WriteCounter{W: writer}
	if !frame.StreamID.Valid() {
		return c.N, common.StreamIdTooLarge
	}

	out := make([]byte, 16)

	out[0] = 128                  // Control bit and Version
	out[1] = 2                    // Version
	out[2] = 0                    // Type
	out[3] = 3                    // Type
	out[4] = 0                    // Flags
	out[5] = 0                    // Length
	out[6] = 0                    // Length
	out[7] = 8                    // Length
	out[8] = frame.StreamID.B1()  // Stream ID
	out[9] = frame.StreamID.B2()  // Stream ID
	out[10] = frame.StreamID.B3() // Stream ID
	out[11] = frame.StreamID.B4() // Stream ID
	out[12] = frame.Status.B1()   // Status
	out[13] = frame.Status.B2()   // Status
	out[14] = frame.Status.B3()   // Status
	out[15] = frame.Status.B4()   // Status

	err := common.WriteExactly(&c, out)
	if err != nil {
		return c.N, err
	}

	return c.N, nil
}
Beispiel #6
0
func (frame *GOAWAY) WriteTo(writer io.Writer) (int64, error) {
	if !frame.LastGoodStreamID.Valid() {
		return 0, common.StreamIdTooLarge
	}

	out := make([]byte, 16)

	out[0] = 128                          // Control bit and Version
	out[1] = 3                            // Version
	out[2] = 0                            // Type
	out[3] = 7                            // Type
	out[4] = 0                            // Flags
	out[5] = 0                            // Length
	out[6] = 0                            // Length
	out[7] = 8                            // Length
	out[8] = frame.LastGoodStreamID.B1()  // Last Good Stream ID
	out[9] = frame.LastGoodStreamID.B2()  // Last Good Stream ID
	out[10] = frame.LastGoodStreamID.B3() // Last Good Stream ID
	out[11] = frame.LastGoodStreamID.B4() // Last Good Stream ID
	out[12] = byte(frame.Status >> 24)    // Status Code
	out[13] = byte(frame.Status >> 16)    // Status Code
	out[14] = byte(frame.Status >> 8)     // Status Code
	out[15] = byte(frame.Status)          // Status Code

	err := common.WriteExactly(writer, out)
	if err != nil {
		return 0, err
	}

	return 16, nil
}
Beispiel #7
0
func (frame *WINDOW_UPDATE) WriteTo(writer io.Writer) (int64, error) {
	c := common.WriteCounter{W: writer}
	out := make([]byte, 16)

	out[0] = 128                                     // Control bit and Version
	out[1] = 3                                       // Version
	out[2] = 0                                       // Type
	out[3] = 9                                       // Type
	out[4] = 0                                       // Flags
	out[5] = 0                                       // Length
	out[6] = 0                                       // Length
	out[7] = 8                                       // Length
	out[8] = frame.StreamID.B1()                     // Stream ID
	out[9] = frame.StreamID.B2()                     // Stream ID
	out[10] = frame.StreamID.B3()                    // Stream ID
	out[11] = frame.StreamID.B4()                    // Stream ID
	out[12] = byte(frame.DeltaWindowSize>>24) & 0x7f // Delta Window Size
	out[13] = byte(frame.DeltaWindowSize >> 16)      // Delta Window Size
	out[14] = byte(frame.DeltaWindowSize >> 8)       // Delta Window Size
	out[15] = byte(frame.DeltaWindowSize)            // Delta Window Size

	err := common.WriteExactly(&c, out)
	if err != nil {
		return c.N, err
	}

	return c.N, nil
}
Beispiel #8
0
func (frame *GOAWAY) WriteTo(writer io.Writer) (int64, error) {
	c := common.WriteCounter{W: writer}
	if !frame.LastGoodStreamID.Valid() {
		return c.N, common.StreamIdTooLarge
	}

	out := make([]byte, 12)

	out[0] = 128                          // Control bit and Version
	out[1] = 2                            // Version
	out[2] = 0                            // Type
	out[3] = 7                            // Type
	out[4] = 0                            // Flags
	out[5] = 0                            // Length
	out[6] = 0                            // Length
	out[7] = 4                            // Length
	out[8] = frame.LastGoodStreamID.B1()  // Last Good Stream ID
	out[9] = frame.LastGoodStreamID.B2()  // Last Good Stream ID
	out[10] = frame.LastGoodStreamID.B3() // Last Good Stream ID
	out[11] = frame.LastGoodStreamID.B4() // Last Good Stream ID

	err := common.WriteExactly(&c, out)
	if err != nil {
		return c.N, err
	}

	return c.N, nil
}
Beispiel #9
0
func (frame *SYN_REPLY) WriteTo(writer io.Writer) (int64, error) {
	if frame.rawHeader == nil {
		return 0, errors.New("Error: Header not written.")
	}
	if !frame.StreamID.Valid() {
		return 0, common.StreamIdTooLarge
	}
	if frame.StreamID.Zero() {
		return 0, common.StreamIdIsZero
	}

	header := frame.rawHeader
	length := 6 + len(header)
	out := make([]byte, 14)

	out[0] = 128                  // Control bit and Version
	out[1] = 2                    // Version
	out[2] = 0                    // Type
	out[3] = 2                    // Type
	out[4] = byte(frame.Flags)    // Flags
	out[5] = byte(length >> 16)   // Length
	out[6] = byte(length >> 8)    // Length
	out[7] = byte(length)         // Length
	out[8] = frame.StreamID.B1()  // Stream ID
	out[9] = frame.StreamID.B2()  // Stream ID
	out[10] = frame.StreamID.B3() // Stream ID
	out[11] = frame.StreamID.B4() // Stream ID
	out[12] = 0                   // Unused
	out[13] = 0                   // Unused

	err := common.WriteExactly(writer, out)
	if err != nil {
		return 0, err
	}

	err = common.WriteExactly(writer, header)
	if err != nil {
		return 14, err
	}

	return int64(len(header) + 14), nil
}
Beispiel #10
0
func (frame *HEADERS) WriteTo(writer io.Writer) (int64, error) {
	c := common.WriteCounter{W: writer}
	if frame.rawHeader == nil {
		return c.N, errors.New("Error: Headers not written.")
	}
	if !frame.StreamID.Valid() {
		return c.N, common.StreamIdTooLarge
	}
	if frame.StreamID.Zero() {
		return c.N, common.StreamIdIsZero
	}

	header := frame.rawHeader
	length := 4 + len(header)
	out := make([]byte, 16)

	out[0] = 128                  // Control bit and Version
	out[1] = 2                    // Version
	out[2] = 0                    // Type
	out[3] = 8                    // Type
	out[4] = byte(frame.Flags)    // Flags
	out[5] = byte(length >> 16)   // Length
	out[6] = byte(length >> 8)    // Length
	out[7] = byte(length)         // Length
	out[8] = frame.StreamID.B1()  // Stream ID
	out[9] = frame.StreamID.B2()  // Stream ID
	out[10] = frame.StreamID.B3() // Stream ID
	out[11] = frame.StreamID.B4() // Stream ID

	err := common.WriteExactly(&c, out)
	if err != nil {
		return c.N, err
	}

	err = common.WriteExactly(&c, header)
	if err != nil {
		return c.N, err
	}

	return c.N, nil
}
Beispiel #11
0
func (frame *PING) WriteTo(writer io.Writer) (int64, error) {
	out := make([]byte, 12)

	out[0] = 128                      // Control bit and Version
	out[1] = 2                        // Version
	out[2] = 0                        // Type
	out[3] = 6                        // Type
	out[4] = 0                        // Flags
	out[5] = 0                        // Length
	out[6] = 0                        // Length
	out[7] = 4                        // Length
	out[8] = byte(frame.PingID >> 24) // Ping ID
	out[9] = byte(frame.PingID >> 16) // Ping ID
	out[10] = byte(frame.PingID >> 8) // Ping ID
	out[11] = byte(frame.PingID)      // Ping ID

	err := common.WriteExactly(writer, out)
	if err != nil {
		return 0, err
	}

	return 12, nil
}