Nfagie Yansaneh
Nfagie Yansaneh

Reputation: 215

pcap_lookupdev always returning NULL

So I have had this with #include <pcap.h> and the error I am getting is error 122, I will explain error 122 soon.

Here is the code. Keep in mind, this is not my code; I used this code to demon-strait what error is occurring

#include <iostream>
#include <pcap/pcap.h>
#include <Windows.h>
using namespace std;

static int packetCount = 0;

void packetHandler(u_char *userData, const struct pcap_pkthdr* pkthdr, const u_char* packet) {
    cout << ++packetCount << " packet(s) captured" << endl;
}

int main() {
    char *dev;
    pcap_t *descr;
    char errbuf[PCAP_ERRBUF_SIZE];

    dev = pcap_lookupdev(errbuf);
    if (dev == NULL) {
        cout << "pcap_lookupdev() failed: " << errbuf << endl;
        system("pause");
        return 1;
    }

    descr = pcap_open_live(dev, BUFSIZ, 0, -1, errbuf);
    if (descr == NULL) {
        cout << "pcap_open_live() failed: " << errbuf << endl;
        system("pause");
        return 1;
    }

    if (pcap_loop(descr, 10, packetHandler, NULL) < 0) {
        cout << "pcap_loop() failed: " << pcap_geterr(descr);
        system("pause");
        return 1;
    }

    cout << "capture finished" << endl;
    system("pause");
    return 0;
}

Now once I compile and run, this is what i get the error :

pcap_lookupdev() failed: PacketGetAdapterNames: The data area passed to a system call is too small. (122) Press any key to continue . . .

I googled similar questions to the one I have posted(the one your reading), but they appear to all be in Linux. The error has something to do with Admin privileges. But I do not know how to achieve this, I run the program as Admin and I get this (Guess what error it is) :

pcap_lookupdev() failed: PacketGetAdapterNames: The data area passed to a system call is too small. (122) Press any key to continue . . .

The same error :( I understand the error but I do not now how to stop it. This error is known as error 122

Also I included Windows.h just for system("pause"), so do not worry about that, also sorry if I got some grammar wrong.

Upvotes: 0

Views: 1405

Answers (1)

I make an offer to improve your source code as below changes :

  • Use of pcap_findalldevs to find all your exist devices.
  • alldevs is a linked-list that contains first address of all looked up devices.
  • /* Print the list */ section, prints a device and get the next device and so on.

Look up and print devices :

pcap_if_t *alldevs, *d;
pcap_t *fp;
int i = 0;
u_int inum;

if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
    fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
    exit(1);
}

/* Print the list */
for(d=alldevs; d; d=d->next)
{
    printf("%d. %s", ++i, d->name);
    if (d->description)
         printf(" (%s)\n", d->description);
    else
         printf(" (No description available)\n");
}

if(i==0)
{
    printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
    return -1;
}

So enter an interface to open it :

printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);

if(inum < 1 || inum > i)
{
    printf("\nInterface number out of range.\n");
    /* Free the device list */
    pcap_freealldevs(alldevs);
    return -1;
}

Conclusively open a device as live and continue your coding :

/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

/* Open the device */
if ( (fp= pcap_open_live(d->name, 100, 1, 20, errbuf) ) == NULL)
{
    fprintf(stderr,"\nError opening adapter\n");
    return -1;
}

In this way, you choose a device arbitrary. try this way, see make any mistake?

Upvotes: 1

Related Questions