VMware vSphere Integrated Containers provider (#206)
* Add Virtual Kubelet provider for VIC Initial virtual kubelet provider for VMware VIC. This provider currently handles creating and starting of a pod VM via the VIC portlayer and persona server. Image store handling via the VIC persona server. This provider currently requires the feature/wolfpack branch of VIC. * Added pod stop and delete. Also added node capacity. Added the ability to stop and delete pod VMs via VIC. Also retrieve node capacity information from the VCH. * Cleanup and readme file Some file clean up and added a Readme.md markdown file for the VIC provider. * Cleaned up errors, added function comments, moved operation code 1. Cleaned up error handling. Set standard for creating errors. 2. Added method prototype comments for all interface functions. 3. Moved PodCreator, PodStarter, PodStopper, and PodDeleter to a new folder. * Add mocking code and unit tests for podcache, podcreator, and podstarter Used the unit test framework used in VIC to handle assertions in the provider's unit test. Mocking code generated using OSS project mockery, which is compatible with the testify assertion framework. * Vendored packages for the VIC provider Requires feature/wolfpack branch of VIC and a few specific commit sha of projects used within VIC. * Implementation of POD Stopper and Deleter unit tests (#4) * Updated files for initial PR
This commit is contained in:
253
vendor/github.com/vmware/vic/pkg/telnet/connection.go
generated
vendored
Normal file
253
vendor/github.com/vmware/vic/pkg/telnet/connection.go
generated
vendored
Normal file
@@ -0,0 +1,253 @@
|
||||
// Copyright 2017 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package telnet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
type optCallBackFunc func(byte, byte)
|
||||
|
||||
type connOpts struct {
|
||||
// conn is the underlying connection
|
||||
conn net.Conn
|
||||
fsm *fsm
|
||||
|
||||
serverOpts map[byte]bool
|
||||
clientOpts map[byte]bool
|
||||
optCallback optCallBackFunc
|
||||
|
||||
Handlers
|
||||
}
|
||||
|
||||
// Conn is the struct representing the telnet connection
|
||||
type Conn struct {
|
||||
connOpts
|
||||
|
||||
// the connection write channel. Everything required to be written to the connection goes to this channel
|
||||
writeCh chan []byte
|
||||
// dataRW is the data buffer. It is written to by the FSM and read from by the data handler
|
||||
dataRW io.ReadWriter
|
||||
cmdBuffer bytes.Buffer
|
||||
handlerWriter io.WriteCloser
|
||||
|
||||
// used in the dataHandlerWrapper to notify that the telnet connection is closed
|
||||
dataHandlerCloseCh chan chan struct{}
|
||||
// used in the dataHandlerWrapper to notify that data has been writeen to the dataRW buffer
|
||||
dataWrittenCh chan bool
|
||||
|
||||
// connWriteDoneCh closes the write loop when the telnet connection is closed
|
||||
connWriteDoneCh chan chan struct{}
|
||||
|
||||
closedMutex sync.Mutex
|
||||
closed bool
|
||||
}
|
||||
|
||||
// Safely read/write concurrently to the data Buffer
|
||||
// databuffer is written to by the FSM and it is read from by the dataHandler
|
||||
type dataReadWriter struct {
|
||||
buf bytes.Buffer
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func (drw *dataReadWriter) Read(p []byte) (int, error) {
|
||||
drw.Lock()
|
||||
defer drw.Unlock()
|
||||
return drw.buf.Read(p)
|
||||
}
|
||||
|
||||
func (drw *dataReadWriter) Write(p []byte) (int, error) {
|
||||
drw.Lock()
|
||||
defer drw.Unlock()
|
||||
return drw.buf.Write(p)
|
||||
}
|
||||
|
||||
// This is the Writer that is passed to the handlers to write to the telnet connection
|
||||
type connectionWriter struct {
|
||||
ch chan []byte
|
||||
}
|
||||
|
||||
func (cw *connectionWriter) Write(b []byte) (int, error) {
|
||||
if cw.ch != nil {
|
||||
cw.ch <- b
|
||||
}
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (cw *connectionWriter) Close() error {
|
||||
close(cw.ch)
|
||||
cw.ch = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func newConn(opts *connOpts) *Conn {
|
||||
tc := &Conn{
|
||||
connOpts: *opts,
|
||||
writeCh: make(chan []byte),
|
||||
dataHandlerCloseCh: make(chan chan struct{}),
|
||||
dataWrittenCh: make(chan bool),
|
||||
connWriteDoneCh: make(chan chan struct{}),
|
||||
closed: false,
|
||||
}
|
||||
if tc.optCallback == nil {
|
||||
tc.optCallback = tc.handleOptionCommand
|
||||
}
|
||||
tc.handlerWriter = &connectionWriter{
|
||||
ch: tc.writeCh,
|
||||
}
|
||||
tc.dataRW = &dataReadWriter{}
|
||||
tc.fsm.tc = tc
|
||||
return tc
|
||||
}
|
||||
|
||||
//UnderlyingConnection returns the underlying TCP connection
|
||||
func (c *Conn) UnderlyingConnection() net.Conn {
|
||||
return c.conn
|
||||
}
|
||||
|
||||
func (c *Conn) writeLoop() {
|
||||
log.Debugf("entered write loop")
|
||||
for {
|
||||
select {
|
||||
case writeBytes := <-c.writeCh:
|
||||
c.conn.Write(writeBytes)
|
||||
case ch := <-c.connWriteDoneCh:
|
||||
ch <- struct{}{}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Conn) startNegotiation() {
|
||||
for k := range c.serverOpts {
|
||||
log.Infof("sending WILL %d", k)
|
||||
c.sendCmd(Will, k)
|
||||
}
|
||||
for k := range c.clientOpts {
|
||||
log.Infof("sending DO %d", k)
|
||||
c.sendCmd(Do, k)
|
||||
}
|
||||
}
|
||||
|
||||
// close closes the telnet connection
|
||||
func (c *Conn) close() {
|
||||
c.closedMutex.Lock()
|
||||
defer c.closedMutex.Unlock()
|
||||
|
||||
c.closed = true
|
||||
log.Infof("Closing the connection")
|
||||
c.conn.Close()
|
||||
c.closeConnLoopWrite()
|
||||
c.closeDatahandler()
|
||||
c.handlerWriter.Close()
|
||||
log.Infof("telnet connection closed")
|
||||
|
||||
// calling the CloseHandler passed by vspc
|
||||
c.CloseHandler(c)
|
||||
|
||||
}
|
||||
|
||||
func (c *Conn) closeConnLoopWrite() {
|
||||
connLoopWriteCh := make(chan struct{})
|
||||
c.connWriteDoneCh <- connLoopWriteCh
|
||||
<-connLoopWriteCh
|
||||
log.Debugf("connection loop write-side closed")
|
||||
}
|
||||
|
||||
func (c *Conn) closeDatahandler() {
|
||||
dataCh := make(chan struct{})
|
||||
c.dataHandlerCloseCh <- dataCh
|
||||
<-dataCh
|
||||
}
|
||||
|
||||
func (c *Conn) sendCmd(cmd byte, opt byte) {
|
||||
c.writeCh <- []byte{Iac, cmd, opt}
|
||||
log.Debugf("Sending command: %v %v", cmd, opt)
|
||||
}
|
||||
|
||||
func (c *Conn) handleOptionCommand(cmd byte, opt byte) {
|
||||
if cmd == Will || cmd == Wont {
|
||||
if _, ok := c.clientOpts[opt]; !ok {
|
||||
c.sendCmd(Dont, opt)
|
||||
return
|
||||
}
|
||||
c.sendCmd(Do, opt)
|
||||
}
|
||||
|
||||
if cmd == Do || cmd == Dont {
|
||||
if _, ok := c.serverOpts[opt]; !ok {
|
||||
c.sendCmd(Wont, opt)
|
||||
return
|
||||
}
|
||||
log.Debugf("Sending WILL command")
|
||||
c.sendCmd(Will, opt)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Conn) dataHandlerWrapper(w io.Writer, r io.Reader) {
|
||||
defer func() {
|
||||
log.Debugf("data handler closed")
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case ch := <-c.dataHandlerCloseCh:
|
||||
ch <- struct{}{}
|
||||
return
|
||||
case <-c.dataWrittenCh:
|
||||
// #nosec: Errors unhandled.
|
||||
if b, _ := ioutil.ReadAll(r); len(b) > 0 {
|
||||
c.DataHandler(w, b, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Conn) cmdHandlerWrapper(w io.Writer, r io.Reader) {
|
||||
// #nosec: Errors unhandled.
|
||||
if cmd, _ := ioutil.ReadAll(r); len(cmd) > 0 {
|
||||
c.CmdHandler(w, cmd, c)
|
||||
}
|
||||
}
|
||||
|
||||
// IsClosed returns true if the connection is already closed
|
||||
func (c *Conn) IsClosed() bool {
|
||||
c.closedMutex.Lock()
|
||||
defer c.closedMutex.Unlock()
|
||||
return c.closed
|
||||
}
|
||||
|
||||
// WriteData writes telnet data to the underlying connection doubling every IAC
|
||||
func (c *Conn) WriteData(b []byte) (int, error) {
|
||||
var escaped []byte
|
||||
for _, v := range b {
|
||||
if v == Iac {
|
||||
escaped = append(escaped, 255)
|
||||
}
|
||||
escaped = append(escaped, v)
|
||||
}
|
||||
if c.IsClosed() {
|
||||
return -1, errors.New("telnet connection is already closed")
|
||||
}
|
||||
c.writeCh <- escaped
|
||||
return len(b), nil
|
||||
}
|
||||
124
vendor/github.com/vmware/vic/pkg/telnet/connection_test.go
generated
vendored
Normal file
124
vendor/github.com/vmware/vic/pkg/telnet/connection_test.go
generated
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
// Copyright 2017 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package telnet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type dummyConn struct {
|
||||
dataBuf bytes.Buffer
|
||||
}
|
||||
|
||||
func (c *dummyConn) Read(b []byte) (n int, err error) {
|
||||
return 3, nil
|
||||
}
|
||||
|
||||
func (c *dummyConn) Write(b []byte) (n int, err error) {
|
||||
return c.dataBuf.Write(b)
|
||||
}
|
||||
|
||||
func (c *dummyConn) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *dummyConn) LocalAddr() net.Addr {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *dummyConn) RemoteAddr() net.Addr {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *dummyConn) SetDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *dummyConn) SetReadDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *dummyConn) SetWriteDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func newTestItem() *Conn {
|
||||
opts := connOpts{
|
||||
conn: &dummyConn{},
|
||||
serverOpts: map[byte]bool{
|
||||
Binary: true,
|
||||
Echo: true,
|
||||
},
|
||||
clientOpts: map[byte]bool{
|
||||
Naocrd: true,
|
||||
Naohts: true,
|
||||
},
|
||||
}
|
||||
return &Conn{
|
||||
connOpts: opts,
|
||||
writeCh: make(chan []byte),
|
||||
connWriteDoneCh: make(chan chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteData(t *testing.T) {
|
||||
conn := newTestItem()
|
||||
data := [][]byte{{10, 15, 23, 210}, {10, Iac, 30, 40}, {10, Iac, Iac, 30, 40}}
|
||||
expected := [][]byte{{10, 15, 23, 210}, {10, Iac, Iac, 30, 40}, {10, Iac, Iac, Iac, Iac, 30, 40}}
|
||||
for i, d := range data {
|
||||
go conn.WriteData(d)
|
||||
received := <-conn.writeCh
|
||||
assert.Equal(t, expected[i], received)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendCmd(t *testing.T) {
|
||||
conn := newTestItem()
|
||||
go conn.sendCmd(Do, Binary)
|
||||
received := <-conn.writeCh
|
||||
assert.Equal(t, []byte{Iac, Do, Binary}, received)
|
||||
}
|
||||
|
||||
func TestNegotiation(t *testing.T) {
|
||||
conn := newTestItem()
|
||||
go conn.startNegotiation()
|
||||
expected := map[byte][]byte{
|
||||
Binary: {Iac, Will, Binary},
|
||||
Echo: {Iac, Will, Echo},
|
||||
Naocrd: {Iac, Do, Naocrd},
|
||||
Naohts: {Iac, Do, Naohts},
|
||||
}
|
||||
for i := 0; i < 4; i++ {
|
||||
r := <-conn.writeCh
|
||||
assert.Equal(t, expected[r[2]], r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteLoop(t *testing.T) {
|
||||
conn := newTestItem()
|
||||
go conn.writeLoop()
|
||||
conn.writeCh <- []byte{1, 2, 3, 4}
|
||||
conn.writeCh <- []byte{5, 6}
|
||||
conn.writeCh <- []byte{7}
|
||||
ch := make(chan struct{})
|
||||
conn.connWriteDoneCh <- ch
|
||||
<-ch
|
||||
assert.Equal(t, []byte{1, 2, 3, 4, 5, 6, 7}, conn.connOpts.conn.(*dummyConn).dataBuf.Bytes())
|
||||
}
|
||||
130
vendor/github.com/vmware/vic/pkg/telnet/fsm.go
generated
vendored
Normal file
130
vendor/github.com/vmware/vic/pkg/telnet/fsm.go
generated
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
// Copyright 2017 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package telnet
|
||||
|
||||
import log "github.com/Sirupsen/logrus"
|
||||
|
||||
type state int
|
||||
|
||||
const (
|
||||
dataState state = iota
|
||||
optionNegotiationState
|
||||
cmdState
|
||||
subnegState
|
||||
subnegEndState
|
||||
errorState
|
||||
)
|
||||
|
||||
type fsm struct {
|
||||
curState state
|
||||
tc *Conn
|
||||
}
|
||||
|
||||
func newFSM() *fsm {
|
||||
f := &fsm{
|
||||
curState: dataState,
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func (fsm *fsm) start() {
|
||||
defer func() {
|
||||
log.Infof("FSM closed")
|
||||
}()
|
||||
for {
|
||||
b := make([]byte, 4096)
|
||||
n, err := fsm.readFromRawConnection(b)
|
||||
if n > 0 {
|
||||
for i := 0; i < n; i++ {
|
||||
ch := b[i]
|
||||
ns := fsm.nextState(ch)
|
||||
fsm.curState = ns
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Debugf("connection read: %v", err)
|
||||
fsm.tc.close()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (fsm *fsm) readFromRawConnection(b []byte) (int, error) {
|
||||
return fsm.tc.conn.Read(b)
|
||||
}
|
||||
|
||||
// this function returns what the next state is and performs the appropriate action
|
||||
func (fsm *fsm) nextState(ch byte) state {
|
||||
var nextState state
|
||||
b := []byte{ch}
|
||||
switch fsm.curState {
|
||||
case dataState:
|
||||
if ch != Iac {
|
||||
fsm.tc.dataRW.Write(b)
|
||||
fsm.tc.dataWrittenCh <- true
|
||||
nextState = dataState
|
||||
} else {
|
||||
nextState = cmdState
|
||||
}
|
||||
|
||||
case cmdState:
|
||||
if ch == Iac { // this is an escaping of IAC to send it as data
|
||||
fsm.tc.dataRW.Write(b)
|
||||
fsm.tc.dataWrittenCh <- true
|
||||
nextState = dataState
|
||||
} else if ch == Do || ch == Dont || ch == Will || ch == Wont {
|
||||
fsm.tc.cmdBuffer.WriteByte(ch)
|
||||
nextState = optionNegotiationState
|
||||
} else if ch == Sb {
|
||||
fsm.tc.cmdBuffer.WriteByte(ch)
|
||||
nextState = subnegState
|
||||
} else { // anything else
|
||||
fsm.tc.cmdBuffer.WriteByte(ch)
|
||||
fsm.tc.cmdHandlerWrapper(fsm.tc.handlerWriter, &fsm.tc.cmdBuffer)
|
||||
fsm.tc.cmdBuffer.Reset()
|
||||
nextState = dataState
|
||||
}
|
||||
case optionNegotiationState:
|
||||
fsm.tc.cmdBuffer.WriteByte(ch)
|
||||
opt := ch
|
||||
cmd := fsm.tc.cmdBuffer.Bytes()[0]
|
||||
fsm.tc.optCallback(cmd, opt)
|
||||
fsm.tc.cmdBuffer.Reset()
|
||||
nextState = dataState
|
||||
case subnegState:
|
||||
if ch == Iac {
|
||||
nextState = subnegEndState
|
||||
} else {
|
||||
nextState = subnegState
|
||||
fsm.tc.cmdBuffer.WriteByte(ch)
|
||||
}
|
||||
case subnegEndState:
|
||||
if ch == Se {
|
||||
fsm.tc.cmdBuffer.WriteByte(ch)
|
||||
fsm.tc.cmdHandlerWrapper(fsm.tc.handlerWriter, &fsm.tc.cmdBuffer)
|
||||
fsm.tc.cmdBuffer.Reset()
|
||||
nextState = dataState
|
||||
} else if ch == Iac { // escaping IAC
|
||||
nextState = subnegState
|
||||
fsm.tc.cmdBuffer.WriteByte(ch)
|
||||
} else {
|
||||
nextState = errorState
|
||||
}
|
||||
case errorState:
|
||||
nextState = dataState
|
||||
log.Infof("Finite state machine is in an error state. This should not happen for correct telnet protocol syntax")
|
||||
}
|
||||
return nextState
|
||||
}
|
||||
176
vendor/github.com/vmware/vic/pkg/telnet/fsm_test.go
generated
vendored
Normal file
176
vendor/github.com/vmware/vic/pkg/telnet/fsm_test.go
generated
vendored
Normal file
@@ -0,0 +1,176 @@
|
||||
// Copyright 2017 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package telnet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type mockConn struct {
|
||||
r io.Reader
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
func (c *mockConn) Read(b []byte) (int, error) {
|
||||
return c.r.Read(b)
|
||||
}
|
||||
|
||||
func (c *mockConn) Write(b []byte) (int, error) {
|
||||
return c.w.Write(b)
|
||||
}
|
||||
|
||||
func (c *mockConn) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *mockConn) LocalAddr() net.Addr {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *mockConn) RemoteAddr() net.Addr {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *mockConn) SetDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *mockConn) SetReadDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *mockConn) SetWriteDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func newMockConn(r io.Reader, w io.Writer) net.Conn {
|
||||
return &mockConn{
|
||||
r: r,
|
||||
w: w,
|
||||
}
|
||||
}
|
||||
|
||||
type cmd struct {
|
||||
cmdBuf []byte
|
||||
called bool
|
||||
}
|
||||
|
||||
func (d *cmd) mockCmdHandler(w io.Writer, b []byte, tc *Conn) {
|
||||
d.called = true
|
||||
d.cmdBuf = b
|
||||
}
|
||||
|
||||
type opt struct {
|
||||
cmd byte
|
||||
optn byte
|
||||
called bool
|
||||
}
|
||||
|
||||
func (o *opt) optCallback(cmd, option byte) {
|
||||
o.cmd = cmd
|
||||
o.optn = option
|
||||
o.called = true
|
||||
}
|
||||
|
||||
type testSample struct {
|
||||
inputSeq []byte
|
||||
expState []state
|
||||
expOpt []*opt
|
||||
expCmd []*cmd
|
||||
}
|
||||
|
||||
var samples = []testSample{
|
||||
{
|
||||
inputSeq: []byte{10, 20, 5, 12, 34, 125, 98},
|
||||
expState: []state{0, 0, 0, 0, 0, 0, 0},
|
||||
expOpt: []*opt{nil, nil, nil, nil, nil, nil, nil},
|
||||
expCmd: []*cmd{nil, nil, nil, nil, nil, nil, nil},
|
||||
},
|
||||
{
|
||||
inputSeq: []byte{Iac, Do, Echo, 10, 20, Iac, Will, Sga},
|
||||
expState: []state{cmdState, optionNegotiationState, dataState, dataState, dataState, cmdState, optionNegotiationState, dataState},
|
||||
expOpt: []*opt{nil, nil, {Do, Echo, true}, nil, nil, nil, nil, {Will, Sga, true}},
|
||||
expCmd: []*cmd{nil, nil, nil, nil, nil, nil, nil, nil},
|
||||
},
|
||||
{
|
||||
inputSeq: []byte{10, 20, Iac, Ayt, 5, Iac, Ao},
|
||||
expState: []state{dataState, dataState, cmdState, dataState, dataState, cmdState, dataState},
|
||||
expOpt: []*opt{nil, nil, nil, nil, nil, nil, nil},
|
||||
expCmd: []*cmd{nil, nil, nil, {[]byte{Ayt}, true}, nil, nil, {[]byte{Ao}, true}},
|
||||
},
|
||||
{
|
||||
inputSeq: []byte{10, Iac, Sb, 5, 12, Iac, Se},
|
||||
expState: []state{dataState, cmdState, subnegState, subnegState, subnegState, subnegEndState, dataState},
|
||||
expOpt: []*opt{nil, nil, nil, nil, nil, nil, nil},
|
||||
expCmd: []*cmd{nil, nil, nil, nil, nil, nil, {[]byte{Sb, 5, 12, Se}, true}},
|
||||
},
|
||||
}
|
||||
|
||||
func TestFSM(t *testing.T) {
|
||||
for count, s := range samples {
|
||||
log.Printf("test sample %d", count)
|
||||
b := make([]byte, 512)
|
||||
outBuf := bytes.NewBuffer(b)
|
||||
inBuf := bytes.NewBuffer(s.inputSeq)
|
||||
cmdPtr := &cmd{
|
||||
called: false,
|
||||
}
|
||||
optPtr := &opt{
|
||||
called: false,
|
||||
}
|
||||
dummyConn := newMockConn(inBuf, outBuf)
|
||||
fsm := newFSM()
|
||||
opts := connOpts{
|
||||
conn: dummyConn,
|
||||
fsm: fsm,
|
||||
Handlers: Handlers{
|
||||
CmdHandler: cmdPtr.mockCmdHandler,
|
||||
DataHandler: defaultDataHandlerFunc,
|
||||
},
|
||||
optCallback: optPtr.optCallback,
|
||||
}
|
||||
tc := newConn(&opts)
|
||||
go tc.dataHandlerWrapper(tc.handlerWriter, tc.dataRW)
|
||||
assert.Equal(t, fsm.curState, dataState)
|
||||
|
||||
for i, ch := range s.inputSeq {
|
||||
ns := fsm.nextState(ch)
|
||||
fsm.curState = ns
|
||||
assert.Equal(t, s.expState[i], ns)
|
||||
if optPtr.called {
|
||||
assert.Equal(t, s.expOpt[i].cmd, optPtr.cmd)
|
||||
assert.Equal(t, s.expOpt[i].optn, optPtr.optn)
|
||||
} else {
|
||||
assert.Nil(t, s.expOpt[i])
|
||||
}
|
||||
if cmdPtr.called {
|
||||
exp := s.expCmd[i].cmdBuf
|
||||
actual := cmdPtr.cmdBuf
|
||||
assert.Equal(t, exp, actual)
|
||||
} else {
|
||||
assert.Nil(t, s.expCmd[i])
|
||||
}
|
||||
optPtr.called = false
|
||||
cmdPtr.called = false
|
||||
}
|
||||
}
|
||||
}
|
||||
94
vendor/github.com/vmware/vic/pkg/telnet/protocol.go
generated
vendored
Normal file
94
vendor/github.com/vmware/vic/pkg/telnet/protocol.go
generated
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
// Copyright 2017 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package telnet
|
||||
|
||||
// Telnet protocol specific values
|
||||
const (
|
||||
// Iac is Interpret as Command
|
||||
Iac byte = 255
|
||||
Dont byte = 254
|
||||
Do byte = 253
|
||||
Wont byte = 252
|
||||
Will byte = 251
|
||||
Null byte = 0
|
||||
|
||||
Se byte = 240 // Subnegotiation End
|
||||
Nop byte = 241 // No Operation
|
||||
Dm byte = 242 // Data Mark
|
||||
Brk byte = 243 // Break
|
||||
IP byte = 244 // Interrupt process
|
||||
Ao byte = 245 // Abort output
|
||||
Ayt byte = 246 // Are You There
|
||||
Ec byte = 247 // Erase Character
|
||||
El byte = 248 // Erase Line
|
||||
Ga byte = 249 // Go Ahead
|
||||
Sb byte = 250 // Subnegotiation Begin
|
||||
|
||||
Binary byte = 0 // 8-bit data path
|
||||
Echo byte = 1 // echo
|
||||
Rcp byte = 2 // prepare to reconnect
|
||||
Sga byte = 3 // suppress go ahead
|
||||
Nams byte = 4 // approximate message size
|
||||
Status byte = 5 // give status
|
||||
Tm byte = 6 // timing mark
|
||||
Rcte byte = 7 // remote controlled transmission and echo
|
||||
Naol byte = 8 // negotiate about output line width
|
||||
Naop byte = 9 // negotiate about output page size
|
||||
Naocrd byte = 10 // negotiate about CR disposition
|
||||
Naohts byte = 11 // negotiate about horizontal tabstops
|
||||
Naohtd byte = 12 // negotiate about horizontal tab disposition
|
||||
Naoffd byte = 13 // negotiate about formfeed disposition
|
||||
Naovts byte = 14 // negotiate about vertical tab stops
|
||||
Naovtd byte = 15 // negotiate about vertical tab disposition
|
||||
Naolfd byte = 16 // negotiate about output LF disposition
|
||||
Xascii byte = 17 // extended ascii character set
|
||||
Logout byte = 18 // force logout
|
||||
Bm byte = 19 // byte macro
|
||||
Det byte = 20 // data entry terminal
|
||||
Supdup byte = 21 // supdup protocol
|
||||
SupdupOutput byte = 22 // supdup output
|
||||
SndLoc byte = 23 // send location
|
||||
Ttype byte = 24 // terminal type
|
||||
Eor byte = 25 // end or record
|
||||
TuID byte = 26 // TACACS user identification
|
||||
OutMrk byte = 27 // output marking
|
||||
TtyLoc byte = 28 // terminal location number
|
||||
Vt3270Regime byte = 29 // 3270 regime
|
||||
X3Pad byte = 30 // X.3 PAD
|
||||
Naws byte = 31 // window size
|
||||
Tspeed byte = 32 // terminal speed
|
||||
Lflow byte = 33 // remote flow control
|
||||
LineMode byte = 34 // Linemode option
|
||||
XDispLoc byte = 35 // X Display Location
|
||||
OldEnviron byte = 36 // Old - Environment variables
|
||||
Authentication byte = 37 // Authenticate
|
||||
Encrypt byte = 38 // Encryption option
|
||||
NewEnviron byte = 39 // New - Environment variables
|
||||
TN3270E byte = 40 // TN3270E
|
||||
XAuth byte = 41 // XAUTH
|
||||
Charset byte = 42 // CHARSET
|
||||
Rsp byte = 43 // Telnet Remote Serial Port
|
||||
ComPortOption byte = 44 // Com Port Control Option
|
||||
SupLocalEcho byte = 45 // Telnet Suppress Local Echo
|
||||
TLS byte = 46 // Telnet Start TLS
|
||||
Kermit byte = 47 // KERMIT
|
||||
SendURL byte = 48 // SEND-URL
|
||||
ForwardX byte = 49 // FORWARD_X
|
||||
PragmaLogon byte = 138 // TELOPT PRAGMA LOGON
|
||||
SspiLogin byte = 139 // TELOPT SSPI LOGON
|
||||
PragmaHeartbeat byte = 140 // TELOPT PRAGMA HEARTBEAT
|
||||
ExtOptList byte = 255 // Extended-Options-List
|
||||
NoOp byte = 0
|
||||
)
|
||||
116
vendor/github.com/vmware/vic/pkg/telnet/server.go
generated
vendored
Normal file
116
vendor/github.com/vmware/vic/pkg/telnet/server.go
generated
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
// Copyright 2017 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package telnet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// DataHandlerFunc is the callback function in the event of receiving data from the telnet client
|
||||
type DataHandlerFunc func(w io.Writer, data []byte, tc *Conn)
|
||||
|
||||
// CmdHandlerFunc is the callback function in the event of receiving a command from the telnet client
|
||||
type CmdHandlerFunc func(w io.Writer, cmd []byte, tc *Conn)
|
||||
|
||||
// CloseHandlerFunc is the callback function in the event of receiving EOF from the telnet client
|
||||
type CloseHandlerFunc func(tc *Conn)
|
||||
|
||||
var defaultDataHandlerFunc = func(w io.Writer, data []byte, tc *Conn) {}
|
||||
|
||||
var defaultCmdHandlerFunc = func(w io.Writer, cmd []byte, tc *Conn) {}
|
||||
|
||||
var defaultCloseHandlerFunc = func(tc *Conn) {}
|
||||
|
||||
type Handlers struct {
|
||||
DataHandler DataHandlerFunc
|
||||
CmdHandler CmdHandlerFunc
|
||||
CloseHandler CloseHandlerFunc
|
||||
}
|
||||
|
||||
// ServerOpts is the telnet server constructor options
|
||||
type ServerOpts struct {
|
||||
Addr string
|
||||
ServerOpts []byte
|
||||
ClientOpts []byte
|
||||
|
||||
Handlers
|
||||
}
|
||||
|
||||
// Server is the struct representing the telnet server
|
||||
type Server struct {
|
||||
ServerOptions map[byte]bool
|
||||
ClientOptions map[byte]bool
|
||||
|
||||
Handlers
|
||||
|
||||
ln net.Listener
|
||||
}
|
||||
|
||||
// NewServer is the constructor of the telnet server
|
||||
func NewServer(opts ServerOpts) *Server {
|
||||
ts := new(Server)
|
||||
ts.ClientOptions = make(map[byte]bool)
|
||||
ts.ServerOptions = make(map[byte]bool)
|
||||
for _, v := range opts.ServerOpts {
|
||||
ts.ServerOptions[v] = true
|
||||
}
|
||||
for _, v := range opts.ClientOpts {
|
||||
ts.ClientOptions[v] = true
|
||||
}
|
||||
ts.DataHandler = defaultDataHandlerFunc
|
||||
if opts.DataHandler != nil {
|
||||
ts.DataHandler = opts.DataHandler
|
||||
}
|
||||
|
||||
ts.CmdHandler = defaultCmdHandlerFunc
|
||||
if opts.CmdHandler != nil {
|
||||
ts.CmdHandler = opts.CmdHandler
|
||||
}
|
||||
|
||||
ts.CloseHandler = defaultCloseHandlerFunc
|
||||
if opts.CloseHandler != nil {
|
||||
ts.CloseHandler = opts.CloseHandler
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", opts.Addr)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("cannot start telnet server: %v", err))
|
||||
}
|
||||
ts.ln = ln
|
||||
return ts
|
||||
}
|
||||
|
||||
// Accept accepts a connection and returns the Telnet connection
|
||||
func (ts *Server) Accept() (*Conn, error) {
|
||||
// #nosec: Errors unhandled.
|
||||
conn, _ := ts.ln.Accept()
|
||||
log.Info("connection received")
|
||||
opts := connOpts{
|
||||
conn: conn,
|
||||
Handlers: ts.Handlers,
|
||||
clientOpts: ts.ClientOptions,
|
||||
fsm: newFSM(),
|
||||
}
|
||||
tc := newConn(&opts)
|
||||
go tc.writeLoop()
|
||||
go tc.dataHandlerWrapper(tc.handlerWriter, tc.dataRW)
|
||||
go tc.fsm.start()
|
||||
go tc.startNegotiation()
|
||||
return tc, nil
|
||||
}
|
||||
Reference in New Issue
Block a user