Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ import (

// NewClient creates TFTP client for server on address provided.
func NewClient(addr string) (*Client, error) {
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, fmt.Errorf("resolving address %s: %v", addr, err)
}
return &Client{
addr: a,
timeout: defaultTimeout,
retries: defaultRetries,
localAddr: &net.UDPAddr{},
}, nil
}

// NewClientWithLocalAddr creates TFTP client for server on local ip address laddr provided.
func NewClientWithLocalAddr(addr string, localaddr string) (*Client, error) {
if localaddr == "" {
return nil, fmt.Errorf("localaddr is empty")
}
if net.ParseIP(localaddr) == nil {
return nil, fmt.Errorf("provided localaddr: %s is not valid ip addr", localaddr)
}
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, fmt.Errorf("resolving address %s: %v", addr, err)
Expand All @@ -18,6 +38,10 @@ func NewClient(addr string) (*Client, error) {
addr: a,
timeout: defaultTimeout,
retries: defaultRetries,
localAddr: &net.UDPAddr{
IP: net.ParseIP(localaddr),
Port: 0,
},
}, nil
}

Expand Down Expand Up @@ -57,17 +81,18 @@ func (c *Client) RequestTSize(s bool) {

// Client stores data about a single TFTP client
type Client struct {
addr *net.UDPAddr
timeout time.Duration
retries int
backoff backoffFunc
blksize int
tsize bool
addr *net.UDPAddr
timeout time.Duration
retries int
backoff backoffFunc
blksize int
tsize bool
localAddr *net.UDPAddr
}

// Send starts outgoing file transmission. It returns io.ReaderFrom or error.
func (c Client) Send(filename string, mode string) (io.ReaderFrom, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
conn, err := net.ListenUDP("udp", c.localAddr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -97,7 +122,7 @@ func (c Client) Send(filename string, mode string) (io.ReaderFrom, error) {

// Receive starts incoming file transmission. It returns io.WriterTo or error.
func (c Client) Receive(filename string, mode string) (io.WriterTo, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
conn, err := net.ListenUDP("udp", c.localAddr)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module github.com/pin/tftp/v3
module github.com/tejaskumark/tftp/v2

go 1.23.0

retract (
v1.0.1 // Contains retractions only.
v1.0.0 // Published accidentally.
)

require golang.org/x/net v0.43.0

require golang.org/x/sys v0.35.0 // indirect
2 changes: 1 addition & 1 deletion receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"time"

"github.com/pin/tftp/v3/netascii"
"github.com/tejaskumark/tftp/v2/netascii"
)

// IncomingTransfer provides methods that expose information associated with
Expand Down
2 changes: 1 addition & 1 deletion sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"time"

"github.com/pin/tftp/v3/netascii"
"github.com/tejaskumark/tftp/v2/netascii"
)

// OutgoingTransfer provides methods to set the outgoing transfer size and
Expand Down