Ajustes a los mensajes de peticion.

master
Félix Arreola Rodríguez 2018-08-21 22:48:20 -05:00
parent 5e57e34de4
commit 3fffcddaf9
5 changed files with 389 additions and 79 deletions

View File

@ -2,7 +2,7 @@
* events.c * events.c
* This file is part of Network-inador * This file is part of Network-inador
* *
* Copyright (C) 2011 - Félix Arreola Rodríguez * Copyright (C) 2018 - Félix Arreola Rodríguez
* *
* Network-inador is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by

View File

@ -53,12 +53,6 @@ typedef struct {
struct rtgenmsg gen; struct rtgenmsg gen;
} nl_req_t; } nl_req_t;
typedef struct {
struct nlmsghdr hdr;
struct ifinfomsg i;
char buf[1024];
} nl_req_if;
Interface * interfaces_locate_by_index (Interface *list, int index) { Interface * interfaces_locate_by_index (Interface *list, int index) {
Interface *iface; Interface *iface;
@ -99,7 +93,7 @@ static IPv4 * _interfaces_append_ipv4_to_struct (Interface *interface, struct in
return new_addr; return new_addr;
} }
static IPv4 * _interfaces_serach_ipv4 (Interface *interface, struct in_addr address, uint32_t prefix) { IPv4 * _interfaces_serach_ipv4 (Interface *interface, struct in_addr address, uint32_t prefix) {
IPv4 *list; IPv4 *list;
list = interface->v4_address; list = interface->v4_address;
@ -452,7 +446,6 @@ void interfaces_add_or_update_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr
} }
new->flags = addr->ifa_flags; new->flags = addr->ifa_flags;
} }
void interfaces_del_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h) { void interfaces_del_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h) {
@ -476,6 +469,7 @@ void interfaces_del_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h) {
} }
printf ("IP eliminada para la interfaz: %d, de la familia: %d\n", addr->ifa_index, addr->ifa_family); printf ("IP eliminada para la interfaz: %d, de la familia: %d\n", addr->ifa_index, addr->ifa_family);
size_t len = IFA_PAYLOAD (h); size_t len = IFA_PAYLOAD (h);
for (attribute = IFA_RTA (addr); RTA_OK (attribute, len); attribute = RTA_NEXT (attribute, len)) { for (attribute = IFA_RTA (addr); RTA_OK (attribute, len); attribute = RTA_NEXT (attribute, len)) {
@ -715,12 +709,12 @@ void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address)
void interfaces_bring_up (int sock, Interface *interface) { void interfaces_bring_up (int sock, Interface *interface) {
struct msghdr rtnl_msg; struct msghdr rtnl_msg;
struct iovec io; struct iovec io;
nl_req_if req;
struct sockaddr_nl kernel; struct sockaddr_nl kernel;
char reply[8192]; char buffer[8192];
int len; int len;
struct nlmsghdr *nl; struct nlmsghdr *nl;
struct ifinfomsg *ifi;
struct nlmsgerr *l_err; struct nlmsgerr *l_err;
struct sockaddr_nl local_nl; struct sockaddr_nl local_nl;
@ -732,24 +726,26 @@ void interfaces_bring_up (int sock, Interface *interface) {
memset (&rtnl_msg, 0, sizeof (rtnl_msg)); memset (&rtnl_msg, 0, sizeof (rtnl_msg));
memset (&kernel, 0, sizeof (kernel)); memset (&kernel, 0, sizeof (kernel));
memset(&req, 0, sizeof(req)); memset (buffer, 0, sizeof (buffer));
kernel.nl_family = AF_NETLINK; /* fill-in kernel address (destination of our message) */ kernel.nl_family = AF_NETLINK; /* fill-in kernel address (destination of our message) */
kernel.nl_groups = 0; kernel.nl_groups = 0;
req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); nl = (struct nlmsghdr *) buffer;
req.hdr.nlmsg_type = RTM_NEWLINK; nl->nlmsg_len = NLMSG_LENGTH (sizeof (struct ifinfomsg));
req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; nl->nlmsg_type = RTM_NEWLINK;
req.hdr.nlmsg_seq = global_nl_seq++; nl->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
req.hdr.nlmsg_pid = local_nl.nl_pid; nl->nlmsg_seq = global_nl_seq++;
nl->nlmsg_pid = local_nl.nl_pid;
req.i.ifi_family = AF_PACKET; ifi = (struct ifinfomsg*) NLMSG_DATA (nl);
req.i.ifi_index = interface->index; ifi->ifi_family = AF_PACKET;
req.i.ifi_change |= IFF_UP; ifi->ifi_index = interface->index;
req.i.ifi_flags |= IFF_UP; ifi->ifi_change |= IFF_UP;
ifi->ifi_flags |= IFF_UP;
io.iov_base = &req; io.iov_base = buffer;
io.iov_len = req.hdr.nlmsg_len; io.iov_len = nl->nlmsg_len;
rtnl_msg.msg_iov = &io; rtnl_msg.msg_iov = &io;
rtnl_msg.msg_iovlen = 1; rtnl_msg.msg_iovlen = 1;
rtnl_msg.msg_name = &kernel; rtnl_msg.msg_name = &kernel;
@ -761,15 +757,15 @@ void interfaces_bring_up (int sock, Interface *interface) {
memset (&io, 0, sizeof (io)); memset (&io, 0, sizeof (io));
memset (&rtnl_msg, 0, sizeof (rtnl_msg)); memset (&rtnl_msg, 0, sizeof (rtnl_msg));
io.iov_base = reply; io.iov_base = buffer;
io.iov_len = sizeof (reply); io.iov_len = sizeof (buffer);
rtnl_msg.msg_iov = &io; rtnl_msg.msg_iov = &io;
rtnl_msg.msg_iovlen = 1; rtnl_msg.msg_iovlen = 1;
rtnl_msg.msg_name = &kernel; rtnl_msg.msg_name = &kernel;
rtnl_msg.msg_namelen = sizeof (kernel); rtnl_msg.msg_namelen = sizeof (kernel);
len = recvmsg (sock, &rtnl_msg, 0); len = recvmsg (sock, &rtnl_msg, 0);
nl = (struct nlmsghdr *) reply; nl = (struct nlmsghdr *) buffer;
for (; NLMSG_OK(nl, len); nl = NLMSG_NEXT(nl, len)) { for (; NLMSG_OK(nl, len); nl = NLMSG_NEXT(nl, len)) {
if (nl->nlmsg_type == NLMSG_DONE) { if (nl->nlmsg_type == NLMSG_DONE) {
printf ("Bring UP Msg type: DONE!\n"); printf ("Bring UP Msg type: DONE!\n");
@ -788,6 +784,83 @@ void interfaces_bring_up (int sock, Interface *interface) {
} }
} }
void interfaces_bring_down (int sock, Interface *interface) {
struct msghdr rtnl_msg;
struct iovec io;
struct sockaddr_nl kernel;
char buffer[8192];
int len;
struct nlmsghdr *nl;
struct ifinfomsg *ifi;
struct nlmsgerr *l_err;
struct sockaddr_nl local_nl;
socklen_t local_size;
/* Recuperar el puerto local del netlink */
local_size = sizeof (local_nl);
getsockname (sock, (struct sockaddr *) &local_nl, &local_size);
memset (&rtnl_msg, 0, sizeof (rtnl_msg));
memset (&kernel, 0, sizeof (kernel));
memset (buffer, 0, sizeof (buffer));
kernel.nl_family = AF_NETLINK; /* fill-in kernel address (destination of our message) */
kernel.nl_groups = 0;
nl = (struct nlmsghdr *) buffer;
nl->nlmsg_len = NLMSG_LENGTH (sizeof (struct ifinfomsg));
nl->nlmsg_type = RTM_NEWLINK;
nl->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
nl->nlmsg_seq = global_nl_seq++;
nl->nlmsg_pid = local_nl.nl_pid;
ifi = (struct ifinfomsg*) NLMSG_DATA (nl);
ifi->ifi_family = AF_PACKET;
ifi->ifi_index = interface->index;
ifi->ifi_change |= IFF_UP;
io.iov_base = buffer;
io.iov_len = nl->nlmsg_len;
rtnl_msg.msg_iov = &io;
rtnl_msg.msg_iovlen = 1;
rtnl_msg.msg_name = &kernel;
rtnl_msg.msg_namelen = sizeof (kernel);
len = sendmsg (sock, (struct msghdr *) &rtnl_msg, 0);
/* Esperar la respuesta */
memset (&io, 0, sizeof (io));
memset (&rtnl_msg, 0, sizeof (rtnl_msg));
io.iov_base = buffer;
io.iov_len = sizeof (buffer);
rtnl_msg.msg_iov = &io;
rtnl_msg.msg_iovlen = 1;
rtnl_msg.msg_name = &kernel;
rtnl_msg.msg_namelen = sizeof (kernel);
len = recvmsg (sock, &rtnl_msg, 0);
nl = (struct nlmsghdr *) buffer;
for (; NLMSG_OK(nl, len); nl = NLMSG_NEXT(nl, len)) {
if (nl->nlmsg_type == NLMSG_DONE) {
printf ("Bring DOWN Msg type: DONE!\n");
break;
}
if (nl->nlmsg_type == NLMSG_ERROR) {
l_err = (struct nlmsgerr*) NLMSG_DATA (nl);
if (nl->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr))) {
printf ("Bring down Error tamaño truncado\n");
} else if (l_err->error != 0) {
// Error:
printf ("Bring down Error: %i\n", l_err->error);
}
break;
}
}
}
void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface) { void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface) {
IPv4 *address; IPv4 *address;

View File

@ -38,9 +38,13 @@ void interfaces_del_rtnl_link (NetworkInadorHandle *handle, struct nlmsghdr *h);
void interfaces_add_or_update_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h); void interfaces_add_or_update_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h);
void interfaces_del_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h); void interfaces_del_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h);
IPv4 * _interfaces_serach_ipv4 (Interface *interface, struct in_addr address, uint32_t prefix);
void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface); void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface);
void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address); void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address);
void interfaces_manual_del_ipv4 (int sock, Interface *interface, IPv4 *address);
void interfaces_bring_up (int sock, Interface *interface); void interfaces_bring_up (int sock, Interface *interface);
void interfaces_bring_down (int sock, Interface *interface);
#endif #endif

