首先,我在这里问是因为在节点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的签名作为示例:
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"]
我从哪里拿'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' ] ],
});