Skip to content

Commit

Permalink
Add data channel (active/passive) information to the ClientContext in…
Browse files Browse the repository at this point in the history
…terface (#248)

* GetLastDataChannel returns the last data channel mode (passive or active)

* use the paramsMutex for lastDataChannel updates

* update README.md with GetLastDataChannel update to ClientContext

* convert data channel to enums
  • Loading branch information
mmcgeefeedo authored Jul 27, 2021
1 parent 61f871a commit f5a67ad
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ type ClientContext interface {

// GetLastCommand returns the last received command
GetLastCommand() string

// GetLastDataChannel returns the last data channel mode
GetLastDataChannel() DataChannel
}

// Settings define all the server settings
Expand Down
25 changes: 25 additions & 0 deletions client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ const (
TransferTypeBinary
)

// DataChannel is the enumerable that represents the data channel (active or passive)
type DataChannel int

// Supported data channel types
const (
DataChannelPassive DataChannel = iota + 1
DataChannelActive
)

const (
maxCommandSize = 4096
)
Expand Down Expand Up @@ -91,6 +100,7 @@ type clientHandler struct {
transferWg sync.WaitGroup // wait group for command that open a transfer connection
transferMu sync.Mutex // this mutex will protect the transfer parameters
transfer transferHandler // Transfer connection (passive or active)s
lastDataChannel DataChannel // Last data channel mode (passive or active)
isTransferOpen bool // indicate if the transfer connection is opened
isTransferAborted bool // indicate if the transfer was aborted
paramsMutex sync.RWMutex // mutex to protect the parameters exposed to the library users
Expand Down Expand Up @@ -231,13 +241,28 @@ func (c *clientHandler) GetLastCommand() string {
return c.command
}

// GetLastDataChannel returns the last data channel mode
func (c *clientHandler) GetLastDataChannel() DataChannel {
c.paramsMutex.RLock()
defer c.paramsMutex.RUnlock()

return c.lastDataChannel
}

func (c *clientHandler) setLastCommand(cmd string) {
c.paramsMutex.Lock()
defer c.paramsMutex.Unlock()

c.command = cmd
}

func (c *clientHandler) setLastDataChannel(channel DataChannel) {
c.paramsMutex.Lock()
defer c.paramsMutex.Unlock()

c.lastDataChannel = channel
}

func (c *clientHandler) closeTransfer() error {
var err error
if c.transfer != nil {
Expand Down
5 changes: 5 additions & 0 deletions client_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func TestLastCommand(t *testing.T) {
assert.Empty(t, cc.GetLastCommand())
}

func TestLastDataChannel(t *testing.T) {
cc := clientHandler{lastDataChannel: DataChannelPassive}
assert.Equal(t, DataChannelPassive, cc.GetLastDataChannel())
}

func TestTransferOpenError(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
Expand Down
3 changes: 3 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ type ClientContext interface {

// GetLastCommand returns the last received command
GetLastCommand() string

// GetLastDataChannel returns the last data channel mode
GetLastDataChannel() DataChannel
}

// FileTransfer defines the inferface for file transfers.
Expand Down
1 change: 1 addition & 0 deletions transfer_active.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (c *clientHandler) handlePORT(param string) error {
}

c.transferMu.Unlock()
c.setLastDataChannel(DataChannelActive)

c.writeMessage(StatusOK, command+" command successful")

Expand Down
1 change: 1 addition & 0 deletions transfer_pasv.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (c *clientHandler) handlePASV(param string) error {
c.transferMu.Lock()
c.transfer = p
c.transferMu.Unlock()
c.setLastDataChannel(DataChannelPassive)

return nil
}
Expand Down

0 comments on commit f5a67ad

Please sign in to comment.