23 #include "sys/types.h"
27 #include "sys/ioctl.h"
28 #include "sys/socket.h"
30 #include "netinet/in.h"
32 #define srand(x) srandom(x)
33 #define rand() random()
53 #define TIME_OFFSET_HIGH 0x01B21DD2
54 #define TIME_OFFSET_LOW 0x13814000
64 fd =
open(
"/dev/urandom", O_RDONLY);
66 fd =
open(
"/dev/random", O_RDONLY | O_NONBLOCK);
68 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
72 for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--) {
87 char* cp = (
char*) buf;
91 i =
read(fd, cp, nbytes);
93 ((errno == EINTR) || (errno == EAGAIN))) {
97 if (lose_counter++ == 8) {
108 for (i = 0; i < nbytes; i++) {
109 *cp++ =
rand() & 0xFF;
121 struct ifreq ifr, *ifrp;
135 #define max(a,b) ((a) > (b) ? (a) : (b))
137 #define ifreq_size(i) max(sizeof(struct ifreq),\
138 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
140 #define ifreq_size(i) sizeof(struct ifreq)
143 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
147 memset(buf, 0,
sizeof(buf));
148 ifc.ifc_len =
sizeof(buf);
150 if (ioctl(sd, SIOCGIFCONF, (
char*) &ifc) < 0) {
155 for (i = 0; i <
n; i += ifreq_size(*ifr)) {
156 ifrp = (
struct ifreq*) ((
char*) ifc.ifc_buf + i);
157 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
159 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) {
162 a = (
unsigned char*) &ifr.ifr_hwaddr.sa_data;
165 if (ioctl(sd, SIOCGENADDR, &ifr) < 0) {
168 a = (
unsigned char*) ifr.ifr_enaddr;
178 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5]) {
182 memcpy(node_id, a, 6);
193 #define MAX_ADJUSTMENT 10
197 static int adjustment = 0;
198 static struct timeval last =
202 unsigned long long clock_reg;
205 gettimeofday(&tv, 0);
206 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
212 if ((tv.tv_sec < last.tv_sec) ||
213 ((tv.tv_sec == last.tv_sec) &&
214 (tv.tv_usec < last.tv_usec))) {
215 clock_seq = (clock_seq + 1) & 0x1FFF;
218 }
else if ((tv.tv_sec == last.tv_sec) &&
219 (tv.tv_usec == last.tv_usec)) {
229 clock_reg = tv.tv_usec * 10 + adjustment;
230 clock_reg += ((
unsigned long long) tv.tv_sec) * 10000000;
231 clock_reg += (((
unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
233 *clock_high = clock_reg >> 32;
234 *clock_low = clock_reg;
235 *ret_clock_seq = clock_seq;
241 static unsigned char node_id[6];
242 static int has_init = 0;
262 memcpy(uu.
node, node_id, 6);
308 unsigned char* out = ptr;
311 out[3] = (
unsigned char) tmp;
313 out[2] = (
unsigned char) tmp;
315 out[1] = (
unsigned char) tmp;
317 out[0] = (
unsigned char) tmp;
320 out[5] = (
unsigned char) tmp;
322 out[4] = (
unsigned char) tmp;
325 out[7] = (
unsigned char) tmp;
327 out[6] = (
unsigned char) tmp;
330 out[9] = (
unsigned char) tmp;
332 out[8] = (
unsigned char) tmp;
334 memcpy(out + 10, uu->
node, 6);
355 tmp = (tmp << 8) | *ptr++;
356 tmp = (tmp << 8) | *ptr++;
357 tmp = (tmp << 8) | *ptr++;
361 tmp = (tmp << 8) | *ptr++;
365 tmp = (tmp << 8) | *ptr++;
369 tmp = (tmp << 8) | *ptr++;
372 memcpy(uu->
node, ptr, 6);
381 v = ((uu[0] ^ uu[4] ^ uu[8] ^ uu[12]) << 0)
382 | ((uu[1] ^ uu[5] ^ uu[9] ^ uu[13]) << 8)
383 | ((uu[2] ^ uu[6] ^ uu[10] ^ uu[14]) << 16)
384 | ((uu[3] ^ uu[7] ^ uu[11] ^ uu[15]) << 24);
385 return v & 0x7fffffff;