diff src/core/ngx_inet.c @ 252:644510700914 NGINX_0_4_11

nginx 0.4.11 *) Feature: the POP3 proxy supports the AUTH LOGIN PLAIN and CRAM-MD5. *) Feature: the ngx_http_perl_module supports the $r->allow_ranges method. *) Bugfix: if the APOP was enabled in the POP3 proxy, then the USER/PASS commands might not work; bug appeared in 0.4.10.
author Igor Sysoev <http://sysoev.ru>
date Wed, 25 Oct 2006 00:00:00 +0400
parents c982febb7588
children 0effe91f6083
line wrap: on
line diff
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -223,10 +223,11 @@ ngx_ptocidr(ngx_str_t *text, void *cidr)
 ngx_int_t
 ngx_parse_url(ngx_conf_t *cf, ngx_url_t *u)
 {
-    u_char              *p;
+    u_char              *p, *host;
     size_t               len;
     ngx_int_t            port;
     ngx_uint_t           i;
+    struct hostent      *h;
 #if (NGX_HAVE_UNIX_DOMAIN)
     struct sockaddr_un  *saun;
 #endif
@@ -390,6 +391,47 @@ ngx_parse_url(ngx_conf_t *cf, ngx_url_t 
 port:
 
     if (u->listen) {
+        if (u->portn == 0) {
+            if (u->default_portn == 0) {
+                u->err = "no port";
+                return NGX_ERROR;
+            }
+
+            u->portn = u->default_portn;
+        }
+
+        if (u->host.len == 1 && u->host.data[0] == '*') {
+            u->host.len = 0;
+        }
+
+        /* AF_INET only */
+
+        if (u->host.len) {
+
+           host = ngx_palloc(cf->temp_pool, u->host.len + 1);
+           if (host == NULL) {
+               return NGX_ERROR;
+           }
+
+           (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
+
+            u->addr.in_addr = inet_addr((const char *) host);
+
+            if (u->addr.in_addr == INADDR_NONE) {
+                h = gethostbyname((const char *) host);
+
+                if (h == NULL || h->h_addr_list[0] == NULL) {
+                    u->err = "host not found";
+                    return NGX_ERROR;
+                }
+
+                u->addr.in_addr = *(in_addr_t *)(h->h_addr_list[0]);
+            }
+
+        } else {
+            u->addr.in_addr = INADDR_ANY;
+        }
+
         return NGX_OK;
     }
 
@@ -558,275 +600,3 @@ ngx_inet_resolve_peer(ngx_conf_t *cf, ng
 
     return peers;
 }
-
-
-ngx_peers_t *
-ngx_inet_upstream_parse(ngx_conf_t *cf, ngx_inet_upstream_t *u)
-{
-    char                *err;
-    u_char              *host;
-    size_t               len;
-    in_addr_t            in_addr;
-    ngx_uint_t           i;
-    ngx_peers_t         *peers;
-    struct hostent      *h;
-    struct sockaddr_in  *sin;
-
-    err = ngx_inet_parse_host_port(u);
-
-    if (err) {
-        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                           "%s in upstream \"%V\"", err, &u->name);
-        return NULL;
-    }
-
-    if (u->default_port) {
-
-        if (u->default_port_value == 0) {
-            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                               "no port in upstream \"%V\"", &u->name);
-            return NULL;
-        }
-
-        u->port = u->default_port_value;
-
-        u->port_text.data = ngx_palloc(cf->pool, sizeof("65536") - 1);
-        if (u->port_text.data == NULL) {
-            return NULL;
-        }
-
-        u->port_text.len = ngx_sprintf(u->port_text.data, "%d",
-                                       u->default_port_value)
-                           - u->port_text.data;
-
-    } else if (u->port) {
-        if (u->port == u->default_port_value) {
-            u->default_port = 1;
-        }
-
-    } else {
-        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                           "no port in upstream \"%V\"", &u->name);
-        return NULL;
-    }
-
-    if (u->host.len == 0) {
-        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                           "no host in upstream \"%V\"", &u->name);
-        return NULL;
-    }
-
-    u->port = htons(u->port);
-
-    host = ngx_palloc(cf->temp_pool, u->host.len + 1);
-    if (host == NULL) {
-        return NULL;
-    }
-
-    (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
-
-    /* AF_INET only */
-
-    in_addr = inet_addr((char *) host);
-
-    if (in_addr == INADDR_NONE) {
-        h = gethostbyname((char *) host);
-
-        if (h == NULL || h->h_addr_list[0] == NULL) {
-            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                               "host %s is not found in upstream \"%V\"",
-                               host, &u->name);
-            return NULL;
-        }
-
-        for (i = 0; h->h_addr_list[i] != NULL; i++) { /* void */ }
-
-        /* MP: ngx_shared_palloc() */
-
-        peers = ngx_pcalloc(cf->pool,
-                            sizeof(ngx_peers_t) + sizeof(ngx_peer_t) * (i - 1));
-        if (peers == NULL) {
-            return NULL;
-        }
-
-        peers->number = i;
-
-        for (i = 0; h->h_addr_list[i] != NULL; i++) {
-
-            sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in));
-            if (sin == NULL) {
-                return NULL;
-            }
-
-            sin->sin_family = AF_INET;
-            sin->sin_port = u->port;
-            sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
-
-            peers->peer[i].sockaddr = (struct sockaddr *) sin;
-            peers->peer[i].socklen = sizeof(struct sockaddr_in);
-
-            len = INET_ADDRSTRLEN - 1 + 1 + u->port_text.len;
-
-            peers->peer[i].name.data = ngx_palloc(cf->pool, len);
-            if (peers->peer[i].name.data == NULL) {
-                return NULL;
-            }
-
-            len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin,
-                                peers->peer[i].name.data, len);
-
-            peers->peer[i].name.data[len++] = ':';
-
-            ngx_memcpy(peers->peer[i].name.data + len,
-                       u->port_text.data, u->port_text.len);
-
-            peers->peer[i].name.len = len + u->port_text.len;
-
-            peers->peer[i].uri_separator = "";
-
-            peers->peer[i].weight = NGX_CONF_UNSET_UINT;
-            peers->peer[i].max_fails = NGX_CONF_UNSET_UINT;
-            peers->peer[i].fail_timeout = NGX_CONF_UNSET;
-        }
-
-    } else {
-
-        /* MP: ngx_shared_palloc() */
-
-        peers = ngx_pcalloc(cf->pool, sizeof(ngx_peers_t));
-        if (peers == NULL) {
-            return NULL;
-        }
-
-        sin = ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in));
-        if (sin == NULL) {
-            return NULL;
-        }
-
-        peers->number = 1;
-
-        sin->sin_family = AF_INET;
-        sin->sin_port = u->port;
-        sin->sin_addr.s_addr = in_addr;
-
-        peers->peer[0].sockaddr = (struct sockaddr *) sin;
-        peers->peer[0].socklen = sizeof(struct sockaddr_in);
-
-        len = u->host.len + 1 + u->port_text.len;
-
-        peers->peer[0].name.len = len;
-
-        peers->peer[0].name.data = ngx_palloc(cf->pool, len);
-        if (peers->peer[0].name.data == NULL) {
-            return NULL;
-        }
-
-        len = u->host.len;
-
-        ngx_memcpy(peers->peer[0].name.data, u->host.data, len);
-
-        peers->peer[0].name.data[len++] = ':';
-
-        ngx_memcpy(peers->peer[0].name.data + len,
-                   u->port_text.data, u->port_text.len);
-
-        peers->peer[0].uri_separator = "";
-    }
-
-    return peers;
-}
-
-
-char *
-ngx_inet_parse_host_port(ngx_inet_upstream_t *u)
-{
-    size_t      i;
-    ngx_int_t   port;
-    ngx_str_t  *url;
-
-    url = &u->url;
-
-    if (u->port_only) {
-        i = 0;
-
-    } else {
-        if (url->data[0] == ':' || url->data[0] == '/') {
-            return "invalid host";
-        }
-
-        i = 1;
-    }
-
-    u->host.data = url->data;
-    u->host_header = *url;
-
-    for ( /* void */ ; i < url->len; i++) {
-
-        if (url->data[i] == ':') {
-            u->port_text.data = &url->data[i] + 1;
-            u->host.len = i;
-
-            if (!u->uri_part) {
-                u->port_text.len = &url->data[url->len] - u->port_text.data;
-                break;
-            }
-        }
-
-        if (url->data[i] == '/') {
-            u->uri.data = &url->data[i];
-            u->uri.len = url->len - i;
-            u->host_header.len = i;
-
-            if (u->host.len == 0) {
-                u->host.len = i;
-            }
-
-            if (u->port_text.data == NULL) {
-                u->default_port = 1;
-                return NULL;
-            }
-
-            u->port_text.len = &url->data[i] - u->port_text.data;
-
-            if (u->port_text.len == 0) {
-                return "invalid port";
-            }
-
-            break;
-        }
-    }
-
-    if (u->port_text.data) {
-
-        if (u->port_text.len == 0) {
-            u->port_text.len = &url->data[i] - u->port_text.data;
-
-            if (u->port_text.len == 0) {
-                return "invalid port";
-            }
-        }
-
-        port = ngx_atoi(u->port_text.data, u->port_text.len);
-
-        if (port == NGX_ERROR || port < 1 || port > 65536) {
-            return "invalid port";
-        }
-
-    } else {
-        port = ngx_atoi(url->data, url->len);
-
-        if (port == NGX_ERROR) {
-            u->default_port = 1;
-            u->host.len = url->len;
-
-            return NULL;
-        }
-
-        u->port_text = *url;
-        u->wildcard = 1;
-    }
-
-    u->port = (in_port_t) port;
-
-    return NULL;
-}