|
|
8335b1 |
--- trunk/modules/metadata/mod_unique_id.c 2011/12/02 23:02:04 1209766
|
|
|
8335b1 |
+++ trunk/modules/metadata/mod_unique_id.c 2013/07/10 16:20:31 1501827
|
|
|
8335b1 |
@@ -31,14 +31,11 @@
|
|
|
8335b1 |
#include "http_log.h"
|
|
|
8335b1 |
#include "http_protocol.h" /* for ap_hook_post_read_request */
|
|
|
8335b1 |
|
|
|
8335b1 |
-#if APR_HAVE_UNISTD_H
|
|
|
8335b1 |
-#include <unistd.h> /* for getpid() */
|
|
|
8335b1 |
-#endif
|
|
|
8335b1 |
+#define ROOT_SIZE 10
|
|
|
8335b1 |
|
|
|
8335b1 |
typedef struct {
|
|
|
8335b1 |
unsigned int stamp;
|
|
|
8335b1 |
- unsigned int in_addr;
|
|
|
8335b1 |
- unsigned int pid;
|
|
|
8335b1 |
+ char root[ROOT_SIZE];
|
|
|
8335b1 |
unsigned short counter;
|
|
|
8335b1 |
unsigned int thread_index;
|
|
|
8335b1 |
} unique_id_rec;
|
|
|
8335b1 |
@@ -64,20 +61,15 @@
|
|
|
8335b1 |
* gethostbyname (gethostname()) is unique across all the machines at the
|
|
|
8335b1 |
* "site".
|
|
|
8335b1 |
*
|
|
|
8335b1 |
- * We also further assume that pids fit in 32-bits. If something uses more
|
|
|
8335b1 |
- * than 32-bits, the fix is trivial, but it requires the unrolled uuencoding
|
|
|
8335b1 |
- * loop to be extended. * A similar fix is needed to support multithreaded
|
|
|
8335b1 |
- * servers, using a pid/tid combo.
|
|
|
8335b1 |
- *
|
|
|
8335b1 |
- * Together, the in_addr and pid are assumed to absolutely uniquely identify
|
|
|
8335b1 |
- * this one child from all other currently running children on all servers
|
|
|
8335b1 |
- * (including this physical server if it is running multiple httpds) from each
|
|
|
8335b1 |
+ * The root is assumed to absolutely uniquely identify this one child
|
|
|
8335b1 |
+ * from all other currently running children on all servers (including
|
|
|
8335b1 |
+ * this physical server if it is running multiple httpds) from each
|
|
|
8335b1 |
* other.
|
|
|
8335b1 |
*
|
|
|
8335b1 |
- * The stamp and counter are used to distinguish all hits for a particular
|
|
|
8335b1 |
- * (in_addr,pid) pair. The stamp is updated using r->request_time,
|
|
|
8335b1 |
- * saving cpu cycles. The counter is never reset, and is used to permit up to
|
|
|
8335b1 |
- * 64k requests in a single second by a single child.
|
|
|
8335b1 |
+ * The stamp and counter are used to distinguish all hits for a
|
|
|
8335b1 |
+ * particular root. The stamp is updated using r->request_time,
|
|
|
8335b1 |
+ * saving cpu cycles. The counter is never reset, and is used to
|
|
|
8335b1 |
+ * permit up to 64k requests in a single second by a single child.
|
|
|
8335b1 |
*
|
|
|
8335b1 |
* The 144-bits of unique_id_rec are encoded using the alphabet
|
|
|
8335b1 |
* [A-Za-z0-9@-], resulting in 24 bytes of printable characters. That is then
|
|
|
8335b1 |
@@ -92,7 +84,7 @@
|
|
|
8335b1 |
* module change.
|
|
|
8335b1 |
*
|
|
|
8335b1 |
* It is highly desirable that identifiers exist for "eternity". But future
|
|
|
8335b1 |
- * needs (such as much faster webservers, moving to 64-bit pids, or moving to a
|
|
|
8335b1 |
+ * needs (such as much faster webservers, or moving to a
|
|
|
8335b1 |
* multithreaded server) may dictate a need to change the contents of
|
|
|
8335b1 |
* unique_id_rec. Such a future implementation should ensure that the first
|
|
|
8335b1 |
* field is still a time_t stamp. By doing that, it is possible for a site to
|
|
|
8335b1 |
@@ -100,7 +92,15 @@
|
|
|
8335b1 |
* wait one entire second, and then start all of their new-servers. This
|
|
|
8335b1 |
* procedure will ensure that the new space of identifiers is completely unique
|
|
|
8335b1 |
* from the old space. (Since the first four unencoded bytes always differ.)
|
|
|
8335b1 |
+ *
|
|
|
8335b1 |
+ * Note: previous implementations used 32-bits of IP address plus pid
|
|
|
8335b1 |
+ * in place of the PRNG output in the "root" field. This was
|
|
|
8335b1 |
+ * insufficient for IPv6-only hosts, required working DNS to determine
|
|
|
8335b1 |
+ * a unique IP address (fragile), and needed a [0, 1) second sleep
|
|
|
8335b1 |
+ * call at startup to avoid pid reuse. Use of the PRNG avoids all
|
|
|
8335b1 |
+ * these issues.
|
|
|
8335b1 |
*/
|
|
|
8335b1 |
+
|
|
|
8335b1 |
/*
|
|
|
8335b1 |
* Sun Jun 7 05:43:49 CEST 1998 -- Alvaro
|
|
|
8335b1 |
* More comments:
|
|
|
8335b1 |
@@ -116,8 +116,6 @@
|
|
|
8335b1 |
* htonl/ntohl. Well, this shouldn't be a problem till year 2106.
|
|
|
8335b1 |
*/
|
|
|
8335b1 |
|
|
|
8335b1 |
-static unsigned global_in_addr;
|
|
|
8335b1 |
-
|
|
|
8335b1 |
/*
|
|
|
8335b1 |
* XXX: We should have a per-thread counter and not use cur_unique_id.counter
|
|
|
8335b1 |
* XXX: in all threads, because this is bad for performance on multi-processor
|
|
|
8335b1 |
@@ -129,7 +127,7 @@
|
|
|
8335b1 |
/*
|
|
|
8335b1 |
* Number of elements in the structure unique_id_rec.
|
|
|
8335b1 |
*/
|
|
|
8335b1 |
-#define UNIQUE_ID_REC_MAX 5
|
|
|
8335b1 |
+#define UNIQUE_ID_REC_MAX 4
|
|
|
8335b1 |
|
|
|
8335b1 |
static unsigned short unique_id_rec_offset[UNIQUE_ID_REC_MAX],
|
|
|
8335b1 |
unique_id_rec_size[UNIQUE_ID_REC_MAX],
|
|
|
8335b1 |
@@ -138,113 +136,32 @@
|
|
|
8335b1 |
|
|
|
8335b1 |
static int unique_id_global_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_server)
|
|
|
8335b1 |
{
|
|
|
8335b1 |
- char str[APRMAXHOSTLEN + 1];
|
|
|
8335b1 |
- apr_status_t rv;
|
|
|
8335b1 |
- char *ipaddrstr;
|
|
|
8335b1 |
- apr_sockaddr_t *sockaddr;
|
|
|
8335b1 |
-
|
|
|
8335b1 |
/*
|
|
|
8335b1 |
* Calculate the sizes and offsets in cur_unique_id.
|
|
|
8335b1 |
*/
|
|
|
8335b1 |
unique_id_rec_offset[0] = APR_OFFSETOF(unique_id_rec, stamp);
|
|
|
8335b1 |
unique_id_rec_size[0] = sizeof(cur_unique_id.stamp);
|
|
|
8335b1 |
- unique_id_rec_offset[1] = APR_OFFSETOF(unique_id_rec, in_addr);
|
|
|
8335b1 |
- unique_id_rec_size[1] = sizeof(cur_unique_id.in_addr);
|
|
|
8335b1 |
- unique_id_rec_offset[2] = APR_OFFSETOF(unique_id_rec, pid);
|
|
|
8335b1 |
- unique_id_rec_size[2] = sizeof(cur_unique_id.pid);
|
|
|
8335b1 |
- unique_id_rec_offset[3] = APR_OFFSETOF(unique_id_rec, counter);
|
|
|
8335b1 |
- unique_id_rec_size[3] = sizeof(cur_unique_id.counter);
|
|
|
8335b1 |
- unique_id_rec_offset[4] = APR_OFFSETOF(unique_id_rec, thread_index);
|
|
|
8335b1 |
- unique_id_rec_size[4] = sizeof(cur_unique_id.thread_index);
|
|
|
8335b1 |
+ unique_id_rec_offset[1] = APR_OFFSETOF(unique_id_rec, root);
|
|
|
8335b1 |
+ unique_id_rec_size[1] = sizeof(cur_unique_id.root);
|
|
|
8335b1 |
+ unique_id_rec_offset[2] = APR_OFFSETOF(unique_id_rec, counter);
|
|
|
8335b1 |
+ unique_id_rec_size[2] = sizeof(cur_unique_id.counter);
|
|
|
8335b1 |
+ unique_id_rec_offset[3] = APR_OFFSETOF(unique_id_rec, thread_index);
|
|
|
8335b1 |
+ unique_id_rec_size[3] = sizeof(cur_unique_id.thread_index);
|
|
|
8335b1 |
unique_id_rec_total_size = unique_id_rec_size[0] + unique_id_rec_size[1] +
|
|
|
8335b1 |
- unique_id_rec_size[2] + unique_id_rec_size[3] +
|
|
|
8335b1 |
- unique_id_rec_size[4];
|
|
|
8335b1 |
+ unique_id_rec_size[2] + unique_id_rec_size[3];
|
|
|
8335b1 |
|
|
|
8335b1 |
/*
|
|
|
8335b1 |
* Calculate the size of the structure when encoded.
|
|
|
8335b1 |
*/
|
|
|
8335b1 |
unique_id_rec_size_uu = (unique_id_rec_total_size*8+5)/6;
|
|
|
8335b1 |
|
|
|
8335b1 |
- /*
|
|
|
8335b1 |
- * Now get the global in_addr. Note that it is not sufficient to use one
|
|
|
8335b1 |
- * of the addresses from the main_server, since those aren't as likely to
|
|
|
8335b1 |
- * be unique as the physical address of the machine
|
|
|
8335b1 |
- */
|
|
|
8335b1 |
- if ((rv = apr_gethostname(str, sizeof(str) - 1, p)) != APR_SUCCESS) {
|
|
|
8335b1 |
- ap_log_error(APLOG_MARK, APLOG_ALERT, rv, main_server, APLOGNO(01563)
|
|
|
8335b1 |
- "unable to find hostname of the server");
|
|
|
8335b1 |
- return HTTP_INTERNAL_SERVER_ERROR;
|
|
|
8335b1 |
- }
|
|
|
8335b1 |
-
|
|
|
8335b1 |
- if ((rv = apr_sockaddr_info_get(&sockaddr, str, AF_INET, 0, 0, p)) == APR_SUCCESS) {
|
|
|
8335b1 |
- global_in_addr = sockaddr->sa.sin.sin_addr.s_addr;
|
|
|
8335b1 |
- }
|
|
|
8335b1 |
- else {
|
|
|
8335b1 |
- ap_log_error(APLOG_MARK, APLOG_ALERT, rv, main_server, APLOGNO(01564)
|
|
|
8335b1 |
- "unable to find IPv4 address of \"%s\"", str);
|
|
|
8335b1 |
-#if APR_HAVE_IPV6
|
|
|
8335b1 |
- if ((rv = apr_sockaddr_info_get(&sockaddr, str, AF_INET6, 0, 0, p)) == APR_SUCCESS) {
|
|
|
8335b1 |
- memcpy(&global_in_addr,
|
|
|
8335b1 |
- (char *)sockaddr->ipaddr_ptr + sockaddr->ipaddr_len - sizeof(global_in_addr),
|
|
|
8335b1 |
- sizeof(global_in_addr));
|
|
|
8335b1 |
- ap_log_error(APLOG_MARK, APLOG_ALERT, rv, main_server, APLOGNO(01565)
|
|
|
8335b1 |
- "using low-order bits of IPv6 address "
|
|
|
8335b1 |
- "as if they were unique");
|
|
|
8335b1 |
- }
|
|
|
8335b1 |
- else
|
|
|
8335b1 |
-#endif
|
|
|
8335b1 |
- return HTTP_INTERNAL_SERVER_ERROR;
|
|
|
8335b1 |
- }
|
|
|
8335b1 |
-
|
|
|
8335b1 |
- apr_sockaddr_ip_get(&ipaddrstr, sockaddr);
|
|
|
8335b1 |
- ap_log_error(APLOG_MARK, APLOG_INFO, 0, main_server, APLOGNO(01566) "using ip addr %s",
|
|
|
8335b1 |
- ipaddrstr);
|
|
|
8335b1 |
-
|
|
|
8335b1 |
- /*
|
|
|
8335b1 |
- * If the server is pummelled with restart requests we could possibly end
|
|
|
8335b1 |
- * up in a situation where we're starting again during the same second
|
|
|
8335b1 |
- * that has been used in previous identifiers. Avoid that situation.
|
|
|
8335b1 |
- *
|
|
|
8335b1 |
- * In truth, for this to actually happen not only would it have to restart
|
|
|
8335b1 |
- * in the same second, but it would have to somehow get the same pids as
|
|
|
8335b1 |
- * one of the other servers that was running in that second. Which would
|
|
|
8335b1 |
- * mean a 64k wraparound on pids ... not very likely at all.
|
|
|
8335b1 |
- *
|
|
|
8335b1 |
- * But protecting against it is relatively cheap. We just sleep into the
|
|
|
8335b1 |
- * next second.
|
|
|
8335b1 |
- */
|
|
|
8335b1 |
- apr_sleep(apr_time_from_sec(1) - apr_time_usec(apr_time_now()));
|
|
|
8335b1 |
return OK;
|
|
|
8335b1 |
}
|
|
|
8335b1 |
|
|
|
8335b1 |
static void unique_id_child_init(apr_pool_t *p, server_rec *s)
|
|
|
8335b1 |
{
|
|
|
8335b1 |
- pid_t pid;
|
|
|
8335b1 |
-
|
|
|
8335b1 |
- /*
|
|
|
8335b1 |
- * Note that we use the pid because it's possible that on the same
|
|
|
8335b1 |
- * physical machine there are multiple servers (i.e. using Listen). But
|
|
|
8335b1 |
- * it's guaranteed that none of them will share the same pids between
|
|
|
8335b1 |
- * children.
|
|
|
8335b1 |
- *
|
|
|
8335b1 |
- * XXX: for multithread this needs to use a pid/tid combo and probably
|
|
|
8335b1 |
- * needs to be expanded to 32 bits
|
|
|
8335b1 |
- */
|
|
|
8335b1 |
- pid = getpid();
|
|
|
8335b1 |
- cur_unique_id.pid = pid;
|
|
|
8335b1 |
-
|
|
|
8335b1 |
- /*
|
|
|
8335b1 |
- * Test our assumption that the pid is 32-bits. It's possible that
|
|
|
8335b1 |
- * 64-bit machines will declare pid_t to be 64 bits but only use 32
|
|
|
8335b1 |
- * of them. It would have been really nice to test this during
|
|
|
8335b1 |
- * global_init ... but oh well.
|
|
|
8335b1 |
- */
|
|
|
8335b1 |
- if ((pid_t)cur_unique_id.pid != pid) {
|
|
|
8335b1 |
- ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, APLOGNO(01567)
|
|
|
8335b1 |
- "oh no! pids are greater than 32-bits! I'm broken!");
|
|
|
8335b1 |
- }
|
|
|
8335b1 |
-
|
|
|
8335b1 |
- cur_unique_id.in_addr = global_in_addr;
|
|
|
8335b1 |
+ ap_random_insecure_bytes(&cur_unique_id.root,
|
|
|
8335b1 |
+ sizeof(cur_unique_id.root));
|
|
|
8335b1 |
|
|
|
8335b1 |
/*
|
|
|
8335b1 |
* If we use 0 as the initial counter we have a little less protection
|
|
|
8335b1 |
@@ -253,13 +170,6 @@
|
|
|
8335b1 |
*/
|
|
|
8335b1 |
ap_random_insecure_bytes(&cur_unique_id.counter,
|
|
|
8335b1 |
sizeof(cur_unique_id.counter));
|
|
|
8335b1 |
-
|
|
|
8335b1 |
- /*
|
|
|
8335b1 |
- * We must always use network ordering for these bytes, so that
|
|
|
8335b1 |
- * identifiers are comparable between machines of different byte
|
|
|
8335b1 |
- * orderings. Note in_addr is already in network order.
|
|
|
8335b1 |
- */
|
|
|
8335b1 |
- cur_unique_id.pid = htonl(cur_unique_id.pid);
|
|
|
8335b1 |
}
|
|
|
8335b1 |
|
|
|
8335b1 |
/* NOTE: This is *NOT* the same encoding used by base64encode ... the last two
|
|
|
8335b1 |
@@ -291,10 +201,8 @@
|
|
|
8335b1 |
unsigned short counter;
|
|
|
8335b1 |
int i,j,k;
|
|
|
8335b1 |
|
|
|
8335b1 |
- new_unique_id.in_addr = cur_unique_id.in_addr;
|
|
|
8335b1 |
- new_unique_id.pid = cur_unique_id.pid;
|
|
|
8335b1 |
+ memcpy(&new_unique_id.root, &cur_unique_id.root, ROOT_SIZE);
|
|
|
8335b1 |
new_unique_id.counter = cur_unique_id.counter;
|
|
|
8335b1 |
-
|
|
|
8335b1 |
new_unique_id.stamp = htonl((unsigned int)apr_time_sec(r->request_time));
|
|
|
8335b1 |
new_unique_id.thread_index = htonl((unsigned int)r->connection->id);
|
|
|
8335b1 |
|