learning_go/book_with_bridge/ftp_server/ftp/port.go
Пивкин Александр Николаевич 77c0e5419c Added devp-deploy to rbd-exporter
2026-01-02 16:13:44 +03:00

42 lines
815 B
Go

package ftp
import "fmt"
func (c *Conn) port(args []string) {
if len(args) != 1 {
c.respond(status501)
return
}
dataPort, err := dataPortFromHostPort(args[0])
if err != nil {
fmt.Print(err)
c.respond(status501)
return
}
c.dataPort = dataPort
c.respond(status200)
}
type dataPort struct {
h1, h2, h3, h4 int // host
p1, p2 int // port
}
func dataPortFromHostPort(hostPort string) (*dataPort, error) {
var dp dataPort
_, err := fmt.Sscanf(hostPort, "%d,%d,%d,%d,%d,%d",
&dp.h1, &dp.h2, &dp.h3, &dp.h4, &dp.p1, &dp.p2)
if err != nil {
return nil, err
}
return &dp, nil
}
func (d *dataPort) toAddress() string {
if d == nil {
return ""
}
// convert hex port bytes to decimal port
port := d.p1<<8 + d.p2
return fmt.Sprintf("%d.%d.%d.%d:%d", d.h1, d.h2, d.h3, d.h4, port)
}