diff --git a/README.md b/README.md index 93ffd7a..a8297df 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ very straightforward in node. ## Installation You will need `libpcap` installed. Most OSX machines seem to have it. All major Linux distributions have it available -either by default or with a package like `libpcap-dev`. +either by default or with a package like `libpcap-dev`. For Windows see below. The easiest way to get `node_pcap` and its tools is with `npm`: @@ -55,6 +55,22 @@ To compile the native code bindings, do this: Assuming it built without errors, you should be able to run the examples and then write your own packet capture programs. +### Windows + +To use this module on Windows you will need to download the [WinPcap Developer Pack](http://www.winpcap.org/devel.htm) and unzip it to your favourite location. For example `C:\src\WpdPack-4.1.2`. + +You will then need to add the path to your environment variables under `WINPCAP_DIR`. + +Now, before running `npm install pcap`, you may need to run one of the following depending on your architecture. + +``` +call "C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\Setenv.cmd" /Release /x86 +# or +call "C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\Setenv.cmd" /Release /x64 +``` + +Now run `npm install pcap` and everything should be fine, fingers crossed. + ## Usage diff --git a/binding.gyp b/binding.gyp index 26a6129..00cdaf6 100644 --- a/binding.gyp +++ b/binding.gyp @@ -1,16 +1,43 @@ { - "targets": [ - { - "target_name": "pcap_binding", - "sources": [ "pcap_binding.cc", "pcap_session.cc" ], - "include_dirs": [ - " -#include -#include -#include #include #include #include + +#if defined(_WIN32) || defined(_WIN64) +#include +#else #include #include #include +#endif + +#include +#include +#include +#include #include "pcap_session.h" diff --git a/pcap_session.cc b/pcap_session.cc index 369895f..a6ab815 100644 --- a/pcap_session.cc +++ b/pcap_session.cc @@ -1,6 +1,11 @@ +#if defined(_WIN32) || defined(_WIN64) +#include +#else +#include +#endif + #include #include -#include #include #include @@ -207,6 +212,10 @@ _NAN_METHOD_RETURN_TYPE PcapSession::Open(bool live, _NAN_METHOD_ARGS) NanReturnUndefined(); } +#if defined(_WIN32) || defined(_WIN64) + // Not available in WinPcap developer pack 4.1.2 yet. Is in WinPcap + // 4.1.3 but this is source only. +#else // fixes a previous to-do that was here. if (args.Length() == 6) { if (args[5]->Int32Value()) { @@ -216,6 +225,7 @@ _NAN_METHOD_RETURN_TYPE PcapSession::Open(bool live, _NAN_METHOD_ARGS) } } } +#endif if (pcap_activate(session->pcap_handle) != 0) { NanThrowError(pcap_geterr(session->pcap_handle)); @@ -287,7 +297,11 @@ _NAN_METHOD_RETURN_TYPE PcapSession::Open(bool live, _NAN_METHOD_ARGS) ret = NanNew("LINKTYPE_LINUX_SLL"); break; default: +#if defined(_WIN32) || defined(_WIN64) + sprintf_s(errbuf, PCAP_ERRBUF_SIZE, "Unknown linktype %d", link_type); +#else snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unknown linktype %d", link_type); +#endif ret = NanNew(errbuf); break; } @@ -326,7 +340,13 @@ NAN_METHOD(PcapSession::Fileno) PcapSession* session = ObjectWrap::Unwrap(args.This()); - int fd = pcap_get_selectable_fd(session->pcap_handle); + int fd; + +#if defined(_WIN32) || defined(_WIN64) + fd = (int)session->pcap_handle; +#elif _linux_ + fd = pcap_get_selectable_fd(session->pcap_handle); +#endif NanReturnValue(NanNew(fd)); } @@ -376,10 +396,16 @@ NAN_METHOD(PcapSession::Inject) bufferData = node::Buffer::Data(buffer_obj); bufferLength = node::Buffer::Length(buffer_obj); +#if defined(_WIN32) || defined(_WIN64) + if (pcap_sendpacket(session->pcap_handle, (const u_char*)bufferData, bufferLength) != 0) { + NanThrowError("Pcap inject failed."); + NanReturnUndefined(); + } +#else if (pcap_inject(session->pcap_handle, bufferData, bufferLength) != (int)bufferLength) { NanThrowError("Pcap inject failed."); NanReturnUndefined(); } +#endif NanReturnUndefined(); } -