📄 mod_unique_id.c
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * mod_unique_id.c: generate a unique identifier for each request * * Original author: Dean Gaudet <dgaudet@arctic.org> * UUencoding modified by: Alvaro Martinez Echevarria <alvaro@lander.es> */#include "httpd.h"#include "http_config.h"#include "http_log.h"#include "multithread.h"typedef struct { unsigned int stamp; unsigned int in_addr; unsigned int pid;#ifdef MULTITHREAD unsigned int tid;#endif unsigned short counter;} unique_id_rec;/* Comments: * * We want an identifier which is unique across all hits, everywhere. * "everywhere" includes multiple httpd instances on the same machine, or on * multiple machines. Essentially "everywhere" should include all possible * httpds across all servers at a particular "site". We make some assumptions * that if the site has a cluster of machines then their time is relatively * synchronized. We also assume that the first address returned by a * gethostbyname (gethostname()) is unique across all the machines at the * "site". * * We also further assume that pids fit in 32-bits. If something uses more * than 32-bits, the fix is trivial, but it requires the unrolled uuencoding * loop to be extended. * * Together, the in_addr and pid are assumed to absolutely uniquely identify * this one child from all other currently running children on all servers * (including this physical server if it is running multiple httpds) from each * other. * * The stamp and counter are used to distinguish all hits for a particular * (in_addr,pid) pair. The stamp is updated using r->request_time, * saving cpu cycles. The counter is never reset, and is used to permit up to * 64k requests in a single second by a single child. * * The 112-bits of unique_id_rec are encoded using the alphabet * [A-Za-z0-9@-], resulting in 19 bytes of printable characters. That is then * stuffed into the environment variable UNIQUE_ID so that it is available to * other modules. The alphabet choice differs from normal base64 encoding * [A-Za-z0-9+/] because + and / are special characters in URLs and we want to * make it easy to use UNIQUE_ID in URLs. * * Note that UNIQUE_ID should be considered an opaque token by other * applications. No attempt should be made to dissect its internal components. * It is an abstraction that may change in the future as the needs of this * module change. * * It is highly desirable that identifiers exist for "eternity". But future * needs (such as much faster webservers, moving to 64-bit pids, or moving to a * multithreaded server) may dictate a need to change the contents of * unique_id_rec. Such a future implementation should ensure that the first * field is still a time_t stamp. By doing that, it is possible for a site to * have a "flag second" in which they stop all of their old-format servers, * wait one entire second, and then start all of their new-servers. This * procedure will ensure that the new space of identifiers is completely unique * from the old space. (Since the first four unencoded bytes always differ.) *//* * Sun Jun 7 05:43:49 CEST 1998 -- Alvaro * More comments: * 1) The UUencoding prodecure is now done in a general way, avoiding * the problems with sizes and paddings that can arise depending on * the architecture. Now the offsets and sizes of the elements of the * unique_id_rec structure are calculated in unique_id_global_init; * and then used to duplicate the structure without the paddings that * might exist. The multithreaded server fix should be now very easy: * just add a new "tid" field to the unique_id_rec structure, and * increase by one UNIQUE_ID_REC_MAX. * 2) unique_id_rec.stamp has been changed from "time_t" to * "unsigned int", because its size is 64bits on some platforms * (linux/alpha), and this caused problems with htonl/ntohl. Well, * this shouldn't be a problem till year 2106. */static unsigned global_in_addr;#ifdef WIN32static DWORD tls_index;BOOL WINAPI DllMain (HINSTANCE dllhandle, DWORD reason, LPVOID reserved){ LPVOID memptr; switch (reason) { case DLL_PROCESS_ATTACH: tls_index = TlsAlloc(); case DLL_THREAD_ATTACH: /* intentional no break */ TlsSetValue(tls_index, calloc(sizeof(unique_id_rec), 1)); break; case DLL_THREAD_DETACH: memptr = TlsGetValue(tls_index); if (memptr) { free (memptr); TlsSetValue (tls_index, 0); } break; } return TRUE;}static unique_id_rec* get_cur_unique_id(int parent){ /* Apache initializes the child process, not the individual child threads. * Copy the original parent record if this->pid is not yet initialized. */ static unique_id_rec *parent_id; unique_id_rec *cur_unique_id = (unique_id_rec *) TlsGetValue(tls_index); if (parent) { parent_id = cur_unique_id; } else if (!cur_unique_id->pid) { memcpy(cur_unique_id, parent_id, sizeof(*parent_id)); } return cur_unique_id;}#else /* !WIN32 *//* Even when not MULTITHREAD, this will return a single structure, since * APACHE_TLS should be defined as empty on single-threaded platforms. */static unique_id_rec* get_cur_unique_id(int parent){ static APACHE_TLS unique_id_rec spcid; return &spcid;}#endif /* !WIN32 *//* * Number of elements in the structure unique_id_rec. */#ifdef MULTITHREAD#define UNIQUE_ID_REC_MAX 5#else#define UNIQUE_ID_REC_MAX 4#endifstatic unsigned short unique_id_rec_offset[UNIQUE_ID_REC_MAX], unique_id_rec_size[UNIQUE_ID_REC_MAX], unique_id_rec_total_size, unique_id_rec_size_uu;static void unique_id_global_init(server_rec *s, pool *p){#ifndef MAXHOSTNAMELEN#define MAXHOSTNAMELEN 256#endif char str[MAXHOSTNAMELEN + 1]; struct hostent *hent;#ifndef NO_GETTIMEOFDAY struct timeval tv;#endif unique_id_rec *cur_unique_id = get_cur_unique_id(1); /* * Calculate the sizes and offsets in cur_unique_id. */ unique_id_rec_offset[0] = XtOffsetOf(unique_id_rec, stamp); unique_id_rec_size[0] = sizeof(cur_unique_id->stamp); unique_id_rec_offset[1] = XtOffsetOf(unique_id_rec, in_addr); unique_id_rec_size[1] = sizeof(cur_unique_id->in_addr); unique_id_rec_offset[2] = XtOffsetOf(unique_id_rec, pid); unique_id_rec_size[2] = sizeof(cur_unique_id->pid);#ifdef MULTITHREAD unique_id_rec_offset[3] = XtOffsetOf(unique_id_rec, tid); unique_id_rec_size[3] = sizeof(cur_unique_id->tid); unique_id_rec_offset[4] = XtOffsetOf(unique_id_rec, counter); unique_id_rec_size[4] = sizeof(cur_unique_id->counter); unique_id_rec_total_size = unique_id_rec_size[0] + unique_id_rec_size[1] + unique_id_rec_size[2] + unique_id_rec_size[3] + unique_id_rec_size[4];#else unique_id_rec_offset[3] = XtOffsetOf(unique_id_rec, counter); unique_id_rec_size[3] = sizeof(cur_unique_id->counter); unique_id_rec_total_size = unique_id_rec_size[0] + unique_id_rec_size[1] + unique_id_rec_size[2] + unique_id_rec_size[3];#endif /* * Calculate the size of the structure when encoded. */ unique_id_rec_size_uu = (unique_id_rec_total_size*8+5)/6; /* * Now get the global in_addr. Note that it is not sufficient to use one * of the addresses from the main_server, since those aren't as likely to * be unique as the physical address of the machine */ if (gethostname(str, sizeof(str) - 1) != 0) { ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ALERT, s, "gethostname: mod_unique_id requires the "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -