Agrego manejador de peticiones por socket unix.

master
Félix Arreola Rodríguez 2018-08-08 23:18:35 -05:00
parent b776278ccb
commit c03db09384
7 changed files with 498 additions and 6 deletions

View File

@ -4,6 +4,7 @@ bin_PROGRAMS = network-inador
network_inador_SOURCES = network-inador.c network-inador.h \ network_inador_SOURCES = network-inador.c network-inador.h \
events.c events.h \ events.c events.h \
interfaces.c interfaces.h \ interfaces.c interfaces.h \
manager.c manager.h \
utils.c utils.h utils.c utils.h
#network_inador_CPPFLAGS = -DGAMEDATA_DIR=\"$(gamedatadir)/\" -DLOCALEDIR=\"$(localedir)\" $(AM_CPPFLAGS) #network_inador_CPPFLAGS = -DGAMEDATA_DIR=\"$(gamedatadir)/\" -DLOCALEDIR=\"$(localedir)\" $(AM_CPPFLAGS)

View File

@ -43,7 +43,7 @@
#include "interfaces.h" #include "interfaces.h"
static Interface * _interfaces_locate_by_index (Interface *list, int index); Interface * interfaces_locate_by_index (Interface *list, int index);
static void _interfaces_append_ipv4_to_struct (Interface *interface, struct in_addr address, uint32_t prefix); static void _interfaces_append_ipv4_to_struct (Interface *interface, struct in_addr address, uint32_t prefix);
int global_nl_seq = 1; int global_nl_seq = 1;
@ -53,7 +53,7 @@ typedef struct {
struct rtgenmsg gen; struct rtgenmsg gen;
} nl_req_t; } nl_req_t;
static Interface * _interfaces_locate_by_index (Interface *list, int index) { Interface * interfaces_locate_by_index (Interface *list, int index) {
Interface *iface; Interface *iface;
iface = list; iface = list;
@ -182,7 +182,7 @@ void interfaces_add_or_update_rtnl_link (NetworkInadorHandle *handle, struct nlm
len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg)); len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
printf ("Mensaje de nueva interfaz\n"); printf ("Mensaje de nueva interfaz\n");
new = _interfaces_locate_by_index (handle->interfaces, iface->ifi_index); new = interfaces_locate_by_index (handle->interfaces, iface->ifi_index);
/* Si el objeto interface no existe, crearlo y ligarlo en la lista de interfaces */ /* Si el objeto interface no existe, crearlo y ligarlo en la lista de interfaces */
if (new == NULL) { if (new == NULL) {
@ -246,7 +246,7 @@ void interfaces_del_rtnl_link (NetworkInadorHandle *handle, struct nlmsghdr *h)
IPv4 *address; IPv4 *address;
iface = NLMSG_DATA(h); iface = NLMSG_DATA(h);
to_del = _interfaces_locate_by_index (handle->interfaces, iface->ifi_index); to_del = interfaces_locate_by_index (handle->interfaces, iface->ifi_index);
if (to_del == NULL) { if (to_del == NULL) {
printf ("Error, solicitaron eliminar interfaz que ya no existe\n"); printf ("Error, solicitaron eliminar interfaz que ya no existe\n");
@ -294,7 +294,7 @@ void interfaces_add_or_update_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr
prefix = addr->ifa_prefixlen; prefix = addr->ifa_prefixlen;
iface = _interfaces_locate_by_index (handle->interfaces, addr->ifa_index); iface = interfaces_locate_by_index (handle->interfaces, addr->ifa_index);
if (iface == NULL) { if (iface == NULL) {
/* No encuentro la interfaz... */ /* No encuentro la interfaz... */
@ -337,7 +337,7 @@ void interfaces_del_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h) {
prefix = addr->ifa_prefixlen; prefix = addr->ifa_prefixlen;
iface = _interfaces_locate_by_index (handle->interfaces, addr->ifa_index); iface = interfaces_locate_by_index (handle->interfaces, addr->ifa_index);
if (iface == NULL) { if (iface == NULL) {
/* No encuentro la interfaz... */ /* No encuentro la interfaz... */
@ -380,6 +380,210 @@ void interfaces_del_ipv4 (NetworkInadorHandle *handle, struct nlmsghdr *h) {
} }
} }
void interfaces_manual_del_ipv4 (int sock, Interface *interface, IPv4 *address) {
struct msghdr rtnl_msg;
struct iovec io;
struct sockaddr_nl kernel;
char buffer[8192];
int len;
struct nlmsghdr *nl;
struct ifaddrmsg *ifa;
struct rtattr *rta;
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 (&kernel, 0, sizeof (kernel));
memset (buffer, 0, sizeof (buffer));
memset (&rtnl_msg, 0, sizeof (rtnl_msg));
memset (&io, 0, sizeof (io));
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 ifaddrmsg));
nl->nlmsg_type = RTM_DELADDR;
nl->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
nl->nlmsg_seq = global_nl_seq++;
nl->nlmsg_pid = local_nl.nl_pid;
ifa = (struct ifaddrmsg*) NLMSG_DATA (nl);
ifa->ifa_family = AF_INET; // we only get ipv4 address here
ifa->ifa_prefixlen = address->prefix;
ifa->ifa_flags = IFA_F_PERMANENT;
ifa->ifa_scope = 0;
ifa->ifa_index = interface->index;
rta = (struct rtattr*) IFA_RTA(ifa);
rta->rta_type = IFA_LOCAL;
memcpy (RTA_DATA(rta), &address->sin_addr, sizeof (struct in_addr));
rta->rta_len = RTA_LENGTH(sizeof (struct in_addr));
// update nlmsghdr length
nl->nlmsg_len = NLMSG_ALIGN(nl->nlmsg_len) + rta->rta_len;
// del interface address
len = sizeof (buffer) - nl->nlmsg_len;
rta = (struct rtattr*) RTA_NEXT (rta, len);
rta->rta_type = IFA_ADDRESS;
memcpy (RTA_DATA(rta), &address->sin_addr, sizeof (struct in_addr));
rta->rta_len = RTA_LENGTH(sizeof (struct in_addr));
// update nlmsghdr length
nl->nlmsg_len += rta->rta_len;
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 ("DEL IP 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 ("DEL IP Error tamaño truncado\n");
} else if (l_err->error != 0) {
// Error:
printf ("DEL IP Error: %i\n", l_err->error);
}
break;
}
}
}
void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address) {
struct msghdr rtnl_msg;
struct iovec io;
struct sockaddr_nl kernel;
char buffer[8192];
int len;
struct nlmsghdr *nl;
struct ifaddrmsg *ifa;
struct rtattr *rta;
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 (&kernel, 0, sizeof (kernel));
memset (buffer, 0, sizeof (buffer));
memset (&io, 0, sizeof (io));
memset (&rtnl_msg, 0, sizeof (rtnl_msg));
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 ifaddrmsg));
nl->nlmsg_type = RTM_NEWADDR;
nl->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
nl->nlmsg_seq = global_nl_seq++;
nl->nlmsg_pid = local_nl.nl_pid;
ifa = (struct ifaddrmsg*) NLMSG_DATA (nl);
ifa->ifa_family = AF_INET; // we only get ipv4 address here
ifa->ifa_prefixlen = address->prefix;
ifa->ifa_flags = IFA_F_PERMANENT;
ifa->ifa_scope = 0;
ifa->ifa_index = interface->index;
rta = (struct rtattr*) IFA_RTA(ifa);
rta->rta_type = IFA_LOCAL;
memcpy (RTA_DATA(rta), &address->sin_addr, sizeof (struct in_addr));
rta->rta_len = RTA_LENGTH(sizeof (struct in_addr));
// update nlmsghdr length
nl->nlmsg_len = NLMSG_ALIGN(nl->nlmsg_len) + rta->rta_len;
// del interface address
len = sizeof (buffer) - nl->nlmsg_len;
rta = (struct rtattr*) RTA_NEXT (rta, len);
rta->rta_type = IFA_ADDRESS;
memcpy (RTA_DATA(rta), &address->sin_addr, sizeof (struct in_addr));
rta->rta_len = RTA_LENGTH(sizeof (struct in_addr));
// update nlmsghdr length
nl->nlmsg_len += rta->rta_len;
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));
memset (buffer, 0, sizeof (buffer));
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 ("Add IP 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 ("Add IP Error tamaño truncado\n");
} else if (l_err->error != 0) {
// Error:
printf ("Add IP Error: %i\n", l_err->error);
}
break;
}
}
}
void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface) {
IPv4 *address;
address = interface->v4_address;
while (address != NULL) {
interfaces_manual_del_ipv4 (handle->netlink_sock_request, interface, address);
address = address->next;
}
}
void interfaces_list_all (NetworkInadorHandle *handle, int sock) { void interfaces_list_all (NetworkInadorHandle *handle, int sock) {
struct msghdr rtnl_msg; /* generic msghdr struct for use with sendmsg */ struct msghdr rtnl_msg; /* generic msghdr struct for use with sendmsg */
struct iovec io; struct iovec io;

View File

@ -23,15 +23,23 @@
#ifndef __INTERFACES_H__ #ifndef __INTERFACES_H__
#define __INTERFACES_H__ #define __INTERFACES_H__
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include "network-inador.h" #include "network-inador.h"
extern int global_nl_seq; extern int global_nl_seq;
void interfaces_list_all (NetworkInadorHandle *handle, int sock); void interfaces_list_all (NetworkInadorHandle *handle, int sock);
Interface * interfaces_locate_by_index (Interface *list, int index);
void interfaces_add_or_update_rtnl_link (NetworkInadorHandle *handle, struct nlmsghdr *h); void interfaces_add_or_update_rtnl_link (NetworkInadorHandle *handle, struct nlmsghdr *h);
void interfaces_del_rtnl_link (NetworkInadorHandle *handle, struct nlmsghdr *h); 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);
void interfaces_clear_all_ipv4_address (NetworkInadorHandle *handle, Interface *interface);
void interfaces_manual_add_ipv4 (int sock, Interface *interface, IPv4 *address);
#endif #endif

239
src/manager.c 100644
View File

@ -0,0 +1,239 @@
/*
* manager.c
* This file is part of Network-inador
*
* Copyright (C) 2011 - 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 <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include "manager.h"
#include "interfaces.h"
#include "network-inador.h"
#define SOCKET_PATH "/tmp/network-inador.socket"
enum {
MANAGER_COMMAND_REQUEST = 0,
MANAGER_COMMAND_LIST_IFACES = 0,
MANAGER_COMMAND_SET_IPV4,
MANAGER_RESPONSE = 128,
MANAGER_RESPONSE_REQUEST_INVALID = 128,
MANAGER_RESPONSE_PROCESING,
MANAGER_RESPONSE_LIST_IFACES,
};
#define MANAGER_IFACE_TYPE_WIRELESS 2
#define MANAGER_IFACE_TYPE_BRIDGE 4
#define MANAGER_IFACE_TYPE_LOOPBACK 8
static void _manager_send_invalid_request (int sock, struct sockaddr_un *client, socklen_t socklen, int seq) {
unsigned char buffer[128];
buffer[0] = MANAGER_RESPONSE_REQUEST_INVALID;
buffer[1] = seq;
sendto (sock, buffer, 2, 0, (struct sockaddr *) client, socklen);
}
static void _manager_send_processing (int sock, struct sockaddr_un *client, socklen_t socklen, int seq) {
unsigned char buffer[128];
buffer[0] = MANAGER_RESPONSE_PROCESING;
buffer[1] = seq;
sendto (sock, buffer, 2, 0, (struct sockaddr *) client, socklen);
}
static void _manager_send_list_interfaces (NetworkInadorHandle *handle, int sock, struct sockaddr_un *client, socklen_t socklen, int seq) {
unsigned char buffer[8192];
Interface *iface_g;
int pos;
int flags;
buffer[0] = MANAGER_RESPONSE_LIST_IFACES;
buffer[1] = seq;
iface_g = handle->interfaces;
pos = 2;
while (iface_g != NULL) {
buffer[pos] = iface_g->index;
flags = 0;
if (iface_g->is_loopback) {
flags |= MANAGER_IFACE_TYPE_LOOPBACK;
}
if (iface_g->is_wireless) {
flags |= MANAGER_IFACE_TYPE_WIRELESS;
}
if (iface_g->is_bridge) {
flags |= MANAGER_IFACE_TYPE_BRIDGE;
}
buffer[pos + 1] = flags;
/* Copiar la mac address */
memcpy (&buffer[pos + 2], iface_g->real_hw, ETHER_ADDR_LEN);
/* Copiar el nombre y el terminador de cadena */
memcpy (&buffer[pos + 2 + ETHER_ADDR_LEN], iface_g->name, strlen (iface_g->name) + 1);
pos += 2 + ETHER_ADDR_LEN + strlen (iface_g->name) + 1;
iface_g = iface_g->next;
}
buffer[pos] = 0;
pos++;
sendto (sock, buffer, pos, 0, (struct sockaddr *) client, socklen);
}
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) {
/* Primero, validar que haya suficientes bytes:
* 1 byte de la interfaz
* 4 bytes de la dirección
* 1 byte del prefix
*/
uint32_t prefix;
struct in_addr sin_addr;
int index;
Interface *iface;
IPv4 address;
if (len < 6) {
/* Bytes unsuficientes */
_manager_send_invalid_request (sock, client, socklen, seq);
return;
}
prefix = buffer_read[5];
if (prefix < 0 || prefix > 32) {
_manager_send_invalid_request (sock, client, socklen, seq);
return;
}
index = buffer_read[0];
iface = interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
_manager_send_invalid_request (sock, client, socklen, seq);
return;
}
memcpy (&address.sin_addr, &buffer_read[1], sizeof (struct in_addr));
address.prefix = prefix;
address.next = NULL;
interfaces_clear_all_ipv4_address (handle, iface);
interfaces_manual_add_ipv4 (handle->netlink_sock_request, iface, &address);
_manager_send_processing (sock, client, socklen, seq);
}
static gboolean _manager_client_data (GIOChannel *source, GIOCondition condition, gpointer data) {
NetworkInadorHandle *handle = (NetworkInadorHandle *) data;
int sock;
unsigned char buffer[4096];
struct sockaddr_un client_name;
socklen_t socklen;
int len;
int seq;
sock = g_io_channel_unix_get_fd (source);
socklen = sizeof (client_name);
len = recvfrom (sock, buffer, sizeof (buffer), 0, (struct sockaddr *) &client_name, &socklen);
/* Procesar aquí la petición */
if (len < 2) {
_manager_send_invalid_request (sock, &client_name, socklen, 0);
return TRUE;
}
seq = buffer[1];
switch (buffer[0]) {
case MANAGER_COMMAND_LIST_IFACES:
_manager_send_list_interfaces (handle, sock, &client_name, socklen, seq);
break;
case MANAGER_COMMAND_SET_IPV4:
_manager_handle_interface_set_ipv4 (handle, &buffer[2], len - 2, sock, &client_name, socklen, seq);
break;
}
return TRUE;
}
int manager_setup_socket (NetworkInadorHandle *handle) {
int sock;
struct sockaddr_un socket_name;
GIOChannel *channel;
sock = socket (AF_UNIX, SOCK_DGRAM, 0);
if (sock < 0) {
perror ("Failed to create AF_UNIX socket");
return -1;
}
memset (&socket_name, 0, sizeof (struct sockaddr_un));
socket_name.sun_family = AF_UNIX;
strncpy (socket_name.sun_path, SOCKET_PATH, sizeof (socket_name.sun_path) - 1);
unlink (SOCKET_PATH);
if (bind (sock, (struct sockaddr *) &socket_name, sizeof (struct sockaddr_un)) < 0) {
perror ("Bind");
return -1;
}
/* TODO: Aplicar permisos aquí */
chmod (SOCKET_PATH, 0666);
channel = g_io_channel_unix_new (sock);
g_io_add_watch (channel, G_IO_IN | G_IO_PRI, _manager_client_data, handle);
return 0;
}

