-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpwospf_neighbors.c
executable file
·87 lines (72 loc) · 2.16 KB
/
pwospf_neighbors.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
#include "pwospf_neighbors.h"
#include "pwospf_protocol.h"
void add_neighbor(ospfv2_neighbor* first_neighbor, ospfv2_neighbor* new_neighbor)
{
if (first_neighbor->next != NULL)
{
new_neighbor->next = first_neighbor->next;
first_neighbor->next = new_neighbor;
}
else
{
first_neighbor->next = new_neighbor;
}
}
void delete_neighbor(ospfv2_neighbor* previous_neighbor)
{
ospfv2_neighbor* temp = previous_neighbor->next;
if (previous_neighbor->next->next != NULL)
{
previous_neighbor->next = previous_neighbor->next->next;
}
else
{
previous_neighbor->next = NULL;
}
free(temp);
}
void check_neighbors_alive(ospfv2_neighbor* first_neighbor)
{
struct ospfv2_neighbor* ptr = first_neighbor;
while(ptr != NULL)
{
if (ptr->next == NULL)
{
break;
}
if (ptr->next->alive == 0)
{
Debug("\n\n**** PWOSPF: Removing the neighbor, [ID = %s] from the alive neighbors table\n\n", inet_ntoa(ptr->next->neighbor_id));
delete_neighbor(ptr);
}
else
{
ptr->next->alive--;
}
ptr = ptr->next;
}
}
void refresh_neighbors_alive(ospfv2_neighbor* first_neighbor, in_addr neighbor_id)
{
struct ospfv2_neighbor* ptr = first_neighbor;
while(ptr != NULL)
{
if (ptr->neighbor_id.s_addr == neighbor_id.s_addr)
{
Debug("-> PWOSPF: Refreshing the neighbor, [ID = %s] in the alive neighbors table\n", inet_ntoa(neighbor_id));
ptr->alive = OSPF_NEIGHBOR_TIMEOUT;
return;
}
ptr = ptr->next;
}
Debug("-> PWOSPF: Adding the neighbor, [ID = %s] to the alive neighbors table\n", inet_ntoa(neighbor_id));
add_neighbor(first_neighbor, create_ospfv2_neighbor(neighbor_id));
}
struct ospfv2_neighbor* create_ospfv2_neighbor(in_addr neighbor_id)
{
struct ospfv2_neighbor* new_neighbor = ((ospfv2_neighbor*)(malloc(sizeof(ospfv2_neighbor))));
new_neighbor->neighbor_id.s_addr = neighbor_id.s_addr;
new_neighbor->alive = OSPF_NEIGHBOR_TIMEOUT;
new_neighbor->next = NULL;
return new_neighbor;
}