/
rawaddr.go
41 lines (34 loc) · 1.2 KB
/
rawaddr.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
package sockets
import (
"errors"
"fmt"
"unsafe"
)
// todo: should these be custom types to store the desired/actual size and addr family?
var (
ErrBufferSize = errors.New("buffer size")
ErrInvalidPointer = errors.New("invalid pointer")
ErrAddrFamily = errors.New("address family")
)
// todo: replace this with generics, along with GetSockName and co.
// RawSockaddr allows structs to be used with Bind and ConnectEx. The
// struct must meet the Wind32 sockaddr requirements specified here:
// https://docs.microsoft.com/en-us/windows/win32/winsock/sockaddr-2
type RawSockaddr interface {
// Sockaddr returns a pointer to the RawSockaddr and the length of the struct.
Sockaddr() (ptr unsafe.Pointer, len int32, err error)
// FromBytes populates the RawsockAddr with the data in the byte array.
// Implementers should check the buffer is correctly sized and the address family
// is appropriate.
// Receivers should be pointers.
FromBytes([]byte) error
}
func validateSockAddr(ptr unsafe.Pointer, len int32) error {
if ptr == nil {
return fmt.Errorf("pointer is %p: %w", ptr, ErrInvalidPointer)
}
if len < 1 {
return fmt.Errorf("buffer size %d < 1: %w", len, ErrBufferSize)
}
return nil
}