Added postinst script for debian packaging.
[dabba.git] / libdabba / nic.c
blob1039adaeffcbb73b8d7246f8afb141e7bfa6324c
1 /**
2 * \file nic.c
3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2011
4 * \date 2011
5 */
7 /* __LICENSE_HEADER_BEGIN__ */
9 /*
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
20 * for more details.
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__ */
30 #include <assert.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
37 #include <net/if.h>
39 #include <libdabba/nic.h>
40 #include <libdabba/strlcpy.h>
42 /**
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)
55 int ret;
56 int sock;
57 struct ifreq ethreq;
59 assert(dev);
60 assert(index);
62 if (strcmp(dev, ANY_INTERFACE) == 0) {
63 *index = 0;
64 return (0);
67 sock = socket(AF_INET, SOCK_DGRAM, 0);
69 if (sock < 0)
70 return (errno);
72 memset(&ethreq, 0, sizeof(ethreq));
73 strlcpy(ethreq.ifr_name, dev, sizeof(ethreq.ifr_name));
75 ret = ioctl(sock, SIOCGIFINDEX, &ethreq);
77 close(sock);
79 if (ret < 0)
80 return (errno);
82 *index = ethreq.ifr_ifindex;
84 return (0);
87 /**
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;
102 struct ifreq ethreq;
103 int ret, sock;
105 assert(index >= 0);
106 assert(dev);
107 assert(dev_len >= IFNAMSIZ);
109 if (index == 0) {
110 strlcpy(dev, alldev, dev_len);
111 return (0);
114 sock = socket(AF_INET, SOCK_DGRAM, 0);
116 if (sock < 0)
117 return (errno);
119 memset(&ethreq, 0, sizeof(ethreq));
120 ethreq.ifr_ifindex = index;
122 ret = ioctl(sock, SIOCGIFNAME, &ethreq);
124 close(sock);
126 if (ret < 0)
127 return (errno);
129 strlcpy(dev, ethreq.ifr_name, dev_len);
131 return (0);