-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_linked_list.c
175 lines (146 loc) · 4.02 KB
/
ip_linked_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "definitions.h"
#include "ip_linked_list.h"
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>
char addLine(IPNode *table, IPNode *line)
{
if(table == NULL) return __ERROR__;
// If the entry is already in the list delet it
// Just to maintain consistency
removeLine(table, line->dstIP, line->gatewayIP, line->netmask);
// blocks the table
sem_wait(&(table->semaphore));
// adds lines in a sorted way
IPNode *prev, *current, *aux;
prev = table;
current = table->next;
char found = 0;
while(current != NULL && !found)
{
if((line->dstIP & line->netmask) > (current->dstIP & current->netmask))
{
// if the new line entry has a minor prefix lights up found flag
found = 1;
}
if(!found)
{
aux = current->next;
prev = current;
current = aux;
}
}
line->next = prev->next;
prev->next = line;
// releases the table
sem_post(&(table->semaphore));
return __OK__;
}
char removeLine(IPNode *table, unsigned int dstIP, unsigned int gatewayIP, unsigned int netmask)
{
// The table is blocked when a deletion is done
IPNode *prev;
prev = searchLine(table, dstIP, gatewayIP, netmask);
sem_wait(&(table->semaphore));
if(prev != NULL)
{
IPNode *n = prev->next;
prev->next = n->next;
free(n);
sem_post(&(table->semaphore));
return __OK__;
}
sem_post(&(table->semaphore));
return __ERROR__;
}
IPNode* searchLineWithMask(IPNode *table, unsigned int ipDest)
{
sem_wait(&(table->semaphore));
if(table == NULL){
printf("tabela nula\n");
};
IPNode *n = table;
if(n->next == NULL)
{
printf("tabela->next nula\n");
}
while(n->next != NULL)
{
// printf("IP dst pacote: %x, Mascara: %x, AND: %x, IP dts tabela: %x\n", ipDest, (unsigned int)(n->next)->netmask,ipDest & ((n->next)->netmask), (unsigned int)((n->next)->dstIP));
if( (ipDest & ((n->next)->netmask)) == (n->next)->dstIP)
{
sem_post(&(table->semaphore));
return n;
}
n = n->next;
}
sem_post(&(table->semaphore));
return NULL;
}
// always returns the previous node to the desired node
IPNode* searchLine(IPNode *table, unsigned int dstIP, unsigned int gatewayIP, unsigned int netmask)
{
sem_wait(&(table->semaphore));
IPNode *n = table;
while(n->next != NULL)
{
if((n->next)->dstIP == dstIP &&
(n->next)->gatewayIP == gatewayIP &&
(n->next)->netmask == netmask)
{
sem_post(&(table->semaphore));
return n;
}
n = n->next;
}
sem_post(&(table->semaphore));
return NULL;
}
void printLine(IPNode *line, unsigned int lineId)
{
//printf("%10d | ", lineId);
unsigned int fourBytes = line->dstIP;
printf("%3u.%3u.%3u.%3u | ", (fourBytes & 0xFF000000)>>24, (fourBytes & 0x00FF0000) >> 16,
(fourBytes & 0x0000FF00) >> 8, fourBytes & 0x000000FF);
fourBytes = line->gatewayIP;
printf("%3u.%3u.%3u.%3u | ", (fourBytes & 0xFF000000)>>24, (fourBytes & 0x00FF0000) >> 16,
(fourBytes & 0x0000FF00) >> 8, fourBytes & 0x000000FF);
fourBytes = line->netmask;
printf("%3u.%3u.%3u.%3u | ", (fourBytes & 0xFF000000)>>24, (fourBytes & 0x00FF0000) >> 16,
(fourBytes & 0x0000FF00) >> 8, fourBytes & 0x000000FF);
printf("%s | ", line->ifaceName);
if(line->ttl == -1)
{
printf("Inf\n");
}
else
{
printf("%3d\n", line->ttl);
}
}
void printTable(IPNode *table)
{
printf("Destino\tGateway\tMáscara\tInterface\tTTL\n");
IPNode *n = table->next;
unsigned int i = 0;
while(n != NULL)
{
printLine(n, i);
n = n->next;
i++;
}
}
IPNode* newLine(unsigned int dstIP, unsigned int gatewayIP, unsigned int mask, short int ttl, char ifaceID, char *ifName)
{
IPNode *node = (IPNode*) malloc(sizeof(IPNode));
node->dstIP = dstIP;
node->gatewayIP = gatewayIP;
node->netmask = mask;
node->ttl = ttl;
if(ifName != NULL) strcpy(node->ifaceName, ifName);
node->ifaceID = ifaceID;
node->next = NULL;
sem_init(&(node->semaphore), 0, 1);
return node;
}