966bb776f56084549375c4489cf5e06577ef2824
[platform/core/system/edge-orchestration.git] / vendor / github.com / miekg / dns / vendor / golang.org / x / net / ipv4 / packet.go
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.
4
5 package ipv4
6
7 import (
8         "net"
9
10         "golang.org/x/net/internal/socket"
11 )
12
13 // BUG(mikio): On Windows, the ReadFrom and WriteTo methods of RawConn
14 // are not implemented.
15
16 // A packetHandler represents the IPv4 datagram handler.
17 type packetHandler struct {
18         *net.IPConn
19         *socket.Conn
20         rawOpt
21 }
22
23 func (c *packetHandler) ok() bool { return c != nil && c.IPConn != nil && c.Conn != nil }
24
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) {
29         if !c.ok() {
30                 return nil, nil, nil, errInvalidConn
31         }
32         return c.readFrom(b)
33 }
34
35 func slicePacket(b []byte) (h, p []byte, err error) {
36         if len(b) < HeaderLen {
37                 return nil, nil, errHeaderTooShort
38         }
39         hdrlen := int(b[0]&0x0f) << 2
40         return b[:hdrlen], b[hdrlen:], nil
41 }
42
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.
48 //
49 // The IPv4 header h must contain appropriate fields that include:
50 //
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>
62 //      Options       = optional
63 func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
64         if !c.ok() {
65                 return errInvalidConn
66         }
67         return c.writeTo(h, p, cm)
68 }