3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2011
7 /* __LICENSE_HEADER_BEGIN__ */
10 * Copyright (C) 2011 Emmanuel Roullit <emmanuel.roullit@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
28 /* __LICENSE_HEADER_END__ */
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
39 #include <libdabba/nic.h>
40 #include <libdabba/strlcpy.h>
43 * \brief Get the interface index of a specific interface
44 * \param[in] dev Device name
45 * \param[out] index Interface index of the interface
46 * \return 0 on success, errno from \c socket(2), \c ioctl(2)
47 * \return or \c close(2) on failure
49 * This function queries the kernel about the interface
50 * index related to the interface name.
53 int devname_to_ifindex(const char *const dev
, int *index
)
62 if (strcmp(dev
, ANY_INTERFACE
) == 0) {
67 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
72 memset(ðreq
, 0, sizeof(ethreq
));
73 strlcpy(ethreq
.ifr_name
, dev
, sizeof(ethreq
.ifr_name
));
75 ret
= ioctl(sock
, SIOCGIFINDEX
, ðreq
);
82 *index
= ethreq
.ifr_ifindex
;
88 * \brief Get the interface name from an interface index
89 * \param[in] index Interface index of the interface
90 * \param[out] dev Device name
91 * \param[in] dev_len Device name buffer length
92 * \return 0 on success, errno from \c socket(2), \c ioctl(2)
93 * \return or \c close(2) on failure
95 * This function queries the kernel about the interface
96 * index related to the interface name.
99 int ifindex_to_devname(const int index
, char *dev
, size_t dev_len
)
101 const char alldev
[] = ANY_INTERFACE
;
107 assert(dev_len
>= IFNAMSIZ
);
110 strlcpy(dev
, alldev
, dev_len
);
114 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
119 memset(ðreq
, 0, sizeof(ethreq
));
120 ethreq
.ifr_ifindex
= index
;
122 ret
= ioctl(sock
, SIOCGIFNAME
, ðreq
);
129 strlcpy(dev
, ethreq
.ifr_name
, dev_len
);