Using go on Windows I have a syscall.Handle called sock that I want to bind to an address specified in a syscall.Sockaddr structure called "sa".
How do I fill out the syscall.Sockaddr structure so that I can later use it with syscall.Bind?
BTW, sock is a raw socket.
var sa syscall.Sockaddr // how to fill out this structure with my ip address???
e := syscall.Bind(sock, sa)
if e != nil {
fmt.Println("bind error", e)
}
A good rule of thumb when you're unsure of something in go is "check the source", especially where the syscall stuff is concerned. I suspect the lack of documentation is semi-intentional because as previously mentioned, it's not the recommended interface - better to use the net package for a portable solution :)
Once you get to the source ($GOROOT/src/pkg/syscall/syscall_windows.go), you'll notice that Sockaddr isn't a struct at all:
type Sockaddr interface {
sockaddr() (ptr uintptr, len int32, err error) // lowercase; only we can define Sockaddrs
}
So lets look for structs that implement this interface:
func (sa *SockaddrInet4) sockaddr() (uintptr, int32, error) {
func (sa *SockaddrInet6) sockaddr() (uintptr, int32, error) {
func (sa *SockaddrUnix) sockaddr() (uintptr, int32, error) {
The Inet6 and Unix implementations are just stubs in the version I'm looking at (1.0.2), but SockaddrInet4 is ready to go:
type SockaddrInet4 struct {
Port int
Addr [4]byte
raw RawSockaddrInet4
}
Just need to fill in Port and Addr.