/libs/pupnp/src/uuid/sysdep.c

https://github.com/lince/ginga-hones · C · 169 lines · 82 code · 33 blank · 54 comment · 1 complexity · 338d9350881d3cb228d53743293cd54f MD5 · raw file

  1. /*
  2. ** Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
  3. ** Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
  4. ** Digital Equipment Corporation, Maynard, Mass.
  5. ** Copyright (c) 1998 Microsoft.
  6. ** To anyone who acknowledges that this file is provided "AS IS"
  7. ** without any express or implied warranty: permission to use, copy,
  8. ** modify, and distribute this file for any purpose is hereby
  9. ** granted without fee, provided that the above copyright notices and
  10. ** this notice appears in all source code copies, and that none of
  11. ** the names of Open Software Foundation, Inc., Hewlett-Packard
  12. ** Company, or Digital Equipment Corporation be used in advertising
  13. ** or publicity pertaining to distribution of the software without
  14. ** specific, written prior permission. Neither Open Software
  15. ** Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital Equipment
  16. ** Corporation makes any representations about the suitability of
  17. ** this software for any purpose.
  18. */
  19. #include "config.h"
  20. #include "sysdep.h"
  21. #include "UpnpInet.h"
  22. #include <string.h>
  23. #include <stdio.h>
  24. /*-----------------------------------------------------------------------------*/
  25. /*
  26. system dependent call to get IEEE node ID.
  27. This sample implementation generates a random node ID
  28. */
  29. void
  30. get_ieee_node_identifier(uuid_node_t *node)
  31. {
  32. char seed[16];
  33. static int inited = 0;
  34. static uuid_node_t saved_node;
  35. if (!inited) {
  36. get_random_info(seed);
  37. seed[0] |= 0x80;
  38. memcpy(&saved_node, seed, sizeof (uuid_node_t));
  39. inited = 1;
  40. };
  41. *node = saved_node;
  42. };
  43. /*-----------------------------------------------------------------------------*/
  44. /*
  45. system dependent call to get the current system time.
  46. Returned as 100ns ticks since Oct 15, 1582, but resolution may be
  47. less than 100ns.
  48. */
  49. #ifdef WIN32
  50. void
  51. get_system_time( uuid_time_t * uuid_time )
  52. {
  53. ULARGE_INTEGER time;
  54. GetSystemTimeAsFileTime( ( FILETIME * ) & time );
  55. /*
  56. NT keeps time in FILETIME format which is 100ns ticks since
  57. Jan 1, 1601. UUIDs use time in 100ns ticks since Oct 15, 1582.
  58. The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec)
  59. + 18 years and 5 leap days.
  60. */
  61. time.QuadPart += ( unsigned __int64 )( 1000 * 1000 * 10 ) // seconds
  62. * ( unsigned __int64 )( 60 * 60 * 24 ) // days
  63. * ( unsigned __int64 )( 17 + 30 + 31 + 365 * 18 + 5 ); // # of days
  64. *uuid_time = time.QuadPart;
  65. };
  66. /*-----------------------------------------------------------------------------*/
  67. void
  68. get_random_info(char seed[16])
  69. {
  70. MD5_CTX c;
  71. typedef struct {
  72. MEMORYSTATUS m;
  73. SYSTEM_INFO s;
  74. FILETIME t;
  75. LARGE_INTEGER pc;
  76. DWORD tc;
  77. DWORD l;
  78. char hostname[MAX_COMPUTERNAME_LENGTH + 1];
  79. } randomness;
  80. randomness r;
  81. /* Initialize memory area so that valgrind does not complain */
  82. memset(&r, 0, sizeof r);
  83. /* memory usage stats */
  84. GlobalMemoryStatus( &r.m );
  85. /* random system stats */
  86. GetSystemInfo( &r.s );
  87. /* 100ns resolution (nominally) time of day */
  88. GetSystemTimeAsFileTime( &r.t );
  89. /* high resolution performance counter */
  90. QueryPerformanceCounter( &r.pc );
  91. /* milliseconds since last boot */
  92. r.tc = GetTickCount();
  93. r.l = MAX_COMPUTERNAME_LENGTH + 1;
  94. GetComputerName( r.hostname, &r.l );
  95. /* MD5 it */
  96. MD5Init(&c);
  97. MD5Update(&c, &r, sizeof r);
  98. MD5Final(seed, &c);
  99. };
  100. #else /* WIN32 */
  101. /*-----------------------------------------------------------------------------*/
  102. void
  103. get_system_time(uuid_time_t *uuid_time)
  104. {
  105. struct timeval tp;
  106. gettimeofday( &tp, ( struct timezone * )0 );
  107. /*
  108. Offset between UUID formatted times and Unix formatted times.
  109. UUID UTC base time is October 15, 1582.
  110. Unix base time is January 1, 1970.
  111. */
  112. *uuid_time = ( tp.tv_sec * 10000000 ) + ( tp.tv_usec * 10 ) +
  113. I64( 0x01B21DD213814000 );
  114. };
  115. /*-----------------------------------------------------------------------------*/
  116. void
  117. get_random_info(char seed[16])
  118. {
  119. MD5_CTX c;
  120. typedef struct {
  121. //struct sysinfo s;
  122. struct timeval t;
  123. char hostname[257];
  124. } randomness;
  125. randomness r;
  126. /* Initialize memory area so that valgrind does not complain */
  127. memset(&r, 0, sizeof r);
  128. /* Get some random stuff */
  129. gettimeofday(&r.t, (struct timezone *)0);
  130. gethostname(r.hostname, 256 );
  131. /* MD5 it */
  132. MD5Init(&c);
  133. MD5Update(&c, &r, sizeof r);
  134. MD5Final(seed, &c);
  135. };
  136. #endif /* WIN32 */