/* uuid_create -- generator a UUID */ int uuid_create(uuid_t * uuid) { uuid_time_t timestamp, last_time; unsigned16 clockseq; uuid_node_t node; uuid_node_t last_node; int f; /* acquire system wide lock so we're alone */ LOCK; /* get current time */ get_current_time(×tamp); /* get node ID */ get_ieee_node_identifier(&node); /* get saved state from NV storage */ f = read_state(&clockseq, &last_time, &last_node); /* if no NV state, or if clock went backwards, or node ID changed (e.g., net card swap) change clockseq */ if (!f || memcmp(&node, &last_node, sizeof(uuid_node_t))) clockseq = true_random(); else if (timestamp < last_time) clockseq++; /* stuff fields into the UUID */ format_uuid_v1(uuid, clockseq, timestamp, node); /* save the state for next time */ write_state(clockseq, timestamp, node); UNLOCK; return(1); };
// make a random alpha-numeric string size characters long void make_rstring(char *str, int size) { const char *cs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; int index; for(index=0; index<size; index++) str[index] = cs[true_random()%62]; str[index] = 0; }
/* * dav_create_uuid_state - seed UUID state with pseudorandom data */ static void create_uuid_state(uuid_state *st) { st->cs = true_random(); get_pseudo_node_identifier(&st->node); }
afs_int32 afs_uuid_create(afsUUID * uuid) { uuid_address_t eaddr; afs_int32 got_no_time = 0, code; if (!uuid_init_done) { uuid_time_t t; u_short *seedp, seed = 0; rand_m = 971;; rand_ia = 11113; rand_ib = 104322; rand_irand = 4181; /* * Generating our 'seed' value * * We start with the current time, but, since the resolution of clocks is * system hardware dependent (eg. Ultrix is 10 msec.) and most likely * coarser than our resolution (10 usec) we 'mixup' the bits by xor'ing * all the bits together. This will have the effect of involving all of * the bits in the determination of the seed value while remaining system * independent. Then for good measure to ensure a unique seed when there * are multiple processes creating UUID's on a system, we add in the PID. */ uuid__get_os_time(&t); seedp = (u_short *) (&t); seed ^= *seedp++; seed ^= *seedp++; seed ^= *seedp++; seed ^= *seedp++; #if defined(KERNEL) && defined(AFS_XBSD_ENV) rand_irand += seed + (afs_uint32) curproc->p_pid; #else rand_irand += seed + (afs_uint32) getpid(); #endif uuid__get_os_time(&time_last); clock_seq = true_random(); #ifdef AFS_NT40_ENV if (afs_winsockInit() < 0) { return WSAGetLastError(); } #endif uuid_init_done = 1; } if ((code = uuid_get_address(&eaddr))) return code; /* get our hardware network address */ do { /* get the current time */ uuid__get_os_time(&time_now); /* * check that our clock hasn't gone backwards and handle it * accordingly with clock_seq * check that we're not generating uuid's faster than we * can accommodate with our uuid_time_adjust fudge factor */ if ((code = time_cmp(&time_now, &time_last)) == -1) { /* A clock_seq value of 0 indicates that it hasn't been initialized. */ if (clock_seq == 0) { clock_seq = true_random(); } clock_seq = (clock_seq + 1) & 0x3fff; if (clock_seq == 0) clock_seq = clock_seq + 1; uuid_time_adjust = 0; } else if (code == 1) { uuid_time_adjust = 0; } else { if (uuid_time_adjust == 0x7fff) /* spin while we wait for the clock to tick */ got_no_time = 1; else uuid_time_adjust++; } } while (got_no_time); time_last.lo = time_now.lo; time_last.hi = time_now.hi; if (uuid_time_adjust != 0) { if (time_now.lo & 0x80000000) { time_now.lo += uuid_time_adjust; if (!(time_now.lo & 0x80000000)) time_now.hi++; } else time_now.lo += uuid_time_adjust; } uuid->time_low = time_now.lo; uuid->time_mid = time_now.hi & 0x0000ffff; uuid->time_hi_and_version = (time_now.hi & 0x0fff0000) >> 16; uuid->time_hi_and_version |= (1 << 12); uuid->clock_seq_low = clock_seq & 0xff; uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3f00) >> 8; uuid->clock_seq_hi_and_reserved |= 0x80; uuid_memcpy((void *)uuid->node, (void *)&eaddr, sizeof(uuid_address_t)); return 0; }
// make a random alpha-numeric string size characters long void make_rstring(int size, string& s) { s = ""; const char *cs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; for(int index=0; index<size; index++) s += cs[true_random()%62]; }