NetworkInador/lib/dhcp_client.c

423 lines
13 KiB
C
Raw Permalink Normal View History

2021-12-28 23:21:31 -06:00
/*
* dhcp_client.c
* This file is part of Network Inador
*
* Copyright (C) 2021 - 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 <stdlib.h>
#include <string.h>
#include <time.h>
2021-12-28 23:21:31 -06:00
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
2021-12-28 23:21:31 -06:00
#include <signal.h>
#include "interfaces.h"
#include "network-inador-private.h"
//#include "manager.h"
2021-12-28 23:21:31 -06:00
#include "dhcp_client.h"
#include "resolv_manager.h"
#include "launch_process.h"
#include "network-inador-manager.h"
2021-12-28 23:21:31 -06:00
2022-01-02 23:07:39 -06:00
static void interfaces_dhcp_clear_info (InterfaceDHCPClientInfo *dhcpc);
/*void interfaces_dhcp_client_ignore_kill (GPid pid, gint status, gpointer data) {
2021-12-28 23:21:31 -06:00
g_spawn_check_exit_status (status, NULL);
}*/
2021-12-28 23:21:31 -06:00
void interfaces_dhcp_prepare_args_for_isc_dhclient (char **argv, int size, char *iface_name, char *pid_file, size_t pid_file_len) {
snprintf (pid_file, pid_file_len, "/run/dhclient-%s.pid", iface_name);
2021-12-28 23:21:31 -06:00
/* Preparar los argumentos para el proceso */
argv[0] = "/sbin/dhclient";
argv[1] = "-d";
argv[2] = "-q";
argv[3] = "-pf";
argv[4] = pid_file;
argv[5] = "-sf";
argv[6] = "/home/gatuno/Proyectos/NetworkInador/src/ni-dhcp-helper";
2021-12-28 23:21:31 -06:00
argv[7] = iface_name;
argv[8] = NULL;
}
void interfaces_dhcp_prepare_args_for_busybox_udhcpc (char **argv, int size, char *iface_name, char *pid_file, size_t pid_file_len) {
snprintf (pid_file, pid_file_len, "/run/dhclient-%s.pid", iface_name);
/* Preparar los argumentos para el proceso */
argv[0] = "/bin/busybox";
argv[1] = "udhcpc";
argv[2] = "-i";
argv[3] = iface_name;
argv[4] = "-p";
argv[5] = pid_file;
argv[6] = "-s";
argv[7] = "/home/gatuno/Proyectos/NetworkInador/src/ni-dhcp-helper";
argv[8] = "-f";
argv[9] = NULL;
}
void interfaces_dhcp_client_killed_cb (void *data, pid_t pid, int status) {
2021-12-28 23:21:31 -06:00
Interface *iface = (Interface *) data;
NetworkInadorHandle *handle = iface->handle;
int ret;
int error = 0;
2021-12-28 23:21:31 -06:00
char *argv[20];
char pid_file[256];
2021-12-28 23:21:31 -06:00
/* Preparar los argumentos */
if (iface->dhcpc.type == IFACE_ISC_DHCLIENT) {
interfaces_dhcp_prepare_args_for_isc_dhclient (argv, 20, iface->name, pid_file, sizeof (pid_file));
} else if (iface->dhcpc.type == IFACE_BUSYBOX_UDHCPC) {
interfaces_dhcp_prepare_args_for_busybox_udhcpc (argv, 20, iface->name, pid_file, sizeof (pid_file));
}
2021-12-28 23:21:31 -06:00
if (WIFEXITED (status)) {
2021-12-28 23:21:31 -06:00
/* Revisar si necesito algo */
}
/* Por limpieza, quitar la vigilancia del proceso */
if (handle->ops.process_unwatch != NULL) {
handle->ops.process_unwatch (iface->dhcpc.process_watch);
}
iface->dhcpc.process_watch = 0;
2022-01-02 23:07:39 -06:00
interfaces_dhcp_clear_info (&iface->dhcpc);
2024-01-07 19:16:40 -06:00
/* FIXME: ¿Debemos limpiar la información de DNS si no la movimos? */
resolv_manager_clear_dhcp_nameservers (iface->handle, iface);
2022-01-02 23:07:39 -06:00
/* Enviar actualización de estado aquí */
iface->dhcpc.dhcp_state = DHCP_CLIENT_KILLED;
//manager_send_event_dhcp_change (iface->handle, &iface->dhcpc);
2022-01-02 23:07:39 -06:00
if (iface->dhcpc.type == IFACE_ISC_DHCLIENT || iface->dhcpc.type == IFACE_BUSYBOX_UDHCPC) {
2021-12-28 23:21:31 -06:00
if (iface->dhcpc.flags & DHCP_CLIENT_FLAG_AUTO_RESTART) {
/* Se cerró o mataron el proceso, reiniciar */
ret = launch_process (
"/",
argv,
NULL,
&iface->dhcpc.process_pid,
NULL,
NULL,
NULL,
&error);
/*ret = g_spawn_async_with_pipes (
2021-12-28 23:21:31 -06:00
"/",
argv,
NULL,
G_SPAWN_DO_NOT_REAP_CHILD,
NULL,
NULL,
&iface->dhcpc.process_pid,
NULL,
NULL,
NULL,
&error);*/
2021-12-28 23:21:31 -06:00
if (ret == FALSE) {
printf ("Error dhcp: %s\n", strerror (error));
2021-12-28 23:21:31 -06:00
2022-01-02 23:07:39 -06:00
iface->dhcpc.dhcp_state = DHCP_CLIENT_KILLED;
2021-12-28 23:21:31 -06:00
iface->dhcpc.process_watch = 0;
} else {
2022-01-02 23:07:39 -06:00
iface->dhcpc.dhcp_state = DHCP_CLIENT_SELECTING;
if (handle->ops.process_watch != NULL) {
iface->dhcpc.process_watch = handle->ops.process_watch (iface->dhcpc.process_pid, interfaces_dhcp_client_killed_cb, iface);
}
2021-12-28 23:21:31 -06:00
}
} else {
/* En caso contrario, solo dejar la muerte escrita */
2022-01-02 23:07:39 -06:00
iface->dhcpc.dhcp_state = DHCP_CLIENT_KILLED;
if (handle->ops.process_unwatch != NULL) {
handle->ops.process_unwatch (iface->dhcpc.process_watch);
}
2021-12-28 23:21:31 -06:00
iface->dhcpc.process_watch = 0;
}
}
}
static void interfaces_dhcp_clear_info (InterfaceDHCPClientInfo *dhcpc) {
memset (&dhcpc->ip, 0, sizeof (dhcpc->ip));
dhcpc->gw_c = 0;
dhcpc->dns_c = 0;
memset (&dhcpc->broadcast, 0, sizeof (dhcpc->broadcast));
memset (&dhcpc->dhcp_server_ip, 0, sizeof (dhcpc->dhcp_server_ip));
dhcpc->lease_time = 0;
dhcpc->prefix = 0;
dhcpc->domain_name[0] = 0;
}
2021-12-28 23:21:31 -06:00
int interfaces_dhcp_client_run (NetworkInadorHandle *handle, int index, int type, uint32_t flags) {
/* IFNAMSIZ */
Interface *iface;
int ret;
int error;
2021-12-28 23:21:31 -06:00
char *argv[20];
char pid_file[256];
2021-12-28 23:21:31 -06:00
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
/* Revisar que tenga la función vigiladora de hijos muertos */
2021-12-28 23:21:31 -06:00
/* Preparar los argumentos */
if (type == IFACE_ISC_DHCLIENT) {
interfaces_dhcp_prepare_args_for_isc_dhclient (argv, 20, iface->name, pid_file, sizeof (pid_file));
} else if (type == IFACE_BUSYBOX_UDHCPC) {
interfaces_dhcp_prepare_args_for_busybox_udhcpc (argv, 20, iface->name, pid_file, sizeof (pid_file));
}
2021-12-28 23:21:31 -06:00
if (iface->dhcpc.type != IFACE_NO_DHCP) {
/* No puedo correr otro tipo de DHCP */
return -1;
}
if ((iface->dhcpc.type == IFACE_ISC_DHCLIENT || iface->dhcpc.type == IFACE_BUSYBOX_UDHCPC) && iface->dhcpc.dhcp_state != DHCP_CLIENT_KILLED) {
2021-12-28 23:21:31 -06:00
/* El cliente de dhcp ya está corriendo, no hacer nada */
return -1;
}
interfaces_dhcp_clear_info (&iface->dhcpc);
ret = launch_process (
"/",
argv,
NULL,
&iface->dhcpc.process_pid,
NULL,
NULL,
NULL,
&error);
/* ret = g_spawn_async_with_pipes (
2021-12-28 23:21:31 -06:00
"/",
argv,
NULL,
G_SPAWN_DO_NOT_REAP_CHILD,
NULL,
NULL,
&iface->dhcpc.process_pid,
NULL,
NULL,
NULL,
&error);*/
2021-12-28 23:21:31 -06:00
/*printf ("Por favor corre el siguiente comando DHCP:\n");
int g = 0;
while (argv[g] != NULL) {
printf ("%s ", argv[g]);
g++;
}
printf ("\n");
ret = TRUE;*/
2021-12-28 23:21:31 -06:00
if (ret == FALSE) {
printf ("Error dhcp: %s\n", strerror (error));
2021-12-28 23:21:31 -06:00
return -1;
}
2022-01-02 23:07:39 -06:00
iface->dhcpc.dhcp_state = DHCP_CLIENT_SELECTING;
iface->dhcpc.type = type;
2021-12-28 23:21:31 -06:00
iface->dhcpc.flags = flags;
if (handle->ops.process_watch != NULL) {
iface->dhcpc.process_watch = handle->ops.process_watch (iface->dhcpc.process_pid, interfaces_dhcp_client_killed_cb, iface);
}
2021-12-28 23:21:31 -06:00
//manager_send_event_dhcp_change (handle, &iface->dhcpc);
2022-01-02 23:07:39 -06:00
2021-12-28 23:21:31 -06:00
return 0;
}
int interfaces_dhcp_client_stop (NetworkInadorHandle *handle, int index) {
2021-12-28 23:21:31 -06:00
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;
2021-12-28 23:21:31 -06:00
}
if (iface->dhcpc.type == IFACE_NO_DHCP) {
return -1;
2021-12-28 23:21:31 -06:00
}
if (iface->dhcpc.type == IFACE_ISC_DHCLIENT || iface->dhcpc.type == IFACE_BUSYBOX_UDHCPC) {
if (iface->dhcpc.dhcp_state != DHCP_CLIENT_KILLED) {
/* Proceso, matar y reiniciar estado */
iface->dhcpc.flags &= (~DHCP_CLIENT_FLAG_AUTO_RESTART);
if (handle->ops.process_unwatch != NULL) {
handle->ops.process_unwatch (iface->dhcpc.process_watch);
}
iface->dhcpc.process_watch = 0;
// Si el proces muere, realmente no importa
kill (iface->dhcpc.process_pid, SIGTERM);
iface->dhcpc.dhcp_state = DHCP_CLIENT_KILLED;
}
iface->dhcpc.type = IFACE_NO_DHCP;
interfaces_dhcp_clear_info (&iface->dhcpc);
/* TODO: Revisar aquí si es pertinente desconfigurar la interfaz y borrar la información obtenida */
resolv_manager_clear_dhcp_nameservers (handle, iface);
2022-01-02 23:07:39 -06:00
/* Enviar actualización de estado aquí */
//manager_send_event_dhcp_change (handle, &iface->dhcpc);
2021-12-28 23:21:31 -06:00
}
return 0;
2021-12-28 23:21:31 -06:00
}
void interfaces_dhcp_client_internal_feed_from_client (NetworkInadorHandle *handle, int index, int status, struct_addr *ip, int prefix, struct_addr *gateways, int gw_c, struct_addr *broadcast, struct_addr *dhcp_server, uint32_t lease_time, struct_addr *dns_list, int dns_count, char *domain_name) {
2021-12-28 23:21:31 -06:00
Interface *iface;
int g;
struct_addr old_dns[7];
int old_dns_c;
int dns_changed;
2021-12-28 23:21:31 -06:00
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return;
}
if (iface->dhcpc.type == IFACE_NO_DHCP) {
return;
}
// TODO: Cuando entre a estado Selecting, previo otro estado diferente, borrar la IP, si es que tenemos
if (iface->dhcpc.type == IFACE_ISC_DHCLIENT || iface->dhcpc.type == IFACE_BUSYBOX_UDHCPC) {
2021-12-28 23:21:31 -06:00
/* Un proceso muerto no puede informar de cambios de estados */
if (iface->dhcpc.dhcp_state == DHCP_CLIENT_KILLED) return;
switch (status) {
case NET_INADOR_DHCP_STATUS_SELECTING:
iface->dhcpc.dhcp_state = DHCP_CLIENT_SELECTING;
break;
case NET_INADOR_DHCP_STATUS_BOUND:
iface->dhcpc.dhcp_state = DHCP_CLIENT_BOUND;
break;
case NET_INADOR_DHCP_STATUS_RENEWED:
iface->dhcpc.dhcp_state = DHCP_CLIENT_RENEWED;
break;
case NET_INADOR_DHCP_STATUS_EXPIRED:
iface->dhcpc.dhcp_state = DHCP_CLIENT_EXPIRED;
/* Borrar la IP anterior, si es que está puesta */
break;
case NET_INADOR_DHCP_STATUS_FAILED:
iface->dhcpc.dhcp_state = DHCP_CLIENT_FAILED;
2021-12-28 23:21:31 -06:00
break;
}
}
printf ("----> DHCP status: %i\n", iface->dhcpc.dhcp_state);
if (status == NET_INADOR_DHCP_STATUS_BOUND || status == NET_INADOR_DHCP_STATUS_RENEWED) {
dns_changed = 0;
/* Copiar la información de DNS para saber si hubo cambios */
old_dns_c = iface->dhcpc.dns_c;
if (old_dns_c != dns_count) {
/* Copiamos los dns viejos solo si la cantidad es diferente */
memcpy (old_dns, iface->dhcpc.dns, old_dns_c * (sizeof (iface->dhcpc.dns[0])));
}
/* Copiar las variables de estado */
memcpy (&iface->dhcpc.ip, ip, sizeof (iface->dhcpc.ip));
iface->dhcpc.prefix = prefix;
iface->dhcpc.gw_c = gw_c;
for (g = 0; g < gw_c && g < 7; g++) {
memcpy (&iface->dhcpc.gateways[g], &gateways[g], 4);
}
memcpy (&iface->dhcpc.broadcast, broadcast, sizeof (iface->dhcpc.broadcast));
memcpy (&iface->dhcpc.dhcp_server_ip, dhcp_server, sizeof (iface->dhcpc.dhcp_server_ip));
iface->dhcpc.lease_time = lease_time;
iface->dhcpc.dns_c = dns_count;
for (g = 0; g < dns_count && g < 7; g++) {
memcpy (&iface->dhcpc.dns[g], &dns_list[g], 4);
}
if (strcmp (iface->dhcpc.domain_name, domain_name) != 0) {
/* El nombre de dominio cambió */
dns_changed = 1;
}
strncpy (iface->dhcpc.domain_name, domain_name, sizeof (iface->dhcpc.domain_name));
if (iface->dhcpc.dns_c != old_dns_c) {
/* Tenemos cambio en los DNS, actualizar el resolv.conf */
dns_changed = 1;
} else if (memcmp (iface->dhcpc.dns, old_dns, dns_count * (sizeof (iface->dhcpc.dns[0]))) != 0) {
/* Tenemos cambio en los DNS, actualizar el resolv.conf */
dns_changed = 1;
}
2024-01-07 19:16:40 -06:00
if (dns_changed && (iface->dhcpc.flags & DHCP_CLIENT_FLAG_DONT_ADD_DNS_INFO) == 0) {
resolv_manager_process_dhcp_nameservers (handle, iface);
}
/* Esto solo es para impresión */
char buf_a[256], buf_b[256], buf_c[256], buf_d[256];
inet_ntop (AF_INET, &iface->dhcpc.ip, buf_a, sizeof (buf_a));
inet_ntop (AF_INET, &iface->dhcpc.broadcast, buf_c, sizeof (buf_c));
inet_ntop (AF_INET, &iface->dhcpc.dhcp_server_ip, buf_d, sizeof (buf_d));
printf ("----> (%i) DHCP Server (%s), IP obtenida: %s/%i, GW: ", (unsigned int) time (NULL), buf_d, buf_a, prefix);
for (g = 0; g < gw_c; g++) {
inet_ntop (AF_INET, &iface->dhcpc.gateways[g], buf_b, sizeof (buf_b));
printf ("%s, ", buf_b);
}
printf ("bcast: %s, lease: %i, DNS: ", buf_c, lease_time);
for (g = 0; g < dns_count; g++) {
inet_ntop (AF_INET, &iface->dhcpc.dns[g], buf_b, sizeof (buf_b));
printf ("%s, ", buf_b);
}
printf (" y domain name <%s>\n", domain_name);
} else if (status == NET_INADOR_DHCP_STATUS_SELECTING || status == NET_INADOR_DHCP_STATUS_EXPIRED || status == NET_INADOR_DHCP_STATUS_FAILED) {
/* Cuando entramos a SELECTING, debemos limpiar los DNS del resolv.conf */
interfaces_dhcp_clear_info (&iface->dhcpc);
2024-01-07 19:16:40 -06:00
/* FIXME: ¿Debemos limpiar la información DNS si no la movimos? */
resolv_manager_clear_dhcp_nameservers (handle, iface);
}
2022-01-02 23:07:39 -06:00
//manager_send_event_dhcp_change (handle, &iface->dhcpc);
2021-12-28 23:21:31 -06:00
}