Skip to content

portfwd: Read bytes ignored on is io.EOF returned from ReadFrom #2970

Closed
@nirs

Description

@nirs

Based on PacketCon.ReadFrom docs:

        // ReadFrom reads a packet from the connection,
	// copying the payload into p. It returns the number of
	// bytes copied into p and the return address that
	// was on the packet.
	// It returns the number of bytes read (0 <= n <= len(p))
	// and any error encountered. Callers should always process
	// the n > 0 bytes returned before considering the error err.
	// ReadFrom can be made to time out and return an error after a
	// fixed time limit; see SetDeadline and SetReadDeadline.

https://pkg.go.dev/net#PacketConn

	g.Go(func() error {
		buf := make([]byte, 65507)
		for {
			n, addr, err := conn.ReadFrom(buf)
			if errors.Is(err, io.EOF) {
				return nil
			}
			if err != nil {
				return err
			}
			msg := &api.TunnelMessage{
				Id:            id + "-" + addr.String(),
				Protocol:      "udp",
				GuestAddr:     guestAddr,
				Data:          buf[:n],
				UdpTargetAddr: addr.String(),
			}
			if err := stream.Send(msg); err != nil {
				return err
			}
		}
	})

It seems that we need to do:

  • read
  • send message with the read bytes
  • handle the err - not clear if it can be io.EOF

Originally posted by @tamird in #2969 (comment)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions