Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dangan249 committed Nov 14, 2013
1 parent d313efa commit 351f40b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 32 deletions.
64 changes: 33 additions & 31 deletions ccs/neu/edu/andang/IPHeader.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package ccs.neu.edu.andang ;

import java.nio.ByteBuffer ;
import java.util.Arrays ;

// IPHeader: represent the header of an IP packet
public class IPHeader{

Expand All @@ -18,8 +15,8 @@ public class IPHeader{
short ttl;
short protocol;
int checksum;
long source_address;
long destination_address;
byte[] source_address = new byte[4];
byte[] destination_address = new byte[4];

byte[] options ;

Expand All @@ -35,22 +32,22 @@ public IPHeader( byte[] baseHeader ){
ttl = (short)(baseHeader[8]&63);
protocol = (short)(baseHeader[9]&63);
checksum = (int)(((baseHeader[10]<<8)&65280)|(baseHeader[11]&255));
source_address = (long)(((baseHeader[12]<<24)&4278190080l)
|((baseHeader[13]<<16)&16711680l)
|((baseHeader[14]<<8)&65280)
|(baseHeader[15]&255));
destination_address = (long)(((baseHeader[16]<<24)&4278190080l)
|((baseHeader[17]<<16)&16711680l)
|((baseHeader[18]<<8)&65280)
|(baseHeader[19]&255));
}
source_address[0] = baseHeader[12];
source_address[1] = baseHeader[13];
source_address[2] = baseHeader[14];
source_address[3] = baseHeader[15];
destination_address[0] = baseHeader[16];
destination_address[1] = baseHeader[17];
destination_address[2] = baseHeader[18];
destination_address[3] = baseHeader[19];
}
else{
throw new RuntimeException("Error in creating bash header for TCPPacket") ;
}
}

// Create an IP Header for an outgoing IP packet
public IPHeader(int length, long source_address, long destination_address) {
public IPHeader(int length, byte[] source_address, byte[] destination_address) {
this.version = 4;
this.ihl = 5;
this.tos = 0;
Expand All @@ -61,8 +58,14 @@ public IPHeader(int length, long source_address, long destination_address) {
this.ttl = 64;
this.protocol = 6;
this.checksum = 0;
this.source_address = source_address;
this.destination_address = destination_address;
this.source_address[0] = source_address[0];
this.source_address[1] = source_address[1];
this.source_address[2] = source_address[2];
this.source_address[3] = source_address[3];
this.destination_address[0] = destination_address[0];
this.destination_address[1] = destination_address[1];
this.destination_address[2] = destination_address[2];
this.destination_address[3] = destination_address[3];
}

public byte[] toByteArray(){
Expand All @@ -84,14 +87,14 @@ public byte[] getHeader() {
header[9] = (byte)(protocol&255);
header[10] = (byte)((checksum>>8)&255);
header[11] = (byte)(checksum&255);
header[12] = (byte)((source_address>>24)&255);
header[13] = (byte)((source_address>>16)&255);
header[14] = (byte)((source_address>>8)&255);
header[15] = (byte)(source_address&255);
header[16] = (byte)((destination_address>>24)&255);
header[17] = (byte)((destination_address>>16)&255);
header[18] = (byte)((destination_address>>8)&255);
header[19] = (byte)(destination_address&255);
header[12] = source_address[0];
header[13] = source_address[1];
header[14] = source_address[2];
header[15] = source_address[3];
header[16] = destination_address[0];
header[17] = destination_address[1];
header[18] = destination_address[2];
header[19] = destination_address[3];
return header;
}

Expand All @@ -103,10 +106,7 @@ public void setOptions(byte[] options) {
this.options = options;
}

// return the number of 32 bit words in the IP header
// including any 'options' fields.
public int getHeaderLength(){return length;}

public int getEntireLength(){return length;}
public int getChecksum(){return checksum;}
public void setCheckSum(int checksum){this.checksum = checksum;}
public byte getVersion(){return version;}
Expand All @@ -116,8 +116,10 @@ public void setOptions(byte[] options) {
public short getOffset(){return offset;}
public short getTTL(){return ttl;}
public short getProtocol(){return protocol;}
public long getSourceAddress(){return source_address;}
public long getDestinationAddress(){return destination_address;}

public byte[] getSourceAddress(){return source_address;}
public byte[] getDestinationAddress(){return destination_address;}

public boolean isFragmentOn(){return (boolean)((flags&2) == 2);}
public boolean isMoreFragmentsOn(){return (boolean)((flags&1) == 1);}

Expand Down
50 changes: 50 additions & 0 deletions ccs/neu/edu/andang/IPPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ccs.neu.edu.andang;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class IPPacket {
private IPHeader IPheader ;
private TCPPacket TCPpacket ;


public IPPacket( IPHeader IPheader, TCPPacket TCPpacket ){
this.IPheader = IPheader;
this.TCPpacket = TCPpacket ;
}
// convert the entire IP packet (data + header) to a byte array
public byte[] toByteArray(){

byte[] IPheaderBytes = IPheader.toByteArray() ;
byte[] TCPpacketBytes = TCPpacket.toByteArray();
ByteArrayOutputStream out = new ByteArrayOutputStream( );

try{
out.write( IPheaderBytes ) ;
out.write(TCPpacketBytes);
}
catch(IOException ex){
System.out.println( ex.toString() ) ;
}
return out.toByteArray() ;
}



public int length(){
return ( IPheader.length + TCPpacket.length() );
}
public IPHeader getIPheader() {
return IPheader;
}
public void setIPheader(IPHeader iPheader) {
IPheader = iPheader;
}
public TCPPacket getTCPpacket() {
return TCPpacket;
}
public void setTCPpacket(TCPPacket tCPpacket) {
TCPpacket = tCPpacket;
}

}
7 changes: 6 additions & 1 deletion ccs/neu/edu/andang/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,17 @@ public static byte[] getChecksumData( TCPPacket packet , InetAddress srcAddress,
}

// return true if the checksum in the receiving header
public static boolean verifyChecksum( TCPPacket packet , InetAddress srcAddress,
public static boolean verifyTCPChecksum( TCPPacket packet , InetAddress srcAddress,
InetAddress dstAddress ){
return 0 == Util.generateChecksum(
getChecksumData( packet , srcAddress, dstAddress) ) ;

}

// return true if the checksum evaluates to 0
public static boolean verifyIPChecksum( byte[] ipHeaderBytes ){
return 0 == Util.generateChecksum(ipHeaderBytes);
}

// return external IP address of this machine, which hosts the program
public static InetAddress getSourceExternalIPAddress(){
Expand Down

0 comments on commit 351f40b

Please sign in to comment.