From 95631700f77ba7fbd426b5ce8ac0c4e8933c3e94 Mon Sep 17 00:00:00 2001 From: hniksic Date: Sun, 25 Nov 2001 09:13:20 -0800 Subject: [PATCH] [svn] Remove host canonicalization code. Published in . --- src/ChangeLog | 18 ++++++++ src/connect.c | 16 +------ src/host.c | 125 +++++++++++++++----------------------------------- src/host.h | 8 ++-- src/http.c | 14 +++--- src/init.c | 2 +- src/main.c | 13 ------ 7 files changed, 68 insertions(+), 128 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 032eace7..3cf327fe 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,21 @@ +2001-11-25 Hrvoje Niksic + + * main.c (private_initialize): Removed. + (main): Don't call private_initialize. + + * http.c: Call lookup_host. + + * host.c (host_init): Removed. + (add_host_to_cache): Initialize host_name_address_map here, on + demand. + (ngethostbyname): Commented out. + + * connect.c (make_connection): Remove dead code; use lookup_host. + + * host.c (store_hostaddress): Renamed to lookup_host and reversed + the args. + Removed host_address_name_map and host_slave_master_map. + 2001-11-25 Hrvoje Niksic * progress.c (dot_create): Align the "[ skipping ... ]" string diff --git a/src/connect.c b/src/connect.c index 3e676b42..aa3153ed 100644 --- a/src/connect.c +++ b/src/connect.c @@ -65,21 +65,9 @@ uerr_t make_connection (int *sock, char *hostname, unsigned short port) { struct sockaddr_in sock_name; - /* struct hostent *hptr; */ - - /* Get internet address of the host. We can do it either by calling - ngethostbyname, or by calling store_hostaddress, from host.c. - storehostaddress is better since it caches calls to - gethostbyname. */ -#if 1 - if (!store_hostaddress ((unsigned char *)&sock_name.sin_addr, hostname)) - return HOSTERR; -#else /* never */ - if (!(hptr = ngethostbyname (hostname))) + + if (!lookup_host (hostname, (unsigned char *)&sock_name.sin_addr)) return HOSTERR; - /* Copy the address of the host to socket description. */ - memcpy (&sock_name.sin_addr, hptr->h_addr, hptr->h_length); -#endif /* never */ /* Set port and protocol */ sock_name.sin_family = AF_INET; diff --git a/src/host.c b/src/host.c index 4fb1197b..2c0ef425 100644 --- a/src/host.c +++ b/src/host.c @@ -64,17 +64,10 @@ extern int errno; /* #### We should map to *lists* of IP addresses. */ struct hash_table *host_name_address_map; + +#if 0 -/* The following two tables are obsolete, since we no longer do host - canonicalization. */ - -/* Mapping between all known addresses (n.n.n.n) to their hosts. This - is the inverse of host_name_address_map. These two tables share - the strdup'ed strings. */ -struct hash_table *host_address_name_map; - -/* Mapping between auxilliary (slave) and master host names. */ -struct hash_table *host_slave_master_map; +/* This function is no longer used. */ /* The same as gethostbyname, but supports internet addresses of the form `N.N.N.N'. On some systems gethostbyname() knows how to do @@ -92,38 +85,20 @@ ngethostbyname (const char *name) hp = gethostbyname (name); return hp; } +#endif -/* Add host name HOST with the address ADDR_TEXT to the cache. - Normally this means that the (HOST, ADDR_TEXT) pair will be to - host_name_address_map and to host_address_name_map. (It is the - caller's responsibility to make sure that HOST is not already in - host_name_address_map.) - - If the ADDR_TEXT has already been seen and belongs to another host, - HOST will be added to host_slave_master_map instead. */ +/* Add host name HOST with the address ADDR_TEXT to the cache. */ static void add_host_to_cache (const char *host, const char *addr_text) { - char *canonical_name = hash_table_get (host_address_name_map, addr_text); - if (canonical_name) - { - DEBUGP (("Mapping %s to %s in host_slave_master_map.\n", - host, canonical_name)); - /* We've already dealt with that host under another name. */ - hash_table_put (host_slave_master_map, - xstrdup_lower (host), - xstrdup_lower (canonical_name)); - } - else - { - /* This is really the first time we're dealing with that host. */ - char *h_copy = xstrdup_lower (host); - char *a_copy = xstrdup (addr_text); - DEBUGP (("Caching %s <-> %s\n", h_copy, a_copy)); - hash_table_put (host_name_address_map, h_copy, a_copy); - hash_table_put (host_address_name_map, a_copy, h_copy); - } + DEBUGP (("Caching %s => %s\n", host, addr_text)); + + if (!host_name_address_map) + host_name_address_map = make_nocase_string_hash_table (0); + + hash_table_put (host_name_address_map, + xstrdup_lower (host), xstrdup (addr_text)); } /* Store the address of HOSTNAME, internet-style (four octets in @@ -133,25 +108,24 @@ add_host_to_cache (const char *host, const char *addr_text) Return 1 on successful finding of the hostname, 0 otherwise. */ int -store_hostaddress (unsigned char *where, const char *hostname) +lookup_host (const char *hostname, unsigned char *store_ip) { unsigned long addr; - char *addr_text; - char *canonical_name; + char *addr_text = NULL; struct hostent *hptr; struct in_addr in; char *inet_s; - /* If the address is of the form d.d.d.d, there will be no trouble - with it. */ + /* If the address is of the form d.d.d.d, no further lookup is + needed. */ addr = (unsigned long)inet_addr (hostname); - /* If we have the numeric address, just store it. */ if ((int)addr != -1) { - /* ADDR is defined to be in network byte order, meaning the code - works on little and big endian 32-bit architectures without - change. On big endian 64-bit architectures we need to be - careful to copy the correct four bytes. */ + /* ADDR is defined to be in network byte order, which is what + this returns, so we can just copy it to STORE_IP. However, + on big endian 64-bit architectures the value will be stored + in the *last*, not first four bytes. OFFSET makes sure that + we copy the correct four bytes. */ int offset; have_addr: #ifdef WORDS_BIGENDIAN @@ -159,13 +133,15 @@ store_hostaddress (unsigned char *where, const char *hostname) #else offset = 0; #endif - memcpy (where, (char *)&addr + offset, 4); + memcpy (store_ip, (char *)&addr + offset, 4); return 1; } - /* By now we know that the address is not of the form d.d.d.d. Try - to find it in our cache of host addresses. */ - addr_text = hash_table_get (host_name_address_map, hostname); + /* By now we know that the host name we got is not of the form + d.d.d.d. Try to find it in our cache of host names. */ + if (host_name_address_map) + addr_text = hash_table_get (host_name_address_map, hostname); + if (addr_text) { DEBUGP (("Found %s in host_name_address_map: %s\n", @@ -174,39 +150,26 @@ store_hostaddress (unsigned char *where, const char *hostname) goto have_addr; } - /* Maybe this host is known to us under another name. If so, we'll - find it in host_slave_master_map, and use the master name to find - its address in host_name_address_map. */ - canonical_name = hash_table_get (host_slave_master_map, hostname); - if (canonical_name) - { - addr_text = hash_table_get (host_name_address_map, canonical_name); - assert (addr_text != NULL); - DEBUGP (("Found %s as slave of %s -> %s\n", - hostname, canonical_name, addr_text)); - addr = (unsigned long)inet_addr (addr_text); - goto have_addr; - } - - /* Since all else has failed, let's try gethostbyname(). Note that - we use gethostbyname() rather than ngethostbyname(), because we - already know that the address is not numerical. */ + /* Look up the host using gethostbyname(). Note that we use + gethostbyname() rather than ngethostbyname(), because we already + know that the address is not numerical. */ hptr = gethostbyname (hostname); if (!hptr) return 0; - /* Copy the address of the host to socket description. */ - memcpy (where, hptr->h_addr_list[0], hptr->h_length); + + /* Store the IP address to the desired location. */ assert (hptr->h_length == 4); + memcpy (store_ip, hptr->h_addr_list[0], hptr->h_length); + + /* Now that we've successfully looked up the host, store this + information in the cache. */ - /* Now that we've gone through the truoble of calling - gethostbyname(), we can store this valuable information to the - cache. First, we have to look for it by address to know if it's - already in the cache by another name. */ /* Originally, we copied to in.s_addr, but it appears to be missing on some systems. */ memcpy (&in, *hptr->h_addr_list, sizeof (in)); inet_s = inet_ntoa (in); add_host_to_cache (hostname, inet_s); + return 1; } @@ -271,20 +234,6 @@ herrmsg (int error) void host_cleanup (void) { - /* host_name_address_map and host_address_name_map share the - strings. Because of that, calling free_keys_and_values once - suffices for both. */ free_keys_and_values (host_name_address_map); hash_table_destroy (host_name_address_map); - hash_table_destroy (host_address_name_map); - free_keys_and_values (host_slave_master_map); - hash_table_destroy (host_slave_master_map); -} - -void -host_init (void) -{ - host_name_address_map = make_string_hash_table (0); - host_address_name_map = make_string_hash_table (0); - host_slave_master_map = make_string_hash_table (0); } diff --git a/src/host.h b/src/host.h index 95566c58..5099a314 100644 --- a/src/host.h +++ b/src/host.h @@ -24,14 +24,12 @@ struct url; /* Function declarations */ -struct hostent *ngethostbyname PARAMS ((const char *)); -int store_hostaddress PARAMS ((unsigned char *, const char *)); - -void host_cleanup PARAMS ((void)); +int lookup_host PARAMS ((const char *, unsigned char *)); +char *herrmsg PARAMS ((int)); int accept_domain PARAMS ((struct url *)); int sufmatch PARAMS ((const char **, const char *)); -char *herrmsg PARAMS ((int)); +void host_cleanup PARAMS ((void)); #endif /* HOST_H */ diff --git a/src/http.c b/src/http.c index 80f5b631..3280555a 100644 --- a/src/http.c +++ b/src/http.c @@ -276,7 +276,7 @@ http_process_connection (const char *hdr, void *arg) /* Whether a persistent connection is active. */ static int pc_active_p; /* Host and port of currently active persistent connection. */ -static unsigned char pc_last_host[4]; +static unsigned char pc_last_host_ip[4]; static unsigned short pc_last_port; /* File descriptor of the currently active persistent connection. */ @@ -347,9 +347,9 @@ register_persistent (const char *host, unsigned short port, int fd } } - /* This store_hostaddress may not fail, because it has the results - in the cache. */ - success = store_hostaddress (pc_last_host, host); + /* This lookup_host cannot fail, because it has the results in the + cache. */ + success = lookup_host (host, pc_last_host_ip); assert (success); pc_last_port = port; pc_last_fd = fd; @@ -371,7 +371,7 @@ persistent_available_p (const char *host, unsigned short port #endif ) { - unsigned char this_host[4]; + unsigned char this_host_ip[4]; /* First, check whether a persistent connection is active at all. */ if (!pc_active_p) return 0; @@ -388,9 +388,9 @@ persistent_available_p (const char *host, unsigned short port if (ssl != pc_active_ssl) return 0; #endif /* HAVE_SSL */ - if (!store_hostaddress (this_host, host)) + if (!lookup_host (host, this_host_ip)) return 0; - if (memcmp (pc_last_host, this_host, 4)) + if (memcmp (pc_last_host_ip, this_host_ip, 4)) return 0; /* Third: check whether the connection is still open. This is important because most server implement a liberal (short) timeout diff --git a/src/init.c b/src/init.c index 83cf070d..f0a4f222 100644 --- a/src/init.c +++ b/src/init.c @@ -525,7 +525,7 @@ cmd_address (const char *com, const char *val, void *closure) memset (&sin, '\0', sizeof (sin)); - if (!store_hostaddress ((unsigned char *)&sin.sin_addr, val)) + if (!lookup_host (val, (unsigned char *)&sin.sin_addr)) { fprintf (stderr, _("%s: %s: Cannot convert `%s' to an IP address.\n"), exec_name, com, val); diff --git a/src/main.c b/src/main.c index 79e17af0..f5cf13c2 100644 --- a/src/main.c +++ b/src/main.c @@ -104,18 +104,6 @@ i18n_initialize (void) textdomain ("wget"); #endif /* HAVE_NLS */ } - -/* It's kosher to declare these here because their interface _has_ to - be void foo(void). */ -void host_init PARAMS ((void)); - -/* This just calls the various initialization functions from the - modules that need one-time initialization. */ -static void -private_initialize (void) -{ - host_init (); -} /* Print the usage message. */ static void @@ -335,7 +323,6 @@ main (int argc, char *const *argv) }; i18n_initialize (); - private_initialize (); append_to_log = 0; -- 2.39.2