Skip to content

Instantly share code, notes, and snippets.

@harthur
Created Sep 3, 2010
Embed
What would you like to do?
Components.utils.import("resource://gre/modules/ctypes.jsm");
var EXPORTED_SYMBOLS = ["open"];
nspr = ctypes.open('/Applications/Minefield.app/Contents/MacOS/libnspr4.dylib');
var PR_AF_INET = 2;
var port = 2387;
// PR_SetNetAddr(val, PR_AF_INET, aPort, &addr);
var PR_IpAddrAny = 1;
var addrType = ctypes.StructType("PRNetAddr",
[{'family': ctypes.int16_t}, {'port': ctypes.int16_t}, {'ip': ctypes.int32_t},
{'pad' : ctypes.char.array(8)}]
);
var PR_SetNetAddr = nspr.declare("PR_SetNetAddr",
ctypes.default_abi,
ctypes.int32_t, // really doesn't return anything
ctypes.int32_t, // val
ctypes.int16_t, // af
ctypes.int16_t, // port
addrType.ptr)
var addr = addrType();
var ret = PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, port, addr.address());
// PR_Bind(serverFD, &addr);
/* open */
var PR_OpenTCPSocket = nspr.declare("PR_OpenTCPSocket", // symbol name
ctypes.default_abi, // cdecl calling convention
ctypes.StructType("PRFileDesc").ptr, // return (PRFileDesc*)
ctypes.int32_t) // first arg
var fd = PR_OpenTCPSocket(PR_AF_INET);
var PR_Bind = nspr.declare("PR_Bind",
ctypes.default_abi,
ctypes.int32_t,
ctypes.StructType("PRFileDesc").ptr,
addrType.ptr);
var success = PR_Bind(fd, addr.address());
dump(success)
@harthur

This comment has been minimized.

Copy link
Owner Author

@harthur harthur commented Sep 3, 2010

Error: expected type pointer, got PRFileDesc.ptr(ctypes.UInt64("0x157ad110"))
Source File: resource://jsbridge/modules/socket.js
Line: 48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment