Statistics
| Branch: | Revision:

streamers / blacklist.c @ 5a3fd26a

History | View | Annotate | Download (4.7 KB)

1
/*
2
 *  Copyright (c) 2010 Stefano Traverso
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7

    
8
#include <stdint.h>
9
#include <stdio.h>
10
#include <stdbool.h>
11
#include <stdlib.h>
12
#include <sys/time.h>
13
#include <net_helper.h>
14
#include <peerset.h>
15
#include <peer.h>
16
#include <string.h>
17

    
18
#include "dbg.h"
19
#include "blacklist.h"
20
#include "nodeid_set.h"
21

    
22

    
23
static struct black_list *bl = NULL;
24

    
25
// Add peer to black list and register the insertion time.
26
int add_to_blacklist(char *node_addr) {
27
        struct black_list *bl2 = NULL;
28
        struct timeval current_time;
29
        gettimeofday(&current_time, NULL);
30

    
31
        //List is not empty: insertion in the head
32
        if (bl != NULL) {
33
                dprintf("BLACKLIST: adding peer %s to the list, insertion_time %f (not empty)\n",  node_addr,(current_time.tv_sec + current_time.tv_usec*1e-6));
34
                bl2 = (struct black_list*) malloc(sizeof(struct black_list));
35
                bl2->blp.insertion_time = current_time.tv_sec + current_time.tv_usec*1e-6;
36
                bl2->blp.node_addr = NULL;
37
                bl2->blp.node_addr = (char*) malloc(strlen(node_addr));
38
                strcpy(bl2->blp.node_addr, node_addr);
39
                bl->previous = bl2;
40
                bl2->next = bl;
41
                bl2->previous = NULL;
42
                bl = bl2;
43
                return 0;
44
                }
45
        else { // This is the first element to insert
46
                bl = (struct black_list*) malloc(sizeof(struct black_list));
47
                bl->blp.insertion_time = current_time.tv_sec + current_time.tv_usec*1e-6;
48
                dprintf("BLACKLIST: adding peer %s to the list, insertion_time %f (empty)\n",  node_addr,(current_time.tv_sec + current_time.tv_usec*1e-6));
49
                bl->blp.node_addr = NULL;
50
                bl->blp.node_addr = (char*) malloc(strlen(node_addr));
51
                strcpy(bl->blp.node_addr, node_addr);
52
                bl->next = NULL;
53
                bl->previous = NULL;
54
                return 0;
55
                }
56
        return 1;
57
        }
58
        
59
// Remove peer from the black list and returns following one
60
struct black_list *remove_from_blacklist(struct black_list *bl_iterator) {
61
        
62
        struct black_list *bl_aux = NULL;
63
     
64
        // I have to remove the first element of the list
65
        if (bl != NULL) {
66
                    dprintf("BLACKLIST: removing peer %s from the list, insertion_time %f\n", bl_iterator->blp.node_addr, bl_iterator->blp.insertion_time);
67
                if (bl_iterator == bl){
68
                        dprintf("BLACKLIST REMO 1: removing peer %s from the list, insertion_time %f (first)\n", bl_iterator->blp.node_addr, bl_iterator->blp.insertion_time);
69
                        //First and last
70
                        if (bl_iterator->next != NULL)
71
                                bl_iterator->next->previous = NULL;
72
                        bl = bl_iterator->next;
73
                        free(bl_iterator);
74
                        bl_iterator = bl;
75
                        return bl_iterator;
76
                        }
77
                else if (bl_iterator->next == NULL) { // If it is the last element
78
                        dprintf("BLACKLIST REMO 2: removing peer %s from the list, insertion_time %f (last)\n", bl_iterator->blp.node_addr, bl_iterator->blp.insertion_time);
79
                        bl_iterator->previous->next = NULL;
80
                        free(bl_iterator);
81
                        bl_iterator = NULL;
82
                        return bl_iterator; 
83
                        }
84
                else { // I have to remove an element in the middle
85
                        dprintf("BLACKLIST REMO 3: removing peer %s from the list, insertion_time %f (middle)\n", bl_iterator->blp.node_addr, bl_iterator->blp.insertion_time);
86
                        bl_iterator->next->previous = bl_iterator->previous;
87
                        bl_iterator->previous->next = bl_iterator->next;
88
                        bl_aux = bl_iterator->next;
89
                        free(bl_iterator);
90
                        return bl_aux;
91
                        }
92
                }
93
        return bl;
94
        }
95
        
96
// Check if peer is blacklisted
97
int black_listed(char *node_addr) {
98
        struct black_list *bl_iterator;
99
        // Start from the beginning of the list
100
        bl_iterator = bl;
101
        //dprintf("BLACKLIST: looking for peer %s in the list\n", node_addr);
102
        // Iterate the list until you get the right element
103
        while (bl_iterator != NULL) {
104
                if (strcmp(bl_iterator->blp.node_addr, node_addr) == 0) {
105
                        dprintf("BLACKLIST: found %s in the list\n", node_addr);
106
                        return 1;
107
                        }
108
                bl_iterator = bl_iterator->next;
109
                }
110
        return 0;
111
        }
112
        
113
// Check what peers can be removed from bl due to timeout expiration
114
int check_black_list_timeouts() {
115
        struct black_list *bl_iterator, *bl_aux;
116
        // Start from the beginning of the list
117
        bl_iterator = bl;
118
        bl_aux = NULL;
119
        
120
        double current_time;
121
        struct timeval current_time_timeval;
122
        gettimeofday(&current_time_timeval, NULL);
123
        // Convert current_time_timeval to double
124
        current_time = current_time_timeval.tv_sec + current_time_timeval.tv_usec*1e-6; 
125
        
126
        if (bl_iterator == NULL) {
127
                //dprintf("BLACKLIST: empty %f...\n", current_time);
128
                }
129
        
130
        while (bl_iterator != NULL) {
131
                if ((current_time - bl_iterator->blp.insertion_time) > BLACKLIST_ENTRY_MAX_LIFETIME) {
132
                        dprintf("BLACKLIST EXPIRATION 1: checking timeout peer %s, time %f...\n", bl_iterator->blp.node_addr, current_time);
133
                        bl_aux = remove_from_blacklist(bl_iterator);
134
                        bl_iterator = bl_aux;
135
                        }
136
                else {
137
                        dprintf("BLACKLIST EXPIRATION 2: checking timeout peer %s, time %f...\n", bl_iterator->blp.node_addr, current_time);
138
                        bl_iterator = bl_iterator->next;
139
                        }
140
                }
141
        return 0;
142
        }