Like Tree2Likes
  • 2 Post By stahta01

code blocks windows pcap basic program

This is a discussion on code blocks windows pcap basic program within the C Programming forums, part of the General Programming Boards category; Code: #include <winsock.h> #include <stdio.h> #include <stdlib.h> #include <pcap.h> /* GIMME a libpcap plz! */ #include <errno.h> int main(int argc, ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    6

    code blocks windows pcap basic program

    Code:
    
    #include <winsock.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <pcap.h>  /* GIMME a libpcap plz! */
    #include <errno.h>
    
    
    
    
    
    
    
    
    int main(int argc, char **argv)
    {
      char *dev; /* name of the device to use */
      char *net; /* dot notation of the network address */
      char *mask;/* dot notation of the network mask    */
      int ret;   /* return code */
      char errbuf[PCAP_ERRBUF_SIZE];
      bpf_u_int32 netp; /* ip          */
      bpf_u_int32 maskp;/* subnet mask */
      struct in_addr addr;
    
    
      /* ask pcap to find a valid device for use to sniff on */
      dev = pcap_lookupdev(errbuf);
    
    
      /* error checking */
      if(dev == NULL)
      {
       printf("%s\n",errbuf);
       exit(1);
      }
    
    
      /* print out device name */
      printf("DEV: %s\n",dev);
    
    
      /* ask pcap for the network address and mask of the device */
      ret = pcap_lookupnet(dev,&netp,&maskp,errbuf);
    
    
      if(ret == -1)
      {
       printf("%s\n",errbuf);
       exit(1);
      }
    
    
      /* get the network address in a human readable form */
      addr.s_addr = netp;
      net = inet_ntoa(addr);
    
    
      if(net == NULL)/* thanks Scott :-P */
      {
        perror("inet_ntoa");
        exit(1);
      }
    
    
      printf("NET: %s\n",net);
    
    
      /* do the same as above for the device's mask */
      addr.s_addr = maskp;
      mask = inet_ntoa(addr);
    
    
      if(mask == NULL)
      {
        perror("inet_ntoa");
        exit(1);
      }
    
    
      printf("MASK: %s\n",mask);
    
    
      return 0;
    }
    gives the errors:

    Code:
    obj\Debug\main.o||In function `main':|
    D:\tcp\tcp test\main.c|31|undefined reference to `pcap_lookupdev'|
    D:\tcp\tcp test\main.c|44|undefined reference to `pcap_lookupnet'|
    D:\tcp\tcp test\main.c|54|undefined reference to `inet_ntoa@4'|
    D:\tcp\tcp test\main.c|66|undefined reference to `inet_ntoa@4'|
    ||=== Build finished: 4 errors, 0 warnings ===|
    ive added all the library paths to code block compiler and linker directories evident by the fact that i dont get error while linking pcap.h.
    please help, thank you.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    LOL, obviously, you do get errors. Can you post a screenshot of your compiler/linker settings in CB?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Those look like linker errors.

    In which case, you need to go to project->settings->linker (or similar) and add libpcap (or whatever its name is) to the list of libraries the linker searches.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    Quote Originally Posted by claudiu View Post
    LOL, obviously, you do get errors. Can you post a screenshot of your compiler/linker settings in CB?
    When asking for help using Code::Blocks it is useful to have Full Compiler Logging turned on.
    FAQ-Compiling (errors) - CodeBlocks

    After turning Full Compiler Logging on, posting the full "build log" (after doing re-build) helps to figure out what you did wrong.

    Tim S.
    Last edited by stahta01; 05-28-2012 at 08:13 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    6
    Code:
    -------------- Clean: Debug in tcp test ---------------
    
    
    Cleaned "tcp test - Debug"
    
    
    -------------- Build: Debug in tcp test ---------------
    
    
    mingw32-gcc.exe -Wall  -g    -I..\..\libpcap-1.2.1 -ID:\libpcap-1.2.1  -c "D:\tcp\tcp test\main.c" -o obj\Debug\main.o
    mingw32-g++.exe -L..\..\libpcap-1.2.1 -LD:\libpcap-1.2.1  -o "bin\Debug\tcp test.exe" obj\Debug\main.o    
    obj\Debug\main.o: In function `main':
    D:/tcp/tcp test/main.c:32: undefined reference to `pcap_lookupdev'
    D:/tcp/tcp test/main.c:45: undefined reference to `pcap_lookupnet'
    D:/tcp/tcp test/main.c:55: undefined reference to `inet_ntoa@4'
    D:/tcp/tcp test/main.c:67: undefined reference to `inet_ntoa@4'
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 3 seconds)
    4 errors, 0 warnings
    this is the build log


    screenshot of link directory

    http://imgur.com/a/BNhIE
    Last edited by Tanuj Martolia; 05-28-2012 at 12:36 PM.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    Try using "pcap" as the library name in Code::Blocks; NOTE this mean add it to the library list!!

    FYI: Look under the tab called "Linker Settings"; Add the library name one at a time in the "Link Libraries" list.

    Tim S.
    Last edited by stahta01; 05-28-2012 at 03:55 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    6
    adding the library supports .a, .so, .lib, .dylib, .bundle files only.
    none of which were present in the libpcap folder. im using 1.2.1

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    If you have no Library then you can NOT proceed; Note, MinGW GCC will link to some dll files (The C Compiler DLLs work; no idea about C++ DLLs).

    Find the library files for your Compiler; I would guess you are using MinGW GCC.

    NOTE: You DO NOT NEED to search for the folder/path when you enter the library name of pcap!! Just enter the name!!
    The above will work once you find/create the .a file.

    Or, you can try to do the full path/name to the pcap DLL.

    Why are you not using a recent version of pcap?
    Just read the pcap seems to have two version 4.2.1 and 1.2.1.

    http://www.winpcap.org/devel.htm
    Has the .a file when I downloaded it. The files libwpcap.a and libpacket.a are in it when I unzipped WpdPack_4_1_2.zip

    I considered this problem solved till you give more feedback.

    Tim S.
    Last edited by stahta01; 05-28-2012 at 08:28 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    6
    i dwonloaded the latest pcap library from here : TCPDUMP/LIBPCAP public repository
    yes i'm new to codeblocks and mingw gcc
    searching for pcap in the library folder gives only .h files and .c files and other file types which are not library file type acc to code blocks
    how do i create the .a file or could you give a link to me for the file?
    thanks for the reply

    btw im using libpcap not winpcap how much is the difference?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    Quote Originally Posted by Tanuj Martolia View Post
    i dwonloaded the latest pcap library from here : TCPDUMP/LIBPCAP public repository
    yes i'm new to codeblocks and mingw gcc
    searching for pcap in the library folder gives only .h files and .c files and other file types which are not library file type acc to code blocks
    how do i create the .a file or could you give a link to me for the file?
    thanks for the reply

    btw im using libpcap not winpcap how much is the difference?
    One, you compile yourself and the other is pre-compiled for Windows users.

    http://www.winpcap.org/install/bin/WpdPack_4_1_2.zip

    NOTE: I only read the pages today after your questions; so, you should know as much as I do about the two sites.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    6
    how do i compile on my own?
    i won't be able to reply anytime soon now but do link me to the tutorials i will see them.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    I just added you to my list of people to ignore.

    Bye, I am NOT your research slave!!

    Tim S.
    Salem and claudiu like this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    6
    Hey stahta01 thanks for the hint
    i copied the .a file from winpcap and used it in libpcap
    now all the sample programs work fine
    but there's one hitch, the device name doesnt show correctly. it shows as '/'.
    any idea whats wrong?

    @salem @claudiu could you please inform him of the recent developments?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Blocks vs Windows settings?
    By LeonK in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2011, 06:16 AM
  2. Replies: 3
    Last Post: 03-16-2011, 06:31 PM
  3. Replies: 1
    Last Post: 01-11-2008, 08:34 AM
  4. Basic Windows Program
    By real_cozzy in forum Windows Programming
    Replies: 2
    Last Post: 12-19-2001, 08:52 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21