1. import pcap;
  2. import std.stdio;
  3. import std.c.stdio;
  4. import std.string;
  5. import std.conv;
  6. import std.c.time;
  7. import std.socket;
  8.  
  9. void main()
  10. {
  11.     //pointer containing all the devs
  12.     pcap_if_t *alldevs;
  13.     pcap_if_t *d;
  14.    
  15.     //our error buffer
  16.     char [PCAP_ERRBUF_SIZE] errbuf;
  17.        
  18.     // Retrieve the device list from the local machine
  19.     if (pcap_findalldevs(&alldevs, cast(char*)(toStringz(errbuf))) == -1)
  20.     {
  21.         writeln("Error in pcap_findalldevs: ", errbuf);
  22.         return;
  23.     }
  24.    
  25.     //building a loop of all devs and letting the user chose one
  26.     pcap_if_t * [] listDevs=[];
  27.     pcap_if_t * chosenDev;
  28.    
  29.     int i=0;
  30.     d=alldevs;
  31.     for(i=0;d!=null;i++){
  32.         listDevs~=d;
  33.         printf("%i - (%s) \n",i,d.description);
  34.         d=d.next;
  35.     }
  36.     writeln("what interface should we use?");
  37.     string chosenstring=readln();
  38.    
  39.     //readline ads an /n, strip it
  40.     chosenstring=chosenstring[0..$-1];
  41.    
  42.     //some basic checks
  43.     if(!chosenstring.isNumeric()){
  44.         writeln("not a number");
  45.         return;
  46.     }
  47.    
  48.     //converting it to an integer
  49.     int chosenindex=to!(int)(chosenstring);
  50.    
  51.     //bounds checking
  52.     if(chosenindex < 0 || chosenindex >= i){
  53.         writeln("invalid index");
  54.         return;
  55.     }
  56.    
  57.     chosenDev=listDevs[chosenindex];
  58.     pcap_t * handle;
  59.    
  60.     //our wanted name, 65536 garantuees a whole package and 1000 is the read timeout
  61.     handle=pcap_open_live(chosenDev.name, 65536,  PCAP_OPENFLAG_PROMISCUOUS,  1000, cast(char *)toStringz(errbuf));
  62.     if(handle==null){
  63.         writeln("could not open a handle to the chosen device");
  64.         return;
  65.     }
  66.    
  67.     writeln("listening...");
  68.     pcap_loop(handle, 0, &packet_handler, null);
  69.     writeln("^pff, I quit it");
  70.     scope(exit){
  71.         pcap_freealldevs(alldevs);
  72.     }
  73. }
  74.  
  75. /* Callback function invoked by libpcap for every incoming packet */
  76. void packet_handler(ubyte *param, const pcap_pkthdr *header, const ubyte *pkt_data)
  77. {
  78.     writeln("let's try some timeval parsing shall we?");
  79.     timeval * time=cast(timeval *)&header.ts;
  80.     writeln(time);
  81.    
  82.     writeln("and here we are with our acces violation");
  83. }