http://wiki.ucis.nl/index.php?title=Projects/Software/TunTapIO/pcap.c&feed=atom&action=history
Projects/Software/TunTapIO/pcap.c - Revision history
2024-03-29T12:18:31Z
Revision history for this page on the wiki
MediaWiki 1.38.4
http://wiki.ucis.nl/index.php?title=Projects/Software/TunTapIO/pcap.c&diff=150&oldid=prev
Admin: 1 revision imported
2022-11-09T14:49:54Z
<p>1 revision imported</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 15:49, 9 November 2022</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>
Admin
http://wiki.ucis.nl/index.php?title=Projects/Software/TunTapIO/pcap.c&diff=149&oldid=prev
Admin: Nieuwe pagina aangemaakt met '<pre> /********************************************************************** * rsfio - filtered raw socket to stdin/out proxy * * Written by: Ivo...'
2009-04-22T23:31:15Z
<p>Nieuwe pagina aangemaakt met '<pre> /********************************************************************** * rsfio - filtered raw socket to stdin/out proxy * * Written by: Ivo...'</p>
<p><b>New page</b></p><div><pre><br />
/**********************************************************************<br />
* rsfio - filtered raw socket to stdin/out proxy *<br />
* Written by: Ivo Smits <Ivo@UFO-Net.nl> *<br />
* *<br />
* Compile using: gcc pcap.c -o lpcio -lpcap *<br />
* *<br />
* Many thanks to: *<br />
* - http://www.blug.linux.no/rfc1149/ *<br />
* - http://linux.about.com/od/commands/l/blcmdl2_select.htm *<br />
* - http://www.linuxjournal.com/article/4659 *<br />
* - http://www.tcpdump.org/pcap.htm *<br />
* - http://www.winpcap.org/docs/man/html/ *<br />
* - http://www.tcpdump.org/lists/workers/2005/06/msg00102.html *<br />
* *<br />
**********************************************************************/<br />
<br />
#include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h><br />
#include <fcntl.h> <br />
#ifndef HAVE_NETINET_IN_H<br />
#include <netinet/in.h><br />
#endif<br />
<br />
#include <sys/ioctl.h><br />
<br />
#include <errno.h><br />
#include <dirent.h><br />
<br />
#include <errno.h><br />
#include <sys/types.h><br />
#include <sys/socket.h><br />
#include <netpacket/packet.h><br />
#include <netinet/in.h><br />
#include <net/ethernet.h><br />
#include <net/if.h><br />
#include <linux/types.h><br />
#include <linux/filter.h><br />
#include <linux/if_ether.h><br />
<br />
#include <pcap.h><br />
<br />
int DumpStats = 1;<br />
int IncludePLen = 1;<br />
<br />
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);<br />
<br />
// The main program, this is where all the magic happens<br />
int main(int argc, char** argv) {<br />
<br />
//Parse command line arguments<br />
if (argc > 1) {<br />
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {<br />
fprintf(stderr, "rsio - raw socket to stdio proxy\n");<br />
fprintf(stderr, "Usage: %s INTF [CLEN] [FILTR]\n", argv[0]);<br />
fprintf(stderr, " INTF: the name of the network interface to connect to\n");<br />
fprintf(stderr, " CLEN: capture size (should be the same as the mtu, default: %d)\n", ETH_FRAME_LEN);<br />
fprintf(stderr, " FILTR: tcpdump style filter expression (man tcpdump)\n");<br />
fprintf(stderr, "Note that the arguments should be in exactly THIS order.\n");<br />
fprintf(stderr, "Report bugs to <Ivo@UFO-Net.nl>\n");<br />
exit(0);<br />
}<br />
} else {<br />
//Application won't run without arguments<br />
//fprintf(stderr, "Try: %s -h\n", argv[0]);<br />
//exit(1);<br />
}<br />
<br />
int CaptureLen = ETH_FRAME_LEN;<br />
//Different capture length?<br />
if (argc > 2) CaptureLen = atoi(argv[2]);<br />
if (CaptureLen < 1) {<br />
fprintf(stderr, "Capture length %d invalid!\n", CaptureLen);<br />
exit(1);<br />
}<br />
fprintf(stderr, "Max packet length: %d\n", CaptureLen);<br />
<br />
char *dev, errbuf[PCAP_ERRBUF_SIZE];<br />
<br />
if (argc > 1) {<br />
dev = argv[1];<br />
} else {<br />
if ((dev = pcap_lookupdev(errbuf)) == NULL) {<br />
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);<br />
exit(1);<br />
}<br />
}<br />
fprintf(stderr, "Device: %s\n", dev);<br />
<br />
pcap_t *pcap;<br />
if ((pcap = pcap_open_live(dev, CaptureLen, 1, 0, errbuf)) == NULL) {<br />
fprintf(stderr, "Couldn't open device: %s\n", errbuf);<br />
exit(1);<br />
}<br />
<br />
if (argc > 3) {<br />
fprintf(stderr, "Filter: %s\n", argv[3]);<br />
struct bpf_program *bpfprog;<br />
if (pcap_compile(pcap, &bpfprog, argv[3], 0, 0) == -1) {<br />
fprintf(stderr, "Couldn't compile filter: %s\n", pcap_geterr(pcap));<br />
exit(1);<br />
}<br />
<br />
if (pcap_setfilter(pcap, &bpfprog) == -1) {<br />
fprintf(stderr, "Couldn't install filter: %s\n", pcap_geterr(pcap));<br />
exit(1);<br />
}<br />
}<br />
<br />
fprintf(stderr, "Starting capture!\n");<br />
pcap_loop(pcap, -1, got_packet, NULL);<br />
<br />
fprintf(stderr, "PCap finished: shutting down!\n");<br />
pcap_close(pcap);<br />
}<br />
<br />
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {<br />
if (DumpStats != 0) fprintf(stderr, "R: %d\n", header->caplen);<br />
if (IncludePLen == 1) write(1, &header->caplen, 4);<br />
write(1, packet, header->caplen);<br />
}<br />
</pre></div>
Admin