NetworkInador/src/netlink-events.c

88 lines
2.5 KiB
C
Raw Normal View History

2018-08-07 15:28:22 -05:00
/*
2018-08-23 13:31:27 -05:00
* netlink-events.c
2018-08-07 15:28:22 -05:00
* This file is part of Network-inador
*
2019-12-31 11:05:50 -06:00
* Copyright (C) 2019, 2020 - Félix Arreola Rodríguez
2018-08-07 15:28:22 -05:00
*
* 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
*/
2019-12-31 11:05:50 -06:00
#include <netlink/socket.h>
#include <netlink/msg.h>
2018-08-07 15:28:22 -05:00
2018-08-17 01:16:01 -05:00
#include <glib.h>
2019-12-31 11:05:50 -06:00
#include "common.h"
2018-08-07 15:28:22 -05:00
#include "interfaces.h"
2020-01-02 12:53:53 -06:00
#include "ip-address.h"
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
static int _netlink_events_route_dispatcher (struct nl_msg *msg, void *arg) {
struct nlmsghdr *reply;
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
reply = nlmsg_hdr (msg);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
switch (reply->nlmsg_type) {
case RTM_NEWLINK:
2020-01-02 12:53:53 -06:00
return interface_receive_message_newlink (msg, arg);
2019-12-31 11:05:50 -06:00
break;
case RTM_DELLINK:
2020-01-02 12:53:53 -06:00
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);
2019-12-31 11:05:50 -06:00
break;
2018-08-07 15:28:22 -05:00
}
2020-01-02 12:53:53 -06:00
return NL_SKIP;
2019-12-31 11:05:50 -06:00
}
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
static gboolean _netlink_events_handle_read (GIOChannel *source, GIOCondition condition, gpointer data) {
NetworkInadorHandle *handle = (NetworkInadorHandle *) data;
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
nl_recvmsgs_default (handle->nl_sock_route_events);
2018-08-07 15:28:22 -05:00
return TRUE;
}
2019-12-31 11:05:50 -06:00
void netlink_events_setup (NetworkInadorHandle *handle) {
struct nl_sock * sock_req;
int fd;
2018-08-07 15:28:22 -05:00
GIOChannel *channel;
2019-12-31 11:05:50 -06:00
sock_req = nl_socket_alloc ();
if (nl_connect (sock_req, NETLINK_ROUTE) != 0) {
perror ("Falló conectar netlink socket para eventos\n");
return;
}
nl_socket_set_nonblocking (sock_req);
2020-01-02 12:53:53 -06:00
nl_socket_add_memberships (sock_req, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR, 0);
2019-12-31 11:05:50 -06:00
nl_socket_disable_seq_check (sock_req);
nl_socket_modify_cb (sock_req, NL_CB_VALID, NL_CB_CUSTOM, _netlink_events_route_dispatcher, handle);
fd = nl_socket_get_fd (sock_req);
channel = g_io_channel_unix_new (fd);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
handle->route_events_source = g_io_add_watch (channel, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, _netlink_events_handle_read, handle);
handle->nl_sock_route_events = sock_req;
2018-08-07 15:28:22 -05:00
}