Skip to content

Commit d668003

Browse files
author
ditmarlange
committed
Longest Prefix funktioniert
1 parent 7daf9c8 commit d668003

File tree

2 files changed

+120
-26
lines changed

2 files changed

+120
-26
lines changed

RN4/src/router/Router.java

+117-25
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,141 @@ public class Router extends Thread {
2121
static Map<String, String[]> routtable = new HashMap<String, String[]>();
2222
static IpPacket ipp;
2323
static NetworkLayer nl;
24+
String deststr = "";
2425

2526
public Router() throws IOException {
2627
int i = 0;
27-
while (i < 2) {
28+
while (true) {
2829
System.out.println("Vor getpacket");
2930
ipp = nl.getPacket();
31+
String findest = "";
32+
Scanner destsc = new Scanner(ipp.getDestinationAddress().toString());
33+
// System.out.println(ipp.getDestinationAddress().toString());
34+
destsc.useDelimiter(":");
35+
while (destsc.hasNext()) {
36+
String next = destsc.next();
37+
if (next.charAt(0) == '/') {
38+
next = next.substring(1);
39+
}
40+
if (next.length() == 1) {
41+
findest += "000000000000";
42+
} else if (next.length() == 2) {
43+
findest += "00000000";
44+
} else if (next.length() == 3) {
45+
findest += "0000";
46+
}
47+
for (i = 0; i < next.length(); i++) {
48+
char strch = next.charAt(i);
49+
switch (strch) {
50+
case '0':
51+
findest += "0000";
52+
break;
53+
case '1':
54+
findest += "0001";
55+
break;
56+
case '2':
57+
findest += "0010";
58+
break;
59+
case '3':
60+
findest += "0011";
61+
break;
62+
case '4':
63+
findest += "0100";
64+
break;
65+
case '5':
66+
findest += "0101";
67+
break;
68+
case '6':
69+
findest += "0110";
70+
break;
71+
case '7':
72+
findest += "0111";
73+
break;
74+
case '8':
75+
findest += "1000";
76+
break;
77+
case '9':
78+
findest += "1001";
79+
break;
80+
case 'a':
81+
findest += "1010";
82+
break;
83+
case 'b':
84+
findest += "1011";
85+
break;
86+
case 'c':
87+
findest += "1100";
88+
break;
89+
case 'd':
90+
findest += "1101";
91+
break;
92+
case 'e':
93+
findest += "1110";
94+
break;
95+
case 'f':
96+
findest += "1111";
97+
break;
98+
}
99+
}
100+
}
101+
102+
// System.out.println(findest);
30103
System.out.println(ipp.getDestinationAddress());
31104
System.out.println(ipp.getSourceAddress());
32-
ipp.setHopLimit(ipp.getHopLimit()-1);
33-
if(ipp.getHopLimit()<1){
105+
ipp.setHopLimit(ipp.getHopLimit() - 1);
106+
if (ipp.getHopLimit() < 1) {
34107
ControlPacket.Type controlType = ControlPacket.Type.valueOf("TimeExceeded");
35108
ControlPacket controlPacket = new ControlPacket(controlType, new byte[0]);
36109
ipp.setControlPayload(controlPacket.getBytes());
37110
// ipp.setDestinationAddress(ipp.getSourceAddress());
38111
// ipp.setNextHopIp(ipp.getDestinationAddress());
39112
nl.sendPacket(ipp);
40-
} else if (!routtable.containsKey(String.valueOf(ipp.getDestinationAddress()))) {
41-
System.out.println("HIER");
42-
ControlPacket.Type controlType = ControlPacket.Type.valueOf("DestinationUnreachable");
43-
ControlPacket controlPacket = new ControlPacket(controlType, new byte[0]);
44-
ipp.setControlPayload(controlPacket.getBytes());
45-
// ipp.setDestinationAddress(ipp.getSourceAddress());
46-
// ipp.setNextHopIp(ipp.getDestinationAddress());
47-
nl.sendPacket(ipp);
48113
} else {
114+
int index = 0;
115+
int longestindex = 0;
116+
for (String s : routtable.keySet()) {
117+
for (char c : findest.toCharArray()) {
118+
if (index == s.length())
119+
break;
120+
if (c != s.charAt(index))
121+
break;
122+
index++;
123+
}
124+
if (index > longestindex) {
125+
longestindex = index;
126+
deststr = s;
127+
}
128+
if (index == s.length()) {
129+
break;
130+
}
131+
}
132+
if (index == 0) {
133+
System.out.println("no common prefix");
134+
ControlPacket.Type controlType = ControlPacket.Type.valueOf("DestinationUnreachable");
135+
ControlPacket controlPacket = new ControlPacket(controlType, new byte[0]);
136+
ipp.setControlPayload(controlPacket.getBytes());
137+
// ipp.setDestinationAddress(ipp.getSourceAddress());
138+
// ipp.setNextHopIp(ipp.getDestinationAddress());
139+
nl.sendPacket(ipp);
140+
deststr = "-1";
141+
} else
142+
System.out.println("Longest prefix sagt: " + deststr);
143+
}
144+
if (!deststr.matches("-1")) {
145+
ipp.setNextPort(Integer.valueOf(routtable.get(deststr)[1]));
49146

50-
ipp.setNextPort(Integer.valueOf(routtable.get(String.valueOf(ipp.getDestinationAddress()))[1]));
51-
52-
Inet6Address nexthopip = (Inet6Address) InetAddress
53-
.getByName((routtable.get(String.valueOf(ipp.getDestinationAddress()))[0]));
147+
Inet6Address nexthopip = (Inet6Address) InetAddress.getByName((routtable.get(deststr)[0]));
54148
ipp.setNextHopIp(nexthopip);
55149
System.out.println("Nexthopip: " + nexthopip + " Nextport: " + ipp.getNextHopPort());
56-
System.out.println(Arrays.toString(nexthopip.getAddress()));
57150
nl.sendPacket(ipp);
58-
// IpPacket retp;
59-
// retp = nl.getPacket();
60-
// System.out.println(retp.getDestinationAddress());
61-
// retp.setNextPort(5000);
62-
// nl.sendPacket(retp);
63-
64151
}
65152
}
66-
i++;
153+
// IpPacket retp;
154+
// retp = nl.getPacket();
155+
// System.out.println(retp.getDestinationAddress());
156+
// retp.setNextPort(5000);
157+
// nl.sendPacket(retp);
158+
67159
}
68160

69161
public static void main(String[] args) throws IOException {
@@ -72,11 +164,11 @@ public static void main(String[] args) throws IOException {
72164
while (in.hasNextLine()) {
73165
String line = in.nextLine();
74166
String[] stary = line.split(";");
75-
//System.out.println(line);
167+
// System.out.println(line);
76168
String[] stary2 = { stary[1], stary[2] };
77169
routtable.put(stary[0], stary2);
78170
}
79-
//System.out.println(routtable.keySet());
171+
// System.out.println(routtable.keySet());
80172
Router rt = new Router();
81173
}
82174

RN4/src/router/routen.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
2001:db8:2::/48;2001:db8:0:3::;5000
44
/0:0:0:0:0:0:0:1;0:0:0:0:0:0:0:1;8000
55
/fe80:0:0:0:4011:72ff:fef6:16b5;fe80:0:0:0:4011:72ff:fef6:16b5;6789
6-
/fe80:0:0:0:850:ef1f:577d:c3e9;fe80:0:0:0:850:ef1f:577d:c3e9;6789
6+
/fe80:0:0:0:850:ef1f:577d:c3e9;fe80:0:0:0:850:ef1f:577d:c3e9;6789
7+
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001;0:0:0:0:0:0:0:1;8000
8+
11111110100000000000000000000000000000000000000000000000000000000000100001010000111011110001111101010111011111011100001111101001;fe80:0:0:0:850:ef1f:577d:c3e9;6789

0 commit comments

Comments
 (0)