#include <stdlib.h>
#include <string.h>
//libnet
#include <libnet.h>
static int sendLibnetLink()
{
int packet_size = 0;
libnet_t *l = NULL;
char *device = "eth0";
char *destination_ip_str = "192.168.8.1";
char *source_ip_str = "192.168.8.83";
libnet_ptag_t eth_ptag = 0;
unsigned char src_mac[ETH_ALEN] = {0x00, 0xe0, 0x4c, 0x3d, 0x59, 0xdc};
unsigned char dst_mac[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
char error_information[LIBNET_ERRBUF_SIZE];
l = libnet_init(LIBNET_LINK, device, error_information);
printf("Using device %s\n", l->device);
if (l == NULL)
{
fprintf(stderr, "libnet_init() failed: %s", error_information);
exit(EXIT_FAILURE);
}
eth_ptag = libnet_build_ethernet(
dst_mac, /* ethernet destination */
src_mac, /* ethernet source */
(0x8033), /* protocol type */
"hello", //senddata, /* payload */
strlen("hello"), //sizeof(senddata), /* payload size */
l, /* libnet handle */
0); /* libnet id */
if (eth_ptag == -1)
{
fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
goto bad;
}
/*
* Write it to the wire.
*/
int c = libnet_write(l);
if (c == -1)
{
fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
goto bad;
}
else
{
fprintf(stderr, "Wrote %d byte IP packet; check the wire.\n", c);
}
libnet_destroy(l);
return (EXIT_SUCCESS);
bad:
libnet_destroy(l);
return (EXIT_FAILURE);
}
//compile
#gcc link.c -o link -lnet
//run
#./link