本文介绍了节点ffi指向结构的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我在这里问是因为在节点ffi中使用指针没有快速的答案,也没有关于结构的指针,这会有所帮助
这是我的节点ffi:
const struct_in_addr = Struct({
's_addr': 'ulong',
});
const struct_sockaddr_in = Struct({
'sin_family': 'short',
'sin_port' : 'ushort',
'in_addr' : struct_in_addr,
'sin_zero' : 'char',
});
var redir = ffi.Library('./libredir', {
//'main' : [ 'int' , [ 'int', 'char* []' ] ],
//'parse_args' : [ 'void', [ 'int', 'char* []' ] ],
'target_init' : [ 'int' , [ 'char *', 'int', [ struct_sockaddr_in, "pointer" ]] ],
'target_connect' : [ 'int' , [ 'int', [ struct_sockaddr_in, "pointer" ] ] ],
'client_accept' : [ 'int' , [ 'int', [ struct_sockaddr_in, "pointer" ] ] ],
'server_socket' : [ 'int' , [ 'char *', 'int', 'int' ] ],
});
以下是target_init的签名:
Here's the signature of target_init as an example:
int target_init(char *addr, int port, struct sockaddr_in *target)
这就是我要得到的:
/home/lz/redir-controller/node_modules/ref/lib/ref.js:397
throw new TypeError('could not determine a proper "type" from: ' + JSON.stringify(type))
^
TypeError: could not determine a proper "type" from: [null,"pointer"]
at coerceType (/home/lz/redir-controller/node_modules/ref/lib/ref.js:397:11)
at Array.map (<anonymous>)
我正在使用 https://github.com/troglobit/redir /blob/master/redir.c 并使用gcc -shared -fpic redir.c -o libredir.so
我怀疑这是struct_sockaddr_in
的问题,但一切似乎都还不错.我什至尝试在 https://github中这样做.com/node-ffi/node-ffi/wiki/Node-FFI-Tutorial#structs ,方法是:
I suspect it's a problem with struct_sockaddr_in
but everything seems ok. I even tried to do as in https://github.com/node-ffi/node-ffi/wiki/Node-FFI-Tutorial#structs by doing:
const _struct_sockaddr_in = Struct({
'sin_family': 'short',
'sin_port' : 'ushort',
'in_addr' : struct_in_addr,
'sin_zero' : 'char',
});
struct_sockaddr_in = ref.refType(_struct_sockaddr_in);
但是现在我得到了
TypeError: could not determine a proper "type" from: [{"indirection":2,"name":"StructType*"},"pointer"]
推荐答案
我不知道'pointer'
的来源,但后续工作有效:
I don't know where I took 'pointer'
from, but the followin works:
const struct_in_addr = Struct({
's_addr': 'ulong',
});
const _struct_sockaddr_in = Struct({
'sin_family': 'short',
'sin_port' : 'ushort',
'in_addr' : struct_in_addr,
'sin_zero' : 'char',
});
struct_sockaddr_in = ref.refType(_struct_sockaddr_in);
const redir = ffi.Library('./libredir', {
//'main' : [ 'int' , [ 'int', 'char* []' ] ],
//'parse_args' : [ 'void', [ 'int', 'char* []' ] ],
'target_init' : [ 'int' , [ 'char *', 'int', struct_sockaddr_in ] ],
'target_connect' : [ 'int' , [ 'int', struct_sockaddr_in ] ],
'client_accept' : [ 'int' , [ 'int', struct_sockaddr_in ] ],
'server_socket' : [ 'int' , [ 'char *', 'int', 'int' ] ],
});
这篇关于节点ffi指向结构的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!