5

I'm trying to install pypcap from: https://github.com/dugsong/pypcap/blob/master/INSTALL#L75

I'm trying to install it on Windows 10 - 64 bit and python 2.7.11 64 bit. I downloaded the source and moved it to c:\pypcap. downloaded pyrex and installed it as well. It put the wpdpack files in c:\wpdpack like the install page says. Created the config file and changed the makefile.

I also installed the mingw from their website with the basic package and the g++ package.

I try to do:

C:\pypcap> set PATH=%PATH%;c:\MinGW\bin;c:\Python27
C:\pypcap> mingw32-make

and here it fails, it gets the following error:

In file included from C:\Python27\include/Python.h:8:0,
                 from pcap.c:4:
C:\Python27\include/pyconfig.h:68:16: fatal error: io.h: No such file or directory
 #include <io.h>

so I don't got this io.h file. What can I do to make it compile and use the pypcap?

Before trying that I tried to do

C:\pypcap>pip install pypcap
Collecting pypcap
  Using cached pypcap-1.1.4.tar.gz
    Complete output from command python setup.py egg_info:
    pcap.h not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in c:\users\USER\appdata\local\temp\pip-build-fvgkhs\pypcap\

and after that I tried using the wheel download but it failed as well.

Thanks in advance for your help

Héctor Valverde
  • 1,089
  • 1
  • 14
  • 34
MyNick
  • 536
  • 1
  • 9
  • 25

2 Answers2

7
  1. Unzip the WinPcap Developer pack so that you have C:\WpdPack\Include and C:\WpdPack\Lib

  2. Unzip the pypcap source so that you have C:\pypcap-1.1.4\setup.py

  3. Install the Microsoft Visual C++ Compiler for Python 2.7

  4. Start the relevant cmd prompt for your Python27 bitness from:
    Start -> Programs -> Microsoft Visual C++ Compiler Package for Python 2.7

  5. Install/build pypcap from that cmd prompt with:

    set INCLUDE=%INCLUDE%;c:\WpdPack\Include
    set LIB=%LIB%;c:\WpdPack\Lib
    pushd C:\pypcap-1.1.4
    python setup.py install
