Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working on Windows 7 #179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand All @@ -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

Expand Down
55 changes: 41 additions & 14 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
{
"targets": [
{
"target_name": "pcap_binding",
"sources": [ "pcap_binding.cc", "pcap_session.cc" ],
"include_dirs": [
"<!(node -e \"require('nan')\")"
],
"link_settings": {
"libraries": [
"-lpcap"
]
}
}
]
"targets": [
{
"target_name": "pcap_binding",
"sources": [ "pcap_binding.cc", "pcap_session.cc" ],
"include_dirs": [
"<!(node -e \"require('nan')\")"
],
'conditions': [
['OS=="win"', {
'include_dirs': [
"C:\\dev\\src\\WpdPack-4.1.2\\Include"
],
'conditions': [
['target_arch == "x64"', {
'link_settings': {
'libraries': [
'-l$(WINPCAP_DIR)\\Lib\\x64\\wpcap',
'-l$(WINPCAP_DIR)\\Lib\\x64\\Packet',
'-lws2_32'
]
}
}, { # target_arch != "x64"
'link_settings': {
'libraries': [
'-l$(WINPCAP_DIR)\\Lib\\wpcap',
'-l$(WINPCAP_DIR)\\Lib\\Packet',
'-lws2_32'
]
}
}]
]
}, { # OS != "win"
"link_settings": {
"libraries": [
"-lpcap"
]
},
}]
]
}
]
}
29 changes: 29 additions & 0 deletions examples/parse_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var pcap = require("../pcap");

var pcapfile = process.argv[2];

if (!pcapfile) {
console.log('ERROR: you must define a pcap file to open')
process.exit(1);
}

console.log('opening:', pcapfile);

var pcap_session = pcap.createOfflineSession(pcapfile);

var packetCount = 0;

pcap_session.on('packet', function (raw_packet) {
var packet = pcap.decode.packet(raw_packet);

packetCount++;

// console.log('packet:', packet);
if (packet.hasOwnProperty('payload')) {
// console.log('payload:', packet.payload.payload);
}
});

pcap_session.on('complete', function() {
console.log('counted', packetCount, 'packets');
});
14 changes: 10 additions & 4 deletions pcap_binding.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#include <assert.h>
#include <pcap/pcap.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#if defined(_WIN32) || defined(_WIN64)
#include <winsock2.h>
#else
#include <sys/time.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#endif

#include <assert.h>
#include <pcap/pcap.h>
#include <iostream>
#include <stdio.h>

#include "pcap_session.h"

Expand Down
32 changes: 29 additions & 3 deletions pcap_session.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#if defined(_WIN32) || defined(_WIN64)
#include <winsock2.h>
#else
#include <sys/ioctl.h>
#endif

#include <assert.h>
#include <pcap/pcap.h>
#include <sys/ioctl.h>
#include <cstring>
#include <string.h>

Expand Down Expand Up @@ -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()) {
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -326,7 +340,13 @@ NAN_METHOD(PcapSession::Fileno)

PcapSession* session = ObjectWrap::Unwrap<PcapSession>(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<Integer>(fd));
}
Expand Down Expand Up @@ -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();
}