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.
12 "golang.org/x/net/internal/socket"
15 // BUG(mikio): On Windows, the JoinSourceSpecificGroup,
16 // LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and
17 // IncludeSourceSpecificGroup methods of PacketConn are not
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.
27 type genericOpt struct {
31 func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
33 // PathMTU returns a path MTU value for the destination associated
35 func (c *Conn) PathMTU() (int, error) {
37 return 0, syscall.EINVAL
39 so, ok := sockOpts[ssoPathMTU]
41 return 0, errOpNoSupport
43 _, mtu, err := so.getMTUInfo(c.Conn)
50 // NewConn returns a new Conn.
51 func NewConn(c net.Conn) *Conn {
52 cc, _ := socket.NewConn(c)
54 genericOpt: genericOpt{Conn: cc},
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 {
69 type dgramOpt struct {
73 func (c *dgramOpt) ok() bool { return c != nil && c.Conn != nil }
75 // SetControlMessage allows to receive the per packet basis IP-level
77 func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
78 if !c.payloadHandler.ok() {
81 return setControlMessage(c.dgramOpt.Conn, &c.payloadHandler.rawOpt, cf, on)
84 // SetDeadline sets the read and write deadlines associated with the
86 func (c *PacketConn) SetDeadline(t time.Time) error {
87 if !c.payloadHandler.ok() {
90 return c.payloadHandler.SetDeadline(t)
93 // SetReadDeadline sets the read deadline associated with the
95 func (c *PacketConn) SetReadDeadline(t time.Time) error {
96 if !c.payloadHandler.ok() {
99 return c.payloadHandler.SetReadDeadline(t)
102 // SetWriteDeadline sets the write deadline associated with the
104 func (c *PacketConn) SetWriteDeadline(t time.Time) error {
105 if !c.payloadHandler.ok() {
106 return syscall.EINVAL
108 return c.payloadHandler.SetWriteDeadline(t)
111 // Close closes the endpoint.
112 func (c *PacketConn) Close() error {
113 if !c.payloadHandler.ok() {
114 return syscall.EINVAL
116 return c.payloadHandler.Close()
119 // NewPacketConn returns a new PacketConn using c as its underlying
121 func NewPacketConn(c net.PacketConn) *PacketConn {
122 cc, _ := socket.NewConn(c.(net.Conn))
124 genericOpt: genericOpt{Conn: cc},
125 dgramOpt: dgramOpt{Conn: cc},
126 payloadHandler: payloadHandler{PacketConn: c, Conn: cc},