i tried to write a packet (for fun) in ARP format (which i build) to the network - i wrote this code below with help looking at the man pages for "packet". I could read ARP packets but when i tried to write one - the code fails - i get an error message printed by perror which says
" Write failed :No such device or address"
Could somebody point out what i have missed out . The arp.h header file is mine and have pasted its contents below.
Thank you.
-Amith
#include <sys/socket.h> #include <features.h> /* for the glibc version number */ #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1 #include <netpacket/packet.h> #include <net/ethernet.h> /* the L2 protocols */ #else #include <asm/types.h> #include <linux/if_packet.h> #include <linux/if_ether.h> /* The L2 protocols */ #endif #include<errno.h> #include<arp.h> /* My include file */
struct Packet{ struct Ethernet_Header ether; struct Arp_Header arp; };
int Int(unsigned char temp) { int x=1; int total=0; int count = 0; while(count < 8) { total = total + ( (temp & 0x01) * ( (x) ) ); temp = temp >> 1; x = x * 2; count++; } return total; }
void Print_Arp_Packet(struct Packet *pkt); void Send_Arp_Packet(struct Packet *pkt);
struct Packet *pkt;
unsigned char buffer[1000]; int arp_counter,tcp_counter;
int socket_type = SOCK_RAW; int protocol; int len=0; int packet_socket;
int main() {
protocol = htons(ETH_P_ALL); packet_socket = socket(PF_PACKET, socket_type, protocol);
if(packet_socket <= 0) { perror("could not create socket"); exit(1); } printf("\n packet socket created \n");
while( 1 )
{
int count=0;
printf("========================================= \n");
len = read(packet_socket,buffer,sizeof(buffer));
pkt = (struct Packet *)buffer;
if((pkt->ether.type[0] == 0X08) && (pkt->ether.type[1] ==0x06)) /* check for ARP Packet */
{
printf("ARP :");
Print_Arp_Packet(pkt);
Send_Arp_Packet(pkt);
}
}
}
void Print_Arp_Packet(struct Packet *pkt)
{
printf(" MAC Adrres:Dest: %X:%X:%X:%X:%X:%X ",pkt->ether.dest_addr[0], pkt->ether.dest_addr[1], pkt->ether.dest_addr[2], \
pkt->ether.dest_addr[3], pkt->ether.dest_addr[4], \
pkt->ether.dest_addr[5]);
printf(" MAC Adrres : Src : %X:%X:%X:%X:%X:%X\n",pkt->ether.source_addr[0], \
pkt->ether.source_addr[1], pkt->ether.source_addr[2], \
pkt->ether.source_addr[3], pkt->ether.source_addr[4], \
pkt->ether.source_addr[5]);
} void Send_Arp_Packet(struct Packet *pkt)
{
struct Packet temp; pkt->ether.dest_addr[0] = pkt->ether.source_addr[0]; /* had set this to 0xFFFFFFFFFFFF initially even that failed*/
pkt->ether.dest_addr[1] = pkt->ether.source_addr[1];
pkt->ether.dest_addr[2] = pkt->ether.source_addr[2];
pkt->ether.dest_addr[3] = pkt->ether.source_addr[3];
pkt->ether.dest_addr[4] = pkt->ether.source_addr[4];
pkt->ether.dest_addr[5] = pkt->ether.source_addr[5];
pkt->ether.source_addr[0] = 0x00; pkt->ether.source_addr[1] = 0x50; pkt->ether.source_addr[0] = 0xBA; /* my MAC Address */ pkt->ether.source_addr[0] = 0x88; pkt->ether.source_addr[0] = 0x15; pkt->ether.source_addr[0] = 0xF9;
pkt->ether.type[0] = 0x08; /* Setting it to 0X0806 - > arp prot type */
pkt->ether.type[1] = 0x06;
pkt->arp.Opcode[1] = 0x02;
memcpy((void *) &temp, (void *) pkt, sizeof(temp)); len = write(packet_socket,&temp,sizeof(temp)); if(len < 0) { perror("Write failed "); exit(0); } printf(" Write success len = %d \n",len); }
/* Write failed : No such device or address */
/* arp.h 's contents */
struct Ethernet_Header { unsigned char dest_addr[6]; unsigned char source_addr[6]; unsigned char type[2]; };
struct Arp_Header { unsigned char Hardware_Type[2]; /* 0x0001 - Ethernet */ unsigned char Protocol_Type[2]; /* 0x0008 - IP */ unsigned char Hardware_Size[1]; /* 0X06 - 6 byte ethernet addr */ unsigned char Protocol_Size[1]; /* 0x04 - 4 byte ip address */ unsigned char Opcode[2]; /* 0x0001 - request */ unsigned char Sender_MAC_Addr[6]; unsigned char Sender_IP_Addr[4]; unsigned char Target_MAC_Addr[6]; unsigned char Target_IP_Addr[4]; };
-- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/