golang.org/x/sys/unix: SockAddrInet4 doesn't implement SockAddr?

200 views
Skip to first unread message

kinoshita...@gmail.com

unread,
Jun 27, 2016, 12:04:56 PM6/27/16
to golang-nuts
Hi everyone,

I am sorry for my newbie question.

I want to study law-level socket programming in go using system calls (such as to implement ARP).

For my first trial I have tested the code below named socket.go on Mac OS X 10.11.5 (darwin amd64),
but I got the following error:

# go run packet.go
./socket.go:17: cannot use sa (type unix.SockaddrInet4) as type unix.Sockaddr in argument to unix.Connect:
        unix.SockaddrInet4 does not implement unix.Sockaddr (unix.sockaddr method has pointer receiver)

Whereas $GOPATH/src/golang.org/x/sys/unix/syscall_darwin.go seems not to implement sockaddr for
SockaddrInet4, $GOPATH/src/golang.org/x/sys/unix/syscall_linux.go implements sockaddr for SockaddrInet4.
So I tested the same code on Linux VM (Arch Linux), but I got the same error.

My questions:
- Why my code causes the error in Linux?
- Is it impossible to use unix.SockaddrInet4 as type unix.Sockaddr in darwin?

Thank you.

----
packet.go
----
package main

import (
"fmt"
"os"

)

func main() {
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_RAW, 0)
if err != nil {
fmt.Println("Can't open the socket:", err)
os.Exit(1)
}
sa := unix.SockaddrInet4{Port: 0, Addr: [4]byte{127, 0, 0, 1}}
err = unix.Connect(fd, sa)
if err != nil {
fmt.Println("Can't connect:", err)
os.Exit(1)
}
}

Michael Jones

unread,
Jun 27, 2016, 12:17:14 PM6/27/16
to kinoshita...@gmail.com, golang-nuts
The compilation error is because Connect wants a pointer. Do this instead… 

sa := &unix.SockaddrInet4{Port: 0, Addr: [4]byte{127, 0, 0, 1}}

Michael Jones

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Minoru KINOSHITA

unread,
Jun 27, 2016, 1:19:17 PM6/27/16
to Michael Jones, golang-nuts
Oh, I can successfully compiled my code both on Darwin and Linux.  Thank you!

--
KINOSHITA Minoru
kinoshita...@gmail.com

2016年6月28日(火) 1:17 Michael Jones <michae...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages