#include <stdio.h>
|
#include <stdio.h>
|
|
|
#include <string.h>
|
#include <string.h>
|
|
|
//packet socket
|
//packet socket
|
#include <sys/socket.h>
|
#include <sys/socket.h>
|
#include <netpacket/packet.h>
|
#include <netpacket/packet.h>
|
#include <net/ethernet.h>
|
#include <net/ethernet.h>
|
|
|
//protocol
|
//protocol
|
#include <linux/if_ether.h>
|
#include <linux/if_ether.h>
|
|
|
//netdevice stuff
|
//netdevice stuff
|
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
#include <net/if.h>
|
#include <net/if.h>
|
|
|
//file open stuff
|
//file open stuff
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <sys/stat.h>
|
#include <sys/stat.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
|
|
//arp stuff
|
//arp stuff
|
//#include <linux/if_arp.h>
|
//#include <linux/if_arp.h>
|
|
|
#define MAC_ADDR_LEN 6
|
#define MAC_ADDR_LEN 6
|
typedef unsigned char MacAddress[MAC_ADDR_LEN];
|
typedef unsigned char MacAddress[MAC_ADDR_LEN];
|
|
|
int main()
|
int main()
|
{
|
{
|
int socket_id, new_sock, iRet = -1;
|
int socket_id, new_sock, iRet = -1;
|
int addrlen, bytesread, nfound =0;
|
int addrlen, bytesread, nfound =0;
|
|
|
int i = 0;
|
int i = 0;
|
|
|
MacAddress localMac = {0x00, 0x00, 0xC0, 0x41, 0x36, 0xD3};
|
MacAddress localMac = {0x00, 0x00, 0xC0, 0x41, 0x36, 0xD3};
|
// MacAddress localMac = {0xD3, 0x36, 0x41, 0xC0, 0x00, 0x00};
|
// MacAddress localMac = {0xD3, 0x36, 0x41, 0xC0, 0x00, 0x00};
|
|
|
char buf[256];
|
char buf[256];
|
|
|
struct sockaddr_ll my_addr;
|
struct sockaddr_ll my_addr;
|
|
|
struct ifreq ethreq;
|
struct ifreq ethreq;
|
|
|
int if_index;
|
int if_index;
|
|
|
//create packet socket from type sock_dgram where headers are automatically thrown out
|
//create packet socket from type sock_dgram where headers are automatically thrown out
|
if ( ( socket_id = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL) ) ) < 0 )
|
if ( ( socket_id = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL) ) ) < 0 )
|
{
|
{
|
perror("socket");
|
perror("socket");
|
exit(1);
|
exit(1);
|
}
|
}
|
else
|
else
|
{
|
{
|
printf("Socket has been created: socket_number %d\n", socket_id);
|
printf("Socket has been created: socket_number %d\n", socket_id);
|
}
|
}
|
|
|
|
|
//GET ethreq for if "eth1"
|
//GET ethreq for if "eth1"
|
strncpy(ethreq.ifr_name,"eth1",IFNAMSIZ);
|
strncpy(ethreq.ifr_name,"eth1",IFNAMSIZ);
|
ioctl(socket_id, SIOCGIFFLAGS, ðreq);
|
ioctl(socket_id, SIOCGIFFLAGS, ðreq);
|
//SET promisc mode for if ethreq
|
//SET promisc mode for if ethreq
|
// ethreq.ifr_flags |= IFF_PROMISC;
|
// ethreq.ifr_flags |= IFF_PROMISC;
|
// ioctl(socket_id, SIOCSIFFLAGS, ðreq);
|
// ioctl(socket_id, SIOCSIFFLAGS, ðreq);
|
//request index
|
//request index
|
ioctl(socket_id, SIOCGIFINDEX, ðreq);
|
ioctl(socket_id, SIOCGIFINDEX, ðreq);
|
|
|
if_index = ethreq.ifr_ifindex;
|
if_index = ethreq.ifr_ifindex;
|
|
|
printf("This is the index of the interface: %d\n", if_index );
|
printf("This is the index of the interface: %d\n", if_index );
|
|
|
memset(&my_addr, '0', sizeof(my_addr) );
|
memset(&my_addr, '0', sizeof(my_addr) );
|
|
|
my_addr.sll_family = AF_PACKET;
|
my_addr.sll_family = AF_PACKET;
|
my_addr.sll_protocol = htons(ETH_P_ALL); //defaults to socket protocol
|
my_addr.sll_protocol = htons(ETH_P_ALL); //defaults to socket protocol
|
my_addr.sll_ifindex = if_index;
|
my_addr.sll_ifindex = if_index;
|
// my_addr.sll_hatype = htons(ARPHRD_ETHER);
|
// my_addr.sll_hatype = htons(ARPHRD_ETHER);
|
// my_addr.sll_pkttype = PACKET_OTHERHOST;
|
// my_addr.sll_pkttype = PACKET_OTHERHOST;
|
my_addr.sll_halen = 6;
|
my_addr.sll_halen = 6;
|
memcpy( &(my_addr.sll_addr), localMac, MAC_ADDR_LEN );
|
memcpy( &(my_addr.sll_addr), localMac, MAC_ADDR_LEN );
|
|
|
//request hw_addres
|
//request hw_addres
|
ioctl(socket_id, SIOCGIFHWADDR, ðreq);
|
ioctl(socket_id, SIOCGIFHWADDR, ðreq);
|
|
|
printf("This is the address of my card: %d\n", my_addr.sll_addr[5] );
|
printf("This is the address of my card: %d\n", my_addr.sll_addr[5] );
|
|
|
//bind to interface goten from ioctl SIOCGIFHWADDR directive (otherwise all packets are recved)
|
//bind to interface goten from ioctl SIOCGIFHWADDR directive (otherwise all packets are recved)
|
if ( bind( socket_id, (struct sockaddr *) &my_addr, sizeof(my_addr) ) )
|
if ( bind( socket_id, (struct sockaddr *) &my_addr, sizeof(my_addr) ) )
|
{
|
{
|
perror("bind");
|
perror("bind");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
struct sockaddr_ll from;
|
struct sockaddr_ll from;
|
int fromlen;
|
int fromlen;
|
|
|
fromlen = sizeof(from);
|
fromlen = sizeof(from);
|
|
|
for (;;)
|
for (;;)
|
{
|
{
|
iRet = recvfrom(socket_id, buf, 256, 0, &from, &fromlen);
|
iRet = recvfrom(socket_id, buf, 256, 0, &from, &fromlen);
|
if ( iRet == -1 )
|
if ( iRet == -1 )
|
{
|
{
|
perror("recvfrom");
|
perror("recvfrom");
|
exit(1);
|
exit(1);
|
}
|
}
|
else
|
else
|
{
|
{
|
printf("Received %d bytes of data.\n", iRet);
|
printf("Received %d bytes of data.\n", iRet);
|
printf("This is the received data:\n");
|
printf("This is the received data:\n");
|
for ( i = 0; i < iRet; i++)
|
for ( i = 0; i < iRet; i++)
|
printf("Byte %d: %X\n", i, (int)buf[i]);
|
printf("Byte %d: %X\n", i, (int)buf[i]);
|
printf("End of transmission!\n");
|
printf("End of transmission!\n");
|
}
|
}
|
}
|
}
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|