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.
12 "golang.org/x/net/internal/socket"
15 // BUG(mikio): On Windows, the JoinSourceSpecificGroup,
16 // LeaveSourceSpecificGroup, ExcludeSourceSpecificGroup and
17 // IncludeSourceSpecificGroup methods of PacketConn and RawConn are
20 // A Conn represents a network endpoint that uses the IPv4 transport.
21 // It is used to control basic IP-level socket options such as TOS and
27 type genericOpt struct {
31 func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
33 // NewConn returns a new Conn.
34 func NewConn(c net.Conn) *Conn {
35 cc, _ := socket.NewConn(c)
37 genericOpt: genericOpt{Conn: cc},
41 // A PacketConn represents a packet network endpoint that uses the
42 // IPv4 transport. It is used to control several IP-level socket
43 // options including multicasting. It also provides datagram based
44 // network I/O methods specific to the IPv4 and higher layer protocols
46 type PacketConn struct {
52 type dgramOpt struct {
56 func (c *dgramOpt) ok() bool { return c != nil && c.Conn != nil }
58 // SetControlMessage sets the per packet IP-level socket options.
59 func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
60 if !c.payloadHandler.ok() {
63 return setControlMessage(c.dgramOpt.Conn, &c.payloadHandler.rawOpt, cf, on)
66 // SetDeadline sets the read and write deadlines associated with the
68 func (c *PacketConn) SetDeadline(t time.Time) error {
69 if !c.payloadHandler.ok() {
72 return c.payloadHandler.PacketConn.SetDeadline(t)
75 // SetReadDeadline sets the read deadline associated with the
77 func (c *PacketConn) SetReadDeadline(t time.Time) error {
78 if !c.payloadHandler.ok() {
81 return c.payloadHandler.PacketConn.SetReadDeadline(t)
84 // SetWriteDeadline sets the write deadline associated with the
86 func (c *PacketConn) SetWriteDeadline(t time.Time) error {
87 if !c.payloadHandler.ok() {
90 return c.payloadHandler.PacketConn.SetWriteDeadline(t)
93 // Close closes the endpoint.
94 func (c *PacketConn) Close() error {
95 if !c.payloadHandler.ok() {
98 return c.payloadHandler.PacketConn.Close()
101 // NewPacketConn returns a new PacketConn using c as its underlying
103 func NewPacketConn(c net.PacketConn) *PacketConn {
104 cc, _ := socket.NewConn(c.(net.Conn))
106 genericOpt: genericOpt{Conn: cc},
107 dgramOpt: dgramOpt{Conn: cc},
108 payloadHandler: payloadHandler{PacketConn: c, Conn: cc},
113 // A RawConn represents a packet network endpoint that uses the IPv4
114 // transport. It is used to control several IP-level socket options
115 // including IPv4 header manipulation. It also provides datagram
116 // based network I/O methods specific to the IPv4 and higher layer
117 // protocols that handle IPv4 datagram directly such as OSPF, GRE.
118 type RawConn struct {
124 // SetControlMessage sets the per packet IP-level socket options.
125 func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
126 if !c.packetHandler.ok() {
127 return syscall.EINVAL
129 return setControlMessage(c.dgramOpt.Conn, &c.packetHandler.rawOpt, cf, on)
132 // SetDeadline sets the read and write deadlines associated with the
134 func (c *RawConn) SetDeadline(t time.Time) error {
135 if !c.packetHandler.ok() {
136 return syscall.EINVAL
138 return c.packetHandler.IPConn.SetDeadline(t)
141 // SetReadDeadline sets the read deadline associated with the
143 func (c *RawConn) SetReadDeadline(t time.Time) error {
144 if !c.packetHandler.ok() {
145 return syscall.EINVAL
147 return c.packetHandler.IPConn.SetReadDeadline(t)
150 // SetWriteDeadline sets the write deadline associated with the
152 func (c *RawConn) SetWriteDeadline(t time.Time) error {
153 if !c.packetHandler.ok() {
154 return syscall.EINVAL
156 return c.packetHandler.IPConn.SetWriteDeadline(t)
159 // Close closes the endpoint.
160 func (c *RawConn) Close() error {
161 if !c.packetHandler.ok() {
162 return syscall.EINVAL
164 return c.packetHandler.IPConn.Close()
167 // NewRawConn returns a new RawConn using c as its underlying
169 func NewRawConn(c net.PacketConn) (*RawConn, error) {
170 cc, err := socket.NewConn(c.(net.Conn))
175 genericOpt: genericOpt{Conn: cc},
176 dgramOpt: dgramOpt{Conn: cc},
177 packetHandler: packetHandler{IPConn: c.(*net.IPConn), Conn: cc},
179 so, ok := sockOpts[ssoHeaderPrepend]
181 return nil, errOpNoSupport
183 if err := so.SetInt(r.dgramOpt.Conn, boolint(true)); err != nil {