Skip to content

Commit 9fc0796

Browse files
committed
first commit
0 parents  commit 9fc0796

11 files changed

+375
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/build/*
2+
node_modules
3+
*.node
4+
*.sh
5+
*.swp
6+
.lock*
7+
npm-debug.log

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2012 Near Infinity Corporation
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# nodeshark
2+
3+
Wrapper around libwireshark providing network packet dissection for [node.js](http://nodejs.org/).
4+
5+
## Installation
6+
7+
$ npm install nodeshark
8+
9+
### Build
10+
11+
```bash
12+
$ svn co http://anonsvn.wireshark.org/wireshark/trunk/ wireshark
13+
$ cd wireshark
14+
$ ./autogen.sh
15+
$ ./configure
16+
$ make
17+
$ sudo make install
18+
$ export WIRESHARK_INCLUDE_DIR=[wireshark source directory]
19+
$ node-waf configure build
20+
```
21+
22+
## Usage
23+
24+
```javascript
25+
var nodeshark = require("nodeshark");
26+
27+
var dissector = new nodeshark.Dissector(nodeshark.LINK_LAYER_TYPE_ETHERNET);
28+
29+
var rawPacket = {
30+
header: {
31+
timestampSeconds: 10,
32+
timestampMicroseconds: 20,
33+
capturedLength: buffer.length,
34+
originalLength: buffer.length
35+
},
36+
data: new Buffer([
37+
0x58, 0x6d, 0x8f, 0x67, 0x8a, 0x4d, 0x00, 0x1b, 0x21, 0xcf, 0xa1, 0x00, 0x08, 0x00, 0x45, 0x00,
38+
0x00, 0x3b, 0xd1, 0xb0, 0x40, 0x00, 0x40, 0x11, 0xc5, 0xde, 0x0a, 0x14, 0x08, 0x65, 0xc0, 0xa8,
39+
0xd0, 0x01, 0xc5, 0x32, 0x00, 0x35, 0x00, 0x27, 0xa3, 0x5b, 0x65, 0x89, 0x01, 0x00, 0x00, 0x01,
40+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6d, 0x61, 0x69, 0x6c, 0x04, 0x6c, 0x69, 0x76, 0x65,
41+
0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01
42+
]);
43+
};
44+
var packet = dissector.dissect(rawPacket);
45+
46+
// OR
47+
48+
var buffer = new Buffer([
49+
0x58, 0x6d, 0x8f, 0x67, 0x8a, 0x4d, 0x00, 0x1b, 0x21, 0xcf, 0xa1, 0x00, 0x08, 0x00, 0x45, 0x00,
50+
0x00, 0x3b, 0xd1, 0xb0, 0x40, 0x00, 0x40, 0x11, 0xc5, 0xde, 0x0a, 0x14, 0x08, 0x65, 0xc0, 0xa8,
51+
0xd0, 0x01, 0xc5, 0x32, 0x00, 0x35, 0x00, 0x27, 0xa3, 0x5b, 0x65, 0x89, 0x01, 0x00, 0x00, 0x01,
52+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6d, 0x61, 0x69, 0x6c, 0x04, 0x6c, 0x69, 0x76, 0x65,
53+
0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01
54+
]);
55+
var packet = dissector.dissect(buffer);
56+
```
57+
58+
You can also use it it conjunction with [pcap-parser](https://github.com/nearinfinity/node-pcap-parser).
59+
60+
```javascript
61+
var pcapp = require('pcap-parser');
62+
63+
var parser = new pcapp.Parser('/path/to/file.pcap');
64+
var dissector;
65+
parser.on('globalHeader', function(globalHeader) {
66+
dissector = new nodeshark.Dissector(globalHeader.linkLayerType);
67+
});
68+
parser.on('packet', function(rawPacket) {
69+
var packet = dissector.dissect(rawPacket);
70+
});
71+
parser.parse();
72+
```
73+
74+
## License
75+
76+
(The MIT License)
77+
78+
Copyright (c) 2012 Near Infinity Corporation
79+
80+
Permission is hereby granted, free of charge, to any person obtaining
81+
a copy of this software and associated documentation files (the
82+
"Software"), to deal in the Software without restriction, including
83+
without limitation the rights to use, copy, modify, merge, publish,
84+
distribute, sublicense, and/or sell copies of the Software, and to
85+
permit persons to whom the Software is furnished to do so, subject to
86+
the following conditions:
87+
88+
The above copyright notice and this permission notice shall be
89+
included in all copies or substantial portions of the Software.
90+
91+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
92+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
93+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
94+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
95+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
96+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
97+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
module.exports = require("./lib/nodeJavaBridge");

lib/nodeJavaBridge.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
var bindings = require("../build/Release/nodejavabridge_bindings");
3+
4+
module.exports = new bindings.Java();

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "nodejavabridge",
3+
"description": "Bridge API to connect with existing Java APIs",
4+
"keywords": ["java", "jvm", "bridge"],
5+
"homepage": "https://github.com/nearinfinity/node-java-bridge",
6+
"version": "0.0.1",
7+
"engines": { "node" : ">=0.6.0" },
8+
"maintainers": [
9+
{ "name": "Jeff Kunkle", "email": "[email protected]" },
10+
{ "name": "Joe Ferner", "email": "[email protected]" }
11+
],
12+
"bugs": { "url": "https://github.com/nearinfinity/node-java-bridge/issues" },
13+
"licenses": [ { "type" : "MIT" } ],
14+
"repositories": [
15+
{ "type": "git", "url": "https://github.com/nearinfinity/node-java-bridge.git" }
16+
],
17+
"devDependencies": {
18+
"nodeunit": "~0.6.4"
19+
},
20+
"scripts": {
21+
"test": "nodeunit test",
22+
"install": "node-waf configure build"
23+
}
24+
}

src/java.cpp

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
#include "java.h"
3+
#include <string.h>
4+
5+
/*static*/ v8::Persistent<v8::FunctionTemplate> Java::s_ct;
6+
7+
/*static*/ void Java::Init(v8::Handle<v8::Object> target) {
8+
v8::HandleScope scope;
9+
10+
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
11+
s_ct = v8::Persistent<v8::FunctionTemplate>::New(t);
12+
s_ct->InstanceTemplate()->SetInternalFieldCount(1);
13+
s_ct->SetClassName(v8::String::NewSymbol("Java"));
14+
15+
NODE_SET_PROTOTYPE_METHOD(s_ct, "newInstance", newInstance);
16+
17+
target->Set(v8::String::NewSymbol("Java"), s_ct->GetFunction());
18+
}
19+
20+
/*static*/ v8::Handle<v8::Value> Java::New(const v8::Arguments& args) {
21+
v8::HandleScope scope;
22+
23+
Java *self = new Java();
24+
self->Wrap(args.This());
25+
return args.This();
26+
}
27+
28+
Java::Java() {
29+
this->m_env = createJVM();
30+
}
31+
32+
Java::~Java() {
33+
34+
}
35+
36+
/*static*/ JNIEnv* Java::createJVM() {
37+
JavaVM* jvm;
38+
JNIEnv* env;
39+
JavaVMInitArgs args;
40+
JavaVMOption options[0];
41+
42+
/* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
43+
args.version = JNI_VERSION_1_2;
44+
args.nOptions = 0;
45+
args.options = options;
46+
args.ignoreUnrecognized = JNI_FALSE;
47+
48+
JNI_CreateJavaVM(&jvm, (void **)&env, &args);
49+
50+
return env;
51+
}
52+
53+
/*static*/ v8::Handle<v8::Value> Java::newInstance(const v8::Arguments& args) {
54+
v8::HandleScope scope;
55+
Java* self = node::ObjectWrap::Unwrap<Java>(args.This());
56+
57+
// argument - className
58+
if(args.Length() < 1 || !args[0]->IsString()) {
59+
return ThrowException(v8::Exception::TypeError(v8::String::New("Argument 0 must be a string")));
60+
}
61+
v8::Local<v8::String> classNameObj = v8::Local<v8::String>::Cast(args[0]);
62+
v8::String::AsciiValue className(classNameObj);
63+
64+
// argument - callback
65+
v8::Handle<v8::Value> callback;
66+
if(args[args.Length()-1]->IsFunction()) {
67+
callback = args[args.Length()-1];
68+
} else {
69+
callback = v8::Null();
70+
}
71+
72+
// run
73+
NewInstanceBaton* baton = new NewInstanceBaton(self, *className, callback);
74+
eio_custom(EIO_NewInstance, EIO_PRI_DEFAULT, EIO_AfterNewInstance, baton);
75+
ev_ref(EV_DEFAULT_UC);
76+
77+
return v8::Undefined();
78+
}
79+
80+
/*static*/ void Java::EIO_NewInstance(eio_req* req) {
81+
NewInstanceBaton* baton = static_cast<NewInstanceBaton*>(req->data);
82+
}
83+
84+
/*static*/ int Java::EIO_AfterNewInstance(eio_req* req) {
85+
NewInstanceBaton* baton = static_cast<NewInstanceBaton*>(req->data);
86+
ev_unref(EV_DEFAULT_UC);
87+
88+
baton->doCallback();
89+
90+
delete baton;
91+
return 0;
92+
}
93+
94+
NewInstanceBaton::NewInstanceBaton(Java* java, const char *className, v8::Handle<v8::Value> &callback) {
95+
this->m_java = java;
96+
this->m_className = strdup(className);
97+
this->m_callback = v8::Persistent<v8::Value>::New(callback);
98+
this->m_java->Ref();
99+
}
100+
101+
NewInstanceBaton::~NewInstanceBaton() {
102+
this->m_callback.Dispose();
103+
this->m_java->Unref();
104+
}
105+
106+
void NewInstanceBaton::doCallback() {
107+
v8::Handle<v8::Value> argv[2];
108+
argv[0] = v8::Undefined();
109+
argv[1] = v8::Undefined();
110+
111+
if(this->m_callback->IsFunction()) {
112+
v8::Function::Cast(*this->m_callback)->Call(v8::Context::GetCurrent()->Global(), 2, argv);
113+
}
114+
}

src/java.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
#ifndef _node_java_h_
3+
#define _node_java_h_
4+
5+
#include <v8.h>
6+
#include <node.h>
7+
#include <jni.h>
8+
9+
class NewInstanceBaton;
10+
11+
class Java : public node::ObjectWrap {
12+
public:
13+
static void Init(v8::Handle<v8::Object> target);
14+
15+
friend class NewInstanceBaton;
16+
17+
private:
18+
Java();
19+
~Java();
20+
static JNIEnv* createJVM();
21+
22+
static v8::Handle<v8::Value> New(const v8::Arguments& args);
23+
static v8::Handle<v8::Value> newInstance(const v8::Arguments& args);
24+
static void EIO_NewInstance(eio_req* req);
25+
static int EIO_AfterNewInstance(eio_req* req);
26+
27+
static v8::Persistent<v8::FunctionTemplate> s_ct;
28+
JNIEnv* m_env;
29+
};
30+
31+
class NewInstanceBaton {
32+
public:
33+
NewInstanceBaton(Java* java, const char *className, v8::Handle<v8::Value> &callback);
34+
~NewInstanceBaton();
35+
void doCallback();
36+
37+
private:
38+
Java* m_java;
39+
v8::Persistent<v8::Value> m_callback;
40+
char* m_className;
41+
};
42+
43+
#endif

src/nodeJavaBridge.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
#include "java.h"
3+
4+
extern "C" {
5+
static void init(v8::Handle<v8::Object> target) {
6+
Java::Init(target);
7+
}
8+
9+
NODE_MODULE(nodejavabridge_bindings, init);
10+
}

test/simple-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
var java = require("../");
4+
var nodeunit = require("nodeunit");
5+
var util = require("util");
6+
7+
exports['Simple'] = nodeunit.testCase({
8+
"create an instance of a class (async)": function(test) {
9+
java.newInstance("java.util.ArrayList", function(list) {
10+
test.ok(list);
11+
test.done();
12+
});
13+
}
14+
});

wscript

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Options, Utils
2+
from os import unlink, symlink, chdir, environ
3+
from os.path import exists
4+
5+
def set_options(opt):
6+
opt.tool_options("compiler_cxx")
7+
8+
def configure(conf):
9+
conf.check_tool("compiler_cxx")
10+
conf.check_tool("node_addon")
11+
12+
# Enables all the warnings that are easy to avoid
13+
conf.env.append_unique('CXXFLAGS', ["-Wall"])
14+
conf.env.append_unique('CXXFLAGS', ['-Isrc/'])
15+
conf.env.append_unique('CXXFLAGS', ['-g'])
16+
conf.env.append_unique('CXXFLAGS', ['-D_FILE_OFFSET_BITS=64'])
17+
conf.env.append_unique('CXXFLAGS', ['-D_LARGEFILE_SOURCE'])
18+
conf.env.append_unique('CXXFLAGS', ['-DHAVE_CONFIG_H'])
19+
20+
jdk_include_dir = environ.get("JDK_INCLUDE_DIR", "/usr/local/share/jdk1.6.0_30/include/")
21+
if jdk_include_dir:
22+
conf.env.append_unique('CXXFLAGS', [ '-I' + jdk_include_dir ])
23+
24+
jdk_additional_include_dir = environ.get("JDK_INCLUDE_DIR", "/usr/local/share/jdk1.6.0_30/include/linux/")
25+
if jdk_additional_include_dir:
26+
conf.env.append_unique('CXXFLAGS', [ '-I' + jdk_additional_include_dir ])
27+
28+
jdk_lib_dir = environ.get("JDK_LIB_DIR", "/usr/local/share/jdk1.6.0_30/jre/lib/i386/client/")
29+
if jdk_lib_dir:
30+
conf.env.append_unique('LINKFLAGS', [ '-L' + jdk_lib_dir ])
31+
32+
conf.env.append_unique('LINKFLAGS', ['-ljvm'])
33+
34+
def build(bld):
35+
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
36+
obj.target = "nodejavabridge_bindings"
37+
obj.source = " ".join([
38+
"src/nodeJavaBridge.cpp",
39+
"src/java.cpp"])
40+
obj.includes = "src/"

0 commit comments

Comments
 (0)