31
src/manager.h 100644
View File

@ -0,0 +1,31 @@
/*
* manager.h
* This file is part of Network-inador
*
* Copyright (C) 2011 - 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
*/
#ifndef __MANAGER_H__
#define __MANAGER_H__
#include "network-inador.h"
int manager_setup_socket (NetworkInadorHandle *handle);
#endif

View File

@ -43,6 +43,7 @@
#include "network-inador.h" #include "network-inador.h"
#include "interfaces.h" #include "interfaces.h"
#include "events.h" #include "events.h"
#include "manager.h"
static GMainLoop *loop = NULL; static GMainLoop *loop = NULL;
@ -88,12 +89,15 @@ int main (int argc, char *argv[]) {
Interface *to_up; Interface *to_up;
nl_sock = create_ntlink_socket (0); nl_sock = create_ntlink_socket (0);
handle.netlink_sock_request = nl_sock;
nl_watch = create_ntlink_socket (-1); nl_watch = create_ntlink_socket (-1);
interfaces_list_all (&handle, nl_sock); interfaces_list_all (&handle, nl_sock);
events_setup_loop (&handle, nl_watch); events_setup_loop (&handle, nl_watch);
manager_setup_socket (&handle);
g_main_loop_run (loop); g_main_loop_run (loop);
return 0; return 0;

View File

@ -23,6 +23,10 @@
#ifndef __NETWORK_INADOR_H__ #ifndef __NETWORK_INADOR_H__
#define __NETWORK_INADOR_H__ #define __NETWORK_INADOR_H__
#include <netinet/in.h>
#include <linux/if.h>
#include <net/ethernet.h>
typedef struct _IPv4 { typedef struct _IPv4 {
struct in_addr sin_addr; struct in_addr sin_addr;
uint32_t prefix; uint32_t prefix;
@ -52,6 +56,7 @@ typedef struct _Interface {
typedef struct { typedef struct {
Interface *interfaces; Interface *interfaces;
int netlink_sock_request;
} NetworkInadorHandle; } NetworkInadorHandle;
#endif #endif