-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR: RawSockaddr validation,
.As(
style
Signed-off-by: Hamza El-Saawy <[email protected]>
- Loading branch information
Showing
4 changed files
with
41 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,20 @@ | ||
package socket | ||
|
||
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: helsaawy - replace this with generics, along with GetSockName and co. | ||
|
||
// RawSockaddr allows structs to be used with Bind and ConnectEx. The | ||
// RawSockaddr allows structs to be used with [Bind] and [ConnectEx]. The | ||
// struct must meet the Win32 sockaddr requirements specified here: | ||
// https://docs.microsoft.com/en-us/windows/win32/winsock/sockaddr-2 | ||
// | ||
// Specifically, the struct size must be least larger than an int16 (unsigned short) | ||
// for the address family. | ||
type RawSockaddr interface { | ||
// Sockaddr returns a pointer to the RawSockaddr and the length of the struct. | ||
// Sockaddr returns a pointer to the RawSockaddr and its struct size, allowing | ||
// for the RawSockaddr's data to be overwritten by syscalls (if necessary). | ||
// | ||
// It is the callers responsibility to validate that the values are valid; invalid | ||
// pointers or size can cause a panic. | ||
Sockaddr() (unsafe.Pointer, int32, 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, n int32) error { | ||
if ptr == nil { | ||
return fmt.Errorf("pointer is %p: %w", ptr, ErrInvalidPointer) | ||
} | ||
if n < 1 { | ||
return fmt.Errorf("buffer size %d < 1: %w", n, ErrBufferSize) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters