Hello!
I am currently trying to get a list of all CAN interfaces on my machine.
Im am using ioctl() with SIOCGIFCONF for this purpose as already
mentioned in a previous post on this mailinglist
(http://old.nabble.com/Enumerating-a-list-of-CAN-interfaces-td27603207.html).

I found out that it makes no difference with which protocol I create the
socket - I always get all interfaces like lo and eth0. My problem is
that there also is a vcan0 which is not listed. Does this method only
work for nonvirtual CAN interfaces and is there any possibility to get
them listed too? Unfortunately I don't have a "real" CAN device for
testing...

Here is the code I am using:

struct ifreq *ifr;
struct ifconf ifc;
int sock, i;
int numif;

memset(&ifc, 0, sizeof(ifc));
ifc.ifc_ifcu.ifcu_req = NULL;
ifc.ifc_len = 0;

/* Create a socket to be able to use ioctl */
if ((sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
        perror("socket");
        exit(1);
}

/* First call to determin how much memory will be needed */
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
        perror("ioctl");
        exit(2);
}
                
/* Malloc the necessary memory */   
if ((ifr = malloc(ifc.ifc_len)) == NULL) {
        perror("malloc");
        exit(3);
}
ifc.ifc_ifcu.ifcu_req = ifr;

/* Request the infomation */
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
        perror("ioctl2");
        exit(4);
}
close(sock);

numif = ifc.ifc_len / sizeof(struct ifreq);
for (i = 0; i < numif; i++) {
        struct ifreq *r = &ifr[i];
        printf("%-8s\n", r->ifr_name);
}

free(ifr);

Regards,
Jan-Niklas

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Socketcan-users mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/socketcan-users

Reply via email to