1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "golang.org/x/net/internal/socket"
14 // BUG(mikio): On Windows, the ReadFrom and WriteTo methods of RawConn
15 // are not implemented.
17 // A packetHandler represents the IPv4 datagram handler.
18 type packetHandler struct {
24 func (c *packetHandler) ok() bool { return c != nil && c.IPConn != nil && c.Conn != nil }
26 // ReadFrom reads an IPv4 datagram from the endpoint c, copying the
27 // datagram into b. It returns the received datagram as the IPv4
28 // header h, the payload p and the control message cm.
29 func (c *packetHandler) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error) {
31 return nil, nil, nil, syscall.EINVAL
36 func slicePacket(b []byte) (h, p []byte, err error) {
37 if len(b) < HeaderLen {
38 return nil, nil, errHeaderTooShort
40 hdrlen := int(b[0]&0x0f) << 2
41 return b[:hdrlen], b[hdrlen:], nil
44 // WriteTo writes an IPv4 datagram through the endpoint c, copying the
45 // datagram from the IPv4 header h and the payload p. The control
46 // message cm allows the datagram path and the outgoing interface to be
47 // specified. Currently only Darwin and Linux support this. The cm
48 // may be nil if control of the outgoing datagram is not required.
50 // The IPv4 header h must contain appropriate fields that include:
52 // Version = <must be specified>
53 // Len = <must be specified>
54 // TOS = <must be specified>
55 // TotalLen = <must be specified>
56 // ID = platform sets an appropriate value if ID is zero
57 // FragOff = <must be specified>
58 // TTL = <must be specified>
59 // Protocol = <must be specified>
60 // Checksum = platform sets an appropriate value if Checksum is zero
61 // Src = platform sets an appropriate value if Src is nil
62 // Dst = <must be specified>
64 func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
68 return c.writeTo(h, p, cm)