Skip to content

Commit

Permalink
Done with ethernet header
Browse files Browse the repository at this point in the history
  • Loading branch information
settler committed Sep 17, 2021
1 parent 8913d11 commit fa9f64b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
Binary file not shown.
Binary file modified out/production/TCP-header-Extractor/Main.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
77 changes: 68 additions & 9 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Scanner;

public class Main {
Expand All @@ -15,17 +16,75 @@ public static void main(String[] args) throws IOException {
"Total lines for comment= <<value>>\n");

System.out.println("Enter path of input file along with file name: ");

// G:\Other computers\My Laptop\college document\0 SEM 3\CE 257 DCN\assignment - 1\Header-Extractor\Sample.txt
Scanner userInput = new Scanner(System.in);
String fileName = userInput.nextLine();
try {
Scanner inputFile = new Scanner(Path.of(fileName),
StandardCharsets.UTF_8);
String filePath = userInput.nextLine();
File inputFile = new File(filePath);
Scanner readInput = new Scanner(inputFile, StandardCharsets.UTF_8);
/**
* @// TODO: 9/17/2021 Add try and catch for wrong file path.
* @// TODO: 9/17/2021 Add how many rows you want to print option.
*/

} catch (IOException e) {
System.out.println("File does not exists");
}
String fileData = inputFile.nextLine();
String fileData = readInput.nextLine();
System.out.println(fileData);
System.out.println("------------------------------------------\n" +
"Output for the row-1\n" +
"------------------------------------------\n");
//Code for Ethernet Header

ethernetHeader frame1 = new ethernetHeader(fileData.substring(0,112),fileData.substring(112));
System.out.println(frame1.toString());
}
}

class HeaderExtractor{
String inputFrameData;
public HeaderExtractor(String inputFrameData) {
this.inputFrameData = inputFrameData;
}

}
class ethernetHeader implements strBinToStrHex{
String headerFrame;
String data;
String destinationMAC;
String sourceMAC;
String type;

public ethernetHeader(String headerFrame, String data) {
this.headerFrame = headerFrame;
this.data = data;
this.destinationMAC = convertStringToHex(headerFrame.substring(0,48));
this.sourceMAC= convertStringToHex(headerFrame.substring(48,96));
this.type = convertStringToHex(headerFrame.substring(96,112));
}

@Override
public String toString() {
System.out.println();
return "Ethernet Header\n" +
"\n" +
"\nDestination MAC Address: " + printMAC(destinationMAC)+
"\nSource MAC Address: " + printMAC(sourceMAC)+
"\nType: " +type;
}
}

interface strBinToStrHex{
default String convertStringToHex(String binaryString) {
BigInteger decimalVal = new BigInteger(binaryString,2);
// this is just to debug the code.
// System.out.println("Print decimal value of "+binaryString +" is "+decimalVal);
return decimalVal.toString(16);
}
default String printMAC(String hexValue){
StringBuilder MACAddress = new StringBuilder(hexValue);
for (int i = 2; i < hexValue.length(); i+= 3) {
// insert character value at offset i
MACAddress.insert(i, ':');
}
return String.valueOf(MACAddress);
}
}

0 comments on commit fa9f64b

Please sign in to comment.