#include "../include/config.h"
#include "include/libnet.h"
#include "include/packet.h"
libnet_t
* libnet_init_pkt(int injection_type
, char *device
, char *err_buf
);
int libnet_write_pkt(libnet_t
*l
, u_int8_t
**packet
, u_int32_t
*len
,
void libnet_print_pkt(u_int8_t
*pktbuf
, u_int32_t pktlen
);
extern unsigned char * genPayload (int data_type
, unsigned int seed
,
extern unsigned char * addPayload (unsigned char *p1
, int l1
,
unsigned char *p2
, int l2
) ;
extern unsigned char * addCrc (unsigned char *p1
, int l1
, unsigned int crc
) ;
extern unsigned char * addL2Pad(unsigned char * pktbuf
,int pktlen
,int l2_pad_length
) ;
extern int libnet_gen_l4_packet(struct packet_desc
*packet_vars
, libnet_t
*l
);
extern int libnet_gen_l3_packet(struct packet_desc
*packet_vars
, libnet_t
*l
);
extern int libnet_gen_l2_packet(struct packet_desc
*packet_vars
, libnet_t
*l
);
char * libnet_call(struct packet_desc
*packet_vars
, u_int32_t
*total_len
) {
char errbuf
[LIBNET_ERRBUF_SIZE
];
* Initialize the library. Root priviledges are required.
LIBNET_LINK
, /* injection type */
NULL
, /* network interface */
errbuf
); /* error buffer */
fprintf(stderr
, "libnet_init() failed: %s", errbuf
);
// The guts of packet gen
if(packet_vars
->l2_pkt_type
==L2_RUNT
) {
l2_length
= packet_vars
->payload
.data_length
;
l2_length
= l2_length
- 4;
pktbuf
= genPayload(packet_vars
->payload
.data_type
,
packet_vars
->payload
.data_seed
,
crc
= libnet_compute_crc(pktbuf
,pktlen
);
pktbuf
= addCrc(pktbuf
,pktlen
,crc
);
pktbuf
= genPayload(packet_vars
->payload
.data_type
,
packet_vars
->payload
.data_seed
,
status
= libnet_gen_l4_packet(packet_vars
, l
);
printf("ERROR libnet_gen_l4_packet failed \n");
status
= libnet_gen_l3_packet(packet_vars
, l
);
printf("ERROR libnet_gen_l3_packet failed \n");
status
= libnet_gen_l2_packet(packet_vars
, l
);
printf("ERROR libnet_gen_l2_packet failed \n");
/* sbehera - need this for IP header alignment */
c
= libnet_write_pkt(l
, &pktbuf
, &pktlen
, pktalign
);
fprintf(stderr
, "Write error: %s\n", libnet_geterror(l
));
payload_start_ptr
= packet_vars
->l2_hdr
+packet_vars
->l3_hdr
+ packet_vars
->l4_hdr
;
if( ( packet_vars
->payload
.error_code
& PG_CHKSUM_ERR
) ){
if (packet_vars
->l4_pkt_type
& L4_PKT_TYPE_TCP
) {
printf("Libnet CheckSum - %x%x\n",pktbuf
[payload_start_ptr
-4],pktbuf
[payload_start_ptr
-3]);
pktbuf
[payload_start_ptr
-4] = pktbuf
[payload_start_ptr
-4]^1;
} else if(packet_vars
->l4_pkt_type
& L4_PKT_TYPE_UDP
) {
printf("Libnet CheckSum - %x%x\n",pktbuf
[payload_start_ptr
-2],pktbuf
[payload_start_ptr
-1]);
pktbuf
[payload_start_ptr
-1] = pktbuf
[payload_start_ptr
-1]^1;
pktbuf
= addL2Pad(pktbuf
,pktlen
,packet_vars
->payload
.l2_pad_length
);
pktlen
+=packet_vars
->payload
.l2_pad_length
;
crc
= libnet_compute_crc(pktbuf
,pktlen
);
printf(" Libnet CRC - %x \n",crc
);
if(packet_vars
->payload
.error_code
& PG_CRC_ERR
) {
printf("Libnet CRCError - %x \n",crc
);
pktbuf
= addCrc(pktbuf
,pktlen
,crc
);
// c = libnet_write_pkt(l, &pktbuf, &pktlen, pktalign);
// crc = libnet_compute_crc(pktbuf,pktlen);
// pktbuf = addCrc(pktbuf,pktlen,crc);
fprintf(stderr
, "Write error: %s\n", libnet_geterror(l
));
fprintf(stderr
, "Wrote %d byte packet; check the wire.\n", pktlen
);
// libnet_print_pkt(pktbuf, pktlen);