frp/vendor/golang.org/x/net/ipv4/packet.go

70 lines
2.2 KiB
Go
Raw Normal View History

2017-06-04 14:47:24 +02:00
// Copyright 2012 The Go Authors. All rights reserved.
2017-06-04 14:07:03 +02:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ipv4
import (
"net"
"syscall"
2017-06-04 14:47:24 +02:00
"golang.org/x/net/internal/socket"
2017-06-04 14:07:03 +02:00
)
2017-06-04 14:47:24 +02:00
// BUG(mikio): On Windows, the ReadFrom and WriteTo methods of RawConn
// are not implemented.
2017-06-04 14:07:03 +02:00
// A packetHandler represents the IPv4 datagram handler.
type packetHandler struct {
2017-06-04 14:47:24 +02:00
*net.IPConn
*socket.Conn
2017-06-04 14:07:03 +02:00
rawOpt
}
2017-06-04 14:47:24 +02:00
func (c *packetHandler) ok() bool { return c != nil && c.IPConn != nil && c.Conn != nil }
2017-06-04 14:07:03 +02:00
// ReadFrom reads an IPv4 datagram from the endpoint c, copying the
2017-06-04 14:47:24 +02:00
// datagram into b. It returns the received datagram as the IPv4
2017-06-04 14:07:03 +02:00
// header h, the payload p and the control message cm.
func (c *packetHandler) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error) {
if !c.ok() {
return nil, nil, nil, syscall.EINVAL
}
2017-06-04 14:47:24 +02:00
return c.readFrom(b)
2017-06-04 14:07:03 +02:00
}
func slicePacket(b []byte) (h, p []byte, err error) {
if len(b) < HeaderLen {
return nil, nil, errHeaderTooShort
}
hdrlen := int(b[0]&0x0f) << 2
return b[:hdrlen], b[hdrlen:], nil
}
// WriteTo writes an IPv4 datagram through the endpoint c, copying the
2017-06-04 14:47:24 +02:00
// datagram from the IPv4 header h and the payload p. The control
2017-06-04 14:07:03 +02:00
// message cm allows the datagram path and the outgoing interface to be
2017-06-04 14:47:24 +02:00
// specified. Currently only Darwin and Linux support this. The cm
2017-06-04 14:07:03 +02:00
// may be nil if control of the outgoing datagram is not required.
//
// The IPv4 header h must contain appropriate fields that include:
//
2017-06-04 14:47:24 +02:00
// Version = <must be specified>
2017-06-04 14:07:03 +02:00
// Len = <must be specified>
// TOS = <must be specified>
// TotalLen = <must be specified>
// ID = platform sets an appropriate value if ID is zero
// FragOff = <must be specified>
// TTL = <must be specified>
// Protocol = <must be specified>
// Checksum = platform sets an appropriate value if Checksum is zero
// Src = platform sets an appropriate value if Src is nil
// Dst = <must be specified>
// Options = optional
func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
if !c.ok() {
return syscall.EINVAL
}
2017-06-04 14:47:24 +02:00
return c.writeTo(h, p, cm)
2017-06-04 14:07:03 +02:00
}