NetworkInador/lib/netlink-events.c

92 lines
2.5 KiB
C
Raw Normal View History

/*
* netlink-events.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 <stdio.h>
#include <unistd.h>
#include <netlink/socket.h>
#include <netlink/msg.h>
#include <linux/nl80211.h>
#include <fcntl.h>
#include "network-inador-private.h"
#include "interfaces.h"
#include "ip-address.h"
#include "routes.h"
int netlink_events_route_dispatcher (struct nl_msg *msg, void *arg) {
struct nlmsghdr *reply;
reply = nlmsg_hdr (msg);
switch (reply->nlmsg_type) {
case RTM_NEWLINK:
return interface_receive_message_newlink (msg, arg);
break;
case RTM_DELLINK:
return interface_receive_message_dellink (msg, arg);
break;
case RTM_NEWADDR:
return ip_address_receive_message_newaddr (msg, arg);
break;
case RTM_DELADDR:
return ip_address_receive_message_deladdr (msg, arg);
break;
case RTM_NEWROUTE:
return routes_receive_message_newroute (msg, arg);
break;
case RTM_DELROUTE:
return routes_receive_message_delroute (msg, arg);
break;
}
return NL_SKIP;
}
void netlink_events_route_events_handle_read (void *data, int fd, int condition) {
NetworkInadorHandle *handle = (NetworkInadorHandle *) data;
nl_recvmsgs_default (handle->route_events.nl_sock);
}
void netlink_events_pipe_route_handle_read (void *data, int fd, int condition) {
NetworkInadorHandle *handle = (NetworkInadorHandle *) data;
char buffer[8192];
int ret;
ret = read (handle->pipe_routes[0], buffer, sizeof (buffer));
if (ret > 0) {
/* Leimos algo en el aviso de re-procesar rutas */
routes_ask (handle);
} else if (ret == 0) {
/* Cerrar el pipe */
close (handle->pipe_routes[0]);
handle->pipe_routes[0] = -1;
handle->ops.input_remove (handle->source_pipe_routes);
handle->source_pipe_routes = 0;
}
}