Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="cs">

#include "pcap.h"
main()
{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int i=0;
    char errbuf[PCAP_ERRBUF_SIZE];
    /* Retrieve the device list from the local machine */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
        exit(1);
    }
    /* Print the list */
    for(d= alldevs; d != NULL; 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;
    }
    /* We don't need any more the device list. Free it */
    pcap_freealldevs(alldevs);
}



Posted
Updated 13-Apr-11 13:30pm
v2

By the looks of it, it is using PCAP. PCAP runs on Windows and *NIX.
 
Share this answer
 
v2
Comments
Member 7766180 13-Apr-11 19:31pm    
It is PCAP but why won't it compile? I'm getting several errors. Using Borland C++ 5.5.1 Free compiler.
LloydA111 13-Apr-11 19:33pm    
Well what errors are you getting?
Member 7766180 13-Apr-11 19:36pm    
Here are the errors-------


Error E2451 DEVV.c 5: Undefined symbol 'pcap_if_t' in function main
Error E2451 DEVV.c 5: Undefined symbol 'alldevs' in function main
Error E2451 DEVV.c 6: Undefined symbol 'd' in function main
Error E2140 DEVV.c 7: Declaration is not allowed here in function main
Error E2140 DEVV.c 8: Declaration is not allowed here in function main
Error E2451 DEVV.c 8: Undefined symbol 'PCAP_ERRBUF_SIZE' in function main
Error E2451 DEVV.c 11: Undefined symbol 'PCAP_SRC_IF_STRING' in function main
Error E2451 DEVV.c 11: Undefined symbol 'NULL' in function main
Warning W8065 DEVV.c 11: Call to function 'pcap_findalldevs_ex' with no prototyp
e in function main
Error E2451 DEVV.c 13: Undefined symbol 'stderr' in function main
Warning W8065 DEVV.c 13: Call to function 'fprintf' with no prototype in functio
n main
Warning W8065 DEVV.c 14: Call to function 'exit' with no prototype in function m
ain
Warning W8065 DEVV.c 20: Call to function 'printf' with no prototype in function
main
Warning W8065 DEVV.c 22: Call to function 'printf' with no prototype in function
main
Warning W8065 DEVV.c 24: Call to function 'printf' with no prototype in function
main
Warning W8065 DEVV.c 29: Call to function 'printf' with no prototype in function
main
Warning W8065 DEVV.c 34: Call to function 'pcap_freealldevs' with no prototype i
n function main
Warning W8070 DEVV.c 35: Function should return a value in function main
LloydA111 13-Apr-11 19:39pm    
Firstly, you need to #include stdio.h to be able to use printf. (You need to enclose stdio.h with < and > )

Also, are you sure the pcap.h header file exists in the place the compiler is looking?
Member 7766180 13-Apr-11 19:48pm    
Down to 8 Errors now. pcap.h is in the same directory.
Thank you for your help.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\DS>cd\test

C:\TEST>bcc32 devv.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
DEVV.c:
Error E2209 pcap.h 45: Unable to open include file 'pcap/pcap.h'
Error E2451 DEVV.c 5: Undefined symbol 'pcap_if_t' in function main
Error E2451 DEVV.c 5: Undefined symbol 'alldevs' in function main
Error E2451 DEVV.c 6: Undefined symbol 'd' in function main
Error E2140 DEVV.c 7: Declaration is not allowed here in function main
Error E2140 DEVV.c 8: Declaration is not allowed here in function main
Error E2451 DEVV.c 8: Undefined symbol 'PCAP_ERRBUF_SIZE' in function main
Error E2451 DEVV.c 11: Undefined symbol 'PCAP_SRC_IF_STRING' in function main
Error E2451 DEVV.c 11: Undefined symbol 'NULL' in function main
Warning W8065 DEVV.c 11: Call to function 'pcap_findalldevs_ex' with no prototyp
e in function main
Error E2451 DEVV.c 13: Undefined symbol 'stderr' in function main
Warning W8065 DEVV.c 13: Call to function 'fprintf' with no prototype in functio
n main
Warning W8065 DEVV.c 14: Call to function 'exit' with no prototype in function m
ain
Warning W8065 DEVV.c 20: Call to function 'printf' with no prototype in function
main

