[Winpcap-users] pcap_freealldevs() causing program crash.

MqX mqx at low-axs.net
Wed Nov 16 18:54:12 GMT 2005


---snip-
pcap_findalldevs(&alldevs, errbuf);    // get devices
printf("Device list:\n");
for(d=devs,i=1;d; d=d->next,i++)    // jump to all devices and print em
{
     printf("%d. %s\n",i,d->description")    // or "d->name"
}
---/snip-

....call pcap_freealldevs() here if you just want to print device list
or go farward...

---snip-
scanf("%d", &inum);    // enter a device

if(inum < 1 || inum > i)   // make sure inum is not greater then avaible 
device ;)
{
    printf("\nInterface number out of range.\n");
    /* Free the device list */
    pcap_freealldevs 
<http://www.winpcap.org/docs/docs31/html/group__wpcapfunc.html#ga8>(alldevs);
    return -1;
}

for(d=devs, i=1; i< inum; d=d->next 
<http://www.winpcap.org/docs/docs31/html/structpcap__if.html#o0>, 
i++);    // select a device

if ((adhandle=pcap_open(...,...)) == NULL)   // open the device
{
    printf("unable to open the device!");
    return -1;
}
pcap_freealldevs(devs);

....

(code to capture packets, set filter, etc)

....

return 0;
---/snip-

now it should be clear where to (at least) free the "device list"... :-)

> But, now, the device names are
> "encrypted" (they show up in weird characters now, unlike before when
> they showed up like they should).

looks like your selected(!) device is "out of range" and points somewhere on the stack.

ok the code is mostly copied from winpcap examples
hope it helps


mqx

Eric Hansen wrote:

> Well, I got my program to not crash (I put the freealldevs() at the 
> very end of my init function).  But, now, the device names are 
> "encrypted" (they show up in weird characters now, unlike before when 
> they showed up like they should).
>
> On 11/16/05, *Gianluca Varenni* <gianluca.varenni at cacetech.com 
> <mailto:gianluca.varenni at cacetech.com>> wrote:
>
>     Well, there's no relationship between pcap_open and
>     pcap_findalldevs (apart from the fact that you may have used the
>     adapter names returned by pcap_findalldevs to open an adapter with
>     pcap_open).
>      
>     pcap_freealldevs should always be called (once) after a successful
>     call to pcap_findalldevs.
>      
>     Have a nice day
>     GV
>      
>
>         ----- Original Message -----
>         *From:* Eric Hansen <mailto:nightskywriter at gmail.com>
>         *To:* winpcap-users at winpcap.org
>         <mailto:winpcap-users at winpcap.org>
>         *Sent:* Wednesday, November 16, 2005 9:01 AM
>         *Subject:* Re: [Winpcap-users] pcap_freealldevs() causing
>         program crash.
>
>         Yup :)  I've debugged the pointer to, to make sure there
>         wasn't something I wasn't catching otherwise.  I think I found
>         my problem though.  What if I do not call pcap_open()?  For
>         example, if I don't call that, should I still be calling
>         freealldevs()?
>
>         On 11/16/05, *Gianluca Varenni* <gianluca.varenni at cacetech.com
>         <mailto:gianluca.varenni at cacetech.com+>> wrote:
>
>             Hi.
>              
>             Did you also check that you aren't calling
>             CDevice::Cleanup multiple times on the same "devs" pointer
>             (thus trying to free the adapters list multiple times)?
>              
>             Have a nice day
>             GV
>              
>
>                 ----- Original Message -----
>                 *From:* Eric Hansen <mailto:nightskywriter at gmail.com>
>                 *To:* WinPCap Mailing List
>                 <mailto:winpcap-users at winpcap.org>
>                 *Sent:* Sunday, November 13, 2005 4:04 PM
>                 *Subject:* [Winpcap-users] pcap_freealldevs() causing
>                 program crash.
>
>                 Does anyone know why, whenever I put in
>                 "pcap_freealldevs(devs);" into my cleanup code (that
>                 frees all the memory, etc...that my program uses), it
>                 always crashes?
>
>                 devs declaration:
>                 pcap_if_t *devs, *d; // Device list
>
>                 My cleanup code:
>                 void CDevice::Cleanup(){
>
>                     // Free the device list
>                     pcap_freealldevs(devs);
>                 }
>
>                 and how I'm using devs:
>                 void CDevice::Devices(){
>                     if(pcap_findalldevs(&devs, error) == -1){
>                         AfxMessageBox(error);
>                     } else{
>                         for(iCount = 0, d = devs; d != NULL; d =
>                 d->next, iCount++){
>                             if(d->description){
>                                 cDescription[iCount] = d->description;
>                             }
>
>                             cDevice[iCount] = d->name;
>                             }
>                         }
>                 }
>
>                 I've been racking my brain for over an hour trying to
>                 figure out what I'm doing.
>
>                 (Note: I call Cleanup() in the WM_DESTROY Windows message)
>                 ------------------------------------------------------------------------
>                 _______________________________________________
>                 Winpcap-users mailing list
>                 Winpcap-users at winpcap.org
>                 <mailto:Winpcap-users at winpcap.org>
>                 https://www.winpcap.org/mailman/listinfo/winpcap-users
>
>
>             _______________________________________________
>             Winpcap-users mailing list
>             Winpcap-users at winpcap.org <mailto:Winpcap-users at winpcap.org>
>             https://www.winpcap.org/mailman/listinfo/winpcap-users
>
>
>
>
>
>         -- 
>         http://tenshi.honeyblade.net (my poetry)
>
>         "You wanted this ending to happen...ROMANTISUTO!"
>
>         ------------------------------------------------------------------------
>         _______________________________________________
>         Winpcap-users mailing list
>         Winpcap-users at winpcap.org <mailto:Winpcap-users at winpcap.org>
>         https://www.winpcap.org/mailman/listinfo/winpcap-users
>
>
>     _______________________________________________
>     Winpcap-users mailing list
>     Winpcap-users at winpcap.org <mailto:Winpcap-users at winpcap.org>
>     https://www.winpcap.org/mailman/listinfo/winpcap-users
>
>
>
>
>
> -- 
> http://tenshi.honeyblade.net (my poetry)
>
> "You wanted this ending to happen...ROMANTISUTO!"
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Winpcap-users mailing list
>Winpcap-users at winpcap.org
>https://www.winpcap.org/mailman/listinfo/winpcap-users
>  
>




More information about the Winpcap-users mailing list