blob: f00f5b052f416dece5a5d71d5831e51e7de722c3 [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001// 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
5package ipv4
6
7import (
8 "net"
9 "syscall"
10
11 "golang.org/x/net/internal/socket"
12)
13
14// BUG(mikio): On Windows, the ReadFrom and WriteTo methods of RawConn
15// are not implemented.
16
17// A packetHandler represents the IPv4 datagram handler.
18type packetHandler struct {
19 *net.IPConn
20 *socket.Conn
21 rawOpt
22}
23
24func (c *packetHandler) ok() bool { return c != nil && c.IPConn != nil && c.Conn != nil }
25
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.
29func (c *packetHandler) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error) {
30 if !c.ok() {
31 return nil, nil, nil, syscall.EINVAL
32 }
33 return c.readFrom(b)
34}
35
36func slicePacket(b []byte) (h, p []byte, err error) {
37 if len(b) < HeaderLen {
38 return nil, nil, errHeaderTooShort
39 }
40 hdrlen := int(b[0]&0x0f) << 2
41 return b[:hdrlen], b[hdrlen:], nil
42}
43
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.
49//
50// The IPv4 header h must contain appropriate fields that include:
51//
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>
63// Options = optional
64func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
65 if !c.ok() {
66 return syscall.EINVAL
67 }
68 return c.writeTo(h, p, cm)
69}