Error E2209 pcap.h 45: Unable to open include file 'pcap/pcap.h'
Error E2451 Four.c 6: Undefined symbol 'pcap_if_t' in function main
Error E2451 Four.c 6: Undefined symbol 'alldevs' in function main
Error E2451 Four.c 7: Undefined symbol 'd' in function main
Error E2140 Four.c 8: Declaration is not allowed here in function main
Error E2140 Four.c 9: Declaration is not allowed here in function main
Error E2451 Four.c 9: Undefined symbol 'PCAP_ERRBUF_SIZE' in function main
Error E2451 Four.c 12: Undefined symbol 'PCAP_SRC_IF_STRING' in function main
Warning W8065 Four.c 12: Call to function 'pcap_findalldevs_ex' with no prototyp
e in function main
Warning W8065 Four.c 15: Call to function 'exit' with no prototype in function m
ain
Warning W8065 Four.c 35: Call to function 'pcap_freealldevs' with no prototype i
n function main
Warning W8070 Four.c 36: Function should return a value in function main
*** 8 errors in Compile ***
you're probably not including the .lib file are you?

first, make sure that those functions are defined in pcap.h (which they probably are), make sure you have the #define statements that pcap.h probably needs to compile in windows (I'm sure if you look up info on pcap.h you'll find out, or it may be written at the top of the header, or it may be specified in a document that came with the library), then make sure you're linking the appropriate .lib file.

[edit]
all this is standard C, so it should work regardless of platform.
[/edit]
 
Share this answer
 
v3
Comments
Member 7766180 13-Apr-11 20:04pm    
Ok I think the problem might be the lib files. Which ones do I need? Where do I place and link them? And most important where do I find them. Thank you.
DS
Albert Holguin 13-Apr-11 20:06pm    
where did you get this code from? look for an explanation there... the winpcap library is a common library, so you can also google to get some answers. i think the programming you're trying to do is a bit above your head.
Member 7766180 13-Apr-11 20:09pm    
Your right, it is above my head at this point. But I don't have a choice to stop, in a deep corneron this one. Got the code from the WinPcap site, they make it sound simple but....
Thanks
DS
Albert Holguin 13-Apr-11 20:11pm    
lol... they always make it sound simple... its probably simple for an advanced C/C++ programmer... the real trick will be to look over as much of the documentation as you possibly can.
Albert Holguin 13-Apr-11 20:12pm    
here's the link telling you how to build this:
http://www.winpcap.org/docs/docs_40_2/html/group__wpcapsamps.html
From WinPcap (almighty google took me there):

Creating an application that uses wpcap.dll:

To create an application that uses wpcap.dll with Microsoft Visual C++, follow these steps:
-Include the file pcap.h at the beginning of every source file that uses the functions exported by library.
-If your program uses Win32 specific functions of WinPcap, remember to include WPCAP among the preprocessor definitions.
-If your program uses the remote capture capabilities of WinPcap, add HAVE_REMOTE among the preprocessor definitions. Do not include remote-ext.h directly in your source files.
-Set the options of the linker to include the wpcap.lib library file. wpcap.lib can be found in the WinPcap developer's pack.
-Set the options of the linker to include the winsock library file ws2_32.lib. This file is distributed with the C compiler and contains the socket functions for Windows. It is needed by some functions used by the samples in the tutorial.

Remember that:

-To add a preprocessor definition, you must select Settings from the Project menu, then select C/C++ from the tab control, and under the category General, you must add the definition under the Preprocessor Definitions text box.
-To add a new library to the project with Microsoft Visual C++ 6.0, you must select Settings from the Project menu, then select Link from the tab control, and then add the name of the new library in the Object/library modules edit box.
-To add a new path where Microsoft Visual C++ 6.0 will look for the libraries, you must select Options from the Tools menu, then Directories from the tab control, Library files from the Show directories for combo box, and the add the path in the Directories box.
-To add a new path where Microsoft Visual C++ 6.0 will look for include files, you must select Options from the Tools menu, then Directories from the tab control, Include files from the Show directories for combo box, and the add the path in the Directories box.

...and from looking at the package that they give for development, you're going to have to figure out how to set default include directories for your compiler (I'm not sure how to do it for the compiler you're using) because the files are all linked to one another based on default settings (they #include<> one another).
 
Share this answer
 
Comments
Member 7766180 13-Apr-11 20:32pm    
Thank you Albert, I'm going to play around a bit and I'll get back to you later. Thank you som much, you are a saint!
DS
Member 7766180 13-Apr-11 20:34pm    
One last question. What kind of project is this in Visual Studio 2010?
Albert Holguin 13-Apr-11 20:36pm    
what do you mean what kind of project? you mean when you start a new project? well, to start off, make if a console project (IO to/from the cmd prompt).
Member 7766180 13-Apr-11 20:59pm    
Thats what I needed. Thank you.
DS
Albert Holguin 13-Apr-11 21:00pm    
don't forget to accept my solution! ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900