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.
10 "golang.org/x/net/internal/socket"
13 // BUG(mikio): On Windows, the ReadFrom and WriteTo methods of RawConn
14 // are not implemented.
16 // A packetHandler represents the IPv4 datagram handler.
17 type packetHandler struct {
23 func (c *packetHandler) ok() bool { return c != nil && c.IPConn != nil && c.Conn != nil }
25 // ReadFrom reads an IPv4 datagram from the endpoint c, copying the
26 // datagram into b. It returns the received datagram as the IPv4
27 // header h, the payload p and the control message cm.
28 func (c *packetHandler) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error) {
30 return nil, nil, nil, errInvalidConn
35 func slicePacket(b []byte) (h, p []byte, err error) {
36 if len(b) < HeaderLen {
37 return nil, nil, errHeaderTooShort
39 hdrlen := int(b[0]&0x0f) << 2
40 return b[:hdrlen], b[hdrlen:], nil
43 // WriteTo writes an IPv4 datagram through the endpoint c, copying the
44 // datagram from the IPv4 header h and the payload p. The control
45 // message cm allows the datagram path and the outgoing interface to be
46 // specified. Currently only Darwin and Linux support this. The cm
47 // may be nil if control of the outgoing datagram is not required.
49 // The IPv4 header h must contain appropriate fields that include:
51 // Version = <must be specified>
52 // Len = <must be specified>
53 // TOS = <must be specified>
54 // TotalLen = <must be specified>
55 // ID = platform sets an appropriate value if ID is zero
56 // FragOff = <must be specified>
57 // TTL = <must be specified>
58 // Protocol = <must be specified>
59 // Checksum = platform sets an appropriate value if Checksum is zero
60 // Src = platform sets an appropriate value if Src is nil
61 // Dst = <must be specified>
63 func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
67 return c.writeTo(h, p, cm)