Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'ffi-napi' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
console.error("usage: node pcap.js /path/to/file.pcap");
console.error();
process.exit();
}
/* NDPI Types */
var voidPtr = exports.voidPtr = ref.refType(ref.types.void);
var u_char = exports.u_char = Struct({
__u_char: ref.types.uchar,
});
var u_charPtr = exports.u_charPtr = ref.refType(u_char);
var pcap_t = exports.pcap_t = voidPtr;
var pcap_tPtr = exports.pcap_tPtr = ref.refType(pcap_t);
var pcap_handler = exports.pcap_handler = ffi.Function(ref.types.void, [
ref.refType(ref.types.uchar),
voidPtr,
ref.refType(ref.types.uchar),
]);
var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
var uint8_t = exports.uint8_t = voidPtr;
var uint8_tPtr = exports.uint8_tPtr = ref.refType(uint8_t);
/* callback */
const onProto = function(id, packet) {
if (id > 0) { console.log("Proto: "+packet+" ("+id+")") }
}
var callback = exports.callback = ffi.Function(ref.types.void, [
ref.refType(ref.types.uchar),
voidPtr,
ref.refType(ref.types.uchar),
]);
var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
var uint8_t = exports.uint8_t = voidPtr;
var uint8_tPtr = exports.uint8_tPtr = ref.refType(uint8_t);
/* callback */
const onProto = function(id, packet) {
if (id > 0) { console.log("Proto: "+packet+" ("+id+")") }
}
var callback = exports.callback = ffi.Function(ref.types.void, [
ref.types.int32,
ref.refType(uint8_t),
]);
var ndpi = ffi.Library('../ndpiexlib.so', {
init: [ref.types.void, [
]],
setDatalinkType: [ref.types.void, [
pcap_tPtr,
]],
processPacket: [ref.types.void, [
voidPtr,
uint8_t,
]],
finish: [ref.types.void, [
]],
]);
var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
// PCAP Header
var pcap_pkthdr = exports.pcap_pkthdr = Struct({
'ts_sec': 'long',
'ts_usec': 'long',
'incl_len': 'int',
'orig_len': 'int'
});
var pktHdr = exports.pktHdr = new pcap_pkthdr;
pktHdr = ref.refType(ref.types.void);
/* NDPI Hook */
var ndpi = exports.ndpi = new ffi.Library('../ndpiexlib.so', {
init: [ref.types.void, [
]],
getResults: [ref.types.void, [
]],
setDatalinkType: [ref.types.void, [
pcap_tPtr,
]],
processPacket: [ref.types.void, [
voidPtr,
uint8_t,
]],
finish: [ref.types.void, [
]],
addProtocolHandler: [ref.types.void, [
callback
]],
// Create the struct required to save the window bounds
const Rect = struct({
left: 'long',
top: 'long',
right: 'long',
bottom: 'long',
});
const RectPointer = refType(Rect);
// Required by QueryFullProcessImageName
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880(v=vs.85).aspx
const PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
// Create FFI declarations for the C++ library and functions needed (User32.dll), using their "Unicode" (UTF-16) version
const user32 = new Library('User32.dll', {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx
GetForegroundWindow: ['pointer', []],
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx
GetWindowThreadProcessId: ['uint32', ['pointer', 'uint32 *']],
// Get window bounds function
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowrect
GetWindowRect: ['bool', ['pointer', RectPointer]],
});
function windows() {
// Windows C++ APIs' functions are declared with capitals, so this rule has to be turned off
// Get a "handle" of the active window
const activeWindowHandle = user32.GetForegroundWindow();
if (isNull(activeWindowHandle)) {
public constructor ({ command }: { command?: string }) {
// @formatter:off
this.client = ffi.Library(
resolvePath(command || PATH_TO_LIBRARY_FILE),
{
td_json_client_create: ['pointer', []],
td_json_client_send: ['void', ['pointer', 'string']],
td_json_client_receive: ['string', ['pointer', 'double']],
td_json_client_execute: ['string', ['pointer', 'string']],
td_json_client_destroy: ['void', ['pointer']]
})
// @formatter:on
}
// Create the struct required to save the window bounds
const Rect = struct({
left: 'long',
top: 'long',
right: 'long',
bottom: 'long'
});
const RectPointer = ref.refType(Rect);
// Required by QueryFullProcessImageName
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880(v=vs.85).aspx
const PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
// Create FFI declarations for the C++ library and functions needed (User32.dll), using their "Unicode" (UTF-16) version
const user32 = new ffi.Library('User32.dll', {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx
GetForegroundWindow: ['pointer', []],
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=vs.85).aspx
GetWindowTextW: ['int', ['pointer', 'pointer', 'int']],
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633521(v=vs.85).aspx
GetWindowTextLengthW: ['int', ['pointer']],
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx
GetWindowThreadProcessId: ['uint32', ['pointer', 'uint32 *']],
// Get window bounds function
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowrect
GetWindowRect: ['bool', ['pointer', RectPointer]]
});
const SIZE_T = 'uint64';
// https://docs.microsoft.com/en-us/windows/desktop/api/psapi/ns-psapi-_process_memory_counters
/* APP VARS */
var voidPtr = exports.voidPtr = ref.refType(ref.types.void);
var u_char = exports.u_char = Struct({
__u_char: ref.types.uchar,
});
var u_charPtr = exports.u_charPtr = ref.refType(u_char);
var uint8_t = exports.uint8_t = voidPtr;
var uint8_tPtr = exports.uint8_tPtr = ref.refType(uint8_t);
// var callbackPtr = ffi.Callback(ref.types.void, [ ref.types.int32, ref.refType(ref.types.uchar) ], onProto);
// var callbackF = ffi.ForeignFunction(callbackPtr, ref.types.void, [ ref.types.int32, ref.refType(ref.types.uchar) ]);
var callback = exports.callback = ffi.Function(ref.types.void, [
ref.types.int32,
ref.refType(ref.types.uchar),
]);
var pcap_t = exports.pcap_t = voidPtr;
var pcap_tPtr = exports.pcap_tPtr = ref.refType(pcap_t);
var pcap_handler = exports.pcap_handler = ffi.Function(ref.types.void, [
ref.refType(ref.types.uchar),
voidPtr,
ref.refType(ref.types.uchar),
]);
var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
// PCAP Header
var pcap_pkthdr = exports.pcap_pkthdr = Struct({
'ts_sec': 'long',
var u_charPtr = exports.u_charPtr = ref.refType(u_char);
var uint8_t = exports.uint8_t = voidPtr;
var uint8_tPtr = exports.uint8_tPtr = ref.refType(uint8_t);
// var callbackPtr = ffi.Callback(ref.types.void, [ ref.types.int32, ref.refType(ref.types.uchar) ], onProto);
// var callbackF = ffi.ForeignFunction(callbackPtr, ref.types.void, [ ref.types.int32, ref.refType(ref.types.uchar) ]);
var callback = exports.callback = ffi.Function(ref.types.void, [
ref.types.int32,
ref.refType(ref.types.uchar),
]);
var pcap_t = exports.pcap_t = voidPtr;
var pcap_tPtr = exports.pcap_tPtr = ref.refType(pcap_t);
var pcap_handler = exports.pcap_handler = ffi.Function(ref.types.void, [
ref.refType(ref.types.uchar),
voidPtr,
ref.refType(ref.types.uchar),
]);
var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
// PCAP Header
var pcap_pkthdr = exports.pcap_pkthdr = Struct({
'ts_sec': 'long',
'ts_usec': 'long',
'incl_len': 'int',
'orig_len': 'int'
});
var pktHdr = exports.pktHdr = new pcap_pkthdr;
pktHdr = ref.refType(ref.types.void);
settings?: FModel.LoadSettings,
): T {
const st = parse_settings(settings)
if (st && st.singleton) {
let inst = get_inst_by_name(dllName)
if (! inst) {
inst = ffi.Library(dllName, gen_api_opts(dllFuncs, fns)) as T
set_inst_by_name(dllName, inst)
}
return inst
}
else {
return ffi.Library(dllName, gen_api_opts(dllFuncs, fns))
}
}
export function load(
dllName: string,
dllFuncs: FModel.DllFuncs,
fns?: FModel.FnName[],
settings?: FModel.LoadSettings,
): T {
const st = parse_settings(settings)
if (st && st.singleton) {
let inst = get_inst_by_name(dllName)
if (! inst) {
inst = ffi.Library(dllName, gen_api_opts(dllFuncs, fns)) as T
set_inst_by_name(dllName, inst)
}
return inst
}
else {
return ffi.Library(dllName, gen_api_opts(dllFuncs, fns))
}
}