Custom syscall.Sockaddr

194 views
Skip to first unread message

elli...@gmail.com

unread,
Jun 24, 2016, 11:32:34 AM6/24/16
to golang-nuts
Hello All,

I am trying to set up an interface from Go to SocketCAN (https://www.kernel.org/doc/Documentation/networking/can.txt).  This implements the linux socket interface, however it is a completely separate socket type from the regular AF_INET or AF_UNIX socket types.

The sockaddr struct for SocketCAN looks like this:

struct sockaddr_can {
    sa_family_t can_family;
    int         can_ifindex;
    union {
        /* transport protocol class address info (e.g. ISOTP) */
        struct { canid_t rx_id, tx_id; } tp;

        /* reserved for future CAN protocols address information */
    } can_addr;
};

Since the union only has one possible entry right now, this is easy enough to write in Go

type CanID uint32
type sockaddrCan struct {
Family uint16
IfIndex int32
TpRxId CanID
TpTxId CanID
}

Everything is straight forward so far, but now if I want to pass this to different syscall functions ( syscall.Bind, syscall.Connect, etc.) I have to implement the syscall.Sockaddr interface, however looking in the code I see this:

type Sockaddr interface {
    sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs
}

So, finally the questions:
  1. Why is this interface private? It says that it is, but provides no rationale.
  2. Does this mean that I have to reimplement all the functions syscall functions using raw syscall.Syscall?  Or is there some clever way around this so I can use the syscall package to make a Sockaddr type that is not already defined.

Thanks in advance.

Elliot

Ian Lance Taylor

unread,
Jun 24, 2016, 1:52:26 PM6/24/16
to elli...@gmail.com, golang-nuts
If this is going to be a standard thing in future Linux kernels, I
suggest that you add support for it to the golang.org/x/sys/unix
package.

I don't actually know the answer to why Sockaddr has a private method.
I agree that it doesn't seem strictly necessary.

Ian

Elliot Morrison-Reed

unread,
Jun 24, 2016, 2:57:57 PM6/24/16
to Ian Lance Taylor, golang-nuts
SocketCAN has been part of mainline linux since kernel 2.6.25 (so getting close to a decade now), and it is pretty much the standard for all CAN bus communication on linux.

Thanks for the hint, I will look into adding support in the golang.org/x/sys/unix package.

Regards,
Elliot

Elliot Morrison-Reed

unread,
Jun 26, 2016, 2:55:18 PM6/26/16
to Ian Lance Taylor, golang-nuts
I added the SockaddrCAN type to the golang.org/x/sys/unix package, and it works great.  I create an issue with links the patch and some test code.

https://github.com/golang/go/issues/16188
Reply all
Reply to author
Forward
0 new messages