-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharp_linked_list.c
More file actions
109 lines (89 loc) · 2.37 KB
/
Copy patharp_linked_list.c
File metadata and controls
109 lines (89 loc) · 2.37 KB
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
#include "definitions.h"
#include "arp_linked_list.h"
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>
char addARPLine(ArpNode *table, ArpNode *line, unsigned char type)
{
if(table == NULL) return __ERROR__;
// If the entry is already in the list delet it
// Just to maintain consistency
removeARPLine(table, line->ipAddress);
sem_wait(&(table->semaphore));
line->type = type;
line->next = table->next;
table->next = line;
sem_post(&(table->semaphore));
return __OK__;
}
char removeARPLine(ArpNode *table, unsigned int ipAddress)
{
// The table is blocked when a deletion is done
ArpNode *prev;
prev = searchARPLine(table, ipAddress);
sem_wait(&(table->semaphore));
if(prev != NULL)
{
ArpNode *n = prev->next;
prev->next = n->next;
free(n);
sem_post(&(table->semaphore));
return __OK__;
}
sem_post(&(table->semaphore));
return __ERROR__;
}
// always returns the previous node to the desired node
ArpNode* searchARPLine(ArpNode *table, unsigned int ipAddress)
{
sem_wait(&(table->semaphore));
ArpNode *n = table;
while(n->next != NULL)
{
if((n->next)->ipAddress == ipAddress)
{
sem_post(&(table->semaphore));
return n;
}
n = n->next;
}
sem_post(&(table->semaphore));
return NULL;
}
void printARPLine(ArpNode *line, unsigned int lineId)
{
printf("%10d | ", lineId);
unsigned int ip = line->ipAddress;
printf("%3u.%3u.%3u.%3u | ", (ip & 0xFF000000)>>24, (ip & 0x00FF0000) >> 16,
(ip & 0x0000FF00) >> 8, ip & 0x000000FF);
unsigned char *mac = line->macAddress;
printf("%2X:%2X:%2X:%2X:%2X:%2X | ", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("%3d\n", line->ttl);
}
void printARPTable(ArpNode *table)
{
printf(" Entrada | Endereço IP | Endereço Ethernet | TTL\n");
ArpNode *n = table->next;
unsigned int i = 0;
while(n != NULL)
{
printARPLine(n, i);
n = n->next;
i++;
}
}
ArpNode* newARPLine(unsigned int ipAddress, unsigned char *macAddress, short int ttl, char *ifName)
{
ArpNode *node = (ArpNode*) malloc(sizeof(ArpNode));
node->ipAddress = ipAddress;
for(unsigned int i = 0; i < 6; i++)
{
node->macAddress[i] = macAddress[i];
}
node->ttl = ttl;
if(ifName != NULL) strcpy(node->ifaceName, ifName);
node->next = NULL;
sem_init(&(node->semaphore), 0, 1);
return node;
}