⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mod_unique_id.c

📁 apache简化版
💻 C
📖 第 1 页 / 共 2 页
字号:
                    "mod_unique_id: unable to gethostbyname(\"%s\")", str);        exit(1);    }    global_in_addr = ((struct in_addr *) hent->h_addr_list[0])->s_addr;    ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, s,                "mod_unique_id: using ip addr %s",                inet_ntoa(*(struct in_addr *) hent->h_addr_list[0]));    /*     * If the server is pummelled with restart requests we could possibly end     * up in a situation where we're starting again during the same second     * that has been used in previous identifiers.  Avoid that situation.     *      * In truth, for this to actually happen not only would it have to restart     * in the same second, but it would have to somehow get the same pids as     * one of the other servers that was running in that second. Which would     * mean a 64k wraparound on pids ... not very likely at all.     *      * But protecting against it is relatively cheap.  We just sleep into the     * next second.     */#ifdef NO_GETTIMEOFDAY    sleep(1);#else    if (gettimeofday(&tv, NULL) == -1) {        sleep(1);    }    else if (tv.tv_usec) {        tv.tv_sec = 0;        tv.tv_usec = 1000000 - tv.tv_usec;        select(0, NULL, NULL, NULL, &tv);    }#endif}static void unique_id_child_init(server_rec *s, pool *p){    pid_t pid;#ifndef NO_GETTIMEOFDAY    struct timeval tv;#endif    /*     * Note that we use the pid because it's possible that on the same     * physical machine there are multiple servers (i.e. using Listen). But     * it's guaranteed that none of them will share the same pids between     * children.     *      * XXX: for multithread this needs to use a pid/tid combo and probably     * needs to be expanded to 32 bits     */    pid = getpid();    cur_unique_id.pid = pid;    /*     * Test our assumption that the pid is 32-bits.  It's possible that     * 64-bit machines will declare pid_t to be 64 bits but only use 32     * of them.  It would have been really nice to test this during     * global_init ... but oh well.     */    if (cur_unique_id.pid != pid) {        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_CRIT, s,                    "oh no! pids are greater than 32-bits!  I'm broken!");    }    cur_unique_id.in_addr = global_in_addr;    /*     * If we use 0 as the initial counter we have a little less protection     * against restart problems, and a little less protection against a clock     * going backwards in time.     */#ifndef NO_GETTIMEOFDAY    if (gettimeofday(&tv, NULL) == -1) {        cur_unique_id.counter = 0;    }    else {	/* Some systems have very low variance on the low end of their	 * system counter, defend against that.	 */        cur_unique_id.counter = tv.tv_usec / 10;    }#else    cur_unique_id.counter = 0;#endif    /*     * We must always use network ordering for these bytes, so that     * identifiers are comparable between machines of different byte     * orderings.  Note in_addr is already in network order.     */    cur_unique_id.pid = htonl(cur_unique_id.pid);    cur_unique_id.counter = htons(cur_unique_id.counter);}/* NOTE: This is *NOT* the same encoding used by uuencode ... the last two * characters should be + and /.  But those two characters have very special * meanings in URLs, and we want to make it easy to use identifiers in * URLs.  So we replace them with @ and -. */static const char uuencoder[64] = {    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '@', '-',};static int gen_unique_id(request_rec *r){    char *str;    /*     * Buffer padded with two final bytes, used to copy the unique_id_red     * structure without the internal paddings that it could have.     */    struct {	unique_id_rec foo;	unsigned char pad[2];    } paddedbuf;    unsigned char *x,*y;    unsigned short counter;    const char *e;    int i,j,k;    /* copy the unique_id if this is an internal redirect (we're never     * actually called for sub requests, so we don't need to test for     * them) */    if (r->prev && (e = ap_table_get(r->subprocess_env, "REDIRECT_UNIQUE_ID"))) {	ap_table_setn(r->subprocess_env, "UNIQUE_ID", e);	return DECLINED;    }    cur_unique_id.stamp = htonl((unsigned int)r->request_time);    /* we'll use a temporal buffer to avoid uuencoding the possible internal     * paddings of the original structure */    x = (unsigned char *) &paddedbuf;    y = (unsigned char *) &cur_unique_id;    k = 0;    for (i = 0; i < UNIQUE_ID_REC_MAX; i++) {        y = ((unsigned char *) &cur_unique_id) + unique_id_rec_offset[i];        for (j = 0; j < unique_id_rec_size[i]; j++, k++) {            x[k] = y[j];        }    }    /*     * We reset two more bytes just in case padding is needed for the uuencoding.     */    x[k++] = '\0';    x[k++] = '\0';        /* alloc str and do the uuencoding */    str = (char *)ap_palloc(r->pool, unique_id_rec_size_uu + 1);    k = 0;    for (i = 0; i < unique_id_rec_total_size; i += 3) {        y = x + i;        str[k++] = uuencoder[y[0] >> 2];        str[k++] = uuencoder[((y[0] & 0x03) << 4) | ((y[1] & 0xf0) >> 4)];        if (k == unique_id_rec_size_uu) break;        str[k++] = uuencoder[((y[1] & 0x0f) << 2) | ((y[2] & 0xc0) >> 6)];        if (k == unique_id_rec_size_uu) break;        str[k++] = uuencoder[y[2] & 0x3f];    }    str[k++] = '\0';    /* set the environment variable */    ap_table_setn(r->subprocess_env, "UNIQUE_ID", str);    /* and increment the identifier for the next call */    counter = ntohs(cur_unique_id.counter) + 1;    cur_unique_id.counter = htons(counter);    return DECLINED;}module MODULE_VAR_EXPORT unique_id_module = {    STANDARD_MODULE_STUFF,    unique_id_global_init,      /* initializer */    NULL,                       /* dir config creater */    NULL,                       /* dir merger --- default is to override */    NULL,                       /* server config */    NULL,                       /* merge server configs */    NULL,                       /* command table */    NULL,                       /* handlers */    NULL,                       /* filename translation */    NULL,                       /* check_user_id */    NULL,                       /* check auth */    NULL,                       /* check access */    NULL,                       /* type_checker */    NULL,                       /* fixups */    NULL,                       /* logger */    NULL,                       /* header parser */    unique_id_child_init,       /* child_init */    NULL,                       /* child_exit */    gen_unique_id               /* post_read_request */};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -