src/raw.c
// create a new raw ethernet socket.
// it fills a sockaddr structure with information about
// a given interface. it uses two ioctl calls, SIOCGIFINDEX and
// SIOCGIFHWADDR, to get the interfaces index number and hardware
// address.
// it takes a pointer to the sockaddr structure, the socket to use,
// and the interface name (as a string) as arguments.
// it returns 0 for success, or a non-zero integer for failure.
create a new raw ethernet socket.
it fills a sockaddr structure with information about
a given interface. it uses two ioctl calls, SIOCGIFINDEX and
SIOCGIFHWADDR, to get the interfaces index number and hardware
address.
it takes a pointer to the sockaddr structure, the socket to use,
and the interface name (as a string) as arguments.
it returns 0 for success, or a non-zero integer for failure.
create a new raw ethernet socket . it fills a sockaddr structure with information about a given interface . it uses two ioctl calls SIOCGIFINDEX and SIOCGIFHWADDR to get the interfaces index number and hardware address . it takes a pointer to the sockaddr structure the socket to use and the interface name ( as a string ) as arguments . it returns 0 for success or a non - zero integer for failure .
int ethterm_init(const char * if_name)
{
	struct ifreq ifr;
	if((ethterm_s = socket(AF_PACKET, SOCK_RAW, htons(ETHTERM_TYPE))) == -1)
		return 1;
	strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
	if(ioctl(ethterm_s, SIOCGIFINDEX, &ifr) == -1)
	{
		ethterm_cleanup();
		return 1;
	}
	ethterm_sockaddr.sll_ifindex = ifr.ifr_ifindex;
	ethterm_sockaddr.sll_family = AF_PACKET;
	ethterm_sockaddr.sll_protocol = htons(ETHTERM_TYPE);
	ethterm_sockaddr.sll_hatype = ARPHRD_ETHER;
	ethterm_sockaddr.sll_pkttype = PACKET_OTHERHOST;
	ethterm_sockaddr.sll_halen = ETH_ALEN;
	if(ioctl(ethterm_s, SIOCGIFHWADDR, &ifr) == -1)
	{
		ethterm_cleanup();
		return 1;
	}
	printf("index of eth0 is: %d\n", ethterm_sockaddr.sll_ifindex);
	printf("mac of eth0 is: %02x:%02x:%02x:%02x:%02x:%02x\n", \
	ifr.ifr_hwaddr.sa_data[0] & 0xff, \
	ifr.ifr_hwaddr.sa_data[1] & 0xff, \
	ifr.ifr_hwaddr.sa_data[2] & 0xff, \
	ifr.ifr_hwaddr.sa_data[3] & 0xff, \
	ifr.ifr_hwaddr.sa_data[4] & 0xff, \
	ifr.ifr_hwaddr.sa_data[5] & 0xff);
	memcpy(ethterm_sockaddr.sll_addr, ethterm_mac_dst, 6);
	ethterm_s_buf = malloc(2048);
	if(ethterm_s_buf == NULL)
	{
		ethterm_cleanup();
		return 1;
	}
	memcpy(ethterm_s_buf, ethterm_mac_dst, 6);
	memcpy(ethterm_s_buf + 6, ifr.ifr_hwaddr.sa_data, 6);
	*((uint16_t *) (ethterm_s_buf + 12)) = htons(ETHTERM_TYPE);
	return 0;
}
int ethterm_init ( const char * if_name ) { struct ifreq ifr ; if ( ( ethterm_s = socket ( AF_PACKET , SOCK_RAW , htons ( ETHTERM_TYPE ) ) ) == -1 ) return 1 ; strncpy ( ifr . ifr_name , if_name , IFNAMSIZ ) ; if ( ioctl ( ethterm_s , SIOCGIFINDEX , & ifr ) == -1 ) { ethterm_cleanup ( ) ; return 1 ; } ethterm_sockaddr . sll_ifindex = ifr . ifr_ifindex ; ethterm_sockaddr . sll_family = AF_PACKET ; ethterm_sockaddr . sll_protocol = htons ( ETHTERM_TYPE ) ; ethterm_sockaddr . sll_hatype = ARPHRD_ETHER ; ethterm_sockaddr . sll_pkttype = PACKET_OTHERHOST ; ethterm_sockaddr . sll_halen = ETH_ALEN ; if ( ioctl ( ethterm_s , SIOCGIFHWADDR , & ifr ) == -1 ) { ethterm_cleanup ( ) ; return 1 ; } printf ( " \n " , ethterm_sockaddr . sll_ifindex ) ; printf ( " \n " , ifr . ifr_hwaddr . sa_data [ 0 ] & 0xff , ifr . ifr_hwaddr . sa_data [ 1 ] & 0xff , ifr . ifr_hwaddr . sa_data [ 2 ] & 0xff , ifr . ifr_hwaddr . sa_data [ 3 ] & 0xff , ifr . ifr_hwaddr . sa_data [ 4 ] & 0xff , ifr . ifr_hwaddr . sa_data [ 5 ] & 0xff ) ; memcpy ( ethterm_sockaddr . sll_addr , ethterm_mac_dst , 6 ) ; ethterm_s_buf = malloc ( 2048 ) ; if ( ethterm_s_buf == NULL ) { ethterm_cleanup ( ) ; return 1 ; } memcpy ( ethterm_s_buf , ethterm_mac_dst , 6 ) ; memcpy ( ethterm_s_buf + 6 , ifr . ifr_hwaddr . sa_data , 6 ) ; * ( ( uint16_t * ) ( ethterm_s_buf + 12 ) ) = htons ( ETHTERM_TYPE ) ; return 0 ; }
create a new raw ethernet socket.
create a new raw ethernet socket .
// create a raw ethernet socket // copy the given interface name to the ifr structure // get the interface index // fill the sockaddr structure // get the interface hardware (mac) address // allocate memory for send buffer // set the dest mac address // set the src mac address // set the type