-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtransport.go
97 lines (81 loc) · 3.09 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package tpm2
import (
"errors"
"io"
"time"
)
// InfiniteTimeout can be used to configure an infinite timeout.
const InfiniteTimeout = -1 * time.Millisecond
var (
// ErrTimeoutNotSupported indicates that a [Transport] implementation does not support
// configuring the command timeout.
ErrTimeoutNotSupported = errors.New("configurable command timeouts are not supported")
// ErrTransportBusy should be returned from calls to Write if a previously
// submitted command has not finished or not all of its bytes have
// been read back yet.
ErrTransportBusy = errors.New("transport is busy")
// ErrTransportClosed indicates that a transport is closed.
ErrTransportClosed = errors.New("transport already closed")
)
// TCTI represents a communication channel to a TPM implementation.
//
// Deprecated: use [Transport] instead.
type TCTI = Transport
// Transport represents a communication channel to a TPM implementation.
//
// Implementations of the [io.Reader] and [io.Writer] parts of this can expect that they
// will be called from the same goroutine and that they won't be used from multiple
// goroutines.
//
// Implementations should handle the [io.Closer] part being called from any goroutine,
// even when a Read or Write is in progress on another goroutine.
type Transport interface {
// Read is used to receive a response to a previously transmitted command.
// Implementations should support a response being read using multiple calls
// to Read (partial reads). The [internal.transportutil.BufferResponses]
// API can assist for transports that don't support this. Implementations that
// support partial reads should not return parts of more than one command in a
// single call.
//
// Implementations should only use io.EOF to indicate that no more bytes will
// ever be read from this transport. Callers should be able to identify the end
// of a response based on the ResponseHeader itself and the number of bytes read.
Read(p []byte) (int, error)
// Write is used to transmit a serialized command to the TPM implementation.
// Implementations should support a command being sent across multiple calls
// to Write, and should be able to identify the end of a command based on the
// CommandHeader itself. The [internal.transportutil.BufferCommands] API can
// assist for transports that don't support this.
Write(p []byte) (int, error)
// Close closes the transport.
Close() error
}
type transportWriter struct {
w io.Writer
}
func (w *transportWriter) Write(data []byte) (int, error) {
n, err := w.w.Write(data)
if err != nil {
return n, &TransportError{"write", err}
}
return n, nil
}
func wrapTransportWriteErrors(w io.Writer) io.Writer {
return &transportWriter{w: w}
}
type transportReader struct {
r io.Reader
}
func (r *transportReader) Read(data []byte) (int, error) {
n, err := r.r.Read(data)
if err != nil {
return n, &TransportError{"read", err}
}
return n, nil
}
func wrapTransportReadErrors(r io.Reader) io.Reader {
return &transportReader{r: r}
}