azhrei
  • 2,303
  • 16
  • 18
  • According to [this](https://github.com/pynetwork/pypcap#windows-notes) WinPcap has compatibility issues with Windows 10, you might want to use npcap SDK + npcap installer with WinPcap compatibility instead – prgDevelop Sep 16 '17 at 16:12
  • ...and then rename the .egg extension of the generated file to .zip and unzip the file. Had to do another search for this. – Niko Jan 16 '18 at 12:58
3

Changed answer

Now you have a fully functional MinGW GCC compiler including header files.

io.h should reside in C:\MinGW\include.

UPDATE

I have successfully compiled it now using Python 2.7.11 (under Wine, since I don't run Windows, but it should be the same) by following the procedure you described in your question.

One thing I had to do was to modify parts of setup.py in pypcap, because it didn't set the proper paths to the pcap includes.

Let me know if/when you get there.

Hope it helps.

CHANGES TO setup.py

Replace the function _pcap_config with this modified version:

def _pcap_config(self, dirs=[ None ]):
    cfg = {}
    if not dirs[0]:
        dirs = [ '/usr', sys.prefix ] + glob.glob('/opt/libpcap*') + \
               glob.glob('../libpcap*') + glob.glob('../wpdpack*')
    for d in dirs:
        for sd in ('include/pcap', 'include', ''):
            if os.path.exists(os.path.join(d, sd, 'pcap.h')):
                cfg.setdefault('include_dirs', []).append(os.path.join(d, sd))
    for d in dirs:
        for sd in ('include/pcap', 'include', ''):
            for sd in ('lib', 'lib64', ''):
                for lib in (('pcap', 'libpcap.a'),
                            ('pcap', 'libpcap.so'),
                            ('pcap', 'libpcap.dylib'),
                            ('wpcap', 'wpcap.lib')):
                    if os.path.exists(os.path.join(d, sd, lib[1])):
                        cfg['library_dirs'] = [ os.path.join(d, sd) ]
                        cfg['libraries'] = [ lib[0] ]
                        if lib[0] == 'wpcap':
                            cfg['libraries'].append('iphlpapi')
                            cfg['extra_compile_args'] = \
                                [ '-DWIN32', '-DWPCAP' ]
                        print 'found', cfg
                        self._write_config_h(cfg)
                        return cfg
    raise Exception("couldn't find pcap build or installation directory")

It basically just adds ALL paths containing pcap.h to the includes.

COMPILATION

Z:\pypcap-master>mingw32-make
python setup.py config --with-pcap=c:\\wpdpack
running config
found {'libraries': ['wpcap', 'iphlpapi'], 'library_dirs': ['c:\\wpdpack\\lib'], 'extra_compile_args': ['-DWIN32', '-DWPCAP'], 'include_dirs': ['c:\\wpdpack\\include/pcap', 'c:\\wpdpack\\include']}
python setup.py build
running build
running build_ext
building 'pcap' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
C:\MinGW\bin\gcc.exe -mdll -O -Wall -Ic:\wpdpack\include/pcap -Ic:\wpdpack\include -IC:\Python27\include -IC:\Python27\PC -c pcap.c -o build\temp.win32-2.7\Release\pcap.o -DWIN32 -DWPCAP
pcap.c: In function '__pyx_f_4pcap_3bpf_filter':
pcap.c:328:48: warning: passing argument 2 of 'PyObject_AsCharBuffer' from incompatible pointer type
   __pyx_1 = (PyObject_AsCharBuffer(__pyx_v_buf,(&__pyx_v_p),(&__pyx_v_n)) < 0);
                                                ^
In file included from C:\Python27\include/Python.h:133:0,
                 from pcap.c:4:
C:\Python27\include/abstract.h:476:22: note: expected 'const char **' but argument is of type 'char **'
      PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
                      ^
pcap.c:337:86: warning: pointer targets in passing argument 2 of 'bpf_filter' differ in signedness [-Wpointer-sign]
   __pyx_1 = (bpf_filter(((struct __pyx_obj_4pcap_bpf *)__pyx_v_self)->fcode.bf_insns,__pyx_v_p,__pyx_v_n,__pyx_v_n) == 0);
                                                                                      ^
In file included from pcap.c:30:0:
c:\wpdpack\include/pcap/pcap.h:354:7: note: expected 'const u_char *' but argument is of type 'char *'
 u_int bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int);
       ^
pcap.c: In function '__pyx_f_4pcap_4pcap_dispatch':
pcap.c:955:91: warning: passing argument 3 of 'pcap_dispatch' from incompatible pointer type
   __pyx_v_n = pcap_dispatch(((struct __pyx_obj_4pcap_pcap *)__pyx_v_self)->__pcap,__pyx_1,__pyx_f_4pcap___pcap_handler,((unsigned char *)(&__pyx_v_ctx)));
                                                                                           ^
In file included from pcap.c:30:0:
c:\wpdpack\include/pcap/pcap.h:300:5: note: expected 'pcap_handler' but argument is of type 'void (*)(void *, struct pcap_pkthdr *, char *)'
 int pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
     ^
pcap.c: In function '__pyx_f_4pcap_4pcap_loop':
pcap.c:1059:99: warning: passing argument 3 of 'pcap_ex_next' from incompatible pointer type
     __pyx_v_n = pcap_ex_next(((struct __pyx_obj_4pcap_pcap *)__pyx_v_self)->__pcap,(&__pyx_v_hdr),(&__pyx_v_pkt));
                                                                                                   ^
In file included from pcap.c:31:0:
pcap_ex.h:13:7: note: expected 'u_char **' but argument is of type 'char **'
 int   pcap_ex_next(pcap_t *pcap, struct pcap_pkthdr **hdr, u_char **pkt);
       ^
pcap.c: In function '__pyx_f_4pcap_4pcap_sendpacket':
pcap.c:1154:98: warning: pointer targets in passing argument 2 of 'pcap_sendpacket' differ in signedness [-Wpointer-sign]
   __pyx_3 = PyInt_FromLong(pcap_sendpacket(((struct __pyx_obj_4pcap_pcap *)__pyx_v_self)->__pcap,__pyx_1,__pyx_2)); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; goto __pyx_L1;}
                                                                                                  ^
In file included from pcap.c:30:0:
c:\wpdpack\include/pcap/pcap.h:311:5: note: expected 'const u_char *' but argument is of type 'char *'
 int pcap_sendpacket(pcap_t *, const u_char *, int);
     ^
pcap.c: In function '__pyx_f_4pcap_4pcap___next__':
pcap.c:1312:99: warning: passing argument 3 of 'pcap_ex_next' from incompatible pointer type
     __pyx_v_n = pcap_ex_next(((struct __pyx_obj_4pcap_pcap *)__pyx_v_self)->__pcap,(&__pyx_v_hdr),(&__pyx_v_pkt));
                                                                                                   ^
In file included from pcap.c:31:0:
pcap_ex.h:13:7: note: expected 'u_char **' but argument is of type 'char **'
 int   pcap_ex_next(pcap_t *pcap, struct pcap_pkthdr **hdr, u_char **pkt);
       ^
C:\MinGW\bin\gcc.exe -mdll -O -Wall -Ic:\wpdpack\include/pcap -Ic:\wpdpack\include -IC:\Python27\include -IC:\Python27\PC -c pcap_ex.c -o build\temp.win32-2.7\Release\pcap_ex.o -DWIN32 -DWPCAP
pcap_ex.c: In function 'pcap_ex_next':
pcap_ex.c:237:34: warning: passing argument 3 of 'pcap_next_ex' from incompatible pointer type
  return (pcap_next_ex(pcap, hdr, pkt));
                                  ^
In file included from pcap_ex.c:16:0:
c:\wpdpack\include/pcap/pcap.h:303:6: note: expected 'const u_char **' but argument is of type 'u_char **'
 int  pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);
      ^
writing build\temp.win32-2.7\Release\pcap.def
creating build\lib.win32-2.7
C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-2.7\Release\pcap.o build\temp.win32-2.7\Release\pcap_ex.o build\temp.win32-2.7\Release\pcap.def -Lc:\wpdpack\lib -LC:\Python27\libs -LC:\Python27\PCbuild -LC:\Python27\PC\VS9.0 -lwpcap -liphlpapi -lpython27 -lmsvcr90 -o build\lib.win32-2.7\pcap.pyd

Z:\pypcap-master>

LINKING

With MinGW (32-bit) you need a 32-bit Python installation. With a 64-bit Python you need to install MinGW (64-bit).

So you can either install a 32-bit Python or install a 64-bit MinGW.

We are close now, you will make it :-)

totoro
  • 2,469
  • 2
  • 19
  • 23
  • Is there a different between libpcap and winpcap? Do i must install libpcap? – MyNick Apr 11 '16 at 17:34
  • I already got winpcap on my machine but i installed it using the wizard. where should i point it to? – MyNick Apr 11 '16 at 17:55
  • WinPcap doesn't install the header files you need to compile pypcap. – totoro Apr 11 '16 at 17:58
  • I downloaded the libpcap and README.Win32 specify you should install winpcap and describe how to do that. The INSTALL.txt file says you should use configure file but there isnt one for windows. how do i install it? – MyNick Apr 11 '16 at 18:07
  • Sorry, you do have the header files from wpdpack. – totoro Apr 11 '16 at 18:07
  • I moved it to c:\wpdpack as i said, so the makefile could find it. – MyNick Apr 11 '16 at 18:08
  • Can you find the file `io.h` on your system? – totoro Apr 11 '16 at 18:32
  • In: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\crt\src C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src – MyNick Apr 11 '16 at 18:35
  • You should have an `io.h` somewhere inside the `C:\MinGW` folder belonging to gcc from MinGW. – totoro Apr 11 '16 at 18:54
  • Dont have it, how can i get it? – MyNick Apr 11 '16 at 19:05
  • To the updated answer: i installed both of them (i now installed the dev too and not only the bin) but doesn't got it. but i got c++io.h in MinGw\lib\gcc\.... – MyNick Apr 11 '16 at 19:31
  • Delete C:\MinGW and reinstall. – totoro Apr 11 '16 at 19:34
  • I done it and i now got the io.h file. BUT now it can't find pcap-stdinc.h, i can see that file in the include dir in wpdpack and for some reason it doesn't add the include folder to the include path (only include\pcap). The setup.py is written so badly that i couldn't find where to add this include (even when i wrote it myself it didn't add it). i tried to compile it alone (i run the command line with this path in addition). i now get error that i dont have "winsock2.h". What now? – MyNick Apr 12 '16 at 17:59
  • where do i get the winsock2.h header? – MyNick Apr 12 '16 at 18:50
  • I removed the MinGW and installed again and now i have it. It compile but i got undefined refernece to everything in python. But it use -Lc:\python27\libs. Why? – MyNick Apr 12 '16 at 19:06
  • So you fixed the setup.py to add both `c:\wpdpack\include/pcap` and `c:\wpdpack\include`? and now the compile is successfull, but the linking is not? – totoro Apr 13 '16 at 05:53
  • Updated answer with modified `setup.py`. – totoro Apr 13 '16 at 05:59
  • Update my answer regarding you linking woes. – totoro Apr 13 '16 at 06:35
  • i download: C:\Program Files\mingw-w64\x86_64-5.3.0-win32-seh-rt_v4-rev0\mingw64\bin\gcc.exe - it can't compile because: unrecognized command line option '-mno-cygwin' but this flag is added NOWHERE and i can't figure out how to remove it. i managed to compile it alone but for the install i must let it compile it with setup.py. do you know how to remove this compilation flag? – MyNick Apr 13 '16 at 07:26
  • Take a look at http://stackoverflow.com/questions/6034390/compiling-with-cython-and-mingw-produces-gcc-error-unrecognized-command-line-o – totoro Apr 13 '16 at 08:48