compile gopacket on windows 64bit

2020-06-04 05:19发布

问题:

I am trying to use gopacket on my windows 10.
I'm using it to sniff and inject packets directly to/from the NIC.
I can easily compile and run my code with GOARCH=386 but can't in GOARCH=amd64.

Worth noticing: I am NOT trying to cross-compile.
I'm using go1.6.windows-386 to compile the 32bit version and when I try to compile with GOARCH=amd64 I use go1.6.windows-amd64.

I used TDM-GCC as linux like compile tools.
The error isn't indicative. it just says

c:/WpdPack/Lib/x64/wpcap.lib: error adding symbols: File in wrong format collect2.exe: error ld returned 1 exit status

Did anyone manage to build this, if it's even possible?

回答1:

OK so I have figured it out.
In order to compile gopacket 64bit on windows you need to do the following:

  1. Install go_amd64 (add go binaries to your PATH)
  2. Install TDM GCC x64 (add TDM-GCC binaries to your PATH)
  3. Also add TDM-GCC\x86_64-w64-mingw32\bin to your PATH
  4. Install Winpcap
  5. Download Winpcap developer's pack and extract it to C:\

Now the point is that there are missing linux static libraries files
(libwpcap.a and libpacket.a) from lib/x64 folder. I don't know why they weren't
included in the developers pack but anyway that's how we can generate them:

  1. find wpcap.dll and packet.dll in your PC (typically in c:\windows\system32
  2. copy them to some other temp folder or else you'll have to supply Admin privs to the following commands
  3. run gendef on those files gendef wpcap.dll and gendef packet.dll (obtainable with MinGW Installation Manager, package mingw32-gendef)
  4. this will generate .def files
  5. Now we'll generate the static libraries files:
  6. run dlltool --as-flags=--64 -m i386:x86-64 -k --output-lib libwpcap.a --input-def wpcap.def
  7. and dlltool --as-flags=--64 -m i386:x86-64 -k --output-lib libpacket.a --input-def packet.def
  8. Now just copy both libwpcap.a and libpacket.a to c:\WpdPack\Lib\x64

That's it.
Now gopacket should compile with no problems.



回答2:

Thank you so much for the solution, it saved me a lot of time!

Just wanted to add that you can do the same with Npcap, modify the gopacket source code to point to Npcap and it will work too.

In case you don't know Npcap:

  • https://nmap.org/npcap/vs-winpcap.html
  • https://nmap.org/npcap/


回答3:

I installed Npcap on Windows in "Wpcap API Compatibility Mode" and gopacket now works fine.