tech-net archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Patch to implement SIOCGIFINDEX

On Apr 2, 2013, at 5:32 AM, Alan Barrett <> wrote:

> On Mon, 01 Apr 2013, Ty Sarna wrote:
>> The following short patch implements SIOCGIFINDEX as found on Linux
>> (and maybe elsewhere), which translates an interface name to its index.
> Why is that useful?  Once one know the index, what can or should one
> do with it?

In this case, stuff it in a sockaddr_can passed to bind().

It's kind of a crumby interface due to the "indexes change" issue,
but it's the de-facto standard. I suppose they were thinking mostly of embedded
systems where interfaces won't come and go. And, I suppose the problem can only
happen if someone yanks the interface you're trying to bind to, in which case
a name-based bind will likely fail anyway. So, I'm not sure it's really much
of a problem in practice.

Still, what I've done now is to add an "ifname" field as well. By default it's
ignored and so the interface is backwards compatible, but I allow setting the
index to -1, in which case the name is used instead.

Addresses coming out of the kernel (recvfrom, getsockname) have both the index
and the name filled.

Home | Main Index | Thread Index | Old Index