Skip to content

Commit 8d3f8e1

Browse files
committed
Improve sockaddr handling
1 parent 1fcecb0 commit 8d3f8e1

File tree

11 files changed

+177
-98
lines changed

11 files changed

+177
-98
lines changed

common/buf/buffer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func (b *Buffer) Extend(n int) []byte {
8888

8989
func (b *Buffer) Advance(from int) {
9090
b.start += from
91+
if b.end < b.start {
92+
b.end = b.start
93+
}
9194
}
9295

9396
func (b *Buffer) Truncate(to int) {

common/bufio/addr_bsd.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

common/bufio/addr_linux.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

common/bufio/addr_windows.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

common/bufio/copy_direct.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func copyWaitWithPool(originSource io.Reader, destination N.ExtendedWriter, sour
5757
notFirstTime = true
5858
if !options.IncreaseBuffer && increaseBufferAfter > 0 && n >= increaseBufferAfter {
5959
options.IncreaseBuffer = true
60-
vectorisedReadWaiter, isVectorisedReadWaiter := CreateVectorisedReadWaiter(N.UnwrapReader(source))
61-
vectorisedWriter, isVectorisedWriter := CreateVectorisedWriter(N.UnwrapWriter(destination))
60+
vectorisedReadWaiter, isVectorisedReadWaiter := CreateVectorisedReadWaiter(source)
61+
vectorisedWriter, isVectorisedWriter := CreateVectorisedWriter(destination)
6262
if !isVectorisedReadWaiter || !isVectorisedWriter {
6363
readWaiter.InitializeReadWaiter(options)
6464
continue

common/bufio/vectorised_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (w *SyscallVectorisedPacketWriter) WriteVectorisedPacket(buffers []*buf.Buf
8383
var innerErr unix.Errno
8484
err := w.rawConn.Write(func(fd uintptr) (done bool) {
8585
var msg unix.Msghdr
86-
name, nameLen := ToSockaddr(destination.AddrPort())
86+
name, nameLen := M.AddrPortToRawSockaddr(destination.AddrPort())
8787
msg.Name = (*byte)(name)
8888
msg.Namelen = nameLen
8989
if len(iovecList) > 0 {

common/bufio/vectorised_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (w *SyscallVectorisedPacketWriter) WriteVectorisedPacket(buffers []*buf.Buf
5353
var n uint32
5454
var innerErr error
5555
err := w.rawConn.Write(func(fd uintptr) (done bool) {
56-
name, nameLen := ToSockaddr(destination.AddrPort())
56+
name, nameLen := M.AddrPortToRawSockaddr(destination.AddrPort())
5757
var bufs *windows.WSABuf
5858
if len(iovecList) > 0 {
5959
bufs = &iovecList[0]

common/metadata/addr_bsd.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
2+
3+
package metadata
4+
5+
import (
6+
"encoding/binary"
7+
"net/netip"
8+
"unsafe"
9+
10+
"golang.org/x/sys/unix"
11+
)
12+
13+
func AddrPortToRawSockaddr(addrPort netip.AddrPort) (name unsafe.Pointer, nameLen uint32) {
14+
if addrPort.Addr().Is4() {
15+
var sa unix.RawSockaddrInet4
16+
sa.Len = unix.SizeofSockaddrInet4
17+
sa.Family = unix.AF_INET
18+
sa.Addr = addrPort.Addr().As4()
19+
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&sa.Port))[:], addrPort.Port())
20+
return unsafe.Pointer(&sa), unix.SizeofSockaddrInet4
21+
} else {
22+
var sa unix.RawSockaddrInet6
23+
sa.Len = unix.SizeofSockaddrInet6
24+
sa.Family = unix.AF_INET6
25+
sa.Addr = addrPort.Addr().As16()
26+
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&sa.Port))[:], addrPort.Port())
27+
return unsafe.Pointer(&sa), unix.SizeofSockaddrInet6
28+
}
29+
}

common/metadata/addr_linux.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package metadata
2+
3+
import (
4+
"encoding/binary"
5+
"net/netip"
6+
"unsafe"
7+
8+
"golang.org/x/sys/unix"
9+
)
10+
11+
func AddrPortToRawSockaddr(addrPort netip.AddrPort) (name unsafe.Pointer, nameLen uint32) {
12+
if addrPort.Addr().Is4() {
13+
var sa unix.RawSockaddrInet4
14+
sa.Family = unix.AF_INET
15+
sa.Addr = addrPort.Addr().As4()
16+
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&sa.Port))[:], addrPort.Port())
17+
return unsafe.Pointer(&sa), unix.SizeofSockaddrInet4
18+
} else {
19+
var sa unix.RawSockaddrInet6
20+
sa.Family = unix.AF_INET6
21+
sa.Addr = addrPort.Addr().As16()
22+
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&sa.Port))[:], addrPort.Port())
23+
return unsafe.Pointer(&sa), unix.SizeofSockaddrInet6
24+
}
25+
}

common/metadata/addr_unix.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build unix
2+
3+
package metadata
4+
5+
import (
6+
"net/netip"
7+
"unsafe"
8+
9+
"golang.org/x/sys/unix"
10+
)
11+
12+
func AddrPortFromSockaddr(sa unix.Sockaddr) netip.AddrPort {
13+
switch addr := sa.(type) {
14+
case *unix.SockaddrInet4:
15+
return netip.AddrPortFrom(netip.AddrFrom4(addr.Addr), uint16(addr.Port))
16+
case *unix.SockaddrInet6:
17+
return netip.AddrPortFrom(netip.AddrFrom16(addr.Addr), uint16(addr.Port))
18+
default:
19+
return netip.AddrPort{}
20+
}
21+
}
22+
23+
func AddrPortToSockaddr(addrPort netip.AddrPort) unix.Sockaddr {
24+
if addrPort.Addr().Is4() {
25+
return &unix.SockaddrInet4{
26+
Port: int(addrPort.Port()),
27+
Addr: addrPort.Addr().As4(),
28+
}
29+
} else {
30+
return &unix.SockaddrInet6{
31+
Port: int(addrPort.Port()),
32+
Addr: addrPort.Addr().As16(),
33+
}
34+
}
35+
}
36+
37+
func AddrPortFromRawSockaddr(sa *unix.RawSockaddr) netip.AddrPort {
38+
switch sa.Family {
39+
case unix.AF_INET:
40+
sa4 := (*unix.RawSockaddrInet4)(unsafe.Pointer(sa))
41+
return netip.AddrPortFrom(netip.AddrFrom4(sa4.Addr), sa4.Port)
42+
case unix.AF_INET6:
43+
sa6 := (*unix.RawSockaddrInet6)(unsafe.Pointer(sa))
44+
return netip.AddrPortFrom(netip.AddrFrom16(sa6.Addr), sa6.Port)
45+
default:
46+
return netip.AddrPort{}
47+
}
48+
}

0 commit comments

Comments
 (0)