首先,我在这里问,因为既没有快速回答节点ffi中指针的使用也没有关于结构的指针,这将有助于 < / p>
这是我的节点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的签名为例:
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那样做:
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"]
答案 0 :(得分:0)
我不知道从哪里开始'pointer'
,但是后续工作:
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' ] ],
});