节点ffi指向结构的指针

卢卡斯·扎内拉(Lucas Zanella)

首先,我在这里问是因为对节点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"]
卢卡斯·扎内拉(Lucas Zanella)

我不知道我从哪里来的'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' ] ],
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章