From 969415b70c7388f6e5da0539a10f919d15ec4612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Arreola=20Rodr=C3=ADguez?= Date: Thu, 23 Aug 2018 14:06:41 -0500 Subject: [PATCH] Agrego comando dhcp al manager. --- src/dhcp.c | 9 +++++++++ src/dhcp.h | 1 + src/manager.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/dhcp.c b/src/dhcp.c index de21e0b..0720fa9 100644 --- a/src/dhcp.c +++ b/src/dhcp.c @@ -38,6 +38,7 @@ #include "dhcp.h" #include "interfaces.h" #include "utils.h" +#include "routes.h" #define DHCPC_PIPEOUT_HAS_IP 0x01 #define DHCPC_PIPEOUT_HAS_SERVER_IP 0x02 @@ -137,6 +138,13 @@ static void _dhcp_parse_client_packet (NetworkInadorHandle *handle, Interface *i interfaces_manual_add_ipv4 (handle->netlink_sock_request, iface, &address); /* Y esperar a que se active la IP para luego configurar la ruta */ + if (has_gateway) { + IPv4 default_dest; + + inet_pton (AF_INET, "0.0.0.0", &default_dest.sin_addr); + default_dest.prefix = 0; + routes_manual_add_ipv4 (handle->netlink_sock_request, iface, &default_dest, route); + } } break; } @@ -275,3 +283,4 @@ void dhcp_stop_client (NetworkInadorHandle *handle, Interface *iface) { kill (iface->dhcp_info.process_pid, SIGTERM); } } + diff --git a/src/dhcp.h b/src/dhcp.h index 4f8ae99..ffc873b 100644 --- a/src/dhcp.h +++ b/src/dhcp.h @@ -24,5 +24,6 @@ #define __DHCP_H__ void dhcp_run_client (NetworkInadorHandle *handle, Interface *iface); +void dhcp_stop_client (NetworkInadorHandle *handle, Interface *iface); #endif diff --git a/src/manager.c b/src/manager.c index a2220b9..df87354 100644 --- a/src/manager.c +++ b/src/manager.c @@ -38,6 +38,7 @@ #include "manager.h" #include "interfaces.h" #include "network-inador.h" +#include "dhcp.h" #define COMMAND_SOCKET_PATH "/tmp/network-inador.socket" @@ -85,7 +86,9 @@ enum { MANAGER_ERROR_PREFIX_INVALID, MANAGER_ERROR_IFACE_INVALID, - MANAGER_ERROR_IPV4_INVALID + MANAGER_ERROR_IPV4_INVALID, + MANAGER_ERROR_DHCP_ALREADY_RUNNING, + MANAGER_ERROR_DHCP_NOT_RUNNING }; #define MANAGER_IFACE_TYPE_WIRELESS 0x02 @@ -398,6 +401,46 @@ static void _manager_send_list_ipv4 (NetworkInadorHandle *handle, ManagerCommand _manager_send_response (request, buffer, pos); } +static void _manager_handle_interface_set_dhcp (NetworkInadorHandle *handle, ManagerCommandRequest *request) { + /* Primero, validar que haya suficientes bytes: + * 1 byte de la interfaz + */ + + int index; + Interface *iface; + unsigned char buffer[8192]; + IPv4 *ip_g; + int pos; + int count; + + if (request->command_len < 1) { + /* Bytes unsuficientes */ + _manager_send_invalid_request (request, MANAGER_ERROR_INCOMPLETE_REQUEST); + return; + } + + index = request->command_data[0]; + + iface = interfaces_locate_by_index (handle->interfaces, index); + + if (iface == NULL) { + _manager_send_invalid_request (request, MANAGER_ERROR_IFACE_INVALID); + return; + } + + if (request->command == MANAGER_COMMAND_RUN_DHCP_CLIENT && iface->dhcp_info.type != IFACE_NO_DHCP_RUNNING) { + _manager_send_invalid_request (request, MANAGER_ERROR_DHCP_ALREADY_RUNNING); + } else if (request->command == MANAGER_COMMAND_STOP_DHCP_CLIENT && iface->dhcp_info.type == IFACE_NO_DHCP_RUNNING) { + _manager_send_invalid_request (request, MANAGER_ERROR_DHCP_NOT_RUNNING); + } else if (request->command == MANAGER_COMMAND_RUN_DHCP_CLIENT) { + dhcp_run_client (handle, iface); + _manager_send_processing (request); + } else if (request->command == MANAGER_COMMAND_STOP_DHCP_CLIENT) { + dhcp_stop_client (handle, iface); + _manager_send_processing (request); + } +} + static void _manager_send_list_routes (NetworkInadorHandle *handle, ManagerCommandRequest *request) { unsigned char buffer[8192]; Routev4 *route_g; @@ -480,10 +523,8 @@ static gboolean _manager_client_data (GIOChannel *source, GIOCondition condition _manager_handle_interface_del_ipv4 (handle, &request); break; case MANAGER_COMMAND_RUN_DHCP_CLIENT: - - break; case MANAGER_COMMAND_STOP_DHCP_CLIENT: - + _manager_handle_interface_set_dhcp (handle, &request); break; case MANAGER_COMMAND_LIST_IPV4: _manager_send_list_ipv4 (handle, &request);