Skip to content

x/sys/unix: expose Setsockopt for CAN Filtering #29638

@iSchluff

Description

@iSchluff

Currently the support for AF_CAN sockets in x/sys/unix is not very useful, because kernel message filtering is not supported due to missing availability of the relevant sockopt. This sadly defeats the purpose of using go for CAN over other languages.

It also can not be worked around easily, as the unsafe setsockopt is only available in private. (Which is reasonable, but one needs unsafe to do ioctl SIOCGIFINDEX anyway when working with CAN sockets)

This comment comment on the original Request for AF_CAN sockets hints at the issue.

So I can see this being solved either with exposition of raw Setsockopt with unsafe pointers or introduction of a separate SetSockoptCanRawFilter function:

type CanFilter struct {
    Id   uint32
    Mask uint32
}

func SetsockoptCanRawFilter(fd, level, opt int, rfilter []CanFilter) error {
    ...
}

Which could be used as follows:

func main() {
    ...
    fd, _ := unix.Socket(unix.AF_CAN, unix.SOCK_RAW, unix.CAN_RAW)
    rfilter := make([]unix.CanFilter, 2)
        rfilter[0].Id = 0x123
        rfilter[0].Mask = unix.CAN_SFF_MASK
        rfilter[1].Id = 0x124
        rfilter[1].Mask = unix.CAN_SFF_MASK

    SetsockoptCanRawFilter(fd, unix.SOL_CAN_RAW, unix.CAN_RAW_FILTER, rfilter)
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.OS-Linux

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions