Hi all,
Friends I have built an IP packet and I need to inject this packet to another computer on the same LAN and hence with a different ethernet header. I have used sockets and managed to inject a raw packet on to the wire but what exactly I want is that the kernel should build the ethernet header for me.
Here is my code ...
Code:
inject(u_char *pkt, int nr)
{
/*open socket*/
fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (fd == -1)
{
printf("\nSocket failed!!");
}
if(setsockopt (fd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one))<0)
printf("iphdrincl error");
/*Select interface*/
memset(&ifr, 0, sizeof(ifr));
strncpy (ifr.ifr_name, iftext, sizeof(ifr.ifr_name) - 1);
ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1)
{
printf("No such interface: %s\n", iftext);
return -1;
}
ioctl(fd, SIOCGIFFLAGS, &ifr);
if ( (ifr.ifr_flags & IFF_UP) == 0)
{
printf("Interface %s is down\n", iftext);
return -1;
}
ioctl(fd, SIOCGIFINDEX, &ifr);
memset(&sa, 0, sizeof (sa));
sa.sll_family = AF_PACKET;
sa.sll_ifindex = ifr.ifr_ifindex;
sa.sll_protocol = htons(ETH_P_ALL);
/*Send*/
c = sendto(fd, pkt, nr, 0, (struct sockaddr *)&sa, sizeof (sa));
}
Note: The setsockopt() IP_HDRINCL option gives an error with errno = 92.
Please help me...any help will be appreciated.