View File

@ -32,6 +32,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <linux/if_addr.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "manager.h" #include "manager.h"
@ -44,50 +45,100 @@ enum {
MANAGER_COMMAND_REQUEST = 0, MANAGER_COMMAND_REQUEST = 0,
MANAGER_COMMAND_LIST_IFACES = 0, MANAGER_COMMAND_LIST_IFACES = 0,
MANAGER_COMMAND_SET_IPV4, MANAGER_COMMAND_BRING_UP_IFACE,
MANAGER_COMMAND_BRING_DOWN_IFACE,
MANAGER_COMMAND_CLEAR_IPV4,
MANAGER_COMMAND_ADD_IPV4,
MANAGER_COMMAND_REMOVE_IPV4,
MANAGER_COMMAND_LIST_IPV4,
MANAGER_COMMAND_RUN_DHCP_CLIENT,
MANAGER_COMMAND_LIST_ROUTES, MANAGER_COMMAND_LIST_ROUTES,
MANAGER_COMMAND_ADD_ROUTE_V4,
MANAGER_COMMAND_REMOVE_ROUTE_V4,
MANAGER_RESPONSE = 128, MANAGER_RESPONSE = 128,
MANAGER_RESPONSE_REQUEST_INVALID = 128, MANAGER_RESPONSE_REQUEST_INVALID = 128,
MANAGER_RESPONSE_PROCESING,
MANAGER_RESPONSE_PROCESSING,
MANAGER_RESPONSE_LIST_IFACES, MANAGER_RESPONSE_LIST_IFACES,
MANAGER_RESPONSE_LIST_IPV4,
MANAGER_RESPONSE_LIST_ROUTES, MANAGER_RESPONSE_LIST_ROUTES,
}; };
enum {
MANAGER_ERROR_UNKNOWN = 0,
MANAGER_ERROR_INCOMPLETE_REQUEST,
MANAGER_ERROR_UNKNOWN_COMMAND,
MANAGER_ERROR_PREFIX_INVALID,
MANAGER_ERROR_IFACE_INVALID,
MANAGER_ERROR_IPV4_INVALID
};
#define MANAGER_IFACE_TYPE_WIRELESS 0x02 #define MANAGER_IFACE_TYPE_WIRELESS 0x02
#define MANAGER_IFACE_TYPE_BRIDGE 0x04 #define MANAGER_IFACE_TYPE_BRIDGE 0x04
#define MANAGER_IFACE_TYPE_LOOPBACK 0x8 #define MANAGER_IFACE_TYPE_LOOPBACK 0x8
#define MANAGER_IFACE_TYPE_VLAN 0x10 #define MANAGER_IFACE_TYPE_VLAN 0x10
#define MANAGER_IFACE_TYPE_NLMON 0x20 #define MANAGER_IFACE_TYPE_NLMON 0x20
static void _manager_send_invalid_request (int sock, struct sockaddr_un *client, socklen_t socklen, int seq) { #define MANAGER_IFACE_IPV4_ADDRESS_SECONDARY 0x01
typedef struct {
int sock;
struct sockaddr_un client;
socklen_t socklen;
int seq;
int command;
unsigned char *full_buffer;
unsigned char *command_data;
int full_len;
int command_len;
} ManagerCommandRequest;
static void _manager_send_invalid_request (ManagerCommandRequest *request, int error) {
unsigned char buffer[128]; unsigned char buffer[128];
buffer[0] = MANAGER_RESPONSE_REQUEST_INVALID; buffer[0] = MANAGER_RESPONSE_REQUEST_INVALID;
buffer[1] = seq; buffer[1] = request->seq;
sendto (sock, buffer, 2, 0, (struct sockaddr *) client, socklen); sendto (request->sock, buffer, 2, 0, (struct sockaddr *) &request->client, request->socklen);
} }
static void _manager_send_processing (int sock, struct sockaddr_un *client, socklen_t socklen, int seq) { static void _manager_send_processing (ManagerCommandRequest *request) {
unsigned char buffer[128]; unsigned char buffer[128];
buffer[0] = MANAGER_RESPONSE_PROCESING; buffer[0] = MANAGER_RESPONSE_PROCESSING;
buffer[1] = seq; buffer[1] = request->seq;
sendto (sock, buffer, 2, 0, (struct sockaddr *) client, socklen); sendto (request->sock, buffer, 2, 0, (struct sockaddr *) &request->client, request->socklen);
} }
static void _manager_send_list_interfaces (NetworkInadorHandle *handle, int sock, struct sockaddr_un *client, socklen_t socklen, int seq) { static void _manager_send_response (ManagerCommandRequest *request, unsigned char *buffer, int len) {
sendto (request->sock, buffer, len, 0, (struct sockaddr *) &request->client, request->socklen);
}
static void _manager_send_list_interfaces (NetworkInadorHandle *handle, ManagerCommandRequest *request) {
unsigned char buffer[8192]; unsigned char buffer[8192];
Interface *iface_g; Interface *iface_g;
int pos; int pos;
int flags; int flags;
buffer[0] = MANAGER_RESPONSE_LIST_IFACES; buffer[0] = MANAGER_RESPONSE_LIST_IFACES;
buffer[1] = seq; buffer[1] = request->seq;
iface_g = handle->interfaces; iface_g = handle->interfaces;
@ -131,10 +182,113 @@ static void _manager_send_list_interfaces (NetworkInadorHandle *handle, int sock
buffer[pos] = 0; buffer[pos] = 0;
pos++; pos++;
sendto (sock, buffer, pos, 0, (struct sockaddr *) client, socklen); _manager_send_response (request, buffer, pos);
} }
static void _manager_handle_interface_set_ipv4 (NetworkInadorHandle *handle, char *buffer_read, int len, int sock, struct sockaddr_un *client, socklen_t socklen, int seq) { static void _manager_handle_clear_iface (NetworkInadorHandle *handle, ManagerCommandRequest *request) {
/* Primero, validar que haya suficientes bytes:
* 1 byte de la interfaz
*/
int index;
Interface *iface;
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;
}
interfaces_clear_all_ipv4_address (handle, iface);
_manager_send_processing (request);
}
static void _manager_handle_bring_up_down_iface (NetworkInadorHandle *handle, ManagerCommandRequest *request) {
/* Primero, validar que haya suficientes bytes:
* 1 byte de la interfaz
*/
int index;
Interface *iface;
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_BRING_UP_IFACE) {
interfaces_bring_up (handle->netlink_sock_request, iface);
} else {
interfaces_bring_down (handle->netlink_sock_request, iface);
}
_manager_send_processing (request);
}
static void _manager_handle_interface_add_ipv4 (NetworkInadorHandle *handle, ManagerCommandRequest *request) {
/* Primero, validar que haya suficientes bytes:
* 1 byte de la interfaz
* 4 bytes de la dirección
* 1 byte del prefix
*/
uint32_t prefix;
int index;
Interface *iface;
IPv4 address;
if (request->command_len < 6) {
/* Bytes insuficientes */
_manager_send_invalid_request (request, MANAGER_ERROR_INCOMPLETE_REQUEST);
return;
}
prefix = request->command_data[5];
if (prefix < 0 || prefix > 32) {
_manager_send_invalid_request (request, MANAGER_ERROR_PREFIX_INVALID);
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;
}
memcpy (&address.sin_addr, &request->command_data[1], sizeof (struct in_addr));
address.prefix = prefix;
address.next = NULL;
interfaces_manual_add_ipv4 (handle->netlink_sock_request, iface, &address);
_manager_send_processing (request);
}
static void _manager_handle_interface_del_ipv4 (NetworkInadorHandle *handle, ManagerCommandRequest *request) {
/* Primero, validar que haya suficientes bytes: /* Primero, validar que haya suficientes bytes:
* 1 byte de la interfaz * 1 byte de la interfaz
* 4 bytes de la dirección * 4 bytes de la dirección
@ -144,42 +298,104 @@ static void _manager_handle_interface_set_ipv4 (NetworkInadorHandle *handle, cha
struct in_addr sin_addr; struct in_addr sin_addr;
int index; int index;
Interface *iface; Interface *iface;
IPv4 address; IPv4 *to_del;
if (len < 6) { if (request->command_len < 6) {
/* Bytes unsuficientes */ /* Bytes insuficientes */
_manager_send_invalid_request (sock, client, socklen, seq); _manager_send_invalid_request (request, MANAGER_ERROR_INCOMPLETE_REQUEST);
return; return;
} }
prefix = buffer_read[5]; prefix = request->command_data[5];
if (prefix < 0 || prefix > 32) { if (prefix < 0 || prefix > 32) {
_manager_send_invalid_request (sock, client, socklen, seq); _manager_send_invalid_request (request, MANAGER_ERROR_PREFIX_INVALID);
return; return;
} }
index = buffer_read[0]; index = request->command_data[0];
iface = interfaces_locate_by_index (handle->interfaces, index); iface = interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) { if (iface == NULL) {
_manager_send_invalid_request (sock, client, socklen, seq); _manager_send_invalid_request (request, MANAGER_ERROR_IFACE_INVALID);
return; return;
} }
memcpy (&address.sin_addr, &buffer_read[1], sizeof (struct in_addr)); memcpy (&sin_addr, &request->command_data[1], sizeof (struct in_addr));
address.prefix = prefix;
address.next = NULL;
interfaces_clear_all_ipv4_address (handle, iface); to_del = _interfaces_serach_ipv4 (iface, sin_addr, prefix);
interfaces_manual_add_ipv4 (handle->netlink_sock_request, iface, &address);
_manager_send_processing (sock, client, socklen, seq); if (to_del == NULL) {
_manager_send_invalid_request (request, MANAGER_ERROR_IPV4_INVALID);
return;
} }
static void _manager_send_list_routes (NetworkInadorHandle *handle, int sock, struct sockaddr_un *client, socklen_t socklen, int seq) { interfaces_manual_del_ipv4 (handle->netlink_sock_request, iface, to_del);
_manager_send_processing (request);
}
static void _manager_send_list_ipv4 (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;
}
buffer[0] = MANAGER_RESPONSE_LIST_IPV4;
buffer[1] = request->seq;
ip_g = iface->v4_address;
count = 0;
pos = 6;
while (ip_g != NULL) {
memcpy (&buffer[pos], &ip_g->sin_addr, sizeof (ip_g->sin_addr));
pos = pos + sizeof (ip_g->sin_addr);
buffer[pos] = ip_g->prefix;
buffer[pos + 1] = 0;
if (ip_g->flags & IFA_F_SECONDARY) {
buffer[pos + 1] |= MANAGER_IFACE_IPV4_ADDRESS_SECONDARY;
}
pos = pos + 2;
ip_g = ip_g->next;
count++;
}
memcpy (&buffer[2], &count, sizeof (int));
_manager_send_response (request, buffer, pos);
}
static void _manager_send_list_routes (NetworkInadorHandle *handle, ManagerCommandRequest *request) {
unsigned char buffer[8192]; unsigned char buffer[8192];
Routev4 *route_g; Routev4 *route_g;
int pos; int pos;
@ -187,7 +403,7 @@ static void _manager_send_list_routes (NetworkInadorHandle *handle, int sock, st
unsigned int count; unsigned int count;
buffer[0] = MANAGER_RESPONSE_LIST_ROUTES; buffer[0] = MANAGER_RESPONSE_LIST_ROUTES;
buffer[1] = seq; buffer[1] = request->seq;
route_g = handle->rtable_v4; route_g = handle->rtable_v4;
@ -213,44 +429,61 @@ static void _manager_send_list_routes (NetworkInadorHandle *handle, int sock, st
memcpy (&buffer[2], &count, sizeof (int)); memcpy (&buffer[2], &count, sizeof (int));
sendto (sock, buffer, pos, 0, (struct sockaddr *) client, socklen); _manager_send_response (request, buffer, pos);
} }
static gboolean _manager_client_data (GIOChannel *source, GIOCondition condition, gpointer data) { static gboolean _manager_client_data (GIOChannel *source, GIOCondition condition, gpointer data) {
NetworkInadorHandle *handle = (NetworkInadorHandle *) data; NetworkInadorHandle *handle = (NetworkInadorHandle *) data;
int sock;
unsigned char buffer[4096]; unsigned char buffer[4096];
struct sockaddr_un client_name; ManagerCommandRequest request;
socklen_t socklen;
int len;
int seq;
sock = g_io_channel_unix_get_fd (source); request.full_buffer = buffer;
socklen = sizeof (client_name); request.sock = g_io_channel_unix_get_fd (source);
len = recvfrom (sock, buffer, sizeof (buffer), 0, (struct sockaddr *) &client_name, &socklen);
request.socklen = sizeof (request.client);
request.full_len = recvfrom (request.sock, buffer, sizeof (buffer), 0, (struct sockaddr *) &request.client, &request.socklen);
request.command_data = &buffer[2];
request.command_len = request.full_len - 2;
request.seq = 0;
/* Procesar aquí la petición */ /* Procesar aquí la petición */
if (len < 2) { if (request.full_len < 2) {
_manager_send_invalid_request (sock, &client_name, socklen, 0); _manager_send_invalid_request (&request, MANAGER_ERROR_INCOMPLETE_REQUEST);
return TRUE; return TRUE;
} }
seq = buffer[1]; request.command = buffer[0];
request.seq = buffer[1];
switch (buffer[0]) { switch (request.command) {
case MANAGER_COMMAND_LIST_IFACES: case MANAGER_COMMAND_LIST_IFACES:
_manager_send_list_interfaces (handle, sock, &client_name, socklen, seq); _manager_send_list_interfaces (handle, &request);
break; break;
case MANAGER_COMMAND_SET_IPV4: case MANAGER_COMMAND_BRING_UP_IFACE:
_manager_handle_interface_set_ipv4 (handle, &buffer[2], len - 2, sock, &client_name, socklen, seq); case MANAGER_COMMAND_BRING_DOWN_IFACE:
_manager_handle_bring_up_down_iface (handle, &request);
break;
case MANAGER_COMMAND_CLEAR_IPV4:
_manager_handle_clear_iface (handle, &request);
break;
case MANAGER_COMMAND_ADD_IPV4:
_manager_handle_interface_add_ipv4 (handle, &request);
break;
case MANAGER_COMMAND_REMOVE_IPV4:
_manager_handle_interface_del_ipv4 (handle, &request);
break;
case MANAGER_COMMAND_LIST_IPV4:
_manager_send_list_ipv4 (handle, &request);
break; break;
case MANAGER_COMMAND_LIST_ROUTES: case MANAGER_COMMAND_LIST_ROUTES:
_manager_send_list_routes (handle, sock, &client_name, socklen, seq); _manager_send_list_routes (handle, &request);
break; break;
default: default:
_manager_send_invalid_request (sock, &client_name, socklen, seq); _manager_send_invalid_request (&request, MANAGER_ERROR_UNKNOWN_COMMAND);
} }
return TRUE; return TRUE;
@ -262,7 +495,7 @@ int manager_setup_socket (NetworkInadorHandle *handle) {
struct sockaddr_un socket_name; struct sockaddr_un socket_name;
GIOChannel *channel; GIOChannel *channel;
sock = socket (AF_UNIX, SOCK_DGRAM, 0); sock = socket (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sock < 0) { if (sock < 0) {
perror ("Failed to create AF_UNIX socket"); perror ("Failed to create AF_UNIX socket");

View File

@ -49,11 +49,11 @@
static GMainLoop *loop = NULL; static GMainLoop *loop = NULL;
int create_ntlink_socket (int groups) { int create_route_netlink_socket (int groups) {
int fd; int fd;
struct sockaddr_nl local; struct sockaddr_nl local;
fd = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); fd = socket (AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
if (fd < 0) { if (fd < 0) {
return -1; return -1;