Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as C by Nathan the Sane ( 11 years ago )
#include <stdio.h>
#include <stdlib.h>
struct ArpPacket
{
unsigned char hType[2];
unsigned char pType[2];
unsigned char hLen;
unsigned char pLen;
unsigned char op[2];
unsigned char* senderAddr;
unsigned char* srcPAddr;
unsigned char* targetAddr;
unsigned char* pAddr;
};
struct EthPacket
{
unsigned char destMacAddr[6];
unsigned char srcMacAddr[6];
unsigned char ethType[2];
struct ArpPacket arpPkt;
};
/* Grabs an arbitrary number of bytes (specified by the length
* parameter) and stuffs them in the pointer we pass.
*/
int getData(int length, unsigned char* dataPtr, FILE* filePtr);
void printData(struct EthPacket ethPkt, struct ArpPacket arpPkt);
int main(int argc, char* argv[])
{
int fileFlag = 1;
struct EthPacket ethPkt;
struct ArpPacket arpPkt;
FILE* file;
file = fopen("packet.bin", "r");
fileFlag = getData(6, ethPkt.destMacAddr, file);
fileFlag = getData(6, ethPkt.srcMacAddr, file);
fileFlag = getData(2, ethPkt.ethType, file);
fileFlag = getData(2, arpPkt.hType, file);
fileFlag = getData(2, arpPkt.pType, file);
fileFlag = getData(1, &arpPkt;.hLen, file);
fileFlag = getData(1, &arpPkt;.pLen, file);
fileFlag = getData(2, arpPkt.op, file);
arpPkt.senderAddr = malloc(arpPkt.hLen);
arpPkt.srcPAddr = malloc(arpPkt.pLen);
arpPkt.targetAddr = malloc(arpPkt.hLen);
arpPkt.pAddr = malloc(arpPkt.pLen);
fileFlag = getData(arpPkt.hLen, arpPkt.senderAddr, file);
fileFlag = getData(arpPkt.pLen, arpPkt.srcPAddr, file);
fileFlag = getData(arpPkt.hLen, arpPkt.targetAddr, file);
fileFlag = getData(arpPkt.pLen, arpPkt.pAddr, file);
if (fileFlag)
{
printData(ethPkt, arpPkt);
}
else
{
printf("Oops, there was a problem parsing the packet. :(\n");
}
free(arpPkt.senderAddr);
free(arpPkt.targetAddr);
free(arpPkt.pAddr);
fclose(file);
return 0;
}
int getData(int length, unsigned char* dataPtr, FILE* filePtr)
{
int counter;
if (length == 1)
{
if ((*dataPtr = fgetc(filePtr)) == EOF)
{
return 0;
}
return 1;
}
for (counter = 0; counter < length; counter++)
{
if ((dataPtr[counter] = fgetc(filePtr)) == EOF)
{
return 0;
}
}
return 1;
}
void printData(struct EthPacket ethPkt, struct ArpPacket arpPkt)
{
int counter;
printf("ETHERNET HEADER\n--------------------\n");
printf("Destination MAC Address: ");
for (counter = 0; counter < 6; counter++)
{
printf("%.2x", ethPkt.destMacAddr[counter]);
if (counter != 5)
{
printf(":");
}
else
{
printf("\n");
}
}
printf("Source MAC Address: ");
for (counter = 0; counter < 6; counter++)
{
printf("%.2x", ethPkt.srcMacAddr[counter]);
if (counter != 5)
{
printf(":");
}
else
{
printf("\n");
}
}
printf("Ethertype: 0x%.4x\n\n", (ethPkt.ethType[0] * 256) + ethPkt.ethType[1]);
printf("ARP PACKET\n------------------\n");
printf("Hardware Type: %d\n", (arpPkt.hType[0] * 256) + arpPkt.hType[1]);
printf("Protocol Type: 0x%.4x\n", (arpPkt.pType[0] * 256) + arpPkt.pType[1]);
printf("Packet Type: ");
switch (arpPkt.op[1])
{
case 1:
{
printf("REQUEST\n");
break;
}
case 2:
{
printf("REPLY\n");
break;
}
}
printf("Sender MAC Address: ");
for (counter = 0; counter < arpPkt.hLen; counter++)
{
printf("%.2x", arpPkt.senderAddr[counter]);
if (counter != arpPkt.hLen - 1)
{
printf(":");
}
else
{
printf("\n");
}
}
printf("Sender Protocol Address: ");
for (counter = 0; counter < arpPkt.pLen; counter++)
{
printf("%d", arpPkt.srcPAddr[counter]);
if (counter != arpPkt.pLen - 1)
{
printf(".");
}
else
{
printf("\n");
}
}
printf("Target MAC Address: ");
for (counter = 0; counter < arpPkt.hLen; counter++)
{
printf("%.2x", arpPkt.targetAddr[counter]);
if (counter != arpPkt.hLen - 1)
{
printf(":");
}
else
{
printf("\n");
}
}
printf("Target Protocol Address: ");
for (counter = 0; counter < arpPkt.pLen; counter++)
{
printf("%d", arpPkt.pAddr[counter]);
if (counter != arpPkt.pLen - 1)
{
printf(".");
}
else
{
printf("\n");
}
}
}
Revise this Paste