docs: Tidy up the CODING file.
[dabba.git] / dabbad / list.c
bloba4452ffd30f6c8e27a17539cd09d9872e0d6fce0
1 /**
2 * \file list.c
3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2012
4 * \date 2012
5 */
7 /* __LICENSE_HEADER_BEGIN__ */
9 /*
10 * Copyright (C) 2012 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 <stdio.h>
31 #include <net/if.h>
32 #include <sys/types.h>
33 #include <ifaddrs.h>
34 #include <libdabba/macros.h>
35 #include <libdabba/strlcpy.h>
36 #include <dabbad/list.h>
38 /**
39 * \brief Get the list of usable interfaces by dabbad and give it to the user
40 * \param[in,out] msg Dabba daemon IPC message
41 * \return 0 on success, -1 if the interface list could not be fetched.
43 * This function only retrieves interfaces which belong to the packet family.
46 int dabbad_ifconf_get(struct dabba_ipc_msg *msg)
48 size_t a, off, ifconf_size;
49 struct ifaddrs *ifaddr, *ifa;
51 if (getifaddrs(&ifaddr) != 0)
52 return -1;
54 ifconf_size = ARRAY_SIZE(msg->msg_body.msg.ifconf);
55 ifa = ifaddr;
57 for (off = 0; ifa && off < msg->msg_body.offset; ifa = ifa->ifa_next) {
58 if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_PACKET)
59 continue;
60 off++;
63 for (a = 0; ifa && a < ifconf_size; ifa = ifa->ifa_next) {
64 if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_PACKET)
65 continue;
67 strlcpy(msg->msg_body.msg.ifconf[a].name, ifa->ifa_name,
68 IFNAMSIZ);
69 a++;
72 msg->msg_body.elem_nr = a;
73 freeifaddrs(ifaddr);
74 return 0;