OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / net / ipv6 / endpoint.go
1 // 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
5 package ipv6
6
7 import (
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.
23 type Conn struct {
24         genericOpt
25 }
26
27 type genericOpt struct {
28         *socket.Conn
29 }
30
31 func (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.
35 func (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.
51 func 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.
63 type PacketConn struct {
64         genericOpt
65         dgramOpt
66         payloadHandler
67 }
68
69 type dgramOpt struct {
70         *socket.Conn
71 }
72
73 func (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.
77 func (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.
86 func (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.
95 func (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.
104 func (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.
112 func (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.
121 func 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 }