@@ -21,49 +21,141 @@ public class Router extends Thread {
21
21
static Map <String , String []> routtable = new HashMap <String , String []>();
22
22
static IpPacket ipp ;
23
23
static NetworkLayer nl ;
24
+ String deststr = "" ;
24
25
25
26
public Router () throws IOException {
26
27
int i = 0 ;
27
- while (i < 2 ) {
28
+ while (true ) {
28
29
System .out .println ("Vor getpacket" );
29
30
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);
30
103
System .out .println (ipp .getDestinationAddress ());
31
104
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 ) {
34
107
ControlPacket .Type controlType = ControlPacket .Type .valueOf ("TimeExceeded" );
35
108
ControlPacket controlPacket = new ControlPacket (controlType , new byte [0 ]);
36
109
ipp .setControlPayload (controlPacket .getBytes ());
37
110
// ipp.setDestinationAddress(ipp.getSourceAddress());
38
111
// ipp.setNextHopIp(ipp.getDestinationAddress());
39
112
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 );
48
113
} 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 ]));
49
146
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 ]));
54
148
ipp .setNextHopIp (nexthopip );
55
149
System .out .println ("Nexthopip: " + nexthopip + " Nextport: " + ipp .getNextHopPort ());
56
- System .out .println (Arrays .toString (nexthopip .getAddress ()));
57
150
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
-
64
151
}
65
152
}
66
- i ++;
153
+ // IpPacket retp;
154
+ // retp = nl.getPacket();
155
+ // System.out.println(retp.getDestinationAddress());
156
+ // retp.setNextPort(5000);
157
+ // nl.sendPacket(retp);
158
+
67
159
}
68
160
69
161
public static void main (String [] args ) throws IOException {
@@ -72,11 +164,11 @@ public static void main(String[] args) throws IOException {
72
164
while (in .hasNextLine ()) {
73
165
String line = in .nextLine ();
74
166
String [] stary = line .split (";" );
75
- //System.out.println(line);
167
+ // System.out.println(line);
76
168
String [] stary2 = { stary [1 ], stary [2 ] };
77
169
routtable .put (stary [0 ], stary2 );
78
170
}
79
- //System.out.println(routtable.keySet());
171
+ // System.out.println(routtable.keySet());
80
172
Router rt = new Router ();
81
173
}
82
174
0 commit comments