blob: 0624c174048e74fc51a1570ccdb46cc9bd3287a1 [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001// Copyright 2013 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 ipv6
6
7import (
8 "net"
9 "syscall"
10 "time"
11
12 "golang.org/x/net/internal/socket"
13)
14
15// BUG(mikio): On Windows, the JoinSourceSpecificGroup,
16// LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and
17// IncludeSourceSpecificGroup methods of PacketConn are not
18// implemented.
19
20// A Conn represents a network endpoint that uses IPv6 transport.
21// It allows to set basic IP-level socket options such as traffic
22// class and hop limit.
23type Conn struct {
24 genericOpt
25}
26
27type genericOpt struct {
28 *socket.Conn
29}
30
31func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
32
33// PathMTU returns a path MTU value for the destination associated
34// with the endpoint.
35func (c *Conn) PathMTU() (int, error) {
36 if !c.ok() {
37 return 0, syscall.EINVAL
38 }
39 so, ok := sockOpts[ssoPathMTU]
40 if !ok {
41 return 0, errOpNoSupport
42 }
43 _, mtu, err := so.getMTUInfo(c.Conn)
44 if err != nil {
45 return 0, err
46 }
47 return mtu, nil
48}
49
50// NewConn returns a new Conn.
51func NewConn(c net.Conn) *Conn {
52 cc, _ := socket.NewConn(c)
53 return &Conn{
54 genericOpt: genericOpt{Conn: cc},
55 }
56}
57
58// A PacketConn represents a packet network endpoint that uses IPv6
59// transport. It is used to control several IP-level socket options
60// including IPv6 header manipulation. It also provides datagram
61// based network I/O methods specific to the IPv6 and higher layer
62// protocols such as OSPF, GRE, and UDP.
63type PacketConn struct {
64 genericOpt
65 dgramOpt
66 payloadHandler
67}
68
69type dgramOpt struct {
70 *socket.Conn
71}
72
73func (c *dgramOpt) ok() bool { return c != nil && c.Conn != nil }
74
75// SetControlMessage allows to receive the per packet basis IP-level
76// socket options.
77func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
78 if !c.payloadHandler.ok() {
79 return syscall.EINVAL
80 }
81 return setControlMessage(c.dgramOpt.Conn, &c.payloadHandler.rawOpt, cf, on)
82}
83
84// SetDeadline sets the read and write deadlines associated with the
85// endpoint.
86func (c *PacketConn) SetDeadline(t time.Time) error {
87 if !c.payloadHandler.ok() {
88 return syscall.EINVAL
89 }
90 return c.payloadHandler.SetDeadline(t)
91}
92
93// SetReadDeadline sets the read deadline associated with the
94// endpoint.
95func (c *PacketConn) SetReadDeadline(t time.Time) error {
96 if !c.payloadHandler.ok() {
97 return syscall.EINVAL
98 }
99 return c.payloadHandler.SetReadDeadline(t)
100}
101
102// SetWriteDeadline sets the write deadline associated with the
103// endpoint.
104func (c *PacketConn) SetWriteDeadline(t time.Time) error {
105 if !c.payloadHandler.ok() {
106 return syscall.EINVAL
107 }
108 return c.payloadHandler.SetWriteDeadline(t)
109}
110
111// Close closes the endpoint.
112func (c *PacketConn) Close() error {
113 if !c.payloadHandler.ok() {
114 return syscall.EINVAL
115 }
116 return c.payloadHandler.Close()
117}
118
119// NewPacketConn returns a new PacketConn using c as its underlying
120// transport.
121func NewPacketConn(c net.PacketConn) *PacketConn {
122 cc, _ := socket.NewConn(c.(net.Conn))
123 return &PacketConn{
124 genericOpt: genericOpt{Conn: cc},
125 dgramOpt: dgramOpt{Conn: cc},
126 payloadHandler: payloadHandler{PacketConn: c, Conn: cc},
127 }
128}