#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <features.h> /* for the glibc version number */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
#endif
#include <sys/ioctl.h>
#include <pcap.h>
#include <libnet.h>
#define DEVICE0 "eth0"
#define DEVICE1 "eth1"
#define IP_DF 0x4000
#define IP_MF 0x2000
int packet = 0;
int paramd = 0;
/*------------------------------------------------------------*/
/* A function for transmit packets */
/*------------------------------------------------------------*/
void Transmit(u_char *foo, const struct pcap_pkthdr *header, const u_char *data)
{
struct ethhdr eth;
struct iphdr *ip;
u_char *ip_data;
int ip_data_len;
libnet_t *l;
libnet_ptag_t eth_tag;
char errbuf[LIBNET_ERRBUF_SIZE];
u_int8_t *macsrc, *macdst;
int r;
memcpy ((char *) ð, data, sizeof(struct ethhdr));
printf("#############################################\n");
printf("Packet #%d (%d bytes read)\n", ++packet, header->caplen);
printf("==ETHERNET_HEADER============================\n");
printf("MAC destination ");
printf(":%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
eth.h_source[0], eth.h_source[1], eth.h_source[2],
eth.h_source[3], eth.h_source[4], eth.h_source[5]);
printf("MAC source ");
printf(":%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
eth.h_dest[0], eth.h_dest[1], eth.h_dest[2],
eth.h_dest[3], eth.h_dest[4], eth.h_dest[5]);
printf("Packet type ID field :%#x\n", ntohs(eth.h_proto));
if (ntohs(eth.h_proto) == ETH_P_IP)
{
ip = (struct iphdr *)(data + sizeof(struct ethhdr));
ip_data_len=ntohs(ip->tot_len);
ip_data=(u_char *) (data+sizeof(struct ethhdr));
// #########################################
macdst = libnet_hex_aton((int8_t*)"00:11:09:CE:F1:14", &r);
macsrc = libnet_hex_aton((int8_t*)"00:15:e9:ef:9c:28", &r);
l = libnet_init(LIBNET_LINK, DEVICE1, errbuf);
if (l == NULL) {
fprintf(stderr, "Error opening context: %s", errbuf);
exit(-1);
}
/* ethernet header */
eth_tag = libnet_build_ethernet(
macdst, /* dst HW addr */
macsrc, /* src HW addr */
ETHERTYPE_IP, /* ether packet type */
(u_int8_t *) ip_data, /* ptr to payload */
ip_data_len, /* payload size */
l, /* libnet tag */
0); /* ptr to packet memory */
if (eth_tag == -1) {
fprintf(stderr,
"Unable to build Ethernet header: %s\n", libnet_geterror(l));
exit(-1);
}
/* write the packet */
if ((libnet_write(l)) == -1) {
fprintf(stderr, "Unable to send packet: %s\n", libnet_geterror(l));
exit(-1);
}
/* exit cleanly */
libnet_destroy(l);
}
// ##################################################
}
/*---------------------*/
/* The main() function */
/*--------------------*/
int main(int argc, char* argv[])
{
pcap_t *handle;
char *dev;
int i;
char errbuf[PCAP_ERRBUF_SIZE];
const u_char *packet;
/* dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "%s", errbuf);
exit(-1);
}
*/
handle = pcap_open_live(DEVICE0, BUFSIZ, 1, 0, errbuf);
if (handle == NULL) {
fprintf(stderr, "%s", errbuf);
exit(-1);
}
if (pcap_loop(handle, -1, Transmit, NULL)) {
pcap_perror(handle, "pcap_loop()");
pcap_close(handle);
exit(-1);
}
pcap_close(handle);
return 0;
}
За криво написаную софтину не ругайте. Интерфейсы задаются в коде. В дальнейшем добавлю использование BPF.