Permalink
Browse files

fixed incompatiblities in nginx 1.5.8 which breaks the resolver API i…

…n the nginx core.
  • Loading branch information...
1 parent 5a4199b commit f364c82039d8d76efa0767a7fd909935d5c40a65 @agentzh agentzh committed Dec 18, 2013
Showing with 142 additions and 20 deletions.
  1. +71 −10 src/ngx_http_lua_socket_tcp.c
  2. +71 −10 src/ngx_http_lua_socket_udp.c
@@ -555,7 +555,9 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
}
rctx->name = host;
+#if !defined(nginx_version) || nginx_version < 1005008
rctx->type = NGX_RESOLVE_A;
+#endif
rctx->handler = ngx_http_lua_socket_resolve_handler;
rctx->data = u;
rctx->timeout = clcf->resolver_timeout;
@@ -623,7 +625,12 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
ngx_http_lua_socket_tcp_upstream_t *u;
u_char *p;
size_t len;
+#if defined(nginx_version) && nginx_version >= 1005008
+ socklen_t socklen;
+ struct sockaddr *sockaddr;
+#else
struct sockaddr_in *sin;
+#endif
ngx_uint_t i;
unsigned waiting;
@@ -676,9 +683,25 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
#if (NGX_DEBUG)
{
+# if defined(nginx_version) && nginx_version >= 1005008
+ u_char text[NGX_SOCKADDR_STRLEN];
+ ngx_str_t addr;
+# else
in_addr_t addr;
+# endif
ngx_uint_t i;
+# if defined(nginx_version) && nginx_version >= 1005008
+ addr.data = text;
+
+ for (i = 0; i < ctx->naddrs; i++) {
+ addr.len = ngx_sock_ntop(ur->addrs[i].sockaddr, ur->addrs[i].socklen,
+ text, NGX_SOCKADDR_STRLEN, 0);
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "name was resolved to %V", &addr);
+ }
+# else
for (i = 0; i < ctx->naddrs; i++) {
dd("addr i: %d %p", (int) i, &ctx->addrs[i]);
@@ -689,6 +712,7 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
(addr >> 24) & 0xff, (addr >> 16) & 0xff,
(addr >> 8) & 0xff, addr & 0xff);
}
+# endif
}
#endif
@@ -715,21 +739,43 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
dd("selected addr index: %d", (int) i);
- len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
+#if defined(nginx_version) && nginx_version >= 1005008
+ socklen = ur->addrs[i].socklen;
- p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
+ sockaddr = ngx_palloc(r->pool, socklen);
+ if (sockaddr == NULL) {
+ goto nomem;
+ }
+
+ ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
+
+ switch (sockaddr->sa_family) {
+#if (NGX_HAVE_INET6)
+ case AF_INET6:
+ ((struct sockaddr_in6 *) sockaddr)->sin6_port = htons(ur->port);
+ break;
+#endif
+ default: /* AF_INET */
+ ((struct sockaddr_in *) sockaddr)->sin_port = htons(ur->port);
+ }
+
+ p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
if (p == NULL) {
- ngx_resolve_name_done(ctx);
- u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
+ goto nomem;
+ }
- lua_pushnil(L);
- lua_pushliteral(L, "out of memory");
+ len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
+ ur->sockaddr = sockaddr;
+ ur->socklen = socklen;
- if (waiting) {
- ngx_http_run_posted_requests(c);
- }
+#else
+ /* for nginx older than 1.5.8 */
- return;
+ len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
+
+ p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
+ if (p == NULL) {
+ goto nomem;
}
sin = (struct sockaddr_in *) &p[len];
@@ -744,6 +790,7 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
ur->sockaddr = (struct sockaddr *) sin;
ur->socklen = sizeof(struct sockaddr_in);
+#endif
ur->host.data = p;
ur->host.len = len;
@@ -763,6 +810,20 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
} else {
(void) ngx_http_lua_socket_resolve_retval_handler(r, u, L);
}
+
+ return;
+
+nomem:
+
+ ngx_resolve_name_done(ctx);
+ u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_NOMEM;
+
+ lua_pushnil(L);
+ lua_pushliteral(L, "no memory");
+
+ if (waiting) {
+ ngx_http_run_posted_requests(c);
+ }
}
@@ -349,7 +349,9 @@ ngx_http_lua_socket_udp_setpeername(lua_State *L)
}
rctx->name = host;
+#if !defined(nginx_version) || nginx_version < 1005008
rctx->type = NGX_RESOLVE_A;
+#endif
rctx->handler = ngx_http_lua_socket_resolve_handler;
rctx->data = u;
rctx->timeout = clcf->resolver_timeout;
@@ -416,7 +418,12 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
ngx_http_lua_socket_udp_upstream_t *u;
u_char *p;
size_t len;
+#if defined(nginx_version) && nginx_version >= 1005008
+ socklen_t socklen;
+ struct sockaddr *sockaddr;
+#else
struct sockaddr_in *sin;
+#endif
ngx_uint_t i;
unsigned waiting;
@@ -476,9 +483,25 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
#if (NGX_DEBUG)
{
+# if defined(nginx_version) && nginx_version >= 1005008
+ u_char text[NGX_SOCKADDR_STRLEN];
+ ngx_str_t addr;
+# else
in_addr_t addr;
+# endif
ngx_uint_t i;
+# if defined(nginx_version) && nginx_version >= 1005008
+ addr.data = text;
+
+ for (i = 0; i < ctx->naddrs; i++) {
+ addr.len = ngx_sock_ntop(ur->addrs[i].sockaddr, ur->addrs[i].socklen,
+ text, NGX_SOCKADDR_STRLEN, 0);
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "name was resolved to %V", &addr);
+ }
+# else
for (i = 0; i < ctx->naddrs; i++) {
dd("addr i: %d %p", (int) i, &ctx->addrs[i]);
@@ -489,6 +512,7 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
(addr >> 24) & 0xff, (addr >> 16) & 0xff,
(addr >> 8) & 0xff, addr & 0xff);
}
+# endif
}
#endif
@@ -515,21 +539,43 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
dd("selected addr index: %d", (int) i);
- len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
+#if defined(nginx_version) && nginx_version >= 1005008
+ socklen = ur->addrs[i].socklen;
- p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
+ sockaddr = ngx_palloc(r->pool, socklen);
+ if (sockaddr == NULL) {
+ goto nomem;
+ }
+
+ ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
+
+ switch (sockaddr->sa_family) {
+#if (NGX_HAVE_INET6)
+ case AF_INET6:
+ ((struct sockaddr_in6 *) sockaddr)->sin6_port = htons(ur->port);
+ break;
+#endif
+ default: /* AF_INET */
+ ((struct sockaddr_in *) sockaddr)->sin_port = htons(ur->port);
+ }
+
+ p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
if (p == NULL) {
- ngx_resolve_name_done(ctx);
- u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
+ goto nomem;
+ }
- lua_pushnil(L);
- lua_pushliteral(L, "out of memory");
+ len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
+ ur->sockaddr = sockaddr;
+ ur->socklen = socklen;
- if (waiting) {
- ngx_http_run_posted_requests(c);
- }
+#else
+ /* for nginx older than 1.5.8 */
- return;
+ len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
+
+ p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
+ if (p == NULL) {
+ goto nomem;
}
sin = (struct sockaddr_in *) &p[len];
@@ -544,6 +590,7 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
ur->sockaddr = (struct sockaddr *) sin;
ur->socklen = sizeof(struct sockaddr_in);
+#endif
ur->host.data = p;
ur->host.len = len;
@@ -563,6 +610,20 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
} else {
(void) ngx_http_lua_socket_resolve_retval_handler(r, u, L);
}
+
+ return;
+
+nomem:
+
+ ngx_resolve_name_done(ctx);
+ u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_NOMEM;
+
+ lua_pushnil(L);
+ lua_pushliteral(L, "no memory");
+
+ if (waiting) {
+ ngx_http_run_posted_requests(c);
+ }
}

0 comments on commit f364c82

Please sign in to comment.