NetworkInador/lib/interfaces.c

804 lines
22 KiB
C

/*
* interfaces.c
* This file is part of Network-inador
*
* Copyright (C) 2019, 2020 - Félix Arreola Rodríguez
*
* Network-inador is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Network-inador is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Network-inador; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <string.h>
#include <netlink/socket.h>
#include <netlink/msg.h>
#include <linux/if_arp.h>
#include <stdarg.h>
#include "network-inador-private.h"
#include "interfaces.h"
#include "routes.h"
#include "ip-address.h"
#include "wireless_if.h"
//#include "manager.h"
#include "network-inador-link-types.h"
static int _interfaces_receive_message_interface (struct nl_msg *msg, void *arg, int first_time);
static int _interfaces_list_first_time (struct nl_msg *msg, void *arg) {
return _interfaces_receive_message_interface (msg, arg, TRUE);
}
int interface_receive_message_newlink (struct nl_msg *msg, void *arg) {
return _interfaces_receive_message_interface (msg, arg, FALSE);
}
static uint32_t interfaces_check_link_type (Interface *iface) {
int g;
/* Revisar primero el INFO_KIND */
for (g = 0; g < (sizeof (linktypes) / sizeof (linktypes[0])); g++) {
if (linktypes[g].rtnl_type == NULL) continue;
if (strcmp (iface->rtnl_type, linktypes[g].rtnl_type) == 0) {
return linktypes[g].link_type;
}
}
if (iface->arp_type == ARPHRD_LOOPBACK)
return NI_LINK_TYPE_LOOPBACK;
else if (iface->arp_type == ARPHRD_INFINIBAND)
return NI_LINK_TYPE_INFINIBAND;
else if (iface->arp_type == ARPHRD_SIT)
return NI_LINK_TYPE_SIT;
else if (iface->arp_type == ARPHRD_TUNNEL6)
return NI_LINK_TYPE_IP6TNL;
else if (iface->arp_type == ARPHRD_PPP)
return NI_LINK_TYPE_PPP;
else if (iface->arp_type == ARPHRD_IEEE802154)
return NI_LINK_TYPE_WPAN;
else if (iface->arp_type == ARPHRD_6LOWPAN)
return NI_LINK_TYPE_6LOWPAN;
if (iface->is_wireless) {
return NI_LINK_TYPE_WIFI;
}
if (iface->arp_type == ARPHRD_ETHER) {
if (strncmp (iface->name, "rmnet", 5) == 0 ||
strncmp (iface->name, "rev_rmnet", 9) == 0 ||
strncmp (iface->name, "ccmni", 5) == 0)
return NI_LINK_TYPE_WWAN_NET;
return NI_LINK_TYPE_ETHERNET;
}
return NI_LINK_TYPE_UNKNOWN;
}
static int _interfaces_receive_message_interface (struct nl_msg *msg, void *arg, int first_time) {
struct nlmsghdr *reply;
struct ifinfomsg *iface_msg;
int remaining;
struct nlattr *attr;
NetworkInadorHandle *handle = (NetworkInadorHandle *) arg;
int was_new = 0, was_update = 0;
uint32_t new_flags;
Interface *iface;
uint32_t u32data;
char temp_name[128];
reply = nlmsg_hdr (msg);
if (reply->nlmsg_type != RTM_NEWLINK) return NL_SKIP;
iface_msg = nlmsg_data (reply);
iface = _interfaces_locate_by_index (handle->interfaces, iface_msg->ifi_index);
if (iface == NULL) {
/* Crear esta interfaz */
iface = malloc (sizeof (Interface));
memset (iface, 0, sizeof (Interface));
iface->handle = handle;
handle->interfaces = f_list_append (handle->interfaces, iface);
was_new = 1;
}
if (iface_msg->ifi_family == AF_BRIDGE) {
/* Tenemos un evento especial, se está agregando una interfaz a un bridge */
nlmsg_for_each_attr(attr, reply, sizeof (struct ifinfomsg), remaining) {
if (nla_type (attr) == IFLA_MASTER) {
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el nuevo master */
return NL_SKIP;
}
u32data = nla_get_u32 (attr);
iface->master_index = u32data;
}
}
//printf ("Interface %d agregada a la interfaz %d (bridge)\n", iface->index, iface->master_index);
was_update = 1;
//manager_send_event_interface_update (handle, iface);
return NL_SKIP;
}
iface->arp_type = iface_msg->ifi_type;
new_flags = iface_msg->ifi_flags;
iface->index = iface_msg->ifi_index;
nlmsg_for_each_attr(attr, reply, sizeof (struct ifinfomsg), remaining) {
switch (nla_type (attr)) {
//nla_len (Attr);
case IFLA_IFNAME:
// Actualizar el nombre de la interfaz */
strncpy (temp_name, nla_data (attr), IFNAMSIZ);
if (strcmp (temp_name, iface->name) != 0) {
was_update = 1;
strncpy (iface->name, nla_data (attr), IFNAMSIZ);
}
break;
case IFLA_ADDRESS:
if (nla_len (attr) > ETHER_ADDR_LEN) {
printf ("----- Warning, address es mayor que ETHER_ADDR_LEN\n");
continue;
}
memcpy (iface->real_hw, nla_data (attr), nla_len (attr));
//printf ("Interface %d has hw addr: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", iface->ifi_index, new->real_hw[0], new->real_hw[1], new->real_hw[2], new->real_hw[3], new->real_hw[4], new->real_hw[5]);
break;
case IFLA_MASTER:
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el nuevo master */
continue;
}
if (first_time == FALSE) continue;
u32data = nla_get_u32 (attr);
iface->master_index = u32data;
//printf ("Interface %d has master: %d\n", iface->index, iface->master_index);
break;
case IFLA_MTU:
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el mtu */
continue;
}
u32data = nla_get_u32 (attr);
if (iface->mtu != u32data) {
iface->mtu = u32data;
was_update = 1;
}
//printf ("Interface %d has mtu: %u\n", iface->ifi_index, new->mtu);
break;
case IFLA_LINKINFO:
{
printf ("La interfaz tiene linkinfo");
struct nlattr *sub_attr;
int sub_remaining;
nla_for_each_nested(sub_attr, attr, sub_remaining) {
printf ("\tLINK_INFO attr: %i\n", nla_type (sub_attr));
switch (nla_type (sub_attr)) {
case IFLA_INFO_KIND:
printf ("IFLA_INFO_KIND: %s\n", nla_data (sub_attr));
strncpy (iface->rtnl_type, nla_data (sub_attr), sizeof (iface->rtnl_type) - 1);
iface->rtnl_type[sizeof (iface->rtnl_type) - 1] = 0;
break;
}
}
}
break;
case IFLA_LINK:
/* Corresponde a la interfaz real de una vlan */
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el vlan parent */
continue;
}
u32data = nla_get_u32 (attr);
iface->vlan_parent = u32data;
//printf ("Interface %d: tiene como padre a %d\n", iface->index, iface->vlan_parent);
break;
case IFLA_PROTINFO:
printf ("OoOoO Interface %i tiene IFLA_PROTINFO oOoOo: %i\n", iface->index, IFLA_PROTINFO);
break;
case IFLA_AF_SPEC:
{
struct nlattr *sub_family;
int sub_family_len;
sub_family = nla_data (attr);
remaining = nla_len (attr);
while (remaining > sizeof (sub_family)) {
sub_family_len = sub_family->nla_len;
if (sub_family_len > remaining) {
printf ("Los sub atributos se acabaron prematuramente\n");
break;
}
printf ("Interface %d, IFLA_AF_SPEC, sub attributo type: %i, tamaño: %i\n", iface->index, sub_family->nla_type, sub_family_len);
if (sub_family->nla_type == AF_INET6) {
struct nlattr *sub_attr;
int sub_remaining;
nla_for_each_nested(sub_attr, sub_family, sub_remaining) {
printf ("\tIFLA_AF_SPEC, family: %i attr: %i, len: %i\n", sub_family->nla_type, nla_type (sub_attr), sub_attr->nla_len);
unsigned char *ttt;
switch (nla_type (sub_attr)) {
case IFLA_INET6_FLAGS:
iface->inet6_data.i6_flags = nla_get_u32 (sub_attr);
break;
case IFLA_INET6_CONF:
ttt = nla_data (sub_attr);
printf ("Size of data: %i, sizeof struct: %i\n", nla_len (sub_attr), sizeof (iface->inet6_data.i6_conf));
memcpy (iface->inet6_data.i6_conf, ttt, sizeof (iface->inet6_data.i6_conf));
break;
case IFLA_INET6_CACHEINFO:
memcpy (&iface->inet6_data.i6_cacheinfo, nla_data (sub_attr), sizeof (iface->inet6_data.i6_cacheinfo));
break;
case IFLA_INET6_ADDR_GEN_MODE:
iface->inet6_data.i6_addr_gen_mode = nla_get_u8 (sub_attr);
break;
}
}
}
/* Avanzar a la siguiente familia presente */
remaining -= RTA_ALIGN (sub_family_len);
sub_family = (struct nlattr *) (((char *) sub_family) + RTA_ALIGN (sub_family_len));
}
}
break;
/*default:
printf ("RTA Attribute \"%hu\" no procesado\n", nla_type (attr));*/
}
}
if (iface->flags != new_flags) {
iface->flags = new_flags;
route_ask_delayed_delroute (handle);
was_update = 1;
}
if (was_new) {
//printf ("+++++ Interfaz nueva creada: %s\n", iface->name);
//wireless_interface_check (handle, iface);
iface->link_type = interfaces_check_link_type (iface);
//manager_send_event_interface_add (handle, iface);
} else if (was_update) {
//manager_send_event_interface_update (handle, iface);
}
return NL_SKIP;
}
Interface * _interfaces_locate_by_index (FList *list, int index) {
Interface *iface;
FList *g;
for (g = list; g != NULL; g = g->next) {
iface = (Interface *) g->data;
if (iface->index == index) {
return iface;
}
}
return NULL;
}
Interface * _interfaces_locate_by_name (FList *list, const char *name) {
Interface *iface;
FList *g;
for (g = list; g != NULL; g = g->next) {
iface = (Interface *) g->data;
if (strcmp (iface->name, name) == 0) {
return iface;
}
}
return NULL;
}
int _interfaces_wait_ack_or_error (struct nl_msg *msg, void *arg) {
int *ret = (int *) arg;
struct nlmsgerr *l_err;
struct nlmsghdr *reply;
reply = nlmsg_hdr (msg);
if (reply->nlmsg_type == NLMSG_ERROR) {
l_err = nlmsg_data (reply);
*ret = l_err->error;
}
return NL_SKIP;
}
int _interfaces_wait_error (struct sockaddr_nl *nla, struct nlmsgerr *l_err, void *arg) {
int *ret = (int *) arg;
*ret = l_err->error;
return NL_SKIP;
}
int interface_receive_message_dellink (struct nl_msg *msg, void *arg) {
NetworkInadorHandle *handle = (NetworkInadorHandle *) arg;
Interface *iface;
struct ifinfomsg *iface_msg;
int remaining;
uint32_t u32data;
struct nlmsghdr *reply;
struct nlattr *attr;
reply = nlmsg_hdr (msg);
if (reply->nlmsg_type != RTM_DELLINK) return NL_SKIP;
iface_msg = nlmsg_data (reply);
iface = _interfaces_locate_by_index (handle->interfaces, iface_msg->ifi_index);
if (iface == NULL) {
printf ("Error, solicitaron eliminar interfaz que ya no existe\n");
return NL_SKIP;
}
if (iface_msg->ifi_family == AF_BRIDGE) {
/* Tenemos un evento especial, se está eliminando una interfaz de un bridge */
nlmsg_for_each_attr(attr, reply, sizeof (struct ifinfomsg), remaining) {
if (nla_type (attr) == IFLA_MASTER) {
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el nuevo master */
return NL_SKIP;
}
u32data = nla_get_u32 (attr);
iface->master_index = 0;
}
}
//printf ("Interface %d se sacó de su bridge\n", iface->index);
/* Generar EVENTO AQUI */
//manager_send_event_interface_update (handle, iface);
return NL_SKIP;
}
route_ask_delayed_delroute (handle);
//printf ("----- Interfaz eliminada: %s\n", iface->name);
handle->interfaces = f_list_remove (handle->interfaces, iface);
/* Antes de eliminar la interfaz, eliminar la lista ligada de todas las direcciones IP */
f_list_free_full (iface->address, free);
//manager_send_event_interface_del (handle, iface->index);
free (iface);
}
int interfaces_change_mac_address (NetworkInadorHandle *handle, int index, void *new_mac) {
/* ETHER_ADDR_LEN */
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
int ret, error;
Interface *iface;
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
//printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_type = iface->arp_type;
iface_hdr.ifi_index = iface->index;
iface_hdr.ifi_flags = iface->flags;
iface_hdr.ifi_change = 0xFFFFFFFF;
msg = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
ret = nla_put (msg, IFLA_ADDRESS, ETHER_ADDR_LEN, new_mac);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
if (ret <= 0) {
return -1;
}
error = 0;
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_VALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_INVALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_ACK, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_err_cb (handle->nl_sock_route, NL_CB_CUSTOM, _interfaces_wait_error, &error);
nl_recvmsgs_default (handle->nl_sock_route);
if (ret <= 0 || error < 0) {
return -1;
}
return 0;
}
int interfaces_change_mtu (NetworkInadorHandle *handle, int index, uint32_t new_mtu) {
/* ETHER_ADDR_LEN */
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
int ret, error;
Interface *iface;
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
//printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_type = iface->arp_type;
iface_hdr.ifi_index = iface->index;
iface_hdr.ifi_flags = iface->flags;
iface_hdr.ifi_change = 0xFFFFFFFF;
msg = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
ret = nla_put (msg, IFLA_MTU, sizeof (new_mtu), &new_mtu);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
if (ret <= 0) {
return -1;
}
error = 0;
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_VALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_INVALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_ACK, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_err_cb (handle->nl_sock_route, NL_CB_CUSTOM, _interfaces_wait_error, &error);
nl_recvmsgs_default (handle->nl_sock_route);
if (ret <= 0 || error < 0) {
return -1;
}
return 0;
}
static int _interfaces_change_admin_up (NetworkInadorHandle *handle, int index, int up) {
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
int ret, error;
Interface *iface;
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
if (up && (iface->flags & IFF_UP)) {
/* Ningún cambio necesario, ya está activa */
return 0;
} else if (up == FALSE && ((iface->flags & IFF_UP) == 0)) {
/* Ningún cambio necesario, ya está desactivada */
return 0;
}
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_type = iface->arp_type;
iface_hdr.ifi_index = iface->index;
if (up) {
iface_hdr.ifi_flags = iface->flags | IFF_UP;
} else {
iface_hdr.ifi_flags = iface->flags & ~IFF_UP;
}
iface_hdr.ifi_change = 0xFFFFFFFF;
msg = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
if (ret <= 0) {
return -1;
}
error = 0;
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_VALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_INVALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_ACK, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_err_cb (handle->nl_sock_route, NL_CB_CUSTOM, _interfaces_wait_error, &error);
nl_recvmsgs_default (handle->nl_sock_route);
if (ret <= 0 || error < 0) {
return -1;
}
return 0;
}
int interfaces_change_set_up (NetworkInadorHandle *handle, int index) {
return _interfaces_change_admin_up (handle, index, TRUE);
}
int interfaces_change_set_down (NetworkInadorHandle *handle, int index) {
return _interfaces_change_admin_up (handle, index, FALSE);
}
int interfaces_change_name (NetworkInadorHandle *handle, int index, char * new_name) {
/* IFNAMSIZ */
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
int ret, error;
Interface *iface;
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
if (strlen (new_name) > IFNAMSIZ) {
return -1;
}
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_type = iface->arp_type;
iface_hdr.ifi_index = iface->index;
iface_hdr.ifi_flags = iface->flags;
iface_hdr.ifi_change = 0xFFFFFFFF;
msg = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
ret = nla_put (msg, IFLA_IFNAME, strlen (new_name) + 1, new_name);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
if (ret <= 0) {
return -1;
}
error = 0;
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_VALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_INVALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_ACK, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_err_cb (handle->nl_sock_route, NL_CB_CUSTOM, _interfaces_wait_error, &error);
ret = nl_recvmsgs_default (handle->nl_sock_route);
if (ret <= 0 || error < 0) {
return -1;
}
return 0;
}
#if 0
int interfaces_set_sysctl_ipv6_params (NetworkInadorHandle *handle, int index, int param, ...) {
uint32_t new_sysctl[DEVCONF_MAX];
Interface *iface;
va_list va_list_params;
uint32_t value;
struct nl_msg * msg, *msg2, *msg3;
struct ifinfomsg iface_hdr;
int ret, error;
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
/* Copiar los sysctl previos para preservarlos */
memcpy (new_sysctl, iface->inet6_data.i6_conf, sizeof (new_sysctl));
va_start (va_list_params, param);
while (param != -1) {
value = va_arg (va_list_params, uint32_t);
if (param < DEVCONF_MAX) {
new_sysctl[param] = value;
}
param = va_arg (va_list_params, int);
}
va_end (va_list_params);
/* Preparar el mensaje de configurar la interfaz */
memset (&iface_hdr, 0, sizeof (iface_hdr));
#define LINK_ATTR_AF_SPEC (1 << 27)
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_index = iface->index;
iface_hdr.ifi_change = LINK_ATTR_AF_SPEC;
msg = nlmsg_alloc_simple (RTM_SETLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
/* FIXME: Aquí el nesting doble o triple */
msg2 = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
msg3 = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
nla_put (msg3, IFLA_INET6_CONF, sizeof (new_sysctl), new_sysctl);
nla_put_nested (msg2, AF_INET6 | NLA_F_NESTED, msg3);
nla_put_nested (msg, IFLA_AF_SPEC | NLA_F_NESTED, msg2);
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
nlmsg_free (msg2);
nlmsg_free (msg3);
if (ret <= 0) {
return -1;
}
error = 0;
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_VALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_INVALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_ACK, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error);
nl_socket_modify_err_cb (handle->nl_sock_route, NL_CB_CUSTOM, _interfaces_wait_error, &error);
nl_recvmsgs_default (handle->nl_sock_route);
if (ret <= 0 || error < 0) {
return -1;
}
return 0;
}
#endif
void interfaces_init (NetworkInadorHandle *handle) {
/* Si es la primera vez que nos llaman, descargar una primera lista de interfaces */
struct nl_msg * msg;
struct rtgenmsg rt_hdr = {
.rtgen_family = AF_PACKET,
};
int ret;
msg = nlmsg_alloc_simple (RTM_GETLINK, NLM_F_REQUEST | NLM_F_DUMP);
ret = nlmsg_append (msg, &rt_hdr, sizeof (rt_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
return;
}
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_VALID, NL_CB_CUSTOM, _interfaces_list_first_time, handle);
nl_recvmsgs_default (handle->nl_sock_route);
ip_address_init (handle);
}
void interfaces_clean_up (NetworkInadorHandle *handle) {
FList *g;
Interface *iface;
for (g = handle->interfaces; g != NULL; g = g->next) {
iface = (Interface *) g->data;
/* Antes de eliminar la interfaz, eliminar la lista ligada de todas las direcciones IP */
f_list_free_full (iface->address, free);
iface->address = NULL;
}
f_list_free_full (handle->interfaces, free);
handle->interfaces = NULL;
}