#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 extMac = {0x55, 0x47, 0x34, 0x22, 0x88, 0x92};
|
MacAddress extMac = {0x55, 0x47, 0x34, 0x22, 0x88, 0x92};
|
// MacAddress extMac = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
// MacAddress extMac = {0x00, 0x00, 0x00, 0x00, 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;
|
|
|
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] );
|
|
|
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 addr_to;
|
struct sockaddr_ll addr_to;
|
int addr_toLen;
|
int addr_toLen;
|
|
|
addr_toLen = sizeof(addr_to);
|
addr_toLen = sizeof(addr_to);
|
|
|
memset(&addr_to, '0', sizeof(addr_to) );
|
memset(&addr_to, '0', sizeof(addr_to) );
|
|
|
addr_to.sll_family = AF_PACKET;
|
addr_to.sll_family = AF_PACKET;
|
addr_to.sll_ifindex = if_index;
|
addr_to.sll_ifindex = if_index;
|
addr_to.sll_halen = 6;
|
addr_to.sll_halen = 6;
|
memcpy( &(addr_to.sll_addr), extMac, MAC_ADDR_LEN );
|
memcpy( &(addr_to.sll_addr), extMac, MAC_ADDR_LEN );
|
|
|
for (i=0; i<256 ; i++ )
|
for (i=0; i<256 ; i++ )
|
buf[i] = 0;
|
buf[i] = 0;
|
|
|
//first 2 bytes are gathered with length and are ignored
|
//first 2 bytes are gathered with length and are ignored
|
buf[0] = 0xAA;
|
buf[0] = 0xAA;
|
buf[1] = 0xAA;
|
buf[1] = 0xAA;
|
//now it gets to fpga: send opcode 0xBA8
|
//now it gets to fpga: send opcode 0xBA8
|
buf[2] = 0xBA;
|
buf[2] = 0xBA;
|
buf[3] = 0x87;
|
buf[3] = 0x87;
|
//opcode sent
|
//opcode sent
|
buf[4] = 0xAA;
|
buf[4] = 0xAA;
|
buf[5] = 0xAA;
|
buf[5] = 0xAA;
|
buf[6] = 0xAA;
|
buf[6] = 0xAA;
|
buf[7] = 0xAA;
|
buf[7] = 0xAA;
|
buf[8] = 0xAA;
|
buf[8] = 0xAA;
|
buf[9] = 0xAA;
|
buf[9] = 0xAA;
|
buf[10] = 0xAA;
|
buf[10] = 0xAA;
|
buf[11] = 0xAA;
|
buf[11] = 0xAA;
|
|
|
// for (;;)
|
// for (;;)
|
// {
|
// {
|
iRet = sendto(socket_id, buf, 46, 0, (struct sockaddr *) &addr_to, addr_toLen);
|
iRet = sendto(socket_id, buf, 46, 0, (struct sockaddr *) &addr_to, addr_toLen);
|
if ( iRet == -1 )
|
if ( iRet == -1 )
|
{
|
{
|
perror("sendto");
|
perror("sendto");
|
exit(1);
|
exit(1);
|
}
|
}
|
else
|
else
|
{
|
{
|
// printf("%s\n", buf);
|
// printf("%s\n", buf);
|
printf("Data sent!\nExiting...\n");
|
printf("Data sent!\nExiting...\n");
|
}
|
}
|
// }
|
// }
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|