1

Is there a way to enumerate all CAN devices on Linux?

It's easy to get a list of the available adapter names plus their indices using if_nameindex but I can't see an obvious way to use that to then iterate though those to find out which are can or not (apart from an ugly hack to see if 'can' is in the name).

I suppose I could create a socket using PF_CAN and then try and bind with each of the indices. I would expect only the CAN interfaces will return successfully but again it feels like a hack.

I've also had a look at the socket ioctls in sys/ioctl.h but I can't see anything there that would be useful. I thought maybe using SIOCGIFADDR would work and I'd be able to get the sa_family field but that doesn't work either.

4

0 回答 0