Skip to content

Commit

Permalink
Merge pull request #58 from digitalrebar/send-packets-from-the-samne-…
Browse files Browse the repository at this point in the history
…address-they-were-recieved-by

Make the server side send packets from the same IP address it recieve…
  • Loading branch information
pin authored Mar 25, 2021
2 parents e4f0737 + ba397e6 commit b0a0cac
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
30 changes: 24 additions & 6 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"fmt"
"net"
"time"

"golang.org/x/net/ipv6"

"golang.org/x/net/ipv4"
)

type connectionError struct {
Expand Down Expand Up @@ -32,15 +36,29 @@ type connConnection struct {
}

type chanConnection struct {
sendConn net.PacketConn
channel chan []byte
addr *net.UDPAddr
timeout time.Duration
complete chan string
sendConn net.PacketConn
channel chan []byte
srcAddr, addr *net.UDPAddr
timeout time.Duration
complete chan string
}

func (c *chanConnection) sendTo(data []byte, addr *net.UDPAddr) error {
_, err := c.sendConn.WriteTo(data, addr)
var err error
if conn, ok := c.sendConn.(*net.UDPConn); ok {
srcAddr := c.srcAddr.IP.To4()
var cmm []byte
if srcAddr != nil {
cm := &ipv4.ControlMessage{Src: srcAddr}
cmm = cm.Marshal()
} else {
cm := &ipv6.ControlMessage{Src: c.srcAddr.IP}
cmm = cm.Marshal()
}
_, _, err = conn.WriteMsgUDP(data, cmm, c.addr)
} else {
_, err = c.sendConn.WriteTo(data, addr)
}
return err
}

Expand Down
12 changes: 10 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,17 @@ func (s *Server) handlePacket(localAddr net.IP, remoteAddr *net.UDPAddr, buffer
if err != nil {
return err
}
listenAddr := &net.UDPAddr{IP: localAddr}
switch p := p.(type) {
case pWRQ:
filename, mode, opts, err := unpackRQ(p)
if err != nil {
return fmt.Errorf("unpack WRQ: %v", err)
}
//fmt.Printf("got WRQ (filename=%s, mode=%s, opts=%v)\n", filename, mode, opts)
if err != nil {
return fmt.Errorf("open transmission: %v", err)
}
wt := &receiver{
send: make([]byte, datagramLength),
receive: make([]byte, datagramLength),
Expand All @@ -346,6 +351,7 @@ func (s *Server) handlePacket(localAddr net.IP, remoteAddr *net.UDPAddr, buffer
}
if s.singlePort {
wt.conn = &chanConnection{
srcAddr: listenAddr,
addr: remoteAddr,
channel: listener,
timeout: s.timeout,
Expand All @@ -354,7 +360,7 @@ func (s *Server) handlePacket(localAddr net.IP, remoteAddr *net.UDPAddr, buffer
}
wt.singlePort = true
} else {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
conn, err := net.ListenUDP("udp", listenAddr)
if err != nil {
return err
}
Expand All @@ -379,6 +385,7 @@ func (s *Server) handlePacket(localAddr net.IP, remoteAddr *net.UDPAddr, buffer
if err != nil {
return fmt.Errorf("unpack RRQ: %v", err)
}
//fmt.Printf("got RRQ (filename=%s, mode=%s, opts=%v)\n", filename, mode, opts)
rf := &sender{
send: make([]byte, datagramLength),
sendA: senderAnticipate{enabled: false},
Expand All @@ -398,14 +405,15 @@ func (s *Server) handlePacket(localAddr net.IP, remoteAddr *net.UDPAddr, buffer
}
if s.singlePort {
rf.conn = &chanConnection{
srcAddr: listenAddr,
addr: remoteAddr,
channel: listener,
timeout: s.timeout,
sendConn: s.conn,
complete: s.gcCollect,
}
} else {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
conn, err := net.ListenUDP("udp", listenAddr)
if err != nil {
return err
}
Expand Down

0 comments on commit b0a0cac

Please sign in to comment.