diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-11-08 12:22:35 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-11-08 12:22:35 -0800 |
commit | 118a6bd6c5aca371ecfcf9f7baf748a7dfd43351 (patch) | |
tree | 0f36050b3b3702a852e7cbda547539c9d446aae8 | |
parent | 2ac32ae20a38029c1fffd9e162c92f9370283dd7 (diff) |
support inet_ntoa and inet_aton, and fix inet_ntop bug with not writing null terminator
-rw-r--r-- | src/library.js | 18 | ||||
-rwxr-xr-x | tests/runner.py | 17 |
2 files changed, 34 insertions, 1 deletions
diff --git a/src/library.js b/src/library.js index 73109334..c7fe9fcb 100644 --- a/src/library.js +++ b/src/library.js @@ -6459,10 +6459,26 @@ LibraryManager.library = { inet_ntop: function(af, src, dst, size) { var addr = getValue(src, 'i32'); var str = __inet_ntop_raw(addr); - writeStringToMemory(str.substr(0, size), dst, true); + writeStringToMemory(str.substr(0, size), dst); return dst; }, + inet_ntoa__deps: ['inet_ntop'], + inet_ntoa: function(in_addr) { + if (!_inet_ntoa.buffer) { + _inet_ntoa.buffer = _malloc(1024); + } + return _inet_ntop(0, in_addr, _inet_ntoa.buffer, 1024); + }, + + inet_aton__deps: ['inet_addr'], + inet_aton: function(cp, inp) { + var addr = _inet_addr(cp); + setValue(inp, addr, 'i32'); + if (addr < 0) return 0; + return 1; + }, + // ========================================================================== // netdb.h // ========================================================================== diff --git a/tests/runner.py b/tests/runner.py index bd056ab4..a572b4ee 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -5219,6 +5219,23 @@ def process(filename): ''' self.do_run(src, '*d4c3b2a1,e07235fe,f0cdab07,cdab,34122143,afbe*\n4e0ab4be\n') + def test_inet2(self): + src = r''' + #include <stdio.h> + #include <arpa/inet.h> + + int main() { + struct in_addr x, x2; + int *y = (int*)&x; + *y = 0x12345678; + printf("%s\n", inet_ntoa(x)); + int r = inet_aton(inet_ntoa(x), &x2); + printf("%s\n", inet_ntoa(x2)); + return 0; + } + ''' + self.do_run(src, '120.86.52.18\n120.86.52.18\n') + def test_gethostbyname(self): if Settings.USE_TYPED_ARRAYS != 2: return self.skip("assume t2 in gethostbyname") |