Skip to content

Commit 3b5950a

Browse files
use a netip.Addr instead of a net.IP in the packetInfo struct
1 parent b27d114 commit 3b5950a

File tree

5 files changed

+36
-33
lines changed

5 files changed

+36
-33
lines changed

send_conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *sconn) LocalAddr() net.Addr {
6060
if c.info != nil {
6161
if udpAddr, ok := addr.(*net.UDPAddr); ok {
6262
addrCopy := *udpAddr
63-
addrCopy.IP = c.info.addr
63+
addrCopy.IP = c.info.addr.AsSlice()
6464
addr = &addrCopy
6565
}
6666
}

sys_conn_no_oob.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
package quic
44

5-
import "net"
5+
import (
6+
"net"
7+
"net/netip"
8+
)
69

710
func newConn(c net.PacketConn, supportsDF bool) (*basicConn, error) {
811
return &basicConn{PacketConn: c, supportsDF: supportsDF}, nil
@@ -12,7 +15,7 @@ func inspectReadBuffer(any) (int, error) { return 0, nil }
1215
func inspectWriteBuffer(any) (int, error) { return 0, nil }
1316

1417
type packetInfo struct {
15-
addr net.IP
18+
addr netip.Addr
1619
}
1720

1821
func (i *packetInfo) OOB() []byte { return nil }

sys_conn_oob.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"net"
10+
"net/netip"
1011
"syscall"
1112
"time"
1213

@@ -173,8 +174,7 @@ func (c *oobConn) ReadPacket() (receivedPacket, error) {
173174

174175
data := msg.OOB[:msg.NN]
175176
var ecn protocol.ECN
176-
var destIP net.IP
177-
var ifIndex uint32
177+
var info *packetInfo
178178
for len(data) > 0 {
179179
hdr, body, remainder, err := unix.ParseOneSocketControlMessage(data)
180180
if err != nil {
@@ -191,15 +191,16 @@ func (c *oobConn) ReadPacket() (receivedPacket, error) {
191191
// struct in_addr ipi_addr; /* Header Destination
192192
// address */
193193
// };
194-
ip := make([]byte, 4)
194+
info = &packetInfo{}
195+
var ip [4]byte
195196
if len(body) == 12 {
196-
ifIndex = binary.LittleEndian.Uint32(body)
197-
copy(ip, body[8:12])
197+
copy(ip[:], body[8:12])
198+
info.ifIndex = binary.LittleEndian.Uint32(body)
198199
} else if len(body) == 4 {
199200
// FreeBSD
200-
copy(ip, body)
201+
copy(ip[:], body)
201202
}
202-
destIP = net.IP(ip)
203+
info.addr = netip.AddrFrom4(ip)
203204
}
204205
}
205206
if hdr.Level == unix.IPPROTO_IPV6 {
@@ -212,22 +213,17 @@ func (c *oobConn) ReadPacket() (receivedPacket, error) {
212213
// unsigned int ipi6_ifindex; /* send/recv interface index */
213214
// };
214215
if len(body) == 20 {
215-
ip := make([]byte, 16)
216-
copy(ip, body[:16])
217-
destIP = net.IP(ip)
218-
ifIndex = binary.LittleEndian.Uint32(body[16:])
216+
var ip [16]byte
217+
copy(ip[:], body[:16])
218+
info = &packetInfo{
219+
addr: netip.AddrFrom16(ip),
220+
ifIndex: binary.LittleEndian.Uint32(body[16:]),
221+
}
219222
}
220223
}
221224
}
222225
data = remainder
223226
}
224-
var info *packetInfo
225-
if destIP != nil {
226-
info = &packetInfo{
227-
addr: destIP,
228-
ifIndex: ifIndex,
229-
}
230-
}
231227
return receivedPacket{
232228
remoteAddr: msg.Addr,
233229
rcvTime: time.Now(),
@@ -265,32 +261,34 @@ func (c *oobConn) capabilities() connCapabilities {
265261
}
266262

267263
type packetInfo struct {
268-
addr net.IP
264+
addr netip.Addr
269265
ifIndex uint32
270266
}
271267

272268
func (info *packetInfo) OOB() []byte {
273269
if info == nil {
274270
return nil
275271
}
276-
if ip4 := info.addr.To4(); ip4 != nil {
272+
if info.addr.Is4() {
273+
ip := info.addr.As4()
277274
// struct in_pktinfo {
278275
// unsigned int ipi_ifindex; /* Interface index */
279276
// struct in_addr ipi_spec_dst; /* Local address */
280277
// struct in_addr ipi_addr; /* Header Destination address */
281278
// };
282279
cm := ipv4.ControlMessage{
283-
Src: ip4,
280+
Src: ip[:],
284281
IfIndex: int(info.ifIndex),
285282
}
286283
return cm.Marshal()
287-
} else if len(info.addr) == 16 {
284+
} else if info.addr.Is6() {
285+
ip := info.addr.As16()
288286
// struct in6_pktinfo {
289287
// struct in6_addr ipi6_addr; /* src/dst IPv6 address */
290288
// unsigned int ipi6_ifindex; /* send/recv interface index */
291289
// };
292290
cm := ipv6.ControlMessage{
293-
Src: info.addr,
291+
Src: ip[:],
294292
IfIndex: int(info.ifIndex),
295293
}
296294
return cm.Marshal()

sys_conn_oob_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ var _ = Describe("OOB Conn Test", func() {
155155
Expect(p.data).To(Equal([]byte("foobar")))
156156
Expect(p.remoteAddr).To(Equal(sentFrom))
157157
Expect(p.info).To(Not(BeNil()))
158-
Expect(p.info.addr.To4()).To(Equal(ip))
158+
Expect(net.IP(p.info.addr.AsSlice())).To(Equal(ip))
159159
})
160160

161161
It("reads packet info on IPv6", func() {
@@ -173,7 +173,7 @@ var _ = Describe("OOB Conn Test", func() {
173173
Expect(p.data).To(Equal([]byte("foobar")))
174174
Expect(p.remoteAddr).To(Equal(sentFrom))
175175
Expect(p.info).To(Not(BeNil()))
176-
Expect(p.info.addr).To(Equal(ip))
176+
Expect(net.IP(p.info.addr.AsSlice())).To(Equal(ip))
177177
})
178178

179179
It("reads packet info on a connection that supports both IPv4 and IPv6", func() {
@@ -182,14 +182,16 @@ var _ = Describe("OOB Conn Test", func() {
182182
port := conn.LocalAddr().(*net.UDPAddr).Port
183183

184184
// IPv4
185-
ip4 := net.ParseIP("127.0.0.1").To4()
185+
ip4 := net.ParseIP("127.0.0.1")
186186
sendPacket("udp4", &net.UDPAddr{IP: ip4, Port: port})
187187

188188
var p receivedPacket
189189
Eventually(packetChan).Should(Receive(&p))
190190
Expect(utils.IsIPv4(p.remoteAddr.(*net.UDPAddr).IP)).To(BeTrue())
191191
Expect(p.info).To(Not(BeNil()))
192-
Expect(p.info.addr.To4()).To(Equal(ip4))
192+
Expect(p.info.addr.Is4In6() || p.info.addr.Is4()).To(BeTrue())
193+
ip := p.info.addr.As4()
194+
Expect(net.IP(ip[:])).To(Equal(ip4.To4()))
193195

194196
// IPv6
195197
ip6 := net.ParseIP("::1")
@@ -198,7 +200,7 @@ var _ = Describe("OOB Conn Test", func() {
198200
Eventually(packetChan).Should(Receive(&p))
199201
Expect(utils.IsIPv4(p.remoteAddr.(*net.UDPAddr).IP)).To(BeFalse())
200202
Expect(p.info).To(Not(BeNil()))
201-
Expect(p.info.addr).To(Equal(ip6))
203+
Expect(net.IP(p.info.addr.AsSlice())).To(Equal(ip6))
202204
})
203205
})
204206

sys_conn_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package quic
44

55
import (
6-
"net"
6+
"net/netip"
77
"syscall"
88

99
"golang.org/x/sys/windows"
@@ -36,7 +36,7 @@ func inspectWriteBuffer(c syscall.RawConn) (int, error) {
3636
}
3737

3838
type packetInfo struct {
39-
addr net.IP
39+
addr netip.Addr
4040
}
4141

4242
func (i *packetInfo) OOB() []byte { return nil }

0 commit comments

Comments
 (0)