@@ -32,14 +32,14 @@ static byte[] getRIPPacket(byte command, byte roverId, Map<InetAddress, RoutingT
32
32
int packetOffset = 8 ;
33
33
for (RoutingTableEntry entry : entries .values ()) {
34
34
// Add IP entry
35
- packetOffset = addIpAddress (ripPacket , packetOffset , entry .ipAddress , entry );
35
+ packetOffset = addIpAddress (ripPacket , packetOffset , entry .ipAddress );
36
36
packetOffset += 3 ; // skip the first 3 bytes since they will be
37
37
// 0 and the subnet mask will at max be 32
38
38
39
39
ripPacket [packetOffset ] = entry .subnetMask ;
40
40
packetOffset += 1 ;
41
41
42
- packetOffset = addIpAddress (ripPacket , packetOffset , entry .nextHop , entry );
42
+ packetOffset = addIpAddress (ripPacket , packetOffset , entry .nextHop );
43
43
44
44
packetOffset += 3 ; // skip the first 3 bytes since they will be
45
45
// 0 and the metric will at max be 15
@@ -49,7 +49,15 @@ static byte[] getRIPPacket(byte command, byte roverId, Map<InetAddress, RoutingT
49
49
return ripPacket ;
50
50
}
51
51
52
- public static List <RoutingTableEntry > decodeRIPPacket (byte [] packet , int packetLength ) throws UnknownHostException {
52
+ /**
53
+ * Decode the byte representation of a string into a list of routing table entries
54
+ *
55
+ * @param packet the byte array representing the packet
56
+ * @param packetLength the length of the payload in the packet
57
+ * @return a list of routing table entries
58
+ * @throws UnknownHostException
59
+ */
60
+ public static List <RoutingTableEntry > decodeRIPPacket (byte [] packet , int packetLength ) throws UnknownHostException {
53
61
int totalEntries = (packetLength - 8 ) / 16 ;
54
62
List <RoutingTableEntry > list = new ArrayList <>();
55
63
RoutingTableEntry entry ;
@@ -72,8 +80,14 @@ public static List<RoutingTableEntry> decodeRIPPacket(byte[] packet, int packetL
72
80
return list ;
73
81
}
74
82
75
-
76
- private static InetAddress getIpFromPacket (byte [] packet , int offset ) throws UnknownHostException {
83
+ /**
84
+ * Returns the IP address as extracted from the packet
85
+ * @param packet the byte array representing the packet
86
+ * @param offset the offset from which to check
87
+ * @return the IP starting from the given offset
88
+ * @throws UnknownHostException
89
+ */
90
+ private static InetAddress getIpFromPacket (byte [] packet , int offset ) throws UnknownHostException {
77
91
StringBuilder res = new StringBuilder ();
78
92
for (int i = 0 ; i < 4 ; i ++) {
79
93
res .append (getNextNBytes (packet , offset + i , 1 ) + (i == 3 ? "" : "." ));
@@ -97,15 +111,36 @@ private static String getNextNBytes(byte[] packet, int offset, int N) {
97
111
return "" + res ;
98
112
}
99
113
100
-
101
- private static int addIpAddress (byte [] ripPacket , int packetOffset , InetAddress ipToAdd , RoutingTableEntry entry ) {
114
+ /**
115
+ * Adds the ip to the packet
116
+ * @param ripPacket the rip packet
117
+ * @param packetOffset the offset from which to start filling
118
+ * @param ipToAdd the ip which has to be added
119
+ * @return
120
+ */
121
+ private static int addIpAddress (byte [] ripPacket , int packetOffset , InetAddress ipToAdd ) {
102
122
for (byte ipSubPart : ipToAdd .getAddress ()) {
103
123
ripPacket [packetOffset ] = ipSubPart ;
104
124
packetOffset += 1 ;
105
125
}
106
126
return packetOffset ;
107
127
}
108
128
129
+ /**
130
+ * Prints the hex dump of the packet
131
+ * @param ripByteRepresentation the byte representation of the rip packet
132
+ */
133
+ private static void printPacket (byte [] ripByteRepresentation ) {
134
+ for (int i = 0 ; i < ripByteRepresentation .length ; i += 1 ) {
135
+ System .out .printf ("%02x" + ((i + 1 ) % 4 == 0 ? "\n " : " -- " ), ripByteRepresentation [i ]);
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Main function which tests the utility
141
+ * @param args arguments passed to the main function
142
+ * @throws Exception
143
+ */
109
144
public static void main (String [] args ) throws Exception {
110
145
// Test for RIP packet util
111
146
RoutingTableEntry packet = new RoutingTableEntry (InetAddress .getByName ("255.255.255.255" ),
@@ -117,16 +152,11 @@ public static void main(String[] args) throws Exception {
117
152
Map <InetAddress , RoutingTableEntry > routingTableEntries = new HashMap <>();
118
153
routingTableEntries .put (InetAddress .getByName ("255.255.255.255" ), packet );
119
154
routingTableEntries .put (InetAddress .getByName ("123.221.1.55" ), packet1 );
120
- byte [] ripByteRepresentation = getRIPPacket ((byte ) 1 , (byte )12 , routingTableEntries );
155
+ byte [] ripByteRepresentation = getRIPPacket ((byte ) 1 , (byte ) 12 , routingTableEntries );
121
156
122
157
printPacket (ripByteRepresentation );
123
158
124
159
System .out .println (decodeRIPPacket (ripByteRepresentation , ripByteRepresentation .length ));
125
160
}
126
161
127
- private static void printPacket (byte [] ripByteRepresentation ) {
128
- for (int i = 0 ; i < ripByteRepresentation .length ; i += 1 ) {
129
- System .out .printf ("%02x" + ((i + 1 ) % 4 == 0 ? "\n " : " -- " ), ripByteRepresentation [i ]);
130
- }
131
- }
132
162
}
0 commit comments