-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsPacket.c
More file actions
96 lines (71 loc) · 1.68 KB
/
lsPacket.c
File metadata and controls
96 lines (71 loc) · 1.68 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
/**
* This file implements the functions used for building and modifying link-state packets.
*
* @author Jeffrey Bromen
* @date 4/16/17
* @info Systems and Networks II
* @info Project 3
*/
#include "lsPacket.h"
void buildLSPacket(char *buffer, int hopCount, int seqNumber, char source, char destination, int cost)
{
memset(buffer, hopCount % 256, 1);
memset(buffer+1, seqNumber % 256, 1);
memset(buffer+2, source, 1);
memset(buffer+3, destination, 1);
int ncost = htonl(cost);
memcpy(buffer+4, &ncost, 4);
}
int getHopCount(char *lsPacket)
{
char hopCount;
memcpy(&hopCount, lsPacket, 1);
return hopCount;
}
int getSequenceNumber(char *lsPacket)
{
char seqNumber;
memcpy(&seqNumber, lsPacket+1, 1);
return seqNumber;
}
char getSourceID(char *lsPacket)
{
char source;
memcpy(&source, lsPacket+2, 1);
return source;
}
char getDestinationID(char *lsPacket)
{
char destination;
memcpy(&destination, lsPacket+3, 1);
return destination;
}
int getCost(char *lsPacket)
{
int cost;
memcpy(&cost, lsPacket+4, 4);
return ntohl(cost);
}
void printLSPacket(char *lsPacket)
{
int hopCount, seqNumber, cost;
char source, destination;
hopCount = getHopCount(lsPacket);
seqNumber = getSequenceNumber(lsPacket);
source = getSourceID(lsPacket);
destination = getDestinationID(lsPacket);
cost = getCost(lsPacket);
printf("Hop Count: %d\n"
"Sequence Number: %d\n"
"Source ID: %c\n"
"Destination ID: %c\n"
"Cost: %d\n",
hopCount, seqNumber, source, destination, cost);
}
int decrementHopCount(char *lsPacket)
{
int hopCount = getHopCount(lsPacket);
hopCount--;
memset(lsPacket, hopCount, 1);
return hopCount;
}