From 4a394cb400f1fead252527de2c9412bda6fc4fd4 Mon Sep 17 00:00:00 2001 From: Saverio Proto Date: Tue, 20 May 2014 23:15:45 +0200 Subject: [PATCH] Implemented meshlink_blacklist feature using the meshlink_blacklist function the application is able to blacklist a node data from with node will be discarded. No more data will be sent to this node. the blacklisting is persistent in the host configuration file --- src/meshlink.c | 9 +++++++++ src/net.c | 2 ++ src/net_packet.c | 8 ++++++++ src/net_socket.c | 4 ++++ src/node.h | 5 +++-- 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/meshlink.c b/src/meshlink.c index bfadf61b..816f77ab 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1248,6 +1248,15 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { } void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) { + node_t *n; + n = (node_t*)node; + n->status.blacklisted=true; + fprintf(stderr, "Blacklisted %s.\n",node->name); + + //Make blacklisting persistent in the config file + append_config_file(mesh, n->name, "blacklisted", "yes"); + return; + } static void __attribute__((constructor)) meshlink_init(void) { diff --git a/src/net.c b/src/net.c index 866bacb1..deac6eee 100644 --- a/src/net.c +++ b/src/net.c @@ -178,6 +178,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { } if(!found) { + //TODO: if the node is blacklisted the connection will not happen, but + //the user will read this debug message "Autoconnecting to %s" that is misleading logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name); outgoing_t *outgoing = xzalloc(sizeof *outgoing); outgoing->name = xstrdup(n->name); diff --git a/src/net_packet.c b/src/net_packet.c index f3809e87..8367a9c5 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -276,10 +276,14 @@ static void receive_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *pac logger(DEBUG_TRAFFIC, LOG_DEBUG, "Received packet of %d bytes from %s (%s)", packet->len, n->name, n->hostname); + if (n->status.blacklisted) { + logger(DEBUG_PROTOCOL, LOG_WARNING, "Dropping packet from blacklisted node %s", n->name); + } else { n->in_packets++; n->in_bytes += packet->len; route(mesh, n, packet); + } } static bool try_mac(meshlink_handle_t *mesh, node_t *n, const vpn_packet_t *inpkt) { @@ -654,6 +658,10 @@ void handle_incoming_vpn_data(event_loop_t *loop, void *data, int flags) { return; } + if (n->status.blacklisted) { + logger(DEBUG_PROTOCOL, LOG_WARNING, "Dropping packet from blacklisted node %s", n->name); + return; + } n->sock = ls - mesh->listen_socket; receive_udppacket(mesh, n, &pkt); diff --git a/src/net_socket.c b/src/net_socket.c index 3cbc5dd6..2ddf837b 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -503,6 +503,7 @@ static struct addrinfo *get_known_addresses(node_t *n) { } void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) { + bool blacklisted = false; timeout_del(&mesh->loop, &outgoing->ev); node_t *n = lookup_node(mesh, outgoing->name); @@ -518,6 +519,9 @@ void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) { read_host_config(mesh, outgoing->config_tree, outgoing->name); outgoing->cfg = lookup_config(outgoing->config_tree, "Address"); + get_config_bool(lookup_config(outgoing->config_tree, "blacklisted"), &blacklisted); + if (blacklisted) return; + if(!outgoing->cfg) { if(n) outgoing->aip = outgoing->ai = get_known_addresses(n); diff --git a/src/node.h b/src/node.h index 3a779af9..f3930895 100644 --- a/src/node.h +++ b/src/node.h @@ -33,8 +33,9 @@ typedef struct node_status_t { unsigned int indirect:1; /* 1 if this node is not directly reachable by us */ unsigned int unused_sptps:1; /* 1 if this node supports SPTPS */ unsigned int udp_confirmed:1; /* 1 if the address is one that we received UDP traffic on */ - unsigned int broadcast:1; /* 1 if the next UDP packet should be broadcast to the local network */ - unsigned int unused:23; + unsigned int broadcast:1; /* 1 if the next UDP packet should be broadcast to the local network */ + unsigned int blacklisted:1; /* 1 if the node is blacklist so we never want to speak with him anymore*/ + unsigned int unused:22; } node_status_t; typedef struct node_t { -- 2.39.2