-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
221 lines (208 loc) · 5.12 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2014 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ipoam
import (
"fmt"
"net"
"syscall"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
const (
// See golang.org/x/net/internal/iana.
ianaProtocolIP = 0
ianaProtocolICMP = 1
ianaProtocolUDP = 17
ianaProtocolIPv6 = 41
ianaProtocolIPv6ICMP = 58
)
// A conn represents a connection endpoint.
type conn struct {
protocol int // protocol number
rawSocket bool // true if c is a raw socket
ip net.IP // local address of c
sport int // source port of c
c net.PacketConn // net.IPConn, net.UDPConn or icmp.PacketConn
r4 *ipv4.RawConn
p4 *ipv4.PacketConn
p6 *ipv6.PacketConn
}
func (c *conn) close() error {
if c == nil || c.c == nil {
return syscall.EINVAL
}
return c.c.Close()
}
func (c *conn) readFrom(b []byte) ([]byte, interface{}, interface{}, net.Addr, error) {
if !c.rawSocket {
n, peer, err := c.c.ReadFrom(b)
if err != nil {
return nil, nil, nil, nil, err
}
return b[:n], nil, nil, peer, nil
}
switch c.protocol {
case ianaProtocolICMP:
if c.r4 != nil {
h, p, cm, err := c.r4.ReadFrom(b)
if err != nil {
return nil, nil, nil, nil, err
}
return p, h, cm, &net.IPAddr{IP: cm.Src}, nil
}
n, cm, peer, err := c.p4.ReadFrom(b)
return b[:n], nil, cm, peer, err
case ianaProtocolIPv6ICMP:
n, cm, peer, err := c.p6.ReadFrom(b)
if err != nil {
return nil, nil, nil, nil, err
}
return b[:n], nil, cm, peer, err
default:
return nil, nil, nil, nil, fmt.Errorf("unknown protocol: %d", c.protocol)
}
}
func (c *conn) setup(maint bool) {
switch la := c.c.LocalAddr().(type) {
case *net.UDPAddr:
c.ip = la.IP
c.sport = la.Port
case *net.IPAddr:
c.rawSocket = true
c.ip = la.IP
}
if c.rawSocket {
switch c.protocol {
case ianaProtocolICMP:
if maint {
c.r4, _ = ipv4.NewRawConn(c.c)
} else {
c.p4 = ipv4.NewPacketConn(c.c)
}
case ianaProtocolIPv6ICMP:
c.p6 = ipv6.NewPacketConn(c.c)
}
} else {
switch c.protocol {
case ianaProtocolICMP, ianaProtocolIPv6ICMP:
if c.ip.To4() != nil {
c.p4 = c.c.(*icmp.PacketConn).IPv4PacketConn()
}
if c.ip.To16() != nil && c.ip.To4() == nil {
c.p6 = c.c.(*icmp.PacketConn).IPv6PacketConn()
}
case ianaProtocolUDP:
if c.ip.To4() != nil {
c.p4 = ipv4.NewPacketConn(c.c)
}
if c.ip.To16() != nil && c.ip.To4() == nil {
c.p6 = ipv6.NewPacketConn(c.c)
}
}
}
}
func (c *conn) writeTo(b []byte, dst net.Addr, ifi *net.Interface) (int, error) {
if !c.rawSocket {
return c.c.WriteTo(b, dst)
}
switch c.protocol {
case ianaProtocolICMP:
var cm *ipv4.ControlMessage
if ifi != nil {
cm = &ipv4.ControlMessage{IfIndex: ifi.Index}
}
if c.r4 != nil {
h := &ipv4.Header{
Version: ipv4.Version,
Len: ipv4.HeaderLen,
TotalLen: ipv4.HeaderLen + len(b),
Protocol: ianaProtocolICMP,
Dst: dst.(*net.IPAddr).IP,
}
if err := c.r4.WriteTo(h, b, cm); err != nil {
return 0, err
}
return len(b), nil
}
return c.p4.WriteTo(b, cm, dst)
case ianaProtocolIPv6ICMP:
var cm *ipv6.ControlMessage
if ifi != nil {
cm = &ipv6.ControlMessage{IfIndex: ifi.Index}
}
return c.p6.WriteTo(b, cm, dst)
default:
return 0, fmt.Errorf("unknown protocol: %d", c.protocol)
}
}
func newProbeConn(network, address string) (*conn, error) {
var err error
var c *conn
switch network {
case "ip4:icmp", "ip4:1", "ip6:ipv6-icmp", "ip6:58":
c, err = newICMPConn(network, address)
case "udp", "udp4", "udp6":
c, err = newUDPConn(network, address)
default:
return nil, net.UnknownNetworkError(network)
}
if err != nil {
return nil, err
}
c.setup(false)
return c, nil
}
func newMaintConn(network, address string) (*conn, error) {
var err error
var c *conn
switch network {
case "ip4:icmp", "ip4:1", "ip6:ipv6-icmp", "ip6:58", "ip4:icmp+ip6:ipv6-icmp":
c, err = newICMPConn(network, address)
default:
return nil, net.UnknownNetworkError(network)
}
if err != nil {
return nil, err
}
c.setup(true)
return c, nil
}
func newICMPConn(network, address string) (*conn, error) {
ip := net.ParseIP(address)
if ip == nil || ip.IsUnspecified() {
switch network {
case "ip4:icmp", "ip4:1", "ip4:icmp+ip6:ipv6-icmp":
ip = net.IPv4zero
case "ip6:ipv6-icmp", "ip6:58":
ip = net.IPv6unspecified
}
}
var conn conn
var networks []string
if ip.To4() != nil {
networks = []string{"ip4:icmp", "udp4"}
conn.protocol = ianaProtocolICMP
}
if ip.To16() != nil && ip.To4() == nil {
networks = []string{"ip6:ipv6-icmp", "udp6"}
conn.protocol = ianaProtocolIPv6ICMP
}
var firstErr, err error
conn.c, firstErr = net.ListenPacket(networks[0], address)
if firstErr != nil {
conn.c, err = icmp.ListenPacket(networks[1], address)
if err != nil {
return nil, firstErr
}
}
return &conn, nil
}
func newUDPConn(network, address string) (*conn, error) {
c, err := net.ListenPacket(network, address)
if err != nil {
return nil, err
}
return &conn{protocol: ianaProtocolUDP, c: c}, nil
}