Skip to content

Commit

Permalink
rework PortStack internals; cleaner API
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 20, 2023
1 parent e6396dc commit 40d0a58
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 158 deletions.
14 changes: 8 additions & 6 deletions stack/port_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type tcphandler func(response []byte, pkt *TCPPacket) (int, error)
type tcpPort struct {
LastRx time.Time
handler tcphandler
Port uint16
port uint16
packets [1]TCPPacket
}

Expand All @@ -39,6 +39,8 @@ func (p *TCPPacket) String() string {
return "TCP Packet: " + p.Eth.String() + " " + p.IP.String() + " " + p.TCP.String() + " payload:" + strconv.Quote(string(p.Payload()))
}

func (p tcpPort) Port() uint16 { return p.port }

// NeedsHandling returns true if the socket needs handling before it can
// admit more pending packets.
func (u *tcpPort) NeedsHandling() bool {
Expand All @@ -50,14 +52,14 @@ func (u *tcpPort) NeedsHandling() bool {

// IsPendingHandling returns true if there are packet(s) pending handling.
func (u *tcpPort) IsPendingHandling() bool {
return u.Port != 0 && !u.packets[0].Rx.IsZero()
return u.port != 0 && !u.packets[0].Rx.IsZero()
}

// HandleEth writes the socket's response into dst to be sent over an ethernet interface.
// HandleEth can return 0 bytes written and a nil error to indicate no action must be taken.
func (u *tcpPort) HandleEth(dst []byte) (n int, err error) {
if u.handler == nil {
panic("nil tcp handler on port " + strconv.Itoa(int(u.Port)))
panic("nil tcp handler on port " + strconv.Itoa(int(u.port)))
}
packet := &u.packets[0]

Expand All @@ -73,10 +75,10 @@ func (u *tcpPort) HandleEth(dst []byte) (n int, err error) {
// Open sets the UDP handler and opens the port.
func (u *tcpPort) Open(port uint16, handler tcphandler) {
if port == 0 || handler == nil {
panic("invalid port or nil handler" + strconv.Itoa(int(u.Port)))
panic("invalid port or nil handler" + strconv.Itoa(int(u.port)))
}
u.handler = handler
u.Port = port
u.port = port
for i := range u.packets {
u.packets[i].Rx = time.Time{} // Invalidate packets.
}
Expand All @@ -93,7 +95,7 @@ func (s *tcpPort) pending() (p uint32) {

func (u *tcpPort) Close() {
u.handler = nil
u.Port = 0 // Port 0 flags the port is inactive.
u.port = 0 // Port 0 flags the port is inactive.
}

func (u *tcpPort) forceResponse() (added bool) {
Expand Down
14 changes: 8 additions & 6 deletions stack/port_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type udphandler func(response []byte, pkt *UDPPacket) (int, error)
type udpPort struct {
LastRx time.Time
handler udphandler
Port uint16
port uint16
packets [1]UDPPacket
}

Expand All @@ -24,6 +24,8 @@ type UDPPacket struct {
payload [_MTU - eth.SizeEthernetHeader - eth.SizeIPv4Header - eth.SizeUDPHeader]byte
}

func (u udpPort) Port() uint16 { return u.port }

// NeedsHandling returns true if the socket needs handling before it can
// admit more pending packets.
func (u *udpPort) NeedsHandling() bool {
Expand All @@ -35,14 +37,14 @@ func (u *udpPort) NeedsHandling() bool {

// IsPendingHandling returns true if there are packet(s) pending handling.
func (u *udpPort) IsPendingHandling() bool {
return u.Port != 0 && !u.packets[0].Rx.IsZero()
return u.port != 0 && !u.packets[0].Rx.IsZero()
}

// HandleEth writes the socket's response into dst to be sent over an ethernet interface.
// HandleEth can return 0 bytes written and a nil error to indicate no action must be taken.
func (u *udpPort) HandleEth(dst []byte) (int, error) {
if u.handler == nil {
panic("nil udp handler on port " + strconv.Itoa(int(u.Port)))
panic("nil udp handler on port " + strconv.Itoa(int(u.port)))
}
packet := &u.packets[0]

Expand All @@ -58,10 +60,10 @@ func (u *udpPort) HandleEth(dst []byte) (int, error) {
// Open sets the UDP handler and opens the port.
func (u *udpPort) Open(port uint16, h udphandler) {
if port == 0 || h == nil {
panic("invalid port or nil handler" + strconv.Itoa(int(u.Port)))
panic("invalid port or nil handler" + strconv.Itoa(int(u.port)))
}
u.handler = h
u.Port = port
u.port = port
}

func (s *udpPort) pending() (p int) {
Expand All @@ -74,7 +76,7 @@ func (s *udpPort) pending() (p int) {
}

func (u *udpPort) Close() {
u.Port = 0 // Port 0 flags the port is inactive.
u.port = 0 // Port 0 flags the port is inactive.
for i := range u.packets {
u.packets[i].Rx = time.Time{} // Invalidate packets.
}
Expand Down
Loading

0 comments on commit 40d0a58

Please sign in to comment.