NetworkInador/src/main.c

144 lines
3.5 KiB
C
Raw Normal View History

2018-08-07 15:28:22 -05:00
/*
2019-12-31 11:05:50 -06:00
* main.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
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2019-12-31 11:05:50 -06:00
#include <assert.h>
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
#include <asm/types.h>
2018-08-07 15:28:22 -05:00
#include <sys/socket.h>
#include <linux/netlink.h>
#include <glib.h>
2019-12-31 11:05:50 -06:00
#include <netlink/socket.h>
#include <netlink/msg.h>
#include "common.h"
2018-08-07 15:28:22 -05:00
#include "interfaces.h"
2018-08-23 13:31:27 -05:00
#include "netlink-events.h"
2021-08-07 19:42:55 -05:00
#include "ip-address.h"
#include "bridge.h"
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
/* Usados para salir en caso de una señal */
static int sigterm_pipe_fds[2] = { -1, -1 };
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
static void _init_handle (NetworkInadorHandle *handle) {
assert (handle != NULL);
2018-08-07 15:28:22 -05:00
memset (handle, 0, sizeof (NetworkInadorHandle));
2019-12-31 11:05:50 -06:00
handle->interfaces = NULL;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
static void _sigterm_handler (int signum) {
//fprintf (stderr, "SIGTERM SIGINT Handler\n");
if (sigterm_pipe_fds[1] >= 0) {
if (write (sigterm_pipe_fds[1], "", 1) == -1 ) {
//fprintf (stderr, "Write to sigterm_pipe failed.\n");
}
close (sigterm_pipe_fds[1]);
sigterm_pipe_fds[1] = -1;
}
}
2019-12-31 11:05:50 -06:00
static gboolean _main_quit_handler (GIOChannel *source, GIOCondition cond, gpointer data) {
GMainLoop *loop = (GMainLoop *) data;
g_main_loop_quit (loop);
}
2019-12-31 11:05:50 -06:00
static void _main_setup_signal (void *loop) {
struct sigaction act;
sigset_t empty_mask;
/* Preparar el pipe para la señal de cierre */
if (pipe (sigterm_pipe_fds) != 0) {
perror ("Failed to create SIGTERM pipe");
sigterm_pipe_fds[0] = -1;
}
/* Instalar un manejador de señales para SIGTERM */
sigemptyset (&empty_mask);
act.sa_mask = empty_mask;
act.sa_flags = 0;
2019-12-31 11:05:50 -06:00
act.sa_handler = &_sigterm_handler;
if (sigaction (SIGTERM, &act, NULL) < 0) {
perror ("Failed to register SIGTERM handler");
}
if (sigaction (SIGINT, &act, NULL) < 0) {
perror ("Failed to register SIGINT handler");
}
if (sigterm_pipe_fds[0] != -1) {
GIOChannel *io;
io = g_io_channel_unix_new (sigterm_pipe_fds[0]);
g_io_channel_set_close_on_unref (io, TRUE);
2019-12-31 11:05:50 -06:00
g_io_add_watch (io, G_IO_IN | G_IO_PRI | G_IO_HUP | G_IO_ERR, _main_quit_handler, loop);
}
}
2019-12-31 11:05:50 -06:00
int main (int argc, char *argv[]) {
NetworkInadorHandle handle;
GMainLoop *loop = NULL;
struct nl_sock * sock_req;
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
#endif
2019-12-31 11:05:50 -06:00
_init_handle (&handle);
2019-12-31 11:05:50 -06:00
/* Crear el socket de peticiones */
sock_req = nl_socket_alloc ();
if (nl_connect (sock_req, NETLINK_ROUTE) != 0) {
perror ("Falló conectar netlink socket\n");
return -1;
}
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
handle.nl_sock_route = sock_req;
/* Crear el socket que escucha eventos */
netlink_events_setup (&handle);
2018-08-07 15:28:22 -05:00
loop = g_main_loop_new (NULL, FALSE);
2019-12-31 11:05:50 -06:00
_main_setup_signal (loop);
2018-08-17 01:16:01 -05:00
2019-12-31 11:05:50 -06:00
interfaces_init (&handle);
2018-08-07 15:28:22 -05:00
g_main_loop_run (loop);
/* Detener la llegada de eventos */
netlink_events_clear (&handle);
// nl_socket_free???
2018-08-07 15:28:22 -05:00
return 0;
}