-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEtherPort.java
343 lines (337 loc) · 11.9 KB
/
EtherPort.java
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* This class represents a virtual ethernet link between two hosts over UDP.
* @author Drake, Ian, Justin
*/
import java.net.*;
import java.io.*;
import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.Arrays;
import java.nio.ByteBuffer;
import java.util.zip.CRC32;
public class EtherPort {
final private LinkedBlockingQueue<DatagramPacket> outQueue;
private RouterHook routerHook;
private int port, jack;
private InetAddress dstAddr, bind, ifaceAddr;
private NetMask nm;
private MACAddress src;
private DatagramSocket sock;
private boolean runThreads;
private int mtu = 1500;
/**
* Makes a new EtherPort, listening on the specified port,
on all interfaces
* @param port The port to be listened on
* @param jack The identifier for this interface
* @param src The MACAddress assigned to this interface
* @param routerHook The callback pointer for this interface
*/
public EtherPort(int port, int jack,MACAddress src, RouterHook routerHook)
throws SocketException{
this.routerHook = routerHook;
this.src = src;
this.jack=jack;
this.port = port;
sock = new DatagramSocket(port);
outQueue = new LinkedBlockingQueue<DatagramPacket>();
startConnection();
}
/**
* Makes a new EtherPort, listening on the specified port, on a specified interface.
* @param port The port to be listened on
* @param jack The identifier for this interface
* @param src The MACAddress assigned to this interface
* @param routerHook The callback pointer for this interface
* @param iface The address of the interface to listen on.
*/
public EtherPort(int port, int jack, MACAddress src,
RouterHook routerHook, InetAddress iface){
this.port = port;
this.src = src;
iface = bind;
this.jack=jack;
this.routerHook = routerHook;
try{
sock = new DatagramSocket(port, iface);
}
catch(SocketException e){
System.out.println("Could not establish socket on local port "
+ port);
}
outQueue = new LinkedBlockingQueue<DatagramPacket>();
startConnection();
}
/**
* Safely stops the interface after sending pending transmits.
*/
protected void stopThreads(){
runThreads = false;
while(outQueue.peek() != null) { } //wait to send everything
sock.close();
}
/**
* Begins transmission again on a halted interface.
*/
protected void startThreads(){
runThreads = true;
// sock.connect()? later.
}
/**
* Sets the endpoint of this interface
* @param dstAddr The new endpoint
*/
protected void setDestIP(InetAddress dstAddr) {
this.dstAddr = dstAddr;
}
/**
* Returns the endpoint of this interface.
*/
public InetAddress getDestIP() {
return dstAddr;
}
/**
* Returns the listening port for this interface
*/
public int getDestPort() {
return port;
}
/**
* Returns this EtherPort's virtual MAC address (always begins with E1
* for our group).
*/
public MACAddress getMAC() {
return src;
}
/**
* Specifies whether or not this interface has an endpoint currently.
*/
public boolean hasEndpoint(){
return !(dstAddr == null);
}
/**
* Parses a byte array into an EtherFrame
* @param payload The byte array containing the frame
*/
private EtherFrame parseFrame(byte[] payload) throws IOException {
ByteBuffer bb = ByteBuffer.wrap(payload);
int fcs = bb.getInt(payload.length-4);
flipBits(payload);
long preambleSFD = bb.getLong();
MACAddress dstAddrs = new MACAddress(Arrays.copyOfRange(payload,9,15));
MACAddress srcAddrs = new MACAddress(Arrays.copyOfRange(payload,15,21));
short type = toShort(Arrays.copyOfRange(payload,21,24));
byte[] data = Arrays.copyOfRange(payload,23,payload.length-4);
EtherFrame rcvdFrame = new EtherFrame(dstAddrs,srcAddrs,type,data);
int crc = compCRC(Arrays.copyOfRange(payload,9,payload.length-4));
if(crc != fcs) {
System.out.println("Corrupt frame!");
throw new IOException("Corrupt frame");
}
//right now we'll just return it anyways since CRC is probably buggy
return rcvdFrame;
}
//making this private prevents "Overridable method in constructor" warning
/**
* Starts the transmit and recieve threads of the interface.
*/
private void startConnection(){
runThreads = true;
Thread receiveThread = new Thread (new Runnable() {
@Override
public void run() { receiveFrame(); }
});
receiveThread.start();
Thread sendThread = new Thread (new Runnable() {
@Override
public void run() { sendFrame();}
});
sendThread.start();
}
/**
* Adds a frame to be transmitted.
* @param eth The EtherFrame to be enqueued
* @param dstAddr The destination IP for the packet
*/
protected void enqueueFrame(EtherFrame eth, InetAddress dstAddr){
byte[] frame = eth.asBytes();
int crc = compCRC(Arrays.copyOfRange(frame,8,frame.length-4));
eth.writeFCS(crc);
frame = eth.asBytes();
byte[] payload = new byte[frame.length+1];
System.arraycopy(frame,0,payload,1,frame.length-4);
flipBits(payload);
payload[0] = (byte)'e';
System.arraycopy(frame,frame.length-4,payload,payload.length-4,4);
DatagramPacket pkt = new DatagramPacket(payload, payload.length,
dstAddr, port);
outQueue.offer(pkt);
System.out.println("Packet offered");
}
/**
* Adds a frame to be transmitted.
* @param dst The destination MACAddress for this frame
* @param type The type of this frame
* @param data The data payload of this frame
*/
protected void enqueueFrame(MACAddress dst, short type, byte[] data){
EtherFrame eth = new EtherFrame(dst,src,type,data);
byte[] frame = eth.asBytes();
int crc = compCRC(Arrays.copyOfRange(frame,8,frame.length-4));
eth.writeFCS(crc);
frame = eth.asBytes();
byte[] payload = new byte[frame.length+1];
System.arraycopy(frame,0,payload,1,frame.length-4);
flipBits(payload);
payload[0] = (byte)'e';
System.arraycopy(frame,frame.length-4,payload,payload.length-4,4);
DatagramPacket pkt = new DatagramPacket(payload, payload.length,
dstAddr, port);
outQueue.offer(pkt);
System.out.println("Packet offered to "+ dstAddr.toString());
}
/**
* Adds a control command to be transmitted over the interface
* @param payload The payload of the command packet
* @param dstAddr The destination address for the packet
*/
protected void enqueueCommand(byte[] payload, InetAddress dstAddr){
DatagramPacket pkt = new DatagramPacket(payload, payload.length,
dstAddr,port);
outQueue.offer(pkt);
}
private void receiveFrame(){
byte[] buf = new byte[1532];
DatagramPacket rcvd = new DatagramPacket(buf,buf.length);
while(runThreads){
try{
sock.receive(rcvd);
int len = rcvd.getLength();
if( buf[0] == (byte) 'e' /*&& 72<=len && len<=(mtu+26)*/) {
byte[] frame = new byte[rcvd.getLength()];
System.arraycopy(rcvd.getData(),0,frame,0,rcvd.getLength());
EtherFrame eth = parseFrame(frame);
EventRegistration evt =
routerHook.getEventReg(new Short(eth.getType()));
System.out.println("type: "+eth.getType());
System.out.println(evt);
if( evt != null &&
(eth.getDst().getLongAddress()==src.getLongAddress()) ||
(eth.getDst().getLongAddress()==MACAddress.BROADCAST_ADDRESS)){
evt.frameReceived(eth.getData(),jack);
}
else if(evt == null &&
eth.getDst().getLongAddress()==src.getLongAddress()
&& eth.getType() == (short)0x0801)
{
System.out.println(new String(eth.getData()));
}
}
else { //if not an 'e' packet, give it to our controller
routerHook.commandRcvd((char)buf[0],
rcvd.getAddress(),
rcvd.getPort(),
getJack(),
buf);
}
}
catch(IOException e){
System.out.println("fission mailed");
}
}
}
private void sendFrame(){
DatagramPacket currpkt=null;
while(runThreads){
try{
currpkt = outQueue.take();
}
catch(InterruptedException e){
System.out.println(e);
}
try{ sock.send(currpkt);}
catch(IOException e){System.out.println(e);}
}
}
private static void flipBits( byte[] bytes ) {
for( int i = 0; i < bytes.length; i++ ) {
byte oldByte = bytes[i];
bytes[i] = (byte) ( ((oldByte & 0x01)<<7) | ((oldByte & 0x02)<<5) |
((oldByte & 0x04)<<3) | ((oldByte & 0x08)<<1) |
((oldByte & 0x10)>>1) | ((oldByte & 0x20)>>3) |
((oldByte & 0x40)>>5) | ((oldByte & 0x80)>>7) );
}
}
/**
* Returns the port this interface is listening on
*/
public int getPort() {
return port;
}
/**
* Returns the port identifier number of this interface.
*/
public int getJack() {
return jack;
}
/**
* Returns the IP of the interface this EtherPort is listening on,
if it is listening on a specific interface.
*/
public InetAddress getBound(){
return bind;
}
/**
* Sets the IP for the interface
* Note: This IP is internal, it is unrelated to the one in getLocalIP()
* It is used mainly as a data storage for the routing table.
*/
protected void setIP( InetAddress ip){
ifaceAddr = ip;
}
/**
* Returns the IP of this interface.
*/
public InetAddress getIP(){
return ifaceAddr;
}
/**
* Sets the netmask for this interface.
*/
protected void setNetMask( NetMask nm ){
this.nm = nm;
}
/**
*Gives the Maximum Transmission Unit for this link
*/
protected int getMTU(){
return mtu;
}
/**
*Sets the Maxium Transmission Unit for this link
*/
protected void setMTU(int mtu){
this.mtu = mtu;
}
private short toShort(byte [] b){
short sh=0;
sh |= b[0] & 0xFF;
sh <<=8;
sh |= b[1] & 0xFF;
return sh;
}
private int compCRC(byte[] b){
byte[] first32 = new byte[32];
byte[] quot = new byte[b.length];
System.arraycopy(b,0,first32,0,32);
flipBits(first32);
System.arraycopy(first32,0,quot,0,32);
System.arraycopy(b,32,quot,32,b.length-32);
CRC32 crc = new CRC32();
crc.update(quot);
ByteBuffer buf = ByteBuffer.allocate(4).putInt((int)crc.getValue());
byte[] flip = buf.array();
flipBits(flip);
return buf.getInt(0);
}
}