При попытке запустить так
sudo ./run
пишет ошибку write: No such device or address
.
Вот код
#include <stdio.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <netinet/if_ether.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
unsigned char ipsrc[4] = { 0xc0, 0xa8, 0x01, 0x05 };
unsigned char ipdst[4] = { 0xc0, 0xa8, 0x01, 0x02 };
unsigned char srchw[6] = { 0x74, 0x2f, 0x68, 0xa2, 0x87, 0xc6 };
unsigned char broadcast[6]= { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
unsigned char unknown[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int main ( int argc, char *argv[] )
{
int sock;
if ( ( sock = socket ( AF_PACKET, SOCK_RAW, htons ( ETH_P_ALL ) ) ) == -1 ) // ETH_P_ARP
{
perror ( "socket" );
exit ( EXIT_FAILURE );
}
int length = sizeof ( struct ether_header ) + sizeof ( struct ether_arp );
char buffer[length+1]; // length == 42
struct ether_header *eh = (struct ether_header *)&buffer[0];
struct ether_arp *ea = (struct ether_arp *) ( sizeof ( struct ether_header ) + &buffer[0] );
strncpy ( eh->ether_shost, srchw, ETH_ALEN );
strncpy ( eh->ether_dhost, broadcast, ETH_ALEN );
eh->ether_type = ETHERTYPE_ARP;
ea->ea_hdr.ar_hrd = ARPHRD_ETHER;
ea->ea_hdr.ar_pro = 0x800;
ea->ea_hdr.ar_hln = 6;
ea->ea_hdr.ar_pln = 4;
ea->ea_hdr.ar_op = ARPOP_REQUEST;
strncpy ( ea->arp_sha, srchw, ETH_ALEN );
strncpy ( ea->arp_tha, unknown, ETH_ALEN );
strncpy ( ea->arp_spa, ipsrc, 4 );
strncpy ( ea->arp_tpa, ipdst, 4 );
if ( write ( sock, buffer, length ) == -1 )
{
perror ( "write" );
}
close ( sock );
}