...
1
2
3
4
5 package ipv4
6
7 import (
8 "net"
9 "time"
10
11 "golang.org/x/net/internal/socket"
12 )
13
14
15
16
17
18
19
20
21
22 type Conn struct {
23 genericOpt
24 }
25
26 type genericOpt struct {
27 *socket.Conn
28 }
29
30 func (c *genericOpt) ok() bool { return c != nil && c.Conn != nil }
31
32
33 func NewConn(c net.Conn) *Conn {
34 cc, _ := socket.NewConn(c)
35 return &Conn{
36 genericOpt: genericOpt{Conn: cc},
37 }
38 }
39
40
41
42
43
44
45 type PacketConn struct {
46 genericOpt
47 dgramOpt
48 payloadHandler
49 }
50
51 type dgramOpt struct {
52 *socket.Conn
53 }
54
55 func (c *dgramOpt) ok() bool { return c != nil && c.Conn != nil }
56
57
58 func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
59 if !c.payloadHandler.ok() {
60 return errInvalidConn
61 }
62 return setControlMessage(c.dgramOpt.Conn, &c.payloadHandler.rawOpt, cf, on)
63 }
64
65
66
67 func (c *PacketConn) SetDeadline(t time.Time) error {
68 if !c.payloadHandler.ok() {
69 return errInvalidConn
70 }
71 return c.payloadHandler.PacketConn.SetDeadline(t)
72 }
73
74
75
76 func (c *PacketConn) SetReadDeadline(t time.Time) error {
77 if !c.payloadHandler.ok() {
78 return errInvalidConn
79 }
80 return c.payloadHandler.PacketConn.SetReadDeadline(t)
81 }
82
83
84
85 func (c *PacketConn) SetWriteDeadline(t time.Time) error {
86 if !c.payloadHandler.ok() {
87 return errInvalidConn
88 }
89 return c.payloadHandler.PacketConn.SetWriteDeadline(t)
90 }
91
92
93 func (c *PacketConn) Close() error {
94 if !c.payloadHandler.ok() {
95 return errInvalidConn
96 }
97 return c.payloadHandler.PacketConn.Close()
98 }
99
100
101
102 func NewPacketConn(c net.PacketConn) *PacketConn {
103 cc, _ := socket.NewConn(c.(net.Conn))
104 p := &PacketConn{
105 genericOpt: genericOpt{Conn: cc},
106 dgramOpt: dgramOpt{Conn: cc},
107 payloadHandler: payloadHandler{PacketConn: c, Conn: cc},
108 }
109 return p
110 }
111
112
113
114
115
116
117 type RawConn struct {
118 genericOpt
119 dgramOpt
120 packetHandler
121 }
122
123
124 func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
125 if !c.packetHandler.ok() {
126 return errInvalidConn
127 }
128 return setControlMessage(c.dgramOpt.Conn, &c.packetHandler.rawOpt, cf, on)
129 }
130
131
132
133 func (c *RawConn) SetDeadline(t time.Time) error {
134 if !c.packetHandler.ok() {
135 return errInvalidConn
136 }
137 return c.packetHandler.IPConn.SetDeadline(t)
138 }
139
140
141
142 func (c *RawConn) SetReadDeadline(t time.Time) error {
143 if !c.packetHandler.ok() {
144 return errInvalidConn
145 }
146 return c.packetHandler.IPConn.SetReadDeadline(t)
147 }
148
149
150
151 func (c *RawConn) SetWriteDeadline(t time.Time) error {
152 if !c.packetHandler.ok() {
153 return errInvalidConn
154 }
155 return c.packetHandler.IPConn.SetWriteDeadline(t)
156 }
157
158
159 func (c *RawConn) Close() error {
160 if !c.packetHandler.ok() {
161 return errInvalidConn
162 }
163 return c.packetHandler.IPConn.Close()
164 }
165
166
167
168 func NewRawConn(c net.PacketConn) (*RawConn, error) {
169 cc, err := socket.NewConn(c.(net.Conn))
170 if err != nil {
171 return nil, err
172 }
173 r := &RawConn{
174 genericOpt: genericOpt{Conn: cc},
175 dgramOpt: dgramOpt{Conn: cc},
176 packetHandler: packetHandler{IPConn: c.(*net.IPConn), Conn: cc},
177 }
178 so, ok := sockOpts[ssoHeaderPrepend]
179 if !ok {
180 return nil, errNotImplemented
181 }
182 if err := so.SetInt(r.dgramOpt.Conn, boolint(true)); err != nil {
183 return nil, err
184 }
185 return r, nil
186 }
187
View as plain text