Sei sulla pagina 1di 4

#pragma once #define __FAVOR_BSD #include <netinet/ip.h> #include <arpa/inet.h> #include <netinet/in.h> #include <net/if_arp.h> #include <netinet/if_ether.

h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <iostream> struct Ethhdr { u_char targ_hw_addr[6]; u_char src_hw_addr[6]; u_short frame_type; }; class ClEthernet { private: char* MACStringToArray(char* MAC); public: char * sourceMAC; /* Sender hardware address. */ char * destMAC; /* Target hardware address. */ unsigned short protocol; //ETHERTYPE_IP 0x0800 , ETHERTYPE_ARP 0x0806 ClEthernet(); void GeneratePacket(); char* packetBuffer; int GetPacketSize(); }; char* ClEthernet::MACStringToArray(char* str) { int i; char c,val; char* buf = new char[6]; for(i=0;i<6;i++) { if( !(c = tolower(*str++))) printf("Invalid hardware address"); if(isdigit(c)) val = c-'0'; else if(c >= 'a' && c <= 'f') val = c-'a'+10; else printf("Invalid hardware address"); *buf = val << 4; if( !(c = tolower(*str++))) printf("Invalid hardware address"); if(isdigit(c)) val = c-'0'; else if(c >= 'a' && c <= 'f') val = c-'a'+10; else printf("Invalid hardware address"); *buf++ |= val; if(*str == ':')str++;

} return buf; }

ClEthernet::ClEthernet() { sourceMAC = "00:00:00:00:00:00"; destMAC = "00:00:00:00:00:00"; packetBuffer = NULL; protocol = ETHERTYPE_IP; } int ClEthernet::GetPacketSize() { return (sizeof(struct Ethhdr)); }

void ClEthernet::GeneratePacket() { int packet_size = (sizeof(struct Ethhdr)); printf("PacketSize = %d\n",packet_size); char* packet=(char*)malloc(packet_size); memset(packet,0,packet_size); printf("Packet after creation is %s\n",packet); if(!packet) { printf("Could not allocate enough memory!\n"); return; } struct Ethhdr *eth = (struct Ethhdr*)packet; printf("lenght of packet before zeroing : %d\n",strlen(packet)); printf("lenght of packet after zeroing : %d\n",strlen(packet)); printf("PAcket after being zeroed : %s",packet); memcpy(eth->targ_hw_addr ,MACStringToArray(destMAC),sizeof(u_char)*6); memcpy(eth->src_hw_addr ,MACStringToArray(sourceMAC),sizeof(u_char)*6); eth->frame_type = htons(protocol); packetBuffer = packet; }

//2 #include #include #include #include #include #include #include #include <stdio.h> <ctype.h> <stdlib.h> <string.h> <errno.h> <netdb.h> <sys/socket.h> <arpa/inet.h>

#include <linux/if_ether.h> //for testing purpose only //#include "ClIP.h" #include "ClEthernet.h" //end of testing const char* DEFAULT_DEVICE = "eth0"; class ClSocket { private: int sockId; public: ClSocket(); bool Send(char* packet,unsigned int packetSize); char* deviceName; char* srcIP; char* destIP; };

ClSocket::ClSocket() { sockId=socket(AF_INET,SOCK_PACKET,htons(ETH_P_ALL)); if(sockId<0) { printf("Error creating socket\n"); } deviceName = (char*)DEFAULT_DEVICE; srcIP = NULL; destIP = NULL; } bool ClSocket::Send(char* packet,unsigned int packetSize) { if(packetSize<0) { printf("PacketSize is <0!\n"); return false; } struct in_addr src_in_addr,targ_in_addr; struct sockaddr sa; src_in_addr.s_addr = inet_addr(srcIP); targ_in_addr.s_addr = inet_addr(destIP); strcpy(sa.sa_data,deviceName); if(sendto(sockId,packet,packetSize,0,&sa,sizeof(sa)) < 0) { printf("Error sending packet\n"); return false; } return true; } int main() {

ClSocket sock; ClEthernet eth; sock.srcIP = "192.168.3.31"; sock.destIP = "192.168.3.1"; ClEthernet eth; eth.sourceMAC = "00:00:00:00:00:00"; eth.destMAC = "11:22:33:44:55:55"; eth.protocol = ETHERTYPE_IP; eth.GeneratePacket(); char* packet = new char[eth.GetPacketSize()]; eth.GeneratePacket(); sock.Send(eth.packetBuffer,eth.GetPacketSize()); //sock.Send("Im traveling on ethernet!",26);

Potrebbero piacerti anche