import pcap; import std.stdio; import std.c.stdio; import std.string; import std.conv; import std.c.time; import std.socket; void main() { //pointer containing all the devs pcap_if_t *alldevs; pcap_if_t *d; //our error buffer char [PCAP_ERRBUF_SIZE] errbuf; // Retrieve the device list from the local machine if (pcap_findalldevs(&alldevs, cast(char*)(toStringz(errbuf))) == -1) { writeln("Error in pcap_findalldevs: ", errbuf); return; } //building a loop of all devs and letting the user chose one pcap_if_t * [] listDevs=[]; pcap_if_t * chosenDev; int i=0; d=alldevs; for(i=0;d!=null;i++){ listDevs~=d; printf("%i - (%s) \n",i,d.description); d=d.next; } writeln("what interface should we use?"); string chosenstring=readln(); //readline ads an /n, strip it chosenstring=chosenstring[0..$-1]; //some basic checks if(!chosenstring.isNumeric()){ writeln("not a number"); return; } //converting it to an integer int chosenindex=to!(int)(chosenstring); //bounds checking if(chosenindex < 0 || chosenindex >= i){ writeln("invalid index"); return; } chosenDev=listDevs[chosenindex]; pcap_t * handle; //our wanted name, 65536 garantuees a whole package and 1000 is the read timeout handle=pcap_open_live(chosenDev.name, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, cast(char *)toStringz(errbuf)); if(handle==null){ writeln("could not open a handle to the chosen device"); return; } writeln("listening..."); pcap_loop(handle, 0, &packet_handler, null); writeln("^pff, I quit it"); scope(exit){ pcap_freealldevs(alldevs); } } /* Callback function invoked by libpcap for every incoming packet */ void packet_handler(ubyte *param, const pcap_pkthdr *header, const ubyte *pkt_data) { writeln("let's try some timeval parsing shall we?"); timeval * time=cast(timeval *)&header.ts; writeln(time); writeln("and here we are with our acces violation"); }