diff --git a/wsd.c b/wsd.c index 0a363d8..0b4b3d2 100644 --- a/wsd.c +++ b/wsd.c @@ -57,9 +57,9 @@ #include // bool #include // FILE, fopen(), fscanf(), snprintf(), asprintf() -#include // srand48() +#include // srand48(), strtoul() #include // usleep() -#include // strcmp(), strdup() +#include // strcmp(), strdup(), strncpy() #include // isdigit(), isspace() #include // time_t, time() #include // errno @@ -71,23 +71,37 @@ static time_t wsd_instance; static char wsd_sequence[UUIDLEN], wsd_endpoint[UUIDLEN]; +static void uuid_endpoint(char uuid[UUIDLEN]); + +static int uuid_parse(char uuid[UUIDLEN], unsigned short UUID[8]) +{ + size_t pos[] = {0,4,9,14,19,24,28,32}; + for(size_t i = 0; i < 8; ++i) { + char str[5]; + strncpy(str, uuid+pos[i], 4); + str[4] = '\0'; + unsigned long s = strtoul(str, NULL, 16); + UUID[i] = s; + } + return 0; +} + static void set_seed(void) { - FILE *fp = fopen("/etc/machine-id", "r"); + char uuid[UUIDLEN]; + unsigned short UUID[8]; unsigned long seed; - time((time_t *)&seed); - - fseek(fp, 0, SEEK_END); + uuid_endpoint(uuid); + uuid_parse(uuid, UUID); - if (fp && 0 != ftell(fp) ) { - unsigned long s; - rewind(fp); - - while (fscanf(fp, "%8lx", &s) > 0) - seed ^= s; - fclose(fp); + time((time_t *)&seed); + + for(size_t i = 0; i < 4; ++i) { + unsigned long s = UUID[2*i+1] | UUID[2*i+0] << 16; + seed ^= s; } + srand48(seed); }