title: LibPCap pcap_loop() does not return any packets
body: <p>I have problem with my C program, everything is fine until I start capturing packets. It should log, but the handler void in pcap_loop() isn't. Also I have another problem with the input in fgets on line 98 when it gets passed into the pcap create void it will not find the interface by the name. When I hardcode the string it works.</p>
  <pre><code>/* 
      Compile and run it using GCC -&gt;
      gcc main.c -o output -L/usr/include -lpcap &amp;&amp; ./output
  */
  #include &lt;stdio.h&gt;
  #include &lt;pcap.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  #define __NEWLINE__ &quot;\r\r\n&quot;
  #define __SEPARATOR__ &quot;------------------------&quot;
  
  /*
      Print all flags in human readable string 
      instead of hexadecimal number
  */
  char* get_interface_flags(bpf_u_int32* decimal_flags) {
      char* flag_string = (char*)malloc(sizeof(char) * 30);
      strcpy(flag_string, &quot;\0&quot;);
  
      const int flag_names[9] = {
          PCAP_IF_LOOPBACK, 
          PCAP_IF_UP, 
          PCAP_IF_RUNNING, 
          PCAP_IF_WIRELESS
      };
  
      const char* flag_values[9] = {
          &quot;LOOPBACK&quot;,
          &quot;UP&quot;,
          &quot;RUNNING&quot;,
          &quot;WIRELESS&quot;
      };
  
      for (size_t i = 0; i &lt; (sizeof(flag_names) / sizeof(flag_names[0])); i++)
      {
          if(flag_names[i] &amp; *decimal_flags) {
              if(strlen(flag_string) != 0) strcat(flag_string, &quot;, &quot;);
              strcat(flag_string, flag_values[i]);
          }
      }
      
      return flag_string;
  }
  
  /*
      Print available information about network interface
      This recursive function can be called again
      if there is more than one interface present
  */
  void print_interface(pcap_if_t* device) {
      // Device parameters
      bpf_u_int32* flags = &amp;device-&gt;flags;
      char* device_name = device-&gt;name;
      char* device_desc = device-&gt;description;
      char* flags_string = get_interface_flags(flags);
  
      // Device addresses  
      pcap_addr_t* addr = device-&gt;addresses;
  
      // We'll print devices with only 1 flag or more to avoid interfaces that aren't up
      if(strlen(flags_string) != 0) {
          fprintf(stdout, &quot;%s: FLAGS(%lu)&lt;%s&gt;%s&quot;, device_name, strlen(flags_string), flags_string, __NEWLINE__);    
          fprintf(stdout, &quot;%s%s&quot;, device_desc, __NEWLINE__);
          fprintf(stdout, &quot;%s%s&quot;, __SEPARATOR__, __NEWLINE__);
      }
  
      // Check if there is another device to print, if so call the recursive function again
      if(device[0].next != NULL) print_interface(device-&gt;next);
      
      free(flags_string);
  }
  
  // Let user select the network interface that will capture packets
  char* get_selected_interface() {
      char* selected_interface = (char*)malloc(sizeof(char) * 20);
      char* device_name;
      char* ip_addr;
      char errbuf[PCAP_ERRBUF_SIZE];
      pcap_if_t* alldevs;
  
      // Get all available network interfaces, returns PCAP_ERROR on failure or 0 on success
      int devices_succefully_returned = pcap_findalldevs(&amp;alldevs, errbuf);
  
      // If there was an error while fetching interfaces, return empty char
      if(devices_succefully_returned == PCAP_ERROR) {
          printf(&quot;%s&quot;, errbuf);
          return '\0';
      }
  
      print_interface(alldevs);
  
      fprintf(stdout, &quot;Name of the network interface to use: &quot;);
      fgets(selected_interface, 20, stdin);
  
      return selected_interface;
  }
  
  void captured_packet(u_char *args, const struct pcap_pkthdr *hdr, const u_char *pkt) {
      fprintf(stdout, &quot;log&quot;);
  }
  
  void capture(char* device) {
      char* errbuf[PCAP_ERRBUF_SIZE];
  
      /*
          Will create a packet capture handle
          Returns NULL if handle can't be created, in this case
          we'll print out the error buffer and exit the program with 1 status code
      */
      pcap_t* created = pcap_create(&quot;eth0&quot;, *errbuf);
  
      if(created == NULL) {
          fprintf(stderr, &quot;%s%s&quot;, *errbuf, __NEWLINE__);
          return exit(EXIT_FAILURE);
      };
  
      int activation_status = pcap_activate(created);
  
      if(activation_status == PCAP_ERROR || activation_status != 0) {
          fprintf(stderr, &quot;%s&quot;, pcap_geterr(created));
          exit(EXIT_FAILURE);
      }
  
      /*
          Interface was activated so start capturing incoming packets
          capture_packet will handle incoming packets.
          We'll capture infinite amount of packets unless user provides a count
      */
      fprintf(stdout, &quot;Interface activated!%s&quot;, __NEWLINE__);
  
      int loop_status = pcap_loop(created, -1, captured_packet, NULL);
  
      switch (loop_status) 
      {
          case PCAP_ERROR_BREAK:
              fprintf(stderr, &quot;Loop was finished beacause of breakloop that was called.&quot;);
              break;
      
          case PCAP_ERROR_NOT_ACTIVATED:
              fprintf(stderr, &quot;Device wasn't activated before it started capturing.&quot;);
              break;
  
          case 0:
              fprintf(stderr, &quot;Loop was terminated due to exhaustion of count&quot;);
              break;
  
          default:
              fprintf(stderr, &quot;Some error happened while trying to loop through packets -&gt; %s&quot;, pcap_geterr(created));
              break;
      }
  
      // Program was successfully closed, exit..
      pcap_close(created);
      exit(0);
  }
  
  int main(int argc, char **argv) {
      if(getuid() != 0) {
          fprintf(stderr, &quot;Please make sure to run this tool as root!%s&quot;, __NEWLINE__);
          return 1;
      }
  
      char* selected = get_selected_interface();
      capture(selected);
      free(selected);
      return 0;
  }
  </code></pre>
  <p>I tried everything, it should log unlimited number of packets on the &quot;eth0&quot; interface but nothing happens.</p>