Re: Raw Packets

Russell Nelson (nelson@crynwr.com)
Sat, 5 Aug 95 10:15 EDT


Date: Fri, 4 Aug 1995 20:09:20 +0200 (GMT+0200)
From: Andrew Cameron <andrew@andy.alt.za>

I would like to know how to send a raw packet onto an ethernet lan with
Linux.

The format I want is

write_raw(Source_Ether_Addr,Dest_Ether_Address,data,datalen)
and
read_raw(Source_Ether_Addr,Dest_Ether_Address,data,datalen)

You ought to be able to figure it out from the following snippet:

get_raw_interface(char *device_name, uchar my_eaddr[])
{
struct ifreq ifreq;

oldhandler = signal(SIGINT, sig_int);

if ((ethfd = socket(AF_INET,SOCK_PACKET,htons(PACKET_TYPE))) == -1)
{ perror("Couldn't open driver");
return 1;
}
strcpy(ifreq.ifr_name, "eth0");
if (ioctl(ethfd, SIOCGIFHWADDR, &ifreq) < 0) {
perror("Couldn't get address");
return 1;
}
memcpy(my_eaddr, ifreq.ifr_hwaddr.sa_data, EADDR_LEN);
return 0;
}

struct ethbuff *
get_raw_packet(void)
{
buff.length = recvfrom(ethfd, buff.buffer, sizeof(buff.buffer), 0, NULL, NULL);
return &buff;
}

struct sockaddr usin={AF_INET, "eth0"};

void
send_raw_pkt(struct ethbuff *buffptr)
{
memcpy(buffptr->buffer+EADDR_LEN, my_own_eaddr, EADDR_LEN);
sendto(ethfd, buffptr->buffer, buffptr->length, 0, &usin, sizeof(struct sockaddr));
}