aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-10-02 12:10:29 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-10-02 12:12:28 -0700
commit0035201655ece68da0e1f201e2028562869328d5 (patch)
treece4462607c831386db7bd543d3b2b24489315704
parent550dc59e5461f276a146ee25940b45854ed1ca76 (diff)
gethostbyname initial support
-rw-r--r--src/library.js43
-rwxr-xr-xtests/runner.py42
2 files changed, 85 insertions, 0 deletions
diff --git a/src/library.js b/src/library.js
index d072d946..5d97b41f 100644
--- a/src/library.js
+++ b/src/library.js
@@ -6344,6 +6344,49 @@ LibraryManager.library = {
},
// ==========================================================================
+ // netdb.h
+ // ==========================================================================
+
+ // All we can do is alias names to ips. you give this a name, it returns an
+ // "ip" that we later know to use as a name. There is no way to do actual
+ // name resolving clientside in a browser.
+ // we do the aliasing in 172.29.*.*, giving us 65536 possibilities
+ // note: lots of leaking here!
+ gethostbyname: function(name) {
+ name = Pointer_stringify(name);
+ if (!_gethostbyname.hostent_layout) {
+ _gethostbyname.hostent_layout = Runtime.generateStructInfo([
+ ['i8*', 'h_name'],
+ ['i8**', 'h_aliases'],
+ ['i32', 'h_addrtype'],
+ ['i32', 'h_length'],
+ ['i8**', 'h_addr_list'],
+ ]);
+ _gethostbyname.table = {};
+ _gethostbyname.id = 0;
+ }
+ var id = _gethostbyname.id++;
+ assert(id < 65535);
+ _gethostbyname.table[id] = name;
+ // generate hostent
+ var ret = _malloc(_gethostbyname.hostent_layout.__size__);
+ var nameBuf = _malloc(name.length+1);
+ writeStringToMemory(name, nameBuf);
+ setValue(ret+_gethostbyname.hostent_layout.h_name, nameBuf, 'i8*');
+ var aliasesBuf = _malloc(4);
+ setValue(aliasesBuf, 0, 'i8*');
+ setValue(ret+_gethostbyname.hostent_layout.h_aliases, aliasesBuf, 'i8**');
+ setValue(ret+_gethostbyname.hostent_layout.h_addrtype, {{{ cDefine("AF_INET") }}}, 'i32');
+ setValue(ret+_gethostbyname.hostent_layout.h_length, 4, 'i32');
+ var addrListBuf = _malloc(12);
+ setValue(addrListBuf, addrListBuf+8, 'i32*');
+ setValue(addrListBuf+4, 0, 'i32*');
+ setValue(addrListBuf+8, 172 | (29 << 8) | ((id & 0xff) << 16) | ((id & 0xff00) << 24), 'i32');
+ setValue(ret+_gethostbyname.hostent_layout.h_addr_list, addrListBuf, 'i8**');
+ return ret;
+ },
+
+ // ==========================================================================
// sockets
// ==========================================================================
diff --git a/tests/runner.py b/tests/runner.py
index c21844d4..2fe41a84 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -5079,6 +5079,48 @@ def process(filename):
'''
self.do_run(src, '*d4c3b2a1,e07235fe,f0cdab07,cdab,34122143,afbe*\n4e0ab4be\n')
+ def test_gethostbyname(self):
+ src = r'''
+ #include <netdb.h>
+ #include <stdio.h>
+
+ void test(char *hostname) {
+ hostent *host = gethostbyname(hostname);
+ if (!host) {
+ printf("no such thing\n");
+ return;
+ }
+ printf("%s : %d : %d\n", host->h_name, host->h_addrtype, host->h_length);
+ char **name = host->h_aliases;
+ while (*name) {
+ printf("- %s\n", *name);
+ name++;
+ }
+ name = host->h_addr_list;
+ while (name && *name) {
+ printf("* ");
+ for (int i = 0; i < host->h_length; i++)
+ printf("%d.", (*name)[i]);
+ printf("\n");
+ name++;
+ }
+ }
+
+ int main() {
+ test("www.cheezburger.com");
+ test("fail.on.this.never.work"); // we will "work" on this - because we are just making aliases of names to ips
+ test("localhost");
+ return 1;
+ }
+ '''
+ self.do_run(src, '''www.cheezburger.com : 1 : 4
+* -84.29.0.0.
+fail.on.this.never.work : 1 : 4
+* -84.29.1.0.
+localhost : 1 : 4
+* -84.29.2.0.
+''')
+
def test_ctype(self):
# The bit fiddling done by the macros using __ctype_b_loc requires this.
Settings.CORRECT_SIGNS = 1