summaryrefslogtreecommitdiff
path: root/pkg/varlinkapi/virtwriter/virtwriter.go
blob: dd171943f827a57d5b30fad4a9db2872cfe6651a (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package virtwriter

import (
	"bufio"
	"encoding/binary"
	"encoding/json"
	"io"
	"time"

	"github.com/pkg/errors"
	"k8s.io/client-go/tools/remotecommand"
)

// SocketDest is the "key" to where IO should go on the varlink
// multiplexed socket
type SocketDest int

const (
	// ToStdout indicates traffic should go stdout
	ToStdout SocketDest = iota
	// ToStdin indicates traffic came from stdin
	ToStdin SocketDest = iota
	// ToStderr indicates traffuc should go to stderr
	ToStderr SocketDest = iota
	// TerminalResize indicates a terminal resize event has occurred
	// and data should be passed to resizer
	TerminalResize SocketDest = iota
	// Quit and detach
	Quit SocketDest = iota
	// Quit from the client
	HangUpFromClient SocketDest = iota
)

// ClientHangup signifies that the client wants to drop its
// connection from the server
var ClientHangup = errors.New("client hangup")

// IntToSocketDest returns a socketdest based on integer input
func IntToSocketDest(i int) SocketDest {
	switch i {
	case ToStdout.Int():
		return ToStdout
	case ToStderr.Int():
		return ToStderr
	case ToStdin.Int():
		return ToStdin
	case TerminalResize.Int():
		return TerminalResize
	case Quit.Int():
		return Quit
	case HangUpFromClient.Int():
		return HangUpFromClient
	default:
		return ToStderr
	}
}

// Int returns the integer representation of the socket dest
func (sd SocketDest) Int() int {
	return int(sd)
}

// VirtWriteCloser are writers for attach which include the dest
// of the data
type VirtWriteCloser struct {
	writer *bufio.Writer
	dest   SocketDest
}

// NewVirtWriteCloser is a constructor
func NewVirtWriteCloser(w *bufio.Writer, dest SocketDest) VirtWriteCloser {
	return VirtWriteCloser{w, dest}
}

// Close is a required method for a writecloser
func (v VirtWriteCloser) Close() error {
	return v.writer.Flush()
}

// Write prepends a header to the input message.  The header is
// 8bytes.  Position one contains the destination.  Positions
// 5,6,7,8 are a big-endian encoded uint32 for len of the message.
func (v VirtWriteCloser) Write(input []byte) (int, error) {
	header := []byte{byte(v.dest), 0, 0, 0}
	// Go makes us define the byte for big endian
	mlen := make([]byte, 4)
	binary.BigEndian.PutUint32(mlen, uint32(len(input)))
	// append the message len to the header
	msg := append(header, mlen...)
	// append the message to the header
	msg = append(msg, input...)
	_, err := v.writer.Write(msg)
	if err != nil {
		return 0, err
	}
	err = v.writer.Flush()
	return len(input), err
}

// Reader decodes the content that comes over the wire and directs it to the proper destination.
func Reader(r *bufio.Reader, output, errput, input io.Writer, resize chan remotecommand.TerminalSize, execEcChan chan int) error {
	var messageSize int64
	headerBytes := make([]byte, 8)

	if r == nil {
		return errors.Errorf("Reader must not be nil")
	}
	for {
		n, err := io.ReadFull(r, headerBytes)
		if err != nil {
			return errors.Wrapf(err, "Virtual Read failed, %d", n)
		}
		if n < 8 {
			return errors.New("short read and no full header read")
		}

		messageSize = int64(binary.BigEndian.Uint32(headerBytes[4:8]))
		switch IntToSocketDest(int(headerBytes[0])) {
		case ToStdout:
			if output != nil {
				_, err := io.CopyN(output, r, messageSize)
				if err != nil {
					return err
				}
			}
		case ToStderr:
			if errput != nil {
				_, err := io.CopyN(errput, r, messageSize)
				if err != nil {
					return err
				}
			}
		case ToStdin:
			if input != nil {
				_, err := io.CopyN(input, r, messageSize)
				if err != nil {
					return err
				}
			}
		case TerminalResize:
			if resize != nil {
				out := make([]byte, messageSize)
				if messageSize > 0 {
					_, err = io.ReadFull(r, out)

					if err != nil {
						return err
					}
				}
				// Resize events come over in bytes, need to be reserialized
				resizeEvent := remotecommand.TerminalSize{}
				if err := json.Unmarshal(out, &resizeEvent); err != nil {
					return err
				}
				resize <- resizeEvent
			}
		case Quit:
			out := make([]byte, messageSize)
			if messageSize > 0 {
				_, err = io.ReadFull(r, out)

				if err != nil {
					return err
				}
			}
			if execEcChan != nil {
				ecInt := binary.BigEndian.Uint32(out)
				execEcChan <- int(ecInt)
			}
			return nil
		case HangUpFromClient:
			// This sleep allows the pipes to flush themselves before tearing everything down.
			// It makes me sick to do it but after a full day I cannot put my finger on the race
			// that occurs when closing things up.  It would require a significant rewrite of code
			// to make the pipes close down properly.  Given that we are currently discussing a
			// rewrite of all things remote, this hardly seems worth resolving.
			//
			// reproducer: echo hello | (podman-remote run -i alpine cat)
			time.Sleep(1 * time.Second)
			return ClientHangup
		default:
			// Something really went wrong
			return errors.New("unknown multiplex destination")
		}
	}
}

// HangUp sends message to peer to close connection
func HangUp(writer *bufio.Writer, ec uint32) (err error) {
	n := 0
	msg := make([]byte, 4)

	binary.BigEndian.PutUint32(msg, ec)

	writeQuit := NewVirtWriteCloser(writer, Quit)
	if n, err = writeQuit.Write(msg); err != nil {
		return
	}

	if n != len(msg) {
		return errors.Errorf("Failed to send complete %s message", string(msg))
	}
	return
}