#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <linux/if.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
int main(int argc, char **argv)
{
struct ifreq stIfr;
int iSockFd = -1, i;
struct sockaddr_ll stSll;
int iRcvNum, iLen = sizeof(stSll);
unsigned char ucDataBuf[1600], ucTmp;
unsigned int k = 0;
if (argc != 2)
{
printf("usage: ./loop ethername.\n");
return 1;
}
if ((iSockFd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
printf("create socket error. errno=%d\n", errno);
return 1;
}
strcpy(stIfr.ifr_name, argv[1]);
ioctl(iSockFd, SIOCGIFINDEX, &stIfr);
bzero(&stSll, sizeof(stSll));
stSll.sll_family = AF_PACKET;
stSll.sll_halen = 6;
stSll.sll_ifindex = stIfr.ifr_ifindex;
if (bind(iSockFd, (struct sockaddr *)&stSll, sizeof(stSll)) == -1)
{
printf("bind error.errno. errno=%d\n", errno);
}
while(1)
{
iRcvNum = recvfrom(iSockFd, ucDataBuf, 1500, 0, (struct sockaddr*)&stSll, &iLen);
printf("%.2x ", ucDataBuf[18]);
printf("%.2x ", ucDataBuf[19]);
}
return 0;
}