From 0407a69d57a80b8fd3d91f1f7ee15f22f40dd22b Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Fri, 25 Jan 2013 15:19:27 +0100 Subject: io: don't do reverse dns lookup on connect/listen This basically reverts commit 62c631fd7d5d5c66694af9266688851ac442508a --- lem/io/server.c | 4 --- lem/io/tcp.c | 78 ++++++++++++++++--------------------------------------- test/httptest.lua | 49 ++++++++++++++++++---------------- 3 files changed, 49 insertions(+), 82 deletions(-) diff --git a/lem/io/server.c b/lem/io/server.c index 9af8cf1..35e0f10 100644 --- a/lem/io/server.c +++ b/lem/io/server.c @@ -16,10 +16,6 @@ * License along with LEM. If not, see . */ -#ifndef MAXPENDING -#define MAXPENDING 50 -#endif - static int server_closed(lua_State *T) { diff --git a/lem/io/tcp.c b/lem/io/tcp.c index ee4c601..14e0b82 100644 --- a/lem/io/tcp.c +++ b/lem/io/tcp.c @@ -16,14 +16,16 @@ * License along with LEM. If not, see . */ +#ifndef MAXPENDING +#define MAXPENDING 50 +#endif + struct tcp_getaddr { struct lem_async a; const char *node; const char *service; - struct addrinfo *result; int sock; int err; - uint16_t port; }; static const int tcp_famnumber[] = { AF_UNSPEC, AF_INET, AF_INET6 }; @@ -34,7 +36,7 @@ tcp_connect_work(struct lem_async *a) { struct tcp_getaddr *g = (struct tcp_getaddr *)a; struct addrinfo hints = { - .ai_flags = AI_CANONNAME, + .ai_flags = 0, .ai_family = tcp_famnumber[g->sock], .ai_socktype = SOCK_STREAM, .ai_protocol = IPPROTO_TCP, @@ -43,11 +45,12 @@ tcp_connect_work(struct lem_async *a) .ai_canonname = NULL, .ai_next = NULL }; + struct addrinfo *result; struct addrinfo *addr; int sock; /* lookup name */ - sock = getaddrinfo(g->node, g->service, &hints, &g->result); + sock = getaddrinfo(g->node, g->service, &hints, &result); if (sock) { g->sock = -1; g->err = sock; @@ -55,9 +58,7 @@ tcp_connect_work(struct lem_async *a) } /* try the addresses in the order returned */ - for (addr = g->result; addr; addr = addr->ai_next) { - uint16_t port; - + for (addr = result; addr; addr = addr->ai_next) { sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); @@ -70,12 +71,12 @@ tcp_connect_work(struct lem_async *a) g->sock = -2; g->err = err; - goto error; + goto out; } /* connect */ if (connect(sock, addr->ai_addr, addr->ai_addrlen)) { - (void)close(sock); + close(sock); continue; } @@ -87,20 +88,16 @@ tcp_connect_work(struct lem_async *a) } g->sock = sock; - if (addr->ai_family == AF_INET6) - port = ((struct sockaddr_in6 *)addr->ai_addr)->sin6_port; - else - port = ((struct sockaddr_in *)addr->ai_addr)->sin_port; - g->port = ntohs(port); - return; + goto out; } g->sock = -4; + goto out; error: - freeaddrinfo(g->result); - if (sock >= 0) - close(sock); + close(sock); +out: + freeaddrinfo(result); } static void @@ -112,20 +109,10 @@ tcp_connect_reap(struct lem_async *a) lem_debug("connection established"); if (sock >= 0) { - struct addrinfo *result = g->result; - const char *name = result->ai_canonname; - uint16_t port = g->port; - - if (name == NULL) - name = g->node; - free(g); stream_new(T, sock, 3); - lua_pushstring(T, name); - lua_pushnumber(T, port); - freeaddrinfo(result); - lem_queue(T, 3); + lem_queue(T, 1); return; } @@ -148,8 +135,8 @@ tcp_connect_reap(struct lem_async *a) g->node, g->service); break; } - lem_queue(T, 2); free(g); + lem_queue(T, 2); } static int @@ -188,10 +175,6 @@ tcp_listen_work(struct lem_async *a) struct addrinfo *addr = NULL; int sock = -1; int ret; - uint16_t port; - - if (g->node != NULL) - hints.ai_flags |= AI_CANONNAME; /* lookup name */ ret = getaddrinfo(g->node, g->service, &hints, &addr); @@ -207,7 +190,7 @@ tcp_listen_work(struct lem_async *a) if (sock < 0) { g->sock = -2; g->err = errno; - goto error; + goto out; } /* set SO_REUSEADDR option if possible */ @@ -240,18 +223,12 @@ tcp_listen_work(struct lem_async *a) } g->sock = sock; - g->result = addr; - if (addr->ai_family == AF_INET6) - port = ((struct sockaddr_in6 *)addr->ai_addr)->sin6_port; - else - port = ((struct sockaddr_in *)addr->ai_addr)->sin_port; - g->port = ntohs(port); - return; + goto out; error: + close(sock); +out: freeaddrinfo(addr); - if (sock >= 0) - close(sock); } static void @@ -265,14 +242,8 @@ tcp_listen_reap(struct lem_async *a) g->node = "*"; if (sock >= 0) { - struct addrinfo *result = g->result; - const char *name = result->ai_canonname; - uint16_t port = g->port; struct ev_io *w; - if (name == NULL) - name = g->node; - free(g); /* create userdata and set the metatable */ @@ -284,10 +255,7 @@ tcp_listen_reap(struct lem_async *a) ev_io_init(w, NULL, sock, EV_READ); w->data = NULL; - lua_pushstring(T, name); - lua_pushnumber(T, port); - freeaddrinfo(result); - lem_queue(T, 3); + lem_queue(T, 1); return; } @@ -315,8 +283,8 @@ tcp_listen_reap(struct lem_async *a) strerror(g->err)); break; } - lem_queue(T, 2); free(g); + lem_queue(T, 2); } static int diff --git a/test/httptest.lua b/test/httptest.lua index 479c286..7e38533 100755 --- a/test/httptest.lua +++ b/test/httptest.lua @@ -29,40 +29,43 @@ local function printf(...) return write(format(...)) end -local domain, port = 'www.google.com', 'http' ---local domain, port = 'localhost', '8080' -local done = false +local domain, port = arg[1] or 'www.google.com', 'http' +local running = 0 -utils.spawn(function() - local conn, name, port = assert(io.tcp.connect(domain, port)) +local function get(n, close) + running = running + 1 - printf('\nConnected to %s:%u\n', name, port) + local req + if close then + req = 'GET / HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n' + else + req = 'GET / HTTP/1.1\r\nHost: %s\r\n\r\n' + end - for i = 1, 2 do - --assert(conn:write('GET / HTTP/1.1\r\nHost: '..domain..'\r\nConnection: close\r\n\r\n')) - assert(conn:write('GET / HTTP/1.1\r\nHost: '..domain..'\r\n\r\n')) + local conn = assert(io.tcp.connect(domain, port)) + assert(conn:write(req:format(domain))) + local res = assert(conn:read('HTTPResponse')) - local res = assert(conn:read('HTTPResponse')) + printf('\n%d: HTTP/%s %d %s\n', n, res.version, res.status, res.text) + for k, v in pairs(res.headers) do + printf('%d: %s: %s\n', n, k, v) + end - printf('\nHTTP/%s %d %s\n', res.version, res.status, res.text) - for k, v in pairs(res.headers) do - printf('%s: %s\n', k, v) - end + local body = assert(res:body()) + printf('\n%d: #body = %d\n', n, #body) - local body = assert(res:body()) - printf('\n#body = %d\n', #body) - --write(body, '\n') - end + conn:close() + running = running - 1 +end - done = true -end) +for i = 1, 2 do + utils.spawn(get, i, (i % 2) == 0) +end -local yield = utils.yield local sleeper = utils.newsleeper() repeat write('.') - --yield() sleeper:sleep(0.001) -until done +until running == 0 -- vim: set ts=2 sw=2 noet: -- cgit v1